diff options
Diffstat (limited to 'include/cru/common/logger.hpp')
-rw-r--r-- | include/cru/common/logger.hpp | 51 |
1 files changed, 26 insertions, 25 deletions
diff --git a/include/cru/common/logger.hpp b/include/cru/common/logger.hpp index bbe06331..08765499 100644 --- a/include/cru/common/logger.hpp +++ b/include/cru/common/logger.hpp @@ -5,32 +5,33 @@ #include <iostream> #include <list> +#include <memory> #include <string_view> namespace cru::log { enum class LogLevel { Debug, Info, Warn, Error }; -struct ILoggerSource : Interface { - // Write the s. LogLevel is just a helper. It has no effect on the content to - // write. - virtual void Write(LogLevel level, const std::wstring_view& s) = 0; +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; }; -class StdioLoggerSource : public ILoggerSource { +class StdioLogSource : public virtual ILogSource { public: - StdioLoggerSource() = default; + StdioLogSource() = default; - CRU_DELETE_COPY(StdioLoggerSource) - CRU_DELETE_MOVE(StdioLoggerSource) + CRU_DELETE_COPY(StdioLogSource) + CRU_DELETE_MOVE(StdioLogSource) - ~StdioLoggerSource() = default; + ~StdioLogSource() override = default; - void Write(LogLevel level, const std::wstring_view& s) override { + void Write(LogLevel level, const std::string_view& s) override { if (level == LogLevel::Error) { - std::wcerr << s; + std::cerr << s; } else { - std::wcout << s; + std::cout << s; } } }; @@ -45,43 +46,43 @@ class Logger : public Object { CRU_DELETE_COPY(Logger) CRU_DELETE_MOVE(Logger) - ~Logger() override; + ~Logger() override = default; public: - void AddSource(ILoggerSource* source); - void RemoveSource(ILoggerSource* source); + void AddSource(std::unique_ptr<ILogSource> source); + void RemoveSource(ILogSource* source); public: - void Log(LogLevel level, const std::wstring_view& s); + void Log(LogLevel level, const std::string_view& s); template <typename... TArgs> - void Debug(const std::wstring_view& format, TArgs&&... args) { + void Debug(const std::string_view& format, TArgs&&... args) { #ifdef CRU_DEBUG Log(LogLevel::Debug, util::Format(format, std::forward<TArgs>(args)...)); #endif } template <typename... TArgs> - void Info(const std::wstring_view& format, TArgs&&... args) { + void Info(const std::string_view& format, TArgs&&... args) { Log(LogLevel::Info, util::Format(format, std::forward<TArgs>(args)...)); } template <typename... TArgs> - void Warn(const std::wstring_view& format, TArgs&&... args) { + void Warn(const std::string_view& format, TArgs&&... args) { Log(LogLevel::Warn, util::Format(format, std::forward<TArgs>(args)...)); } template <typename... TArgs> - void Error(const std::wstring_view& format, TArgs&&... args) { + void Error(const std::string_view& format, TArgs&&... args) { Log(LogLevel::Error, util::Format(format, std::forward<TArgs>(args)...)); } public: - std::list<ILoggerSource*> sources_; + std::list<std::unique_ptr<ILogSource>> sources_; }; template <typename... TArgs> -void Debug(const std::wstring_view& format, TArgs&&... args) { +void Debug(const std::string_view& format, TArgs&&... args) { #ifdef CRU_DEBUG Logger::GetInstance()->Log( LogLevel::Debug, util::Format(format, std::forward<TArgs>(args)...)); @@ -89,19 +90,19 @@ void Debug(const std::wstring_view& format, TArgs&&... args) { } template <typename... TArgs> -void Info(const std::wstring_view& format, TArgs&&... args) { +void Info(const std::string_view& format, TArgs&&... args) { Logger::GetInstance()->Log( LogLevel::Info, util::Format(format, std::forward<TArgs>(args)...)); } template <typename... TArgs> -void Warn(const std::wstring_view& format, TArgs&&... args) { +void Warn(const std::string_view& format, TArgs&&... args) { Logger::GetInstance()->Log( LogLevel::Warn, util::Format(format, std::forward<TArgs>(args)...)); } template <typename... TArgs> -void Error(const std::wstring_view& format, TArgs&&... args) { +void Error(const std::string_view& format, TArgs&&... args) { Logger::GetInstance()->Log( LogLevel::Error, util::Format(format, std::forward<TArgs>(args)...)); } |