2014-10-28 05:36:00 -02:00
|
|
|
// Copyright 2014 Citra Emulator Project
|
2014-12-16 21:38:14 -08:00
|
|
|
// Licensed under GPLv2 or any later version
|
2014-10-28 05:36:00 -02:00
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#include <algorithm>
|
2018-02-19 17:51:27 -07:00
|
|
|
#include <chrono>
|
|
|
|
#include <condition_variable>
|
|
|
|
#include <memory>
|
2018-07-20 15:31:25 -04:00
|
|
|
#include <mutex>
|
2018-02-19 17:51:27 -07:00
|
|
|
#include <thread>
|
2018-07-20 15:31:25 -04:00
|
|
|
#include <vector>
|
2018-02-19 17:51:27 -07:00
|
|
|
#ifdef _WIN32
|
2018-10-05 12:52:49 +09:30
|
|
|
#include <share.h> // For _SH_DENYWR
|
|
|
|
#include <windows.h> // For OutputDebugStringW
|
2018-02-19 17:51:27 -07:00
|
|
|
#else
|
|
|
|
#define _SH_DENYWR 0
|
|
|
|
#endif
|
2023-06-24 02:00:26 +03:00
|
|
|
#include "common/file_util.h"
|
2016-09-20 23:52:38 -07:00
|
|
|
#include "common/logging/backend.h"
|
2014-10-28 05:36:00 -02:00
|
|
|
#include "common/logging/log.h"
|
|
|
|
#include "common/logging/text_formatter.h"
|
2018-02-19 17:51:27 -07:00
|
|
|
#include "common/string_util.h"
|
2018-02-19 17:51:27 -07:00
|
|
|
#include "common/threadsafe_queue.h"
|
2014-10-28 05:36:00 -02:00
|
|
|
|
2023-06-24 01:51:16 +03:00
|
|
|
namespace Common::Log {
|
2014-10-28 05:36:00 -02:00
|
|
|
|
2018-02-19 17:51:27 -07:00
|
|
|
/**
|
|
|
|
* Static state as a singleton.
|
|
|
|
*/
|
|
|
|
class Impl {
|
|
|
|
public:
|
|
|
|
static Impl& Instance() {
|
|
|
|
static Impl backend;
|
|
|
|
return backend;
|
|
|
|
}
|
|
|
|
|
2021-04-14 23:05:42 -04:00
|
|
|
Impl(const Impl&) = delete;
|
|
|
|
Impl& operator=(const Impl&) = delete;
|
|
|
|
|
|
|
|
Impl(Impl&&) = delete;
|
|
|
|
Impl& operator=(Impl&&) = delete;
|
2018-02-19 17:51:27 -07:00
|
|
|
|
2019-03-02 14:25:50 -05:00
|
|
|
void PushEntry(Class log_class, Level log_level, const char* filename, unsigned int line_num,
|
|
|
|
const char* function, std::string message) {
|
|
|
|
message_queue.Push(
|
|
|
|
CreateEntry(log_class, log_level, filename, line_num, function, std::move(message)));
|
2018-02-19 17:51:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void AddBackend(std::unique_ptr<Backend> backend) {
|
2019-04-01 12:29:59 -04:00
|
|
|
std::lock_guard lock{writing_mutex};
|
2018-02-19 17:51:27 -07:00
|
|
|
backends.push_back(std::move(backend));
|
|
|
|
}
|
|
|
|
|
2018-07-20 15:27:17 -04:00
|
|
|
void RemoveBackend(std::string_view backend_name) {
|
2019-04-01 12:29:59 -04:00
|
|
|
std::lock_guard lock{writing_mutex};
|
2021-04-20 12:53:02 -04:00
|
|
|
|
|
|
|
std::erase_if(backends, [&backend_name](const auto& backend) {
|
|
|
|
return backend_name == backend->GetName();
|
|
|
|
});
|
2018-02-19 17:51:27 -07:00
|
|
|
}
|
|
|
|
|
2023-06-24 02:27:24 +03:00
|
|
|
const Filter& GetGlobalFilter() const {
|
|
|
|
return filter;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetGlobalFilter(const Filter& f) {
|
|
|
|
filter = f;
|
|
|
|
}
|
|
|
|
|
2018-07-20 15:27:17 -04:00
|
|
|
Backend* GetBackend(std::string_view backend_name) {
|
|
|
|
const auto it =
|
|
|
|
std::find_if(backends.begin(), backends.end(),
|
|
|
|
[&backend_name](const auto& i) { return backend_name == i->GetName(); });
|
2018-02-19 17:51:27 -07:00
|
|
|
if (it == backends.end())
|
|
|
|
return nullptr;
|
|
|
|
return it->get();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Impl() {
|
|
|
|
backend_thread = std::thread([&] {
|
|
|
|
Entry entry;
|
|
|
|
auto write_logs = [&](Entry& e) {
|
2019-04-01 12:29:59 -04:00
|
|
|
std::lock_guard lock{writing_mutex};
|
2018-02-19 17:51:27 -07:00
|
|
|
for (const auto& backend : backends) {
|
|
|
|
backend->Write(e);
|
|
|
|
}
|
|
|
|
};
|
2018-10-08 23:28:54 +02:00
|
|
|
while (true) {
|
|
|
|
entry = message_queue.PopWait();
|
2018-09-09 13:08:57 +02:00
|
|
|
if (entry.final_entry) {
|
2018-02-19 17:51:27 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
write_logs(entry);
|
|
|
|
}
|
2018-09-09 13:08:57 +02:00
|
|
|
|
2018-02-19 17:51:27 -07:00
|
|
|
// Drain the logging queue. Only writes out up to MAX_LOGS_TO_WRITE to prevent a case
|
|
|
|
// where a system is repeatedly spamming logs even on close.
|
|
|
|
constexpr int MAX_LOGS_TO_WRITE = 100;
|
|
|
|
int logs_written = 0;
|
|
|
|
while (logs_written++ < MAX_LOGS_TO_WRITE && message_queue.Pop(entry)) {
|
|
|
|
write_logs(entry);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
~Impl() {
|
2018-09-09 13:08:57 +02:00
|
|
|
Entry entry;
|
|
|
|
entry.final_entry = true;
|
|
|
|
message_queue.Push(entry);
|
2018-02-19 17:51:27 -07:00
|
|
|
backend_thread.join();
|
|
|
|
}
|
|
|
|
|
2019-03-02 14:25:50 -05:00
|
|
|
Entry CreateEntry(Class log_class, Level log_level, const char* filename, unsigned int line_nr,
|
|
|
|
const char* function, std::string message) const {
|
|
|
|
using std::chrono::duration_cast;
|
2020-08-03 10:31:57 -04:00
|
|
|
using std::chrono::microseconds;
|
2019-03-02 14:25:50 -05:00
|
|
|
using std::chrono::steady_clock;
|
|
|
|
|
2020-08-03 10:31:57 -04:00
|
|
|
return {
|
|
|
|
.timestamp = duration_cast<microseconds>(steady_clock::now() - time_origin),
|
|
|
|
.log_class = log_class,
|
|
|
|
.log_level = log_level,
|
|
|
|
.filename = filename,
|
|
|
|
.line_num = line_nr,
|
|
|
|
.function = function,
|
|
|
|
.message = std::move(message),
|
|
|
|
.final_entry = false,
|
|
|
|
};
|
2019-03-02 14:25:50 -05:00
|
|
|
}
|
|
|
|
|
2018-09-09 13:08:57 +02:00
|
|
|
std::mutex writing_mutex;
|
2018-02-19 17:51:27 -07:00
|
|
|
std::thread backend_thread;
|
|
|
|
std::vector<std::unique_ptr<Backend>> backends;
|
|
|
|
Common::MPSCQueue<Log::Entry> message_queue;
|
|
|
|
Filter filter;
|
2019-03-02 14:28:58 -05:00
|
|
|
std::chrono::steady_clock::time_point time_origin{std::chrono::steady_clock::now()};
|
2018-02-19 17:51:27 -07:00
|
|
|
};
|
|
|
|
|
2023-06-24 02:00:26 +03:00
|
|
|
ConsoleBackend::~ConsoleBackend() = default;
|
|
|
|
|
2018-02-19 17:51:27 -07:00
|
|
|
void ConsoleBackend::Write(const Entry& entry) {
|
|
|
|
PrintMessage(entry);
|
|
|
|
}
|
|
|
|
|
2023-06-24 02:00:26 +03:00
|
|
|
ColorConsoleBackend::~ColorConsoleBackend() = default;
|
|
|
|
|
2018-02-19 17:51:27 -07:00
|
|
|
void ColorConsoleBackend::Write(const Entry& entry) {
|
|
|
|
PrintColoredMessage(entry);
|
|
|
|
}
|
|
|
|
|
2023-06-24 02:00:26 +03:00
|
|
|
LogcatBackend::~LogcatBackend() = default;
|
|
|
|
|
2020-05-09 19:05:12 +05:30
|
|
|
void LogcatBackend::Write(const Entry& entry) {
|
|
|
|
PrintMessageToLogcat(entry);
|
|
|
|
}
|
|
|
|
|
2021-04-20 12:57:45 -04:00
|
|
|
FileBackend::FileBackend(const std::string& filename) {
|
|
|
|
const auto old_filename = filename + ".old.txt";
|
|
|
|
|
|
|
|
if (FileUtil::Exists(old_filename)) {
|
|
|
|
FileUtil::Delete(old_filename);
|
2021-01-03 19:53:23 +00:00
|
|
|
}
|
2020-12-31 16:10:01 -05:00
|
|
|
if (FileUtil::Exists(filename)) {
|
2021-04-20 12:57:45 -04:00
|
|
|
FileUtil::Rename(filename, old_filename);
|
2020-12-31 16:10:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// _SH_DENYWR allows read only access to the file for other programs.
|
|
|
|
// It is #defined to 0 on other platforms
|
2023-06-24 02:00:26 +03:00
|
|
|
file = std::make_unique<FileUtil::IOFile>(filename, "w", _SH_DENYWR);
|
2020-12-31 16:10:01 -05:00
|
|
|
}
|
2018-02-19 17:51:27 -07:00
|
|
|
|
2023-06-24 02:00:26 +03:00
|
|
|
FileBackend::~FileBackend() = default;
|
|
|
|
|
2018-02-19 17:51:27 -07:00
|
|
|
void FileBackend::Write(const Entry& entry) {
|
|
|
|
// prevent logs from going over the maximum size (in case its spamming and the user doesn't
|
|
|
|
// know)
|
2018-09-06 16:03:28 -04:00
|
|
|
constexpr std::size_t MAX_BYTES_WRITTEN = 50 * 1024L * 1024L;
|
2023-06-24 02:00:26 +03:00
|
|
|
if (!file->IsOpen() || bytes_written > MAX_BYTES_WRITTEN) {
|
2018-02-19 17:51:27 -07:00
|
|
|
return;
|
|
|
|
}
|
2023-06-24 02:00:26 +03:00
|
|
|
bytes_written += file->WriteString(FormatLogMessage(entry).append(1, '\n'));
|
2018-02-19 17:51:27 -07:00
|
|
|
if (entry.log_level >= Level::Error) {
|
2023-06-24 02:00:26 +03:00
|
|
|
file->Flush();
|
2018-02-19 17:51:27 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-24 02:27:24 +03:00
|
|
|
DebuggerBackend::~DebuggerBackend() = default;
|
|
|
|
|
2018-10-05 12:52:49 +09:30
|
|
|
void DebuggerBackend::Write(const Entry& entry) {
|
|
|
|
#ifdef _WIN32
|
|
|
|
::OutputDebugStringW(Common::UTF8ToUTF16W(FormatLogMessage(entry).append(1, '\n')).c_str());
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2023-06-24 02:27:24 +03:00
|
|
|
void SetGlobalFilter(const Filter& filter) {
|
|
|
|
Impl::Instance().SetGlobalFilter(filter);
|
2014-10-28 05:36:00 -02:00
|
|
|
}
|
|
|
|
|
2018-02-19 17:51:27 -07:00
|
|
|
void AddBackend(std::unique_ptr<Backend> backend) {
|
|
|
|
Impl::Instance().AddBackend(std::move(backend));
|
|
|
|
}
|
|
|
|
|
2018-07-20 15:27:17 -04:00
|
|
|
void RemoveBackend(std::string_view backend_name) {
|
2018-02-19 17:51:27 -07:00
|
|
|
Impl::Instance().RemoveBackend(backend_name);
|
|
|
|
}
|
|
|
|
|
2018-07-20 15:27:17 -04:00
|
|
|
Backend* GetBackend(std::string_view backend_name) {
|
2018-02-19 17:51:27 -07:00
|
|
|
return Impl::Instance().GetBackend(backend_name);
|
2015-03-06 19:15:02 +01:00
|
|
|
}
|
|
|
|
|
2018-04-05 22:33:12 -06:00
|
|
|
void FmtLogMessageImpl(Class log_class, Level log_level, const char* filename,
|
|
|
|
unsigned int line_num, const char* function, const char* format,
|
|
|
|
const fmt::format_args& args) {
|
2018-08-13 21:44:53 -04:00
|
|
|
auto& instance = Impl::Instance();
|
2023-06-24 02:27:24 +03:00
|
|
|
const auto& filter = instance.GetGlobalFilter();
|
|
|
|
if (!filter.CheckMessage(log_class, log_level))
|
|
|
|
return;
|
|
|
|
|
2019-03-02 14:25:50 -05:00
|
|
|
instance.PushEntry(log_class, log_level, filename, line_num, function,
|
|
|
|
fmt::vformat(format, args));
|
2014-10-28 05:36:00 -02:00
|
|
|
}
|
2023-06-24 01:51:16 +03:00
|
|
|
} // namespace Common::Log
|