2014-10-28 07:36:00 +00:00
|
|
|
// Copyright 2014 Citra Emulator Project
|
2014-12-17 05:38:14 +00:00
|
|
|
// Licensed under GPLv2 or any later version
|
2014-10-28 07:36:00 +00:00
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2018-07-02 18:10:41 +01:00
|
|
|
#include <algorithm>
|
2018-07-20 20:31:25 +01:00
|
|
|
#include <atomic>
|
2018-07-02 18:10:41 +01:00
|
|
|
#include <chrono>
|
2018-07-14 19:47:14 +01:00
|
|
|
#include <climits>
|
2018-07-02 18:10:41 +01:00
|
|
|
#include <condition_variable>
|
|
|
|
#include <memory>
|
2018-07-20 20:31:25 +01:00
|
|
|
#include <mutex>
|
2018-07-02 18:10:41 +01:00
|
|
|
#include <thread>
|
2018-07-20 20:31:25 +01:00
|
|
|
#include <vector>
|
2021-05-26 00:32:56 +01:00
|
|
|
|
2018-07-02 18:10:41 +01:00
|
|
|
#ifdef _WIN32
|
2018-12-07 15:21:18 +00:00
|
|
|
#include <windows.h> // For OutputDebugStringW
|
2018-07-02 18:10:41 +01:00
|
|
|
#endif
|
2021-05-26 00:32:56 +01:00
|
|
|
|
2015-08-02 17:55:31 +01:00
|
|
|
#include "common/assert.h"
|
2021-06-13 12:52:02 +01:00
|
|
|
#include "common/fs/file.h"
|
2021-05-26 00:32:56 +01:00
|
|
|
#include "common/fs/fs.h"
|
2021-06-23 22:18:27 +01:00
|
|
|
#include "common/literals.h"
|
|
|
|
|
2016-09-21 07:52:38 +01:00
|
|
|
#include "common/logging/backend.h"
|
2014-10-28 07:36:00 +00:00
|
|
|
#include "common/logging/log.h"
|
|
|
|
#include "common/logging/text_formatter.h"
|
2021-04-15 00:07:40 +01:00
|
|
|
#include "common/settings.h"
|
2018-03-22 10:21:29 +00:00
|
|
|
#include "common/string_util.h"
|
2018-07-02 18:10:41 +01:00
|
|
|
#include "common/threadsafe_queue.h"
|
2014-10-28 07:36:00 +00:00
|
|
|
|
2021-04-15 01:19:52 +01:00
|
|
|
namespace Common::Log {
|
2014-10-28 07:36:00 +00:00
|
|
|
|
2018-07-02 18:10:41 +01:00
|
|
|
/**
|
|
|
|
* Static state as a singleton.
|
|
|
|
*/
|
|
|
|
class Impl {
|
|
|
|
public:
|
|
|
|
static Impl& Instance() {
|
|
|
|
static Impl backend;
|
|
|
|
return backend;
|
|
|
|
}
|
|
|
|
|
2021-04-15 04:05:42 +01:00
|
|
|
Impl(const Impl&) = delete;
|
|
|
|
Impl& operator=(const Impl&) = delete;
|
|
|
|
|
|
|
|
Impl(Impl&&) = delete;
|
|
|
|
Impl& operator=(Impl&&) = delete;
|
2018-07-02 18:10:41 +01:00
|
|
|
|
2019-03-02 19:25:50 +00: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-07-02 18:10:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void AddBackend(std::unique_ptr<Backend> backend) {
|
2019-04-01 17:29:59 +01:00
|
|
|
std::lock_guard lock{writing_mutex};
|
2018-07-02 18:10:41 +01:00
|
|
|
backends.push_back(std::move(backend));
|
|
|
|
}
|
|
|
|
|
2018-07-20 20:27:17 +01:00
|
|
|
void RemoveBackend(std::string_view backend_name) {
|
2019-04-01 17:29:59 +01:00
|
|
|
std::lock_guard lock{writing_mutex};
|
2021-04-20 17:53:02 +01:00
|
|
|
|
|
|
|
std::erase_if(backends, [&backend_name](const auto& backend) {
|
|
|
|
return backend_name == backend->GetName();
|
|
|
|
});
|
2018-07-02 18:10:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const Filter& GetGlobalFilter() const {
|
|
|
|
return filter;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetGlobalFilter(const Filter& f) {
|
|
|
|
filter = f;
|
|
|
|
}
|
|
|
|
|
2018-07-20 20:27:17 +01: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-07-02 18:10:41 +01: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 17:29:59 +01:00
|
|
|
std::lock_guard lock{writing_mutex};
|
2018-07-02 18:10:41 +01:00
|
|
|
for (const auto& backend : backends) {
|
|
|
|
backend->Write(e);
|
|
|
|
}
|
|
|
|
};
|
2018-10-08 22:28:54 +01:00
|
|
|
while (true) {
|
|
|
|
entry = message_queue.PopWait();
|
2018-09-09 12:08:57 +01:00
|
|
|
if (entry.final_entry) {
|
2018-07-02 18:10:41 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
write_logs(entry);
|
|
|
|
}
|
2018-09-09 12:08:57 +01:00
|
|
|
|
2021-06-23 22:18:27 +01: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.
|
2018-07-14 19:47:14 +01:00
|
|
|
const int MAX_LOGS_TO_WRITE = filter.IsDebug() ? INT_MAX : 100;
|
2018-07-02 18:10:41 +01:00
|
|
|
int logs_written = 0;
|
|
|
|
while (logs_written++ < MAX_LOGS_TO_WRITE && message_queue.Pop(entry)) {
|
|
|
|
write_logs(entry);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
~Impl() {
|
2018-09-09 12:08:57 +01:00
|
|
|
Entry entry;
|
|
|
|
entry.final_entry = true;
|
|
|
|
message_queue.Push(entry);
|
2018-07-02 18:10:41 +01:00
|
|
|
backend_thread.join();
|
|
|
|
}
|
|
|
|
|
2019-03-02 19:25:50 +00: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 15:31:57 +01:00
|
|
|
using std::chrono::microseconds;
|
2019-03-02 19:25:50 +00:00
|
|
|
using std::chrono::steady_clock;
|
|
|
|
|
2020-08-03 15:31:57 +01: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 19:25:50 +00:00
|
|
|
}
|
|
|
|
|
2018-09-09 12:08:57 +01:00
|
|
|
std::mutex writing_mutex;
|
2018-07-02 18:10:41 +01:00
|
|
|
std::thread backend_thread;
|
|
|
|
std::vector<std::unique_ptr<Backend>> backends;
|
2021-04-15 01:19:52 +01:00
|
|
|
MPSCQueue<Entry> message_queue;
|
2018-07-02 18:10:41 +01:00
|
|
|
Filter filter;
|
2019-03-02 19:28:58 +00:00
|
|
|
std::chrono::steady_clock::time_point time_origin{std::chrono::steady_clock::now()};
|
2018-07-02 18:10:41 +01:00
|
|
|
};
|
|
|
|
|
2021-06-13 12:52:02 +01:00
|
|
|
ConsoleBackend::~ConsoleBackend() = default;
|
|
|
|
|
2018-07-02 18:10:41 +01:00
|
|
|
void ConsoleBackend::Write(const Entry& entry) {
|
|
|
|
PrintMessage(entry);
|
|
|
|
}
|
|
|
|
|
2021-06-13 12:52:02 +01:00
|
|
|
ColorConsoleBackend::~ColorConsoleBackend() = default;
|
|
|
|
|
2018-07-02 18:10:41 +01:00
|
|
|
void ColorConsoleBackend::Write(const Entry& entry) {
|
|
|
|
PrintColoredMessage(entry);
|
|
|
|
}
|
|
|
|
|
2021-05-26 00:32:56 +01:00
|
|
|
FileBackend::FileBackend(const std::filesystem::path& filename) {
|
|
|
|
auto old_filename = filename;
|
|
|
|
old_filename += ".old.txt";
|
2021-04-20 17:57:45 +01:00
|
|
|
|
2021-05-26 00:32:56 +01:00
|
|
|
// Existence checks are done within the functions themselves.
|
|
|
|
// We don't particularly care if these succeed or not.
|
2021-06-19 08:43:16 +01:00
|
|
|
FS::RemoveFile(old_filename);
|
2021-05-26 00:32:56 +01:00
|
|
|
void(FS::RenameFile(filename, old_filename));
|
2020-12-31 21:10:01 +00:00
|
|
|
|
2021-06-13 12:52:02 +01:00
|
|
|
file =
|
|
|
|
std::make_unique<FS::IOFile>(filename, FS::FileAccessMode::Write, FS::FileType::TextFile);
|
2020-12-31 21:10:01 +00:00
|
|
|
}
|
2018-07-02 18:10:41 +01:00
|
|
|
|
2021-06-13 12:52:02 +01:00
|
|
|
FileBackend::~FileBackend() = default;
|
|
|
|
|
2018-07-02 18:10:41 +01:00
|
|
|
void FileBackend::Write(const Entry& entry) {
|
2021-06-23 22:18:27 +01:00
|
|
|
using namespace Common::Literals;
|
2018-07-02 18:10:41 +01:00
|
|
|
// prevent logs from going over the maximum size (in case its spamming and the user doesn't
|
|
|
|
// know)
|
2021-06-23 22:18:27 +01:00
|
|
|
constexpr std::size_t MAX_BYTES_WRITTEN = 100_MiB;
|
|
|
|
constexpr std::size_t MAX_BYTES_WRITTEN_EXTENDED = 1_GiB;
|
2020-07-29 18:25:37 +01:00
|
|
|
|
2021-06-13 12:52:02 +01:00
|
|
|
if (!file->IsOpen()) {
|
2020-07-29 18:25:37 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Settings::values.extended_logging && bytes_written > MAX_BYTES_WRITTEN_EXTENDED) {
|
|
|
|
return;
|
|
|
|
} else if (!Settings::values.extended_logging && bytes_written > MAX_BYTES_WRITTEN) {
|
2018-07-02 18:10:41 +01:00
|
|
|
return;
|
|
|
|
}
|
2020-07-29 18:25:37 +01:00
|
|
|
|
2021-06-13 12:52:02 +01:00
|
|
|
bytes_written += file->WriteString(FormatLogMessage(entry).append(1, '\n'));
|
2018-07-02 18:10:41 +01:00
|
|
|
if (entry.log_level >= Level::Error) {
|
2021-06-19 08:48:02 +01:00
|
|
|
file->Flush();
|
2018-07-02 18:10:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-13 12:52:02 +01:00
|
|
|
DebuggerBackend::~DebuggerBackend() = default;
|
|
|
|
|
2018-10-05 04:22:49 +01:00
|
|
|
void DebuggerBackend::Write(const Entry& entry) {
|
|
|
|
#ifdef _WIN32
|
2021-04-15 01:19:52 +01:00
|
|
|
::OutputDebugStringW(UTF8ToUTF16W(FormatLogMessage(entry).append(1, '\n')).c_str());
|
2018-10-05 04:22:49 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-07-02 18:10:41 +01:00
|
|
|
void SetGlobalFilter(const Filter& filter) {
|
|
|
|
Impl::Instance().SetGlobalFilter(filter);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AddBackend(std::unique_ptr<Backend> backend) {
|
|
|
|
Impl::Instance().AddBackend(std::move(backend));
|
|
|
|
}
|
2015-03-06 18:15:02 +00:00
|
|
|
|
2018-07-20 20:27:17 +01:00
|
|
|
void RemoveBackend(std::string_view backend_name) {
|
2018-07-02 18:10:41 +01:00
|
|
|
Impl::Instance().RemoveBackend(backend_name);
|
|
|
|
}
|
|
|
|
|
2018-07-20 20:27:17 +01:00
|
|
|
Backend* GetBackend(std::string_view backend_name) {
|
2018-07-02 18:10:41 +01:00
|
|
|
return Impl::Instance().GetBackend(backend_name);
|
2015-03-06 18:15:02 +00:00
|
|
|
}
|
|
|
|
|
2018-04-06 05:42:09 +01: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-14 02:44:53 +01:00
|
|
|
auto& instance = Impl::Instance();
|
|
|
|
const auto& filter = instance.GetGlobalFilter();
|
2018-07-02 18:10:41 +01:00
|
|
|
if (!filter.CheckMessage(log_class, log_level))
|
2018-03-22 10:21:29 +00:00
|
|
|
return;
|
2018-07-02 18:10:41 +01:00
|
|
|
|
2019-03-02 19:25:50 +00:00
|
|
|
instance.PushEntry(log_class, log_level, filename, line_num, function,
|
|
|
|
fmt::vformat(format, args));
|
2014-10-28 07:36:00 +00:00
|
|
|
}
|
2021-04-15 01:19:52 +01:00
|
|
|
} // namespace Common::Log
|