2017-09-16 19:47:32 +02:00
|
|
|
|
#include "Log.h"
|
2016-04-27 00:27:24 +02:00
|
|
|
|
#include "File.h"
|
|
|
|
|
#include "StrFmt.h"
|
2017-05-13 20:30:37 +02:00
|
|
|
|
#include "sema.h"
|
2016-04-27 00:27:24 +02:00
|
|
|
|
|
2017-08-13 17:32:48 +02:00
|
|
|
|
#include "Utilities/sysinfo.h"
|
2017-11-19 19:59:23 +01:00
|
|
|
|
#include "Utilities/Thread.h"
|
2016-07-21 00:00:31 +02:00
|
|
|
|
#include "rpcs3_version.h"
|
Utilities: explicitly add more includes found by GCC
Utilities/Log.cpp: In member function 'void logs::file_writer::log(logs::level, const char*, std::size_t)':
Utilities/Log.cpp:559:9: error: 'memcpy' is not a member of 'std'
std::memcpy(pos, text, frag);
^~~~~~
Utilities/Log.cpp:559:9: note: suggested alternative: 'empty'
std::memcpy(pos, text, frag);
^~~~~~
empty
Utilities/Log.cpp:560:9: error: 'memcpy' is not a member of 'std'
std::memcpy(m_fptr, text + frag, size - frag);
^~~~~~
Utilities/Log.cpp:560:9: note: suggested alternative: 'empty'
std::memcpy(m_fptr, text + frag, size - frag);
^~~~~~
empty
Utilities/Log.cpp:564:9: error: 'memcpy' is not a member of 'std'
std::memcpy(pos, text, size);
^~~~~~
Utilities/Log.cpp:564:9: note: suggested alternative: 'empty'
std::memcpy(pos, text, size);
^~~~~~
empty
Utilities/sync.h: In member function 'int futex(int*, int, int, const timespec*, int*, int)::futex_map::operator()(int*, int, int, const timespec*, int*, uint)':
Utilities/sync.h:110:20: error: 'find' is not a member of 'std'
map.erase(std::find(map.find(uaddr), map.end(), ref));
^~~~
Utilities/sync.h:110:20: note: suggested alternative: 'rend'
map.erase(std::find(map.find(uaddr), map.end(), ref));
^~~~
rend
2018-08-30 19:39:44 +02:00
|
|
|
|
#include <cstring>
|
2016-04-27 00:27:24 +02:00
|
|
|
|
#include <string>
|
2017-05-13 20:30:37 +02:00
|
|
|
|
#include <unordered_map>
|
2017-11-19 19:59:23 +01:00
|
|
|
|
#include <thread>
|
|
|
|
|
#include <chrono>
|
|
|
|
|
|
|
|
|
|
using namespace std::literals::chrono_literals;
|
2016-04-27 00:27:24 +02:00
|
|
|
|
|
2017-02-22 10:52:03 +01:00
|
|
|
|
#ifdef _WIN32
|
2017-08-23 19:44:31 +02:00
|
|
|
|
#define NOMINMAX
|
2017-02-22 10:52:03 +01:00
|
|
|
|
#include <Windows.h>
|
|
|
|
|
#else
|
2017-08-23 19:44:31 +02:00
|
|
|
|
#include <sys/mman.h>
|
2018-03-16 22:34:16 +01:00
|
|
|
|
#include <sys/stat.h>
|
2017-02-22 10:52:03 +01:00
|
|
|
|
#endif
|
|
|
|
|
|
2017-08-29 16:11:36 +02:00
|
|
|
|
#include <zlib.h>
|
|
|
|
|
|
2017-05-13 20:30:37 +02:00
|
|
|
|
static std::string empty_string()
|
|
|
|
|
{
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-27 00:27:24 +02:00
|
|
|
|
// Thread-specific log prefix provider
|
2017-05-13 20:30:37 +02:00
|
|
|
|
thread_local std::string(*g_tls_log_prefix)() = &empty_string;
|
2014-06-17 17:44:03 +02:00
|
|
|
|
|
2016-08-03 22:51:05 +02:00
|
|
|
|
template<>
|
|
|
|
|
void fmt_class_string<logs::level>::format(std::string& out, u64 arg)
|
|
|
|
|
{
|
|
|
|
|
format_enum(out, arg, [](auto lev)
|
|
|
|
|
{
|
|
|
|
|
switch (lev)
|
|
|
|
|
{
|
|
|
|
|
case logs::level::always: return "Nothing";
|
|
|
|
|
case logs::level::fatal: return "Fatal";
|
|
|
|
|
case logs::level::error: return "Error";
|
|
|
|
|
case logs::level::todo: return "TODO";
|
|
|
|
|
case logs::level::success: return "Success";
|
|
|
|
|
case logs::level::warning: return "Warning";
|
|
|
|
|
case logs::level::notice: return "Notice";
|
|
|
|
|
case logs::level::trace: return "Trace";
|
2017-11-02 07:27:57 +01:00
|
|
|
|
case logs::level::_uninit: return unknown;
|
2016-08-03 22:51:05 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return unknown;
|
|
|
|
|
});
|
|
|
|
|
}
|
2016-05-13 16:01:48 +02:00
|
|
|
|
|
|
|
|
|
namespace logs
|
2014-06-17 17:44:03 +02:00
|
|
|
|
{
|
2017-11-19 19:59:23 +01:00
|
|
|
|
// Memory-mapped buffer size
|
|
|
|
|
constexpr u64 s_log_size = 32 * 1024 * 1024;
|
2017-08-23 19:44:31 +02:00
|
|
|
|
|
2016-04-27 00:27:24 +02:00
|
|
|
|
class file_writer
|
|
|
|
|
{
|
|
|
|
|
fs::file m_file;
|
2017-08-29 16:09:06 +02:00
|
|
|
|
std::string m_name;
|
2017-11-19 19:59:23 +01:00
|
|
|
|
std::thread m_writer;
|
|
|
|
|
fs::file m_fout;
|
2017-11-19 23:01:29 +01:00
|
|
|
|
fs::file m_fout2;
|
2017-11-19 19:59:23 +01:00
|
|
|
|
u64 m_max_size;
|
2016-04-27 00:27:24 +02:00
|
|
|
|
|
2017-08-23 19:44:31 +02:00
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
::HANDLE m_fmap;
|
|
|
|
|
#endif
|
2017-08-26 22:03:14 +02:00
|
|
|
|
uchar* m_fptr{};
|
2017-11-21 19:45:02 +01:00
|
|
|
|
z_stream m_zs{};
|
|
|
|
|
semaphore<> m_m;
|
2017-08-23 19:44:31 +02:00
|
|
|
|
|
2017-11-19 19:59:23 +01:00
|
|
|
|
alignas(128) atomic_t<u64> m_buf{0}; // MSB (40 bit): push begin, LSB (24 bis): push size
|
|
|
|
|
alignas(128) atomic_t<u64> m_out{0}; // Amount of bytes written to file
|
|
|
|
|
|
2017-11-21 19:45:02 +01:00
|
|
|
|
uchar m_zout[65536];
|
|
|
|
|
|
|
|
|
|
// Write buffered logs immediately
|
|
|
|
|
bool flush(u64 bufv);
|
|
|
|
|
|
2016-04-27 00:27:24 +02:00
|
|
|
|
public:
|
|
|
|
|
file_writer(const std::string& name);
|
|
|
|
|
|
2017-08-23 19:44:31 +02:00
|
|
|
|
virtual ~file_writer();
|
2016-04-27 00:27:24 +02:00
|
|
|
|
|
|
|
|
|
// Append raw data
|
2017-08-26 22:03:14 +02:00
|
|
|
|
void log(logs::level sev, const char* text, std::size_t size);
|
2016-04-27 00:27:24 +02:00
|
|
|
|
};
|
|
|
|
|
|
2017-09-16 21:10:55 +02:00
|
|
|
|
struct channel_info
|
|
|
|
|
{
|
|
|
|
|
channel* pointer = nullptr;
|
|
|
|
|
level enabled = level::notice;
|
|
|
|
|
|
|
|
|
|
void set_level(level value)
|
|
|
|
|
{
|
|
|
|
|
enabled = value;
|
|
|
|
|
|
|
|
|
|
if (pointer)
|
|
|
|
|
{
|
|
|
|
|
pointer->enabled = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct stored_message
|
|
|
|
|
{
|
|
|
|
|
message m;
|
|
|
|
|
u64 stamp;
|
|
|
|
|
std::string prefix;
|
|
|
|
|
std::string text;
|
|
|
|
|
};
|
|
|
|
|
|
2016-04-27 00:27:24 +02:00
|
|
|
|
struct file_listener : public file_writer, public listener
|
|
|
|
|
{
|
2017-08-20 23:58:25 +02:00
|
|
|
|
file_listener(const std::string& name);
|
2016-04-27 00:27:24 +02:00
|
|
|
|
|
2017-08-23 19:44:31 +02:00
|
|
|
|
virtual ~file_listener() = default;
|
|
|
|
|
|
2016-04-27 00:27:24 +02:00
|
|
|
|
// Encode level, current thread name, channel name and write log message
|
2017-02-22 10:52:03 +01:00
|
|
|
|
virtual void log(u64 stamp, const message& msg, const std::string& prefix, const std::string& text) override;
|
2017-09-16 21:10:55 +02:00
|
|
|
|
|
|
|
|
|
// Channel registry
|
|
|
|
|
std::unordered_map<std::string, channel_info> channels;
|
|
|
|
|
|
|
|
|
|
// Messages for delayed listener initialization
|
|
|
|
|
std::vector<stored_message> messages;
|
2016-04-27 00:27:24 +02:00
|
|
|
|
};
|
|
|
|
|
|
2016-07-21 00:00:31 +02:00
|
|
|
|
static file_listener* get_logger()
|
2014-06-17 17:44:03 +02:00
|
|
|
|
{
|
2016-02-01 22:55:43 +01:00
|
|
|
|
// Use magic static
|
2017-11-19 15:09:40 +01:00
|
|
|
|
static file_listener logger("RPCS3");
|
2016-07-21 00:00:31 +02:00
|
|
|
|
return &logger;
|
2014-06-17 17:44:03 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-02-22 10:52:03 +01:00
|
|
|
|
static u64 get_stamp()
|
|
|
|
|
{
|
|
|
|
|
static struct time_initializer
|
|
|
|
|
{
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
LARGE_INTEGER freq;
|
|
|
|
|
LARGE_INTEGER start;
|
|
|
|
|
|
|
|
|
|
time_initializer()
|
|
|
|
|
{
|
|
|
|
|
QueryPerformanceFrequency(&freq);
|
|
|
|
|
QueryPerformanceCounter(&start);
|
|
|
|
|
}
|
|
|
|
|
#else
|
|
|
|
|
std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now();
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
u64 get() const
|
|
|
|
|
{
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
LARGE_INTEGER now;
|
|
|
|
|
QueryPerformanceCounter(&now);
|
|
|
|
|
const LONGLONG diff = now.QuadPart - start.QuadPart;
|
|
|
|
|
return diff / freq.QuadPart * 1'000'000 + diff % freq.QuadPart * 1'000'000 / freq.QuadPart;
|
|
|
|
|
#else
|
|
|
|
|
return std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::steady_clock::now() - start).count();
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
} timebase{};
|
|
|
|
|
|
|
|
|
|
return timebase.get();
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-13 20:30:37 +02:00
|
|
|
|
channel GENERAL("");
|
|
|
|
|
channel LOADER("LDR");
|
|
|
|
|
channel MEMORY("MEM");
|
|
|
|
|
channel RSX("RSX");
|
|
|
|
|
channel HLE("HLE");
|
|
|
|
|
channel PPU("PPU");
|
|
|
|
|
channel SPU("SPU");
|
|
|
|
|
|
|
|
|
|
// Channel registry mutex
|
|
|
|
|
semaphore<> g_mutex;
|
|
|
|
|
|
2017-08-20 23:58:25 +02:00
|
|
|
|
// Must be set to true in main()
|
|
|
|
|
atomic_t<bool> g_init{false};
|
|
|
|
|
|
2017-05-13 20:30:37 +02:00
|
|
|
|
void reset()
|
|
|
|
|
{
|
|
|
|
|
semaphore_lock lock(g_mutex);
|
|
|
|
|
|
2017-09-16 21:10:55 +02:00
|
|
|
|
for (auto&& pair : get_logger()->channels)
|
2017-05-13 20:30:37 +02:00
|
|
|
|
{
|
|
|
|
|
pair.second.set_level(level::notice);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void set_level(const std::string& ch_name, level value)
|
|
|
|
|
{
|
|
|
|
|
semaphore_lock lock(g_mutex);
|
|
|
|
|
|
2017-09-16 21:10:55 +02:00
|
|
|
|
get_logger()->channels[ch_name].set_level(value);
|
2017-05-13 20:30:37 +02:00
|
|
|
|
}
|
2017-08-20 23:58:25 +02:00
|
|
|
|
|
|
|
|
|
// Must be called in main() to stop accumulating messages in g_messages
|
|
|
|
|
void set_init()
|
|
|
|
|
{
|
|
|
|
|
if (!g_init)
|
|
|
|
|
{
|
|
|
|
|
semaphore_lock lock(g_mutex);
|
2017-09-16 21:10:55 +02:00
|
|
|
|
get_logger()->messages.clear();
|
2017-08-20 23:58:25 +02:00
|
|
|
|
g_init = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-06-17 17:44:03 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-25 00:22:19 +01:00
|
|
|
|
logs::listener::~listener()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-21 00:00:31 +02:00
|
|
|
|
void logs::listener::add(logs::listener* _new)
|
|
|
|
|
{
|
|
|
|
|
// Get first (main) listener
|
|
|
|
|
listener* lis = get_logger();
|
|
|
|
|
|
2017-08-20 23:58:25 +02:00
|
|
|
|
semaphore_lock lock(g_mutex);
|
|
|
|
|
|
2016-07-21 00:00:31 +02:00
|
|
|
|
// Install new listener at the end of linked list
|
|
|
|
|
while (lis->m_next || !lis->m_next.compare_and_swap_test(nullptr, _new))
|
|
|
|
|
{
|
|
|
|
|
lis = lis->m_next;
|
|
|
|
|
}
|
2017-08-20 23:58:25 +02:00
|
|
|
|
|
|
|
|
|
// Send initial messages
|
2017-09-16 21:10:55 +02:00
|
|
|
|
for (const auto& msg : get_logger()->messages)
|
2017-08-20 23:58:25 +02:00
|
|
|
|
{
|
|
|
|
|
_new->log(msg.stamp, msg.m, msg.prefix, msg.text);
|
|
|
|
|
}
|
2016-07-21 00:00:31 +02:00
|
|
|
|
}
|
|
|
|
|
|
2016-08-05 18:49:45 +02:00
|
|
|
|
void logs::message::broadcast(const char* fmt, const fmt_type_info* sup, const u64* args)
|
2014-06-17 17:44:03 +02:00
|
|
|
|
{
|
2017-02-22 10:52:03 +01:00
|
|
|
|
// Get timestamp
|
|
|
|
|
const u64 stamp = get_stamp();
|
|
|
|
|
|
2017-05-13 20:30:37 +02:00
|
|
|
|
// Register channel
|
|
|
|
|
if (ch->enabled == level::_uninit)
|
|
|
|
|
{
|
|
|
|
|
semaphore_lock lock(g_mutex);
|
|
|
|
|
|
2017-09-16 21:10:55 +02:00
|
|
|
|
auto& info = get_logger()->channels[ch->name];
|
2017-05-13 20:30:37 +02:00
|
|
|
|
|
|
|
|
|
if (info.pointer && info.pointer != ch)
|
|
|
|
|
{
|
|
|
|
|
fmt::throw_exception("logs::channel repetition: %s", ch->name);
|
|
|
|
|
}
|
|
|
|
|
else if (!info.pointer)
|
|
|
|
|
{
|
|
|
|
|
info.pointer = ch;
|
|
|
|
|
ch->enabled = info.enabled;
|
|
|
|
|
|
|
|
|
|
// Check level again
|
|
|
|
|
if (info.enabled < sev)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get text
|
2017-08-23 22:14:52 +02:00
|
|
|
|
thread_local std::string text; text.clear();
|
2017-05-13 20:30:37 +02:00
|
|
|
|
fmt::raw_append(text, fmt, sup, args);
|
|
|
|
|
std::string prefix = g_tls_log_prefix();
|
2016-07-21 00:00:31 +02:00
|
|
|
|
|
|
|
|
|
// Get first (main) listener
|
|
|
|
|
listener* lis = get_logger();
|
2017-08-20 23:58:25 +02:00
|
|
|
|
|
|
|
|
|
if (!g_init)
|
|
|
|
|
{
|
|
|
|
|
semaphore_lock lock(g_mutex);
|
|
|
|
|
|
|
|
|
|
if (!g_init)
|
|
|
|
|
{
|
|
|
|
|
while (lis)
|
|
|
|
|
{
|
|
|
|
|
lis->log(stamp, *this, prefix, text);
|
|
|
|
|
lis = lis->m_next;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Store message additionally
|
2017-09-16 21:10:55 +02:00
|
|
|
|
get_logger()->messages.emplace_back(stored_message{*this, stamp, std::move(prefix), text});
|
2017-08-20 23:58:25 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-09-16 13:54:25 +02:00
|
|
|
|
|
2016-07-21 00:00:31 +02:00
|
|
|
|
// Send message to all listeners
|
|
|
|
|
while (lis)
|
|
|
|
|
{
|
2017-02-22 10:52:03 +01:00
|
|
|
|
lis->log(stamp, *this, prefix, text);
|
2016-07-21 00:00:31 +02:00
|
|
|
|
lis = lis->m_next;
|
|
|
|
|
}
|
2014-06-17 17:44:03 +02:00
|
|
|
|
}
|
|
|
|
|
|
2016-02-01 22:55:43 +01:00
|
|
|
|
[[noreturn]] extern void catch_all_exceptions();
|
|
|
|
|
|
2016-05-13 16:01:48 +02:00
|
|
|
|
logs::file_writer::file_writer(const std::string& name)
|
2017-09-16 19:37:49 +02:00
|
|
|
|
: m_name(name)
|
2014-06-17 17:44:03 +02:00
|
|
|
|
{
|
2017-11-19 15:09:40 +01:00
|
|
|
|
const std::string log_name = fs::get_config_dir() + name + ".log";
|
|
|
|
|
const std::string buf_name = fs::get_config_dir() + name + ".buf";
|
|
|
|
|
|
2016-01-12 22:57:16 +01:00
|
|
|
|
try
|
2014-06-17 17:44:03 +02:00
|
|
|
|
{
|
2018-03-16 22:34:16 +01:00
|
|
|
|
if (!m_file.open(buf_name, fs::read + fs::rewrite + fs::lock))
|
2014-06-17 17:44:03 +02:00
|
|
|
|
{
|
2017-11-19 15:09:40 +01:00
|
|
|
|
if (fs::g_tls_error == fs::error::acces)
|
|
|
|
|
{
|
|
|
|
|
if (fs::exists(buf_name))
|
|
|
|
|
{
|
|
|
|
|
fmt::throw_exception("Another instance of %s is running. Close it or kill its process, if necessary.", name);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
fmt::throw_exception("Cannot create %s.log (access denied)."
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
"\nNote that %s cannot be installed in Program Files or similar directory with limited permissions."
|
|
|
|
|
#else
|
|
|
|
|
"\nPlease, check %s permissions in '~/.config/'."
|
|
|
|
|
#endif
|
|
|
|
|
, name, name);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fmt::throw_exception("Cannot create %s.log (error %s)", name, fs::g_tls_error);
|
2014-06-17 17:44:03 +02:00
|
|
|
|
}
|
2017-08-23 19:44:31 +02:00
|
|
|
|
|
2017-11-19 19:59:23 +01:00
|
|
|
|
// Check free space
|
|
|
|
|
fs::device_stat stats{};
|
2017-11-23 01:49:01 +01:00
|
|
|
|
if (!fs::statfs(fs::get_config_dir(), stats) || stats.avail_free < s_log_size * 8)
|
2017-11-19 19:59:23 +01:00
|
|
|
|
{
|
|
|
|
|
fmt::throw_exception("Not enough free space (%f KB)", stats.avail_free / 1000000.);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Limit log size to ~25% of free space
|
|
|
|
|
m_max_size = stats.avail_free / 4;
|
|
|
|
|
|
|
|
|
|
// Initialize memory mapped file
|
2017-08-23 19:44:31 +02:00
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
m_fmap = CreateFileMappingW(m_file.get_handle(), 0, PAGE_READWRITE, s_log_size >> 32, s_log_size & 0xffffffff, 0);
|
2017-08-30 16:15:35 +02:00
|
|
|
|
m_fptr = m_fmap ? (uchar*)MapViewOfFile(m_fmap, FILE_MAP_WRITE, 0, 0, 0) : nullptr;
|
2017-08-23 19:44:31 +02:00
|
|
|
|
#else
|
2017-08-24 20:19:13 +02:00
|
|
|
|
m_file.trunc(s_log_size);
|
2017-08-23 19:44:31 +02:00
|
|
|
|
m_fptr = (uchar*)::mmap(0, s_log_size, PROT_READ | PROT_WRITE, MAP_SHARED, m_file.get_handle(), 0);
|
|
|
|
|
#endif
|
2017-08-29 16:09:06 +02:00
|
|
|
|
|
2017-09-16 19:37:49 +02:00
|
|
|
|
verify(name.c_str()), m_fptr;
|
2017-08-30 16:15:35 +02:00
|
|
|
|
|
|
|
|
|
// Rotate backups (TODO)
|
2017-11-19 15:09:40 +01:00
|
|
|
|
fs::remove_file(fs::get_config_dir() + name + "1.log.gz");
|
2017-09-16 19:37:49 +02:00
|
|
|
|
fs::create_dir(fs::get_config_dir() + "old_logs");
|
2017-11-19 15:09:40 +01:00
|
|
|
|
fs::rename(fs::get_config_dir() + m_name + ".log.gz", fs::get_config_dir() + "old_logs/" + m_name + ".log.gz", true);
|
2017-11-19 19:59:23 +01:00
|
|
|
|
|
|
|
|
|
// Actual log file (allowed to fail)
|
|
|
|
|
m_fout.open(log_name, fs::rewrite);
|
2017-11-19 23:01:29 +01:00
|
|
|
|
|
2018-03-16 22:34:16 +01:00
|
|
|
|
// Compressed log, make it inaccessible (foolproof)
|
|
|
|
|
if (!m_fout2.open(log_name + ".gz", fs::rewrite + fs::unread) || deflateInit2(&m_zs, 9, Z_DEFLATED, 16 + 15, 9, Z_DEFAULT_STRATEGY) != Z_OK)
|
2017-11-21 19:45:02 +01:00
|
|
|
|
{
|
|
|
|
|
m_fout2.close();
|
|
|
|
|
}
|
2018-03-16 22:34:16 +01:00
|
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
// Autodelete compressed log file
|
|
|
|
|
FILE_DISPOSITION_INFO disp;
|
|
|
|
|
disp.DeleteFileW = TRUE;
|
|
|
|
|
SetFileInformationByHandle(m_fout2.get_handle(), FileDispositionInfo, &disp, sizeof(disp));
|
|
|
|
|
#endif
|
2014-06-17 17:44:03 +02:00
|
|
|
|
}
|
2017-11-23 16:37:08 +01:00
|
|
|
|
catch (const std::exception& e)
|
|
|
|
|
{
|
|
|
|
|
std::thread([text = std::string{e.what()}]{ report_fatal_error(text); }).detach();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2016-02-01 22:55:43 +01:00
|
|
|
|
catch (...)
|
2014-06-17 17:44:03 +02:00
|
|
|
|
{
|
2017-11-23 16:37:08 +01:00
|
|
|
|
std::thread([]{ report_fatal_error("Unknown error" HERE); }).detach();
|
|
|
|
|
return;
|
2016-01-12 22:57:16 +01:00
|
|
|
|
}
|
2017-08-26 22:03:14 +02:00
|
|
|
|
|
2017-11-19 19:59:23 +01:00
|
|
|
|
m_writer = std::thread([this]()
|
2017-08-29 16:11:36 +02:00
|
|
|
|
{
|
2017-11-19 19:59:23 +01:00
|
|
|
|
thread_ctrl::set_native_priority(-1);
|
2017-08-29 16:11:36 +02:00
|
|
|
|
|
2017-11-19 19:59:23 +01:00
|
|
|
|
while (true)
|
2017-08-29 16:11:36 +02:00
|
|
|
|
{
|
2017-11-19 19:59:23 +01:00
|
|
|
|
const u64 bufv = m_buf;
|
2017-08-29 16:11:36 +02:00
|
|
|
|
|
2017-11-19 19:59:23 +01:00
|
|
|
|
if (bufv & 0xffffff)
|
|
|
|
|
{
|
|
|
|
|
// Wait if threads are writing logs
|
|
|
|
|
std::this_thread::yield();
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-21 19:45:02 +01:00
|
|
|
|
if (!flush(bufv))
|
2017-11-19 19:59:23 +01:00
|
|
|
|
{
|
2017-11-21 19:45:02 +01:00
|
|
|
|
if (m_out == -1)
|
2017-11-19 19:59:23 +01:00
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::this_thread::sleep_for(10ms);
|
|
|
|
|
}
|
2017-08-29 16:11:36 +02:00
|
|
|
|
}
|
2017-11-19 19:59:23 +01:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logs::file_writer::~file_writer()
|
|
|
|
|
{
|
2017-11-23 16:37:08 +01:00
|
|
|
|
if (!m_fptr)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-19 19:59:23 +01:00
|
|
|
|
// Stop writer thread
|
|
|
|
|
while (m_out << 24 < m_buf)
|
|
|
|
|
{
|
|
|
|
|
std::this_thread::yield();
|
2017-08-29 16:11:36 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-19 19:59:23 +01:00
|
|
|
|
m_out = -1;
|
|
|
|
|
m_writer.join();
|
|
|
|
|
|
2017-11-21 19:45:02 +01:00
|
|
|
|
if (m_fout2)
|
|
|
|
|
{
|
|
|
|
|
m_zs.avail_in = 0;
|
|
|
|
|
m_zs.next_in = nullptr;
|
|
|
|
|
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
m_zs.avail_out = sizeof(m_zout);
|
|
|
|
|
m_zs.next_out = m_zout;
|
|
|
|
|
|
|
|
|
|
if (deflate(&m_zs, Z_FINISH) == Z_STREAM_ERROR || m_fout2.write(m_zout, sizeof(m_zout) - m_zs.avail_out) != sizeof(m_zout) - m_zs.avail_out)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
while (m_zs.avail_out == 0);
|
|
|
|
|
|
|
|
|
|
deflateEnd(&m_zs);
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-23 19:44:31 +02:00
|
|
|
|
#ifdef _WIN32
|
2018-03-16 22:34:16 +01:00
|
|
|
|
// Cancel compressed log file autodeletion
|
|
|
|
|
FILE_DISPOSITION_INFO disp;
|
|
|
|
|
disp.DeleteFileW = FALSE;
|
|
|
|
|
SetFileInformationByHandle(m_fout2.get_handle(), FileDispositionInfo, &disp, sizeof(disp));
|
|
|
|
|
|
2017-08-23 19:44:31 +02:00
|
|
|
|
UnmapViewOfFile(m_fptr);
|
|
|
|
|
CloseHandle(m_fmap);
|
|
|
|
|
#else
|
2018-03-16 22:34:16 +01:00
|
|
|
|
// Restore compressed log file permissions
|
|
|
|
|
::fchmod(m_fout2.get_handle(), S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
|
2017-08-23 19:44:31 +02:00
|
|
|
|
::munmap(m_fptr, s_log_size);
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-21 19:45:02 +01:00
|
|
|
|
bool logs::file_writer::flush(u64 bufv)
|
|
|
|
|
{
|
|
|
|
|
semaphore_lock lock(m_m);
|
|
|
|
|
|
|
|
|
|
const u64 st = +m_out;
|
|
|
|
|
const u64 end = std::min<u64>((st + s_log_size) & ~(s_log_size - 1), bufv >> 24);
|
|
|
|
|
|
|
|
|
|
if (end > st)
|
|
|
|
|
{
|
|
|
|
|
// Avoid writing too big fragments
|
|
|
|
|
const u64 size = std::min<u64>(end - st, sizeof(m_zout) / 2);
|
|
|
|
|
|
|
|
|
|
// Write uncompressed
|
2017-11-26 13:34:24 +01:00
|
|
|
|
if (m_fout && st < m_max_size && m_fout.write(m_fptr + st % s_log_size, size) != size)
|
2017-11-21 19:45:02 +01:00
|
|
|
|
{
|
|
|
|
|
m_fout.close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Write compressed
|
2017-11-26 13:34:24 +01:00
|
|
|
|
if (m_fout2 && st < m_max_size)
|
2017-11-21 19:45:02 +01:00
|
|
|
|
{
|
|
|
|
|
m_zs.avail_in = size;
|
|
|
|
|
m_zs.next_in = m_fptr + st % s_log_size;
|
|
|
|
|
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
m_zs.avail_out = sizeof(m_zout);
|
|
|
|
|
m_zs.next_out = m_zout;
|
|
|
|
|
|
|
|
|
|
if (deflate(&m_zs, Z_NO_FLUSH) == Z_STREAM_ERROR || m_fout2.write(m_zout, sizeof(m_zout) - m_zs.avail_out) != sizeof(m_zout) - m_zs.avail_out)
|
|
|
|
|
{
|
|
|
|
|
deflateEnd(&m_zs);
|
|
|
|
|
m_fout2.close();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
while (m_zs.avail_out == 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_out += size;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-26 22:03:14 +02:00
|
|
|
|
void logs::file_writer::log(logs::level sev, const char* text, std::size_t size)
|
2014-06-27 09:22:00 +02:00
|
|
|
|
{
|
2017-11-23 16:37:08 +01:00
|
|
|
|
if (!m_fptr)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-21 19:45:02 +01:00
|
|
|
|
// TODO: write bigger fragment directly in blocking manner
|
|
|
|
|
while (size && size <= 0xffffff)
|
2017-08-26 22:03:14 +02:00
|
|
|
|
{
|
2017-11-21 19:45:02 +01:00
|
|
|
|
u64 bufv;
|
|
|
|
|
|
2017-11-19 19:59:23 +01:00
|
|
|
|
const auto pos = m_buf.atomic_op([&](u64& v) -> uchar*
|
|
|
|
|
{
|
|
|
|
|
const u64 v1 = v >> 24;
|
|
|
|
|
const u64 v2 = v & 0xffffff;
|
2017-08-26 22:03:14 +02:00
|
|
|
|
|
2017-11-21 19:45:02 +01:00
|
|
|
|
if (UNLIKELY(v2 + size > 0xffffff || v1 + v2 + size >= m_out + s_log_size))
|
2017-11-19 19:59:23 +01:00
|
|
|
|
{
|
2017-11-21 19:45:02 +01:00
|
|
|
|
bufv = v;
|
2017-11-19 19:59:23 +01:00
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2017-08-23 19:44:31 +02:00
|
|
|
|
|
2017-11-19 19:59:23 +01:00
|
|
|
|
v += size;
|
|
|
|
|
return m_fptr + (v1 + v2) % s_log_size;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (UNLIKELY(!pos))
|
|
|
|
|
{
|
2017-11-21 19:45:02 +01:00
|
|
|
|
if ((bufv & 0xffffff) + size > 0xffffff || bufv & 0xffffff)
|
|
|
|
|
{
|
|
|
|
|
// Concurrency limit reached
|
|
|
|
|
std::this_thread::yield();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// Queue is full, need to write out
|
|
|
|
|
flush(bufv);
|
|
|
|
|
}
|
2017-11-19 19:59:23 +01:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (pos + size > m_fptr + s_log_size)
|
|
|
|
|
{
|
|
|
|
|
const auto frag = m_fptr + s_log_size - pos;
|
|
|
|
|
std::memcpy(pos, text, frag);
|
|
|
|
|
std::memcpy(m_fptr, text + frag, size - frag);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
std::memcpy(pos, text, size);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_buf += (u64{size} << 24) - size;
|
|
|
|
|
break;
|
2017-08-26 22:03:14 +02:00
|
|
|
|
}
|
2014-06-27 09:22:00 +02:00
|
|
|
|
}
|
2014-06-17 17:44:03 +02:00
|
|
|
|
|
2017-08-20 23:58:25 +02:00
|
|
|
|
logs::file_listener::file_listener(const std::string& name)
|
|
|
|
|
: file_writer(name)
|
|
|
|
|
, listener()
|
|
|
|
|
{
|
|
|
|
|
// Write UTF-8 BOM
|
2017-08-26 22:03:14 +02:00
|
|
|
|
file_writer::log(logs::level::always, "\xEF\xBB\xBF", 3);
|
2017-08-20 23:58:25 +02:00
|
|
|
|
|
|
|
|
|
// Write initial message
|
|
|
|
|
stored_message ver;
|
|
|
|
|
ver.m.ch = nullptr;
|
|
|
|
|
ver.m.sev = level::always;
|
|
|
|
|
ver.stamp = 0;
|
2017-09-16 21:11:17 +02:00
|
|
|
|
ver.text = fmt::format("RPCS3 v%s | %s\n%s", rpcs3::version.to_string(), rpcs3::get_branch(), utils::get_system_info());
|
2017-09-16 13:54:25 +02:00
|
|
|
|
|
2017-08-26 22:03:14 +02:00
|
|
|
|
file_writer::log(logs::level::always, ver.text.data(), ver.text.size());
|
|
|
|
|
file_writer::log(logs::level::always, "\n", 1);
|
2017-09-16 21:10:55 +02:00
|
|
|
|
messages.emplace_back(std::move(ver));
|
2017-08-20 23:58:25 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-02-22 10:52:03 +01:00
|
|
|
|
void logs::file_listener::log(u64 stamp, const logs::message& msg, const std::string& prefix, const std::string& _text)
|
2014-06-17 17:44:03 +02:00
|
|
|
|
{
|
2017-08-23 22:14:52 +02:00
|
|
|
|
thread_local std::string text;
|
2016-01-12 22:57:16 +01:00
|
|
|
|
|
|
|
|
|
// Used character: U+00B7 (Middle Dot)
|
2016-07-21 00:00:31 +02:00
|
|
|
|
switch (msg.sev)
|
2014-06-17 17:44:03 +02:00
|
|
|
|
{
|
2016-07-21 00:00:31 +02:00
|
|
|
|
case level::always: text = u8"·A "; break;
|
|
|
|
|
case level::fatal: text = u8"·F "; break;
|
|
|
|
|
case level::error: text = u8"·E "; break;
|
|
|
|
|
case level::todo: text = u8"·U "; break;
|
|
|
|
|
case level::success: text = u8"·S "; break;
|
|
|
|
|
case level::warning: text = u8"·W "; break;
|
|
|
|
|
case level::notice: text = u8"·! "; break;
|
|
|
|
|
case level::trace: text = u8"·T "; break;
|
2017-11-02 07:27:57 +01:00
|
|
|
|
case level::_uninit: text = u8"· "; break;
|
2014-06-17 17:44:03 +02:00
|
|
|
|
}
|
2015-04-25 15:29:05 +02:00
|
|
|
|
|
2018-03-16 22:34:16 +01:00
|
|
|
|
// Print µs timestamp
|
2017-02-22 10:52:03 +01:00
|
|
|
|
const u64 hours = stamp / 3600'000'000;
|
|
|
|
|
const u64 mins = (stamp % 3600'000'000) / 60'000'000;
|
|
|
|
|
const u64 secs = (stamp % 60'000'000) / 1'000'000;
|
|
|
|
|
const u64 frac = (stamp % 1'000'000);
|
|
|
|
|
fmt::append(text, "%u:%02u:%02u.%06u ", hours, mins, secs, frac);
|
2015-04-25 15:29:05 +02:00
|
|
|
|
|
2016-08-05 18:49:45 +02:00
|
|
|
|
if (prefix.size() > 0)
|
2016-01-12 22:57:16 +01:00
|
|
|
|
{
|
2016-08-05 18:49:45 +02:00
|
|
|
|
text += "{";
|
|
|
|
|
text += prefix;
|
|
|
|
|
text += "} ";
|
2016-01-12 22:57:16 +01:00
|
|
|
|
}
|
2017-09-16 13:54:25 +02:00
|
|
|
|
|
2017-08-20 23:58:25 +02:00
|
|
|
|
if (msg.ch && '\0' != *msg.ch->name)
|
2016-01-12 22:57:16 +01:00
|
|
|
|
{
|
2016-07-21 00:00:31 +02:00
|
|
|
|
text += msg.ch->name;
|
|
|
|
|
text += msg.sev == level::todo ? " TODO: " : ": ";
|
2016-01-12 22:57:16 +01:00
|
|
|
|
}
|
2016-07-21 00:00:31 +02:00
|
|
|
|
else if (msg.sev == level::todo)
|
2016-01-12 22:57:16 +01:00
|
|
|
|
{
|
2016-07-21 00:00:31 +02:00
|
|
|
|
text += "TODO: ";
|
2016-01-12 22:57:16 +01:00
|
|
|
|
}
|
2017-09-16 13:54:25 +02:00
|
|
|
|
|
2016-08-05 18:49:45 +02:00
|
|
|
|
text += _text;
|
2016-07-21 00:00:31 +02:00
|
|
|
|
text += '\n';
|
2015-01-18 23:54:56 +01:00
|
|
|
|
|
2017-08-26 22:03:14 +02:00
|
|
|
|
file_writer::log(msg.sev, text.data(), text.size());
|
2015-01-18 23:54:56 +01:00
|
|
|
|
}
|