From d7874664af04ac7fd56cfffbaba4511b3a77f12d Mon Sep 17 00:00:00 2001 From: Yuqian Yang Date: Mon, 17 Nov 2025 13:19:34 +0800 Subject: Add stdio logger. --- src/base/log/Logger.cpp | 94 ++++++++++++++++++++++++----------------- src/platform/gui/win/Window.cpp | 12 +++--- 2 files changed, 62 insertions(+), 44 deletions(-) (limited to 'src') diff --git a/src/base/log/Logger.cpp b/src/base/log/Logger.cpp index c5d8b0b5..a3582305 100644 --- a/src/base/log/Logger.cpp +++ b/src/base/log/Logger.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include @@ -15,33 +16,15 @@ #endif namespace cru::log { -Logger *Logger::GetInstance() { - static Logger logger; - - logger.AddLogTarget(std::make_unique()); - -#ifdef _WIN32 - logger.AddLogTarget(std::make_unique()); -#endif +ILogger* ILogger::GetInstance() { + static SimpleStdioLogger logger; + logger.LoadDebugTagFromEnv(); return &logger; } -void Logger::AddLogTarget(std::unique_ptr target) { - std::lock_guard lock(target_list_mutex_); - target_list_.push_back(std::move(target)); -} - -void Logger::RemoveLogTarget(ILogTarget *target) { - std::lock_guard lock(target_list_mutex_); - target_list_.erase( - std::remove_if(target_list_.begin(), target_list_.end(), - [target](const auto &t) { return t.get() == target; }), - target_list_.end()); -} - namespace { -const char *LogLevelToString(LogLevel level) { +const char* LogLevelToString(LogLevel level) { switch (level) { case LogLevel::Debug: return "DEBUG"; @@ -63,15 +46,46 @@ std::string GetLogTime() { calendar->tm_sec); } -std::string MakeLogFinalMessage(const LogInfo &log_info) { +std::string MakeLogFinalMessage(const LogInfo& log_info) { return std::format("[{}] {} {}: {}", GetLogTime(), LogLevelToString(log_info.level), log_info.tag, log_info.message); } } // namespace +void ILogger::LoadDebugTagFromEnv(const char* env_var, std::string sep) { + auto env = std::getenv(env_var); + if (env != nullptr) { + for (auto tag : cru::string::Split(std::string(env), sep)) { + AddDebugTag(std::move(tag)); + } + } +} + +void SimpleStdioLogger::AddDebugTag(std::string tag) { + debug_tags_.insert(tag); +} + +void SimpleStdioLogger::RemoveDebugTag(const std::string& tag) { + debug_tags_.erase(tag); +} + +void SimpleStdioLogger::Log(LogInfo log_info) { + if (log_info.level == LogLevel::Debug && + std::ranges::none_of(debug_tags_, [&log_info](const std::string& tag) { + return log_info.tag.starts_with(tag); + })) { + return; + } + std::clog << MakeLogFinalMessage(log_info) << std::endl; +} + Logger::Logger() : log_stop_(false), log_thread_(&Logger::LogThreadRun, this) { - LoadDebugTagFromEnv(); + AddLogTarget(std::make_unique()); + +#ifdef _WIN32 + AddLogTarget(std::make_unique()); +#endif } Logger::~Logger() { @@ -88,18 +102,22 @@ void Logger::AddDebugTag(std::string tag) { debug_tags_.insert(std::move(tag)); } -void Logger::RemoveDebugTag(const std::string &tag) { +void Logger::RemoveDebugTag(const std::string& tag) { std::unique_lock lock(log_queue_mutex_); debug_tags_.erase(tag); } -void Logger::LoadDebugTagFromEnv(const char *env_var, std::string sep) { - auto env = std::getenv(env_var); - if (env != nullptr) { - for (auto tag : cru::string::Split(std::string(env), sep)) { - AddDebugTag(std::move(tag)); - } - } +void Logger::AddLogTarget(std::unique_ptr target) { + std::lock_guard lock(target_list_mutex_); + target_list_.push_back(std::move(target)); +} + +void Logger::RemoveLogTarget(ILogTarget* target) { + std::lock_guard lock(target_list_mutex_); + target_list_.erase( + std::remove_if(target_list_.begin(), target_list_.end(), + [target](const auto& t) { return t.get() == target; }), + target_list_.end()); } void Logger::Log(LogInfo log_info) { @@ -113,7 +131,7 @@ void Logger::LogThreadRun() { bool stop = false; while (true) { - std::vector target_list; + std::vector target_list; { std::unique_lock lock(log_queue_mutex_); @@ -126,16 +144,16 @@ void Logger::LogThreadRun() { { std::lock_guard lock_guard(target_list_mutex_); - for (const auto &target : target_list_) { + for (const auto& target : target_list_) { target_list.push_back(target.get()); } } - for (const auto &target : target_list) { - for (auto &log_info : queue) { + for (const auto& target : target_list) { + for (auto& log_info : queue) { if (log_info.level == LogLevel::Debug && std::ranges::none_of(debug_tags_, - [&log_info](const std::string &tag) { + [&log_info](const std::string& tag) { return log_info.tag.starts_with(tag); })) { continue; @@ -150,7 +168,7 @@ void Logger::LogThreadRun() { } } -LoggerCppStream::LoggerCppStream(Logger *logger, LogLevel level, +LoggerCppStream::LoggerCppStream(ILogger* logger, LogLevel level, std::string tag) : logger_(logger), level_(level), tag_(std::move(tag)) {} diff --git a/src/platform/gui/win/Window.cpp b/src/platform/gui/win/Window.cpp index 5046868e..dfbe0f96 100644 --- a/src/platform/gui/win/Window.cpp +++ b/src/platform/gui/win/Window.cpp @@ -138,8 +138,8 @@ void WinNativeWindow::SetVisibility(WindowVisibilityType visibility) { Size WinNativeWindow::GetClientSize() { return GetClientRect().GetSize(); } void WinNativeWindow::SetClientSize(const Size& size) { - CRU_LOG_TAG_INFO("{} set client size to {}.", - static_cast(GetWindowHandle()), size); + CRU_LOG_TAG_DEBUG("{} set client size to {}.", + static_cast(GetWindowHandle()), size); client_rect_.SetSize(size); @@ -156,8 +156,8 @@ void WinNativeWindow::SetClientSize(const Size& size) { Rect WinNativeWindow::GetClientRect() { return client_rect_; } void WinNativeWindow::SetClientRect(const Rect& rect) { - CRU_LOG_TAG_INFO("{} set client rect to {}.", - static_cast(GetWindowHandle()), rect); + CRU_LOG_TAG_DEBUG("{} set client rect to {}.", + static_cast(GetWindowHandle()), rect); client_rect_ = rect; @@ -185,8 +185,8 @@ Rect WinNativeWindow::GetWindowRect() { } void WinNativeWindow::SetWindowRect(const Rect& rect) { - CRU_LOG_TAG_INFO("{} set window rect to {}.", - static_cast(GetWindowHandle()), rect); + CRU_LOG_TAG_DEBUG("{} set window rect to {}.", + static_cast(GetWindowHandle()), rect); client_rect_ = CalcClientRectFromWindow(rect, style_flag_, dpi_); -- cgit v1.2.3