diff options
author | crupest <crupest@outlook.com> | 2020-06-28 00:03:11 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2020-06-28 00:03:11 +0800 |
commit | 06d1d0442276a05b6caad6e3468f4afb1e8ee5df (patch) | |
tree | ebd46f0fb7343dc57bf947b7b5fffc139c3ddeac /include/cru/common/logger.hpp | |
parent | e11be6caa9ef9b2b198ca61846e32f469627556e (diff) | |
download | cru-06d1d0442276a05b6caad6e3468f4afb1e8ee5df.tar.gz cru-06d1d0442276a05b6caad6e3468f4afb1e8ee5df.tar.bz2 cru-06d1d0442276a05b6caad6e3468f4afb1e8ee5df.zip |
...
Diffstat (limited to 'include/cru/common/logger.hpp')
-rw-r--r-- | include/cru/common/logger.hpp | 92 |
1 files changed, 0 insertions, 92 deletions
diff --git a/include/cru/common/logger.hpp b/include/cru/common/logger.hpp deleted file mode 100644 index bd16678b..00000000 --- a/include/cru/common/logger.hpp +++ /dev/null @@ -1,92 +0,0 @@ -#pragma once -#include "cru/common/Base.hpp" - -#include <fmt/format.h> -#include <fmt/ostream.h> -#include <iostream> -#include <list> -#include <memory> -#include <string_view> - -namespace cru::log { - -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; -}; - -class StdioLogSource : public virtual ILogSource { - public: - StdioLogSource() = default; - - CRU_DELETE_COPY(StdioLogSource) - CRU_DELETE_MOVE(StdioLogSource) - - ~StdioLogSource() override = default; - - void Write(LogLevel level, const 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 - // either) or other more complicated implementation. Currently, I settled - // with this. - if (level == LogLevel::Error) { - std::cerr << s; - } else { - std::cout << s; - } - } -}; - -class Logger : public Object { - public: - static Logger* GetInstance(); - - public: - Logger() = default; - - CRU_DELETE_COPY(Logger) - CRU_DELETE_MOVE(Logger) - - ~Logger() override = default; - - public: - void AddSource(std::unique_ptr<ILogSource> source); - void RemoveSource(ILogSource* source); - - public: - void Log(LogLevel level, const std::string_view& s); - - public: - std::list<std::unique_ptr<ILogSource>> sources_; -}; - -template <typename... TArgs> -void Debug([[maybe_unused]] TArgs&&... args) { -#ifdef CRU_DEBUG - Logger::GetInstance()->Log(LogLevel::Debug, - fmt::format(std::forward<TArgs>(args)...)); -#endif -} - -template <typename... TArgs> -void Info(TArgs&&... args) { - Logger::GetInstance()->Log(LogLevel::Info, - fmt::format(std::forward<TArgs>(args)...)); -} - -template <typename... TArgs> -void Warn(TArgs&&... args) { - Logger::GetInstance()->Log(LogLevel::Warn, - fmt::format(std::forward<TArgs>(args)...)); -} - -template <typename... TArgs> -void Error(TArgs&&... args) { - Logger::GetInstance()->Log(LogLevel::Error, - fmt::format(std::forward<TArgs>(args)...)); -} -} // namespace cru::log |