diff options
Diffstat (limited to 'include')
| -rw-r--r-- | include/cru/base/log/Logger.h | 79 |
1 files changed, 49 insertions, 30 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__) |
