diff options
| -rw-r--r-- | include/cru/base/log/Logger.h | 79 | ||||
| -rw-r--r-- | src/base/log/Logger.cpp | 94 | ||||
| -rw-r--r-- | src/platform/gui/win/Window.cpp | 12 |
3 files changed, 111 insertions, 74 deletions
diff --git a/include/cru/base/log/Logger.h b/include/cru/base/log/Logger.h index 8f7c32e0..671d004e 100644 --- a/include/cru/base/log/Logger.h +++ b/include/cru/base/log/Logger.h @@ -30,28 +30,19 @@ struct CRU_BASE_API ILogTarget : virtual Interface { virtual void Write(LogLevel level, std::string s) = 0; }; -class CRU_BASE_API Logger : public Object { - public: - static Logger* GetInstance(); - - public: - Logger(); - ~Logger() override; - - public: - void AddLogTarget(std::unique_ptr<ILogTarget> source); - void RemoveLogTarget(ILogTarget* source); +struct CRU_BASE_API ILogger : virtual Interface { + static ILogger* GetInstance(); - void AddDebugTag(std::string tag); - void RemoveDebugTag(const std::string& tag); + virtual void AddDebugTag(std::string tag) = 0; + virtual void RemoveDebugTag(const std::string& tag) = 0; void LoadDebugTagFromEnv(const char* env_var = "CRU_LOG_DEBUG", std::string sep = ","); - public: + virtual void Log(LogInfo log_info) = 0; + void Log(LogLevel level, std::string tag, std::string message) { Log(LogInfo(level, std::move(tag), std::move(message))); } - void Log(LogInfo log_info); template <typename... Args> void FormatLog(LogLevel level, std::string tag, @@ -61,6 +52,35 @@ class CRU_BASE_API Logger : public Object { std::vformat(message_format, std::make_format_args(args...))); } + protected: + std::unordered_set<std::string> debug_tags_; +}; + +class CRU_BASE_API SimpleStdioLogger : public Object, public virtual ILogger { + public: + void AddDebugTag(std::string tag) override; + void RemoveDebugTag(const std::string& tag) override; + void Log(LogInfo log_info) override; + + private: + std::unordered_set<std::string> debug_tags_; +}; + +class CRU_BASE_API Logger : public Object, public virtual ILogger { + public: + Logger(); + ~Logger() override; + + public: + void AddDebugTag(std::string tag) override; + void RemoveDebugTag(const std::string& tag) override; + + void AddLogTarget(std::unique_ptr<ILogTarget> source); + void RemoveLogTarget(ILogTarget* source); + + public: + void Log(LogInfo log_info) override; + private: void LogThreadRun(); @@ -78,8 +98,7 @@ class CRU_BASE_API Logger : public Object { class CRU_BASE_API LoggerCppStream : public Object { public: - explicit LoggerCppStream(Logger* logger, LogLevel level, std::string tag); - ~LoggerCppStream() override = default; + explicit LoggerCppStream(ILogger* logger, LogLevel level, std::string tag); LoggerCppStream WithLevel(LogLevel level) const; LoggerCppStream WithTag(std::string tag) const; @@ -100,25 +119,25 @@ class CRU_BASE_API LoggerCppStream : public Object { } private: - Logger* logger_; + ILogger* logger_; LogLevel level_; std::string tag_; }; } // namespace cru::log -#define CRU_LOG_TAG_DEBUG(...) \ - cru::log::Logger::GetInstance()->FormatLog(cru::log::LogLevel::Debug, \ - kLogTag, __VA_ARGS__) +#define CRU_LOG_TAG_DEBUG(...) \ + cru::log::ILogger::GetInstance()->FormatLog(cru::log::LogLevel::Debug, \ + kLogTag, __VA_ARGS__) -#define CRU_LOG_TAG_INFO(...) \ - cru::log::Logger::GetInstance()->FormatLog(cru::log::LogLevel::Info, \ - kLogTag, __VA_ARGS__) +#define CRU_LOG_TAG_INFO(...) \ + cru::log::ILogger::GetInstance()->FormatLog(cru::log::LogLevel::Info, \ + kLogTag, __VA_ARGS__) -#define CRU_LOG_TAG_WARN(...) \ - cru::log::Logger::GetInstance()->FormatLog(cru::log::LogLevel::Warn, \ - kLogTag, __VA_ARGS__) +#define CRU_LOG_TAG_WARN(...) \ + cru::log::ILogger::GetInstance()->FormatLog(cru::log::LogLevel::Warn, \ + kLogTag, __VA_ARGS__) -#define CRU_LOG_TAG_ERROR(...) \ - cru::log::Logger::GetInstance()->FormatLog(cru::log::LogLevel::Error, \ - kLogTag, __VA_ARGS__) +#define CRU_LOG_TAG_ERROR(...) \ + cru::log::ILogger::GetInstance()->FormatLog(cru::log::LogLevel::Error, \ + kLogTag, __VA_ARGS__) 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 <cstdlib> #include <ctime> #include <format> +#include <iostream> #include <memory> #include <mutex> @@ -15,33 +16,15 @@ #endif namespace cru::log { -Logger *Logger::GetInstance() { - static Logger logger; - - logger.AddLogTarget(std::make_unique<StdioLogTarget>()); - -#ifdef _WIN32 - logger.AddLogTarget(std::make_unique<platform::win::WinDebugLogTarget>()); -#endif +ILogger* ILogger::GetInstance() { + static SimpleStdioLogger logger; + logger.LoadDebugTagFromEnv(); return &logger; } -void Logger::AddLogTarget(std::unique_ptr<ILogTarget> target) { - std::lock_guard<std::mutex> lock(target_list_mutex_); - target_list_.push_back(std::move(target)); -} - -void Logger::RemoveLogTarget(ILogTarget *target) { - std::lock_guard<std::mutex> 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<StdioLogTarget>()); + +#ifdef _WIN32 + AddLogTarget(std::make_unique<platform::win::WinDebugLogTarget>()); +#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<ILogTarget> target) { + std::lock_guard<std::mutex> lock(target_list_mutex_); + target_list_.push_back(std::move(target)); +} + +void Logger::RemoveLogTarget(ILogTarget* target) { + std::lock_guard<std::mutex> 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<ILogTarget *> target_list; + std::vector<ILogTarget*> target_list; { std::unique_lock lock(log_queue_mutex_); @@ -126,16 +144,16 @@ void Logger::LogThreadRun() { { std::lock_guard<std::mutex> 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<void*>(GetWindowHandle()), size); + CRU_LOG_TAG_DEBUG("{} set client size to {}.", + static_cast<void*>(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<void*>(GetWindowHandle()), rect); + CRU_LOG_TAG_DEBUG("{} set client rect to {}.", + static_cast<void*>(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<void*>(GetWindowHandle()), rect); + CRU_LOG_TAG_DEBUG("{} set window rect to {}.", + static_cast<void*>(GetWindowHandle()), rect); client_rect_ = CalcClientRectFromWindow(rect, style_flag_, dpi_); |
