1
0
Fork 0
forked from suyu/suyu

logging: Make use of bounded queue

This commit is contained in:
Morph 2023-03-19 03:20:05 -04:00
parent 306840a580
commit f28ca5361f

View file

@ -28,7 +28,7 @@
#ifdef _WIN32 #ifdef _WIN32
#include "common/string_util.h" #include "common/string_util.h"
#endif #endif
#include "common/threadsafe_queue.h" #include "common/bounded_threadsafe_queue.h"
namespace Common::Log { namespace Common::Log {
@ -204,11 +204,11 @@ public:
void PushEntry(Class log_class, Level log_level, const char* filename, unsigned int line_num, void PushEntry(Class log_class, Level log_level, const char* filename, unsigned int line_num,
const char* function, std::string&& message) { const char* function, std::string&& message) {
if (!filter.CheckMessage(log_class, log_level)) if (!filter.CheckMessage(log_class, log_level)) {
return; return;
const Entry& entry = }
CreateEntry(log_class, log_level, filename, line_num, function, std::move(message)); message_queue.Push(
message_queue.Push(entry); CreateEntry(log_class, log_level, filename, line_num, function, std::move(message)));
} }
private: private:
@ -225,7 +225,7 @@ private:
ForEachBackend([&entry](Backend& backend) { backend.Write(entry); }); ForEachBackend([&entry](Backend& backend) { backend.Write(entry); });
}; };
while (!stop_token.stop_requested()) { while (!stop_token.stop_requested()) {
entry = message_queue.PopWait(stop_token); message_queue.PopWait(entry, stop_token);
if (entry.filename != nullptr) { if (entry.filename != nullptr) {
write_logs(); write_logs();
} }
@ -233,7 +233,7 @@ private:
// Drain the logging queue. Only writes out up to MAX_LOGS_TO_WRITE to prevent a // 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. // case where a system is repeatedly spamming logs even on close.
int max_logs_to_write = filter.IsDebug() ? INT_MAX : 100; int max_logs_to_write = filter.IsDebug() ? INT_MAX : 100;
while (max_logs_to_write-- && message_queue.Pop(entry)) { while (max_logs_to_write-- && message_queue.TryPop(entry)) {
write_logs(); write_logs();
} }
}); });
@ -273,7 +273,7 @@ private:
ColorConsoleBackend color_console_backend{}; ColorConsoleBackend color_console_backend{};
FileBackend file_backend; FileBackend file_backend;
MPSCQueue<Entry, true> message_queue{}; 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; std::jthread backend_thread;
}; };