From 707e8afb2917c7652dbe006961ba8ae735cd79d8 Mon Sep 17 00:00:00 2001 From: lizzie Date: Sat, 9 May 2026 05:26:17 +0200 Subject: [PATCH] [common] remove logging thread, simply write in place like a maniac (#3928) theoretically, it's better because distributes load of logging across various threads this should work because 99% of I/O solutions are blocking by default EXCEPT, maybe android differs? please check logcat didn't get affected (again) by me underestimating android ~~stupidity~~ brillaince Signed-off-by: lizzie Reviewed-on: https://git.eden-emu.dev/eden-emu/eden/pulls/3928 Reviewed-by: CamilleLaVey --- src/common/logging.cpp | 58 ++++++++++++------------------------------ 1 file changed, 16 insertions(+), 42 deletions(-) diff --git a/src/common/logging.cpp b/src/common/logging.cpp index 0b82d3e727..68e033b129 100644 --- a/src/common/logging.cpp +++ b/src/common/logging.cpp @@ -349,35 +349,8 @@ struct Impl { // Well, I mean it's the default constructor! explicit Impl() noexcept : filter(Level::Trace) {} - void StartBackendThread() noexcept { - backend_thread = std::jthread([this](std::stop_token stop_token) { - Common::SetCurrentThreadName("Logger"); - Entry entry; - const auto write_logs = [this, &entry]() { - ForEachBackend([&entry](Backend& backend) { - backend.Write(entry); - }); - }; - do { - message_queue.PopWait(entry, stop_token); - write_logs(); - } while (!stop_token.stop_requested()); - // 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. - int max_logs_to_write = filter.IsDebug() ? INT_MAX : 100; - while (max_logs_to_write-- && message_queue.TryPop(entry)) - write_logs(); - }); - } - - void StopBackendThread() noexcept { - backend_thread.request_stop(); - if (backend_thread.joinable()) - backend_thread.join(); - ForEachBackend([](Backend& backend) { backend.Flush(); }); - } - - void ForEachBackend(auto lambda) noexcept { + template + void ForEachBackend(F&& lambda) noexcept { lambda(static_cast(color_console_backend)); #ifndef __OPENORBIS__ if (file_backend) @@ -402,9 +375,7 @@ struct Impl { #ifdef ANDROID LogcatBackend lc_backend{}; #endif - MPSCQueue message_queue{}; std::chrono::steady_clock::time_point time_origin{std::chrono::steady_clock::now()}; - std::jthread backend_thread; }; } // namespace @@ -428,13 +399,11 @@ void Initialize() { } void Start() { - if (logging_instance) - logging_instance->StartBackendThread(); } void Stop() { if (logging_instance) - logging_instance->StopBackendThread(); + logging_instance->ForEachBackend([](Backend& backend) { backend.Flush(); }); } void SetGlobalFilter(const Filter& filter) { @@ -449,14 +418,19 @@ void SetColorConsoleBackendEnabled(bool enabled) { void FmtLogMessageImpl(Class log_class, Level log_level, const char* filename, unsigned int line_num, const char* function, fmt::string_view format, const fmt::format_args& args) { if (logging_instance && logging_instance->filter.CheckMessage(log_class, log_level)) { - logging_instance->message_queue.EmplaceWait(Entry{ - .message = fmt::vformat(format, args), - .timestamp = std::chrono::duration_cast(std::chrono::steady_clock::now() - logging_instance->time_origin), - .log_class = log_class, - .log_level = log_level, - .filename = TrimSourcePath(filename), - .function = function, - .line_num = line_num, + auto const flush = ::Settings::values.log_flush_line.GetValue(); + logging_instance->ForEachBackend([=](Backend& backend) { + backend.Write(Entry{ + .message = fmt::vformat(format, args), + .timestamp = std::chrono::duration_cast(std::chrono::steady_clock::now() - logging_instance->time_origin), + .log_class = log_class, + .log_level = log_level, + .filename = TrimSourcePath(filename), + .function = function, + .line_num = line_num, + }); + if (flush) + backend.Flush(); }); } }