diff options
author | crupest <crupest@outlook.com> | 2020-06-29 00:31:21 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2020-06-29 00:31:21 +0800 |
commit | 5c3dae62b9218dbd2493ff6390db062013ca4bdc (patch) | |
tree | 3425a9efa118eaf89627e6903cecb50a3daedb1d /include/cru/common/Logger.hpp | |
parent | 6b5aff7b7e50fae15cb010b340099163725f664c (diff) | |
download | cru-5c3dae62b9218dbd2493ff6390db062013ca4bdc.tar.gz cru-5c3dae62b9218dbd2493ff6390db062013ca4bdc.tar.bz2 cru-5c3dae62b9218dbd2493ff6390db062013ca4bdc.zip |
...
Diffstat (limited to 'include/cru/common/Logger.hpp')
-rw-r--r-- | include/cru/common/Logger.hpp | 34 |
1 files changed, 31 insertions, 3 deletions
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 |