mirror of
https://git.eden-emu.dev/eden-emu/eden
synced 2026-05-14 02:07:07 +02:00
[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 <lizzie@eden-emu.dev> Reviewed-on: https://git.eden-emu.dev/eden-emu/eden/pulls/3928 Reviewed-by: CamilleLaVey <camillelavey99@gmail.com>
This commit is contained in:
parent
bf115ef5a7
commit
707e8afb29
1 changed files with 16 additions and 42 deletions
|
|
@ -349,35 +349,8 @@ struct Impl {
|
||||||
// Well, I mean it's the default constructor!
|
// Well, I mean it's the default constructor!
|
||||||
explicit Impl() noexcept : filter(Level::Trace) {}
|
explicit Impl() noexcept : filter(Level::Trace) {}
|
||||||
|
|
||||||
void StartBackendThread() noexcept {
|
template<typename F>
|
||||||
backend_thread = std::jthread([this](std::stop_token stop_token) {
|
void ForEachBackend(F&& lambda) noexcept {
|
||||||
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 {
|
|
||||||
lambda(static_cast<Backend&>(color_console_backend));
|
lambda(static_cast<Backend&>(color_console_backend));
|
||||||
#ifndef __OPENORBIS__
|
#ifndef __OPENORBIS__
|
||||||
if (file_backend)
|
if (file_backend)
|
||||||
|
|
@ -402,9 +375,7 @@ struct Impl {
|
||||||
#ifdef ANDROID
|
#ifdef ANDROID
|
||||||
LogcatBackend lc_backend{};
|
LogcatBackend lc_backend{};
|
||||||
#endif
|
#endif
|
||||||
MPSCQueue<Entry> message_queue{};
|
|
||||||
std::chrono::steady_clock::time_point time_origin{std::chrono::steady_clock::now()};
|
std::chrono::steady_clock::time_point time_origin{std::chrono::steady_clock::now()};
|
||||||
std::jthread backend_thread;
|
|
||||||
};
|
};
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
|
|
@ -428,13 +399,11 @@ void Initialize() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void Start() {
|
void Start() {
|
||||||
if (logging_instance)
|
|
||||||
logging_instance->StartBackendThread();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Stop() {
|
void Stop() {
|
||||||
if (logging_instance)
|
if (logging_instance)
|
||||||
logging_instance->StopBackendThread();
|
logging_instance->ForEachBackend([](Backend& backend) { backend.Flush(); });
|
||||||
}
|
}
|
||||||
|
|
||||||
void SetGlobalFilter(const Filter& filter) {
|
void SetGlobalFilter(const Filter& filter) {
|
||||||
|
|
@ -449,7 +418,9 @@ 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) {
|
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)) {
|
if (logging_instance && logging_instance->filter.CheckMessage(log_class, log_level)) {
|
||||||
logging_instance->message_queue.EmplaceWait(Entry{
|
auto const flush = ::Settings::values.log_flush_line.GetValue();
|
||||||
|
logging_instance->ForEachBackend([=](Backend& backend) {
|
||||||
|
backend.Write(Entry{
|
||||||
.message = fmt::vformat(format, args),
|
.message = fmt::vformat(format, args),
|
||||||
.timestamp = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::steady_clock::now() - logging_instance->time_origin),
|
.timestamp = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::steady_clock::now() - logging_instance->time_origin),
|
||||||
.log_class = log_class,
|
.log_class = log_class,
|
||||||
|
|
@ -458,6 +429,9 @@ void FmtLogMessageImpl(Class log_class, Level log_level, const char* filename, u
|
||||||
.function = function,
|
.function = function,
|
||||||
.line_num = line_num,
|
.line_num = line_num,
|
||||||
});
|
});
|
||||||
|
if (flush)
|
||||||
|
backend.Flush();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} // namespace Common::Log
|
} // namespace Common::Log
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue