aboutsummaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
author杨宇千 <crupest@outlook.com>2019-09-08 22:24:28 +0800
committer杨宇千 <crupest@outlook.com>2019-09-08 22:24:28 +0800
commita8ba16ddab3291e59a33b36405462094f8cdf5bc (patch)
treeaa849cdc1f9b4f7a0aa150f562b2e618c63ecceb /include
parent8b9a306d6f24dbc08aeee16b115260406e79126d (diff)
downloadcru-a8ba16ddab3291e59a33b36405462094f8cdf5bc.tar.gz
cru-a8ba16ddab3291e59a33b36405462094f8cdf5bc.tar.bz2
cru-a8ba16ddab3291e59a33b36405462094f8cdf5bc.zip
...
Diffstat (limited to 'include')
-rw-r--r--include/cru/common/logger.hpp104
-rw-r--r--include/cru/common/pre_config.hpp2
-rw-r--r--include/cru/platform/debug.hpp8
-rw-r--r--include/cru/win/win_pre_config.hpp2
4 files changed, 108 insertions, 8 deletions
diff --git a/include/cru/common/logger.hpp b/include/cru/common/logger.hpp
new file mode 100644
index 00000000..4da81eb1
--- /dev/null
+++ b/include/cru/common/logger.hpp
@@ -0,0 +1,104 @@
+#pragma once
+
+#include "cru/common/base.hpp"
+#include "cru/common/format.hpp"
+
+#include <iostream>
+#include <list>
+#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;
+};
+
+class StdioLoggerSource : public ILoggerSource {
+ public:
+ StdioLoggerSource() = default;
+
+ CRU_DELETE_COPY(StdioLoggerSource)
+ CRU_DELETE_MOVE(StdioLoggerSource)
+
+ ~StdioLoggerSource() = default;
+
+ void Write(LogLevel level, const std::wstring_view& s) override {
+ if (level == LogLevel::Error) {
+ std::wcerr << s;
+ } else {
+ std::wcout << s;
+ }
+ }
+};
+
+class Logger : public Object {
+ public:
+ static Logger* GetInstance();
+
+ public:
+ Logger() = default;
+
+ CRU_DELETE_COPY(Logger)
+ CRU_DELETE_MOVE(Logger)
+
+ ~Logger() override;
+
+ public:
+ void AddSource(ILoggerSource* source);
+ void RemoveSource(ILoggerSource* source);
+
+ public:
+ void Log(LogLevel level, const std::wstring_view& s);
+
+ template <typename... TArgs>
+ void Debug(const std::wstring_view& format, TArgs&&... args) {
+ Log(LogLevel::Debug, util::Format(format, std::forward<TArgs>(args)...));
+ }
+
+ template <typename... TArgs>
+ void Info(const std::wstring_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) {
+ Log(LogLevel::Warn, util::Format(format, std::forward<TArgs>(args)...));
+ }
+
+ template <typename... TArgs>
+ void Error(const std::wstring_view& format, TArgs&&... args) {
+ Log(LogLevel::Error, util::Format(format, std::forward<TArgs>(args)...));
+ }
+
+ public:
+ std::list<ILoggerSource*> sources_;
+};
+
+template <typename... TArgs>
+void Debug(const std::wstring_view& format, TArgs&&... args) {
+ Logger::GetInstance()->Log(
+ LogLevel::Debug, util::Format(format, std::forward<TArgs>(args)...));
+}
+
+template <typename... TArgs>
+void Info(const std::wstring_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) {
+ Logger::GetInstance()->Log(
+ LogLevel::Warn, util::Format(format, std::forward<TArgs>(args)...));
+}
+
+template <typename... TArgs>
+void Error(const std::wstring_view& format, TArgs&&... args) {
+ Logger::GetInstance()->Log(
+ LogLevel::Error, util::Format(format, std::forward<TArgs>(args)...));
+}
+} // namespace cru::logger
diff --git a/include/cru/common/pre_config.hpp b/include/cru/common/pre_config.hpp
index aa4d680b..96b1bab5 100644
--- a/include/cru/common/pre_config.hpp
+++ b/include/cru/common/pre_config.hpp
@@ -3,3 +3,5 @@
#ifdef _DEBUG
#define CRU_DEBUG
#endif
+
+#define _CRT_SECURE_NO_WARNINGS
diff --git a/include/cru/platform/debug.hpp b/include/cru/platform/debug.hpp
deleted file mode 100644
index 18472201..00000000
--- a/include/cru/platform/debug.hpp
+++ /dev/null
@@ -1,8 +0,0 @@
-#pragma once
-#include "cru/common/pre_config.hpp"
-
-#include <string_view>
-
-namespace cru::platform {
-void DebugMessage(const std::wstring_view& message);
-}
diff --git a/include/cru/win/win_pre_config.hpp b/include/cru/win/win_pre_config.hpp
index 7466a62c..a7dc72f3 100644
--- a/include/cru/win/win_pre_config.hpp
+++ b/include/cru/win/win_pre_config.hpp
@@ -1,3 +1,5 @@
+#pragma once
+
#include "cru/common/pre_config.hpp"
#define NOMINMAX