diff options
Diffstat (limited to 'src/common')
-rw-r--r-- | src/common/CMakeLists.txt | 3 | ||||
-rw-r--r-- | src/common/Format.cpp | 2 | ||||
-rw-r--r-- | src/common/Logger.cpp | 108 | ||||
-rw-r--r-- | src/common/log/Logger.cpp | 86 | ||||
-rw-r--r-- | src/common/log/StdioLogTarget.cpp | 22 |
5 files changed, 111 insertions, 110 deletions
diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index 706f0ed0..330c7226 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -2,13 +2,14 @@ add_library(cru_base SHARED Base.cpp Exception.cpp Format.cpp - Logger.cpp PropertyTree.cpp String.cpp StringUtil.cpp io/Stream.cpp io/Resource.cpp io/MemoryStream.cpp + log/Logger.cpp + log/StdioLogTarget.cpp platform/Exception.cpp ) target_compile_definitions(cru_base PRIVATE CRU_BASE_EXPORT_API) diff --git a/src/common/Format.cpp b/src/common/Format.cpp index 3362db65..d58c90b7 100644 --- a/src/common/Format.cpp +++ b/src/common/Format.cpp @@ -46,7 +46,7 @@ FormatToken ParsePlaceHolder(String place_holder_string) { } } -std::vector<FormatToken> ParseToFormatTokenList(const String& str) { +std::vector<FormatToken> ParseToFormatTokenList(StringView str) { std::vector<FormatToken> result; auto push_char = [&result](char16_t c) { diff --git a/src/common/Logger.cpp b/src/common/Logger.cpp deleted file mode 100644 index 49261396..00000000 --- a/src/common/Logger.cpp +++ /dev/null @@ -1,108 +0,0 @@ -#include "cru/common/Logger.h" - -#include <array> -#include <cstdlib> -#include <ctime> -#include <iostream> -#include <memory> -#include <mutex> -#include <string_view> - -namespace cru::log { -namespace { -Logger *CreateLogger() { - const auto logger = new Logger(); - return logger; -} -} // namespace - -Logger *Logger::GetInstance() { - static std::unique_ptr<Logger> logger{CreateLogger()}; - return logger.get(); -} - -void Logger::AddSource(std::unique_ptr<ILogSource> source) { - sources_.push_back(std::move(source)); -} - -void Logger::RemoveSource(ILogSource *source) { - sources_.remove_if([source](const std::unique_ptr<ILogSource> &s) { - return s.get() == source; - }); -} - -namespace { -String LogLevelToString(LogLevel level) { - switch (level) { - case LogLevel::Debug: - return u"DEBUG"; - case LogLevel::Info: - return u"INFO"; - case LogLevel::Warn: - return u"WARN"; - case LogLevel::Error: - return u"ERROR"; - default: - std::terminate(); - } -} - -String GetLogTime() { - auto time = std::time(nullptr); - auto calendar = std::localtime(&time); - return Format(u"{}:{}:{}", calendar->tm_hour, calendar->tm_min, - calendar->tm_sec); -} -} // namespace - -void Logger::Log(LogLevel level, StringView message) { -#ifndef CRU_DEBUG - if (level == LogLevel::Debug) { - return; - } -#endif - for (const auto &source : sources_) { - source->Write(level, Format(u"[{}] {}: {}\n", GetLogTime(), - LogLevelToString(level), message)); - } -} - -void Logger::Log(LogLevel level, StringView tag, StringView message) { -#ifndef CRU_DEBUG - if (level == LogLevel::Debug) { - return; - } -#endif - for (const auto &source : sources_) { - source->Write(level, Format(u"[{}] {} {}: {}\n", GetLogTime(), - LogLevelToString(level), tag, message)); - } -} - -namespace { -std::mutex stdio_lock; - -void WriteStdio(LogLevel level, StringView s) { - std::string m = s.ToString().ToUtf8(); - - if (level == LogLevel::Error) { - std::cerr << m; - } else { - std::cout << m; - } -} -} // namespace - -StdioLogSource::StdioLogSource(bool use_lock) : use_lock_(use_lock) {} - -StdioLogSource::~StdioLogSource() {} - -void StdioLogSource::Write(LogLevel level, StringView s) { - if (use_lock_) { - std::lock_guard<std::mutex> guard(stdio_lock); - WriteStdio(level, s); - } else { - WriteStdio(level, s); - } -} -} // namespace cru::log diff --git a/src/common/log/Logger.cpp b/src/common/log/Logger.cpp new file mode 100644 index 00000000..bcafde21 --- /dev/null +++ b/src/common/log/Logger.cpp @@ -0,0 +1,86 @@ +#include "cru/common/log/Logger.h" +#include "cru/common/log/StdioLogTarget.h" + +#include <array> +#include <cstdlib> +#include <ctime> + +namespace cru::log { +Logger *Logger::GetInstance() { + static Logger logger; + + logger.AddLogTarget(std::make_unique<StdioLogTarget>()); + + return &logger; +} + +void Logger::AddLogTarget(std::unique_ptr<ILogTarget> target) { + std::lock_guard<std::mutex> lock(target_list_mutex_); + target_list_.push_back(std::move(target)); +} + +void Logger::RemoveLogTarget(ILogTarget *target) { + std::lock_guard<std::mutex> lock(target_list_mutex_); + target_list_.erase( + std::remove_if(target_list_.begin(), target_list_.end(), + [target](const auto &t) { return t.get() == target; }), + target_list_.end()); +} + +namespace { +String LogLevelToString(LogLevel level) { + switch (level) { + case LogLevel::Debug: + return u"DEBUG"; + case LogLevel::Info: + return u"INFO"; + case LogLevel::Warn: + return u"WARN"; + case LogLevel::Error: + return u"ERROR"; + default: + std::terminate(); + } +} + +String GetLogTime() { + auto time = std::time(nullptr); + auto calendar = std::localtime(&time); + return Format(u"{}:{}:{}", calendar->tm_hour, calendar->tm_min, + calendar->tm_sec); +} + +String MakeLogFinalMessage(const LogInfo &log_info) { + if (log_info.tag.empty()) { + return Format(u"[{}] {}: {}", GetLogTime(), + LogLevelToString(log_info.level), log_info.message); + } else { + return Format(u"[{}] {} {}: {}", GetLogTime(), + LogLevelToString(log_info.level), log_info.tag, + log_info.message); + } +} +} // namespace + +Logger::Logger() + : log_thread_([this] { + while (true) { + auto log_info = log_queue_.Pull(); + std::lock_guard<std::mutex> lock_guard{target_list_mutex_}; + for (const auto &target : target_list_) { + target->Write(log_info.level, MakeLogFinalMessage(log_info)); + } + } + }) {} + +Logger::~Logger() {} + +void Logger::Log(LogInfo log_info) { +#ifndef CRU_DEBUG + if (log_info.level == LogLevel::Debug) { + return; + } +#endif + log_queue_.Push(std::move(log_info)); +} +} // namespace cru::log diff --git a/src/common/log/StdioLogTarget.cpp b/src/common/log/StdioLogTarget.cpp new file mode 100644 index 00000000..ad29a3ce --- /dev/null +++ b/src/common/log/StdioLogTarget.cpp @@ -0,0 +1,22 @@ +#include "cru/common/log/StdioLogTarget.h" + +#include <iostream> + +namespace cru::log { +StdioLogTarget::StdioLogTarget() {} + +StdioLogTarget::~StdioLogTarget() {} + +void StdioLogTarget::Write(log::LogLevel level, StringView s) { +#ifdef CRU_PLATFORM_WINDOWS +#else + std::string m = s.ToUtf8(); + + if (level == log::LogLevel::Error) { + std::cerr << m; + } else { + std::cout << m; + } +#endif +} +} // namespace cru::log |