From f5c801de04f423269d9cefa3d725b52b87c7a0b9 Mon Sep 17 00:00:00 2001 From: Yuqian Yang Date: Fri, 5 Dec 2025 20:49:10 +0800 Subject: Refactor CRU_LOG_TAG_*. --- include/cru/base/log/Logger.h | 66 ++++++++--------------------------- include/cru/ui/controls/ControlHost.h | 2 +- 2 files changed, 15 insertions(+), 53 deletions(-) (limited to 'include') diff --git a/include/cru/base/log/Logger.h b/include/cru/base/log/Logger.h index 671d004e..d98ee3e3 100644 --- a/include/cru/base/log/Logger.h +++ b/include/cru/base/log/Logger.h @@ -2,11 +2,10 @@ #include "../Base.h" #include -#include // IWYU pragma: keep +#include #include #include #include -#include #include #include #include @@ -44,14 +43,6 @@ struct CRU_BASE_API ILogger : virtual Interface { Log(LogInfo(level, std::move(tag), std::move(message))); } - template - void FormatLog(LogLevel level, std::string tag, - std::string_view message_format, Args&&... args) { - // Clang is buggy in consteval, so we can't use std::format for now. - Log(level, std::move(tag), - std::vformat(message_format, std::make_format_args(args...))); - } - protected: std::unordered_set debug_tags_; }; @@ -95,49 +86,20 @@ class CRU_BASE_API Logger : public Object, public virtual ILogger { std::mutex target_list_mutex_; std::vector> target_list_; }; - -class CRU_BASE_API LoggerCppStream : public Object { - public: - explicit LoggerCppStream(ILogger* logger, LogLevel level, std::string tag); - - LoggerCppStream WithLevel(LogLevel level) const; - LoggerCppStream WithTag(std::string tag) const; - - private: - void Consume(std::string_view str); - - public: - LoggerCppStream& operator<<(std::string_view str) { - this->Consume(str); - return *this; - } - - template - LoggerCppStream& operator<<(T&& arg) { - this->Consume(ToString(std::forward(arg))); - return *this; - } - - private: - ILogger* logger_; - LogLevel level_; - std::string tag_; -}; - } // namespace cru::log -#define CRU_LOG_TAG_DEBUG(...) \ - cru::log::ILogger::GetInstance()->FormatLog(cru::log::LogLevel::Debug, \ - kLogTag, __VA_ARGS__) - -#define CRU_LOG_TAG_INFO(...) \ - cru::log::ILogger::GetInstance()->FormatLog(cru::log::LogLevel::Info, \ - kLogTag, __VA_ARGS__) +#define CRU_DEFINE_LOG_FUNC(level) \ + template \ + void CruLog##level(std::string tag, std::format_string message_fmt, \ + Args&&... args) { \ + cru::log::ILogger::GetInstance()->Log( \ + cru::log::LogLevel::level, std::move(tag), \ + std::format(message_fmt, std::forward(args)...)); \ + } -#define CRU_LOG_TAG_WARN(...) \ - cru::log::ILogger::GetInstance()->FormatLog(cru::log::LogLevel::Warn, \ - kLogTag, __VA_ARGS__) +CRU_DEFINE_LOG_FUNC(Debug) +CRU_DEFINE_LOG_FUNC(Info) +CRU_DEFINE_LOG_FUNC(Warn) +CRU_DEFINE_LOG_FUNC(Error) -#define CRU_LOG_TAG_ERROR(...) \ - cru::log::ILogger::GetInstance()->FormatLog(cru::log::LogLevel::Error, \ - kLogTag, __VA_ARGS__) +#undef CRU_DEFINE_LOG_FUNC \ No newline at end of file diff --git a/include/cru/ui/controls/ControlHost.h b/include/cru/ui/controls/ControlHost.h index 5e8d678b..ddd9d893 100644 --- a/include/cru/ui/controls/ControlHost.h +++ b/include/cru/ui/controls/ControlHost.h @@ -100,7 +100,7 @@ class CRU_UI_API ControlHost : public Object { Guard logging_guard([&] { log += "\nEnd dispatching routed event " + (original_sender->*event_ptr)()->GetName() + "."; - CRU_LOG_TAG_DEBUG("{}", log); + CruLogDebug(kLogTag, "{}", log); }); std::vector> receive_list; -- cgit v1.2.3