diff options
Diffstat (limited to 'include/cru/common')
-rw-r--r-- | include/cru/common/Base.hpp | 4 | ||||
-rw-r--r-- | include/cru/common/Logger.hpp | 34 |
2 files changed, 35 insertions, 3 deletions
diff --git a/include/cru/common/Base.hpp b/include/cru/common/Base.hpp index ff7ab31e..409c2b0e 100644 --- a/include/cru/common/Base.hpp +++ b/include/cru/common/Base.hpp @@ -44,4 +44,8 @@ struct Interface { } using Index = gsl::index; + +#define CRU_DEFINE_CLASS_LOG_TAG(tag) \ + private: \ + constexpr static std::string_view log_tag = tag; } // namespace cru diff --git a/include/cru/common/Logger.hpp b/include/cru/common/Logger.hpp index bd16678b..f76e4626 100644 --- a/include/cru/common/Logger.hpp +++ b/include/cru/common/Logger.hpp @@ -15,7 +15,7 @@ enum class LogLevel { Debug, Info, Warn, Error }; struct ILogSource : virtual Interface { // Write the string s. LogLevel is just a helper. It has no effect on the // content to write. - virtual void Write(LogLevel level, const std::string_view& s) = 0; + virtual void Write(LogLevel level, std::string_view s) = 0; }; class StdioLogSource : public virtual ILogSource { @@ -27,7 +27,7 @@ class StdioLogSource : public virtual ILogSource { ~StdioLogSource() override = default; - void Write(LogLevel level, const std::string_view& s) override { + void Write(LogLevel level, std::string_view s) override { // TODO: Emmm... Since it is buggy to use narrow char in UTF-8 on Windows. I // think this implementation might be broken. (However, I didn't test it.) // Maybe, I should detect Windows and use wide char (And I didn't test this @@ -58,7 +58,8 @@ class Logger : public Object { void RemoveSource(ILogSource* source); public: - void Log(LogLevel level, const std::string_view& s); + void Log(LogLevel level, std::string_view s); + void Log(LogLevel level, std::string_view tag, std::string_view s); public: std::list<std::unique_ptr<ILogSource>> sources_; @@ -89,4 +90,31 @@ void Error(TArgs&&... args) { Logger::GetInstance()->Log(LogLevel::Error, fmt::format(std::forward<TArgs>(args)...)); } + +template <typename... TArgs> +void TagDebug([[maybe_unused]] std::string_view tag, + [[maybe_unused]] TArgs&&... args) { +#ifdef CRU_DEBUG + Logger::GetInstance()->Log(LogLevel::Debug, tag, + fmt::format(std::forward<TArgs>(args)...)); +#endif +} + +template <typename... TArgs> +void TagInfo(std::string_view tag, TArgs&&... args) { + Logger::GetInstance()->Log(LogLevel::Info, tag, + fmt::format(std::forward<TArgs>(args)...)); +} + +template <typename... TArgs> +void TagWarn(std::string_view tag, TArgs&&... args) { + Logger::GetInstance()->Log(LogLevel::Warn, tag, + fmt::format(std::forward<TArgs>(args)...)); +} + +template <typename... TArgs> +void TagError(std::string_view tag, TArgs&&... args) { + Logger::GetInstance()->Log(LogLevel::Error, tag, + fmt::format(std::forward<TArgs>(args)...)); +} } // namespace cru::log |