aboutsummaryrefslogtreecommitdiff
path: root/src/base/log
diff options
context:
space:
mode:
Diffstat (limited to 'src/base/log')
-rw-r--r--src/base/log/Logger.cpp94
1 files changed, 56 insertions, 38 deletions
diff --git a/src/base/log/Logger.cpp b/src/base/log/Logger.cpp
index c5d8b0b5..a3582305 100644
--- a/src/base/log/Logger.cpp
+++ b/src/base/log/Logger.cpp
@@ -7,6 +7,7 @@
#include <cstdlib>
#include <ctime>
#include <format>
+#include <iostream>
#include <memory>
#include <mutex>
@@ -15,33 +16,15 @@
#endif
namespace cru::log {
-Logger *Logger::GetInstance() {
- static Logger logger;
-
- logger.AddLogTarget(std::make_unique<StdioLogTarget>());
-
-#ifdef _WIN32
- logger.AddLogTarget(std::make_unique<platform::win::WinDebugLogTarget>());
-#endif
+ILogger* ILogger::GetInstance() {
+ static SimpleStdioLogger logger;
+ logger.LoadDebugTagFromEnv();
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 {
-const char *LogLevelToString(LogLevel level) {
+const char* LogLevelToString(LogLevel level) {
switch (level) {
case LogLevel::Debug:
return "DEBUG";
@@ -63,15 +46,46 @@ std::string GetLogTime() {
calendar->tm_sec);
}
-std::string MakeLogFinalMessage(const LogInfo &log_info) {
+std::string MakeLogFinalMessage(const LogInfo& log_info) {
return std::format("[{}] {} {}: {}", GetLogTime(),
LogLevelToString(log_info.level), log_info.tag,
log_info.message);
}
} // namespace
+void ILogger::LoadDebugTagFromEnv(const char* env_var, std::string sep) {
+ auto env = std::getenv(env_var);
+ if (env != nullptr) {
+ for (auto tag : cru::string::Split(std::string(env), sep)) {
+ AddDebugTag(std::move(tag));
+ }
+ }
+}
+
+void SimpleStdioLogger::AddDebugTag(std::string tag) {
+ debug_tags_.insert(tag);
+}
+
+void SimpleStdioLogger::RemoveDebugTag(const std::string& tag) {
+ debug_tags_.erase(tag);
+}
+
+void SimpleStdioLogger::Log(LogInfo log_info) {
+ if (log_info.level == LogLevel::Debug &&
+ std::ranges::none_of(debug_tags_, [&log_info](const std::string& tag) {
+ return log_info.tag.starts_with(tag);
+ })) {
+ return;
+ }
+ std::clog << MakeLogFinalMessage(log_info) << std::endl;
+}
+
Logger::Logger() : log_stop_(false), log_thread_(&Logger::LogThreadRun, this) {
- LoadDebugTagFromEnv();
+ AddLogTarget(std::make_unique<StdioLogTarget>());
+
+#ifdef _WIN32
+ AddLogTarget(std::make_unique<platform::win::WinDebugLogTarget>());
+#endif
}
Logger::~Logger() {
@@ -88,18 +102,22 @@ void Logger::AddDebugTag(std::string tag) {
debug_tags_.insert(std::move(tag));
}
-void Logger::RemoveDebugTag(const std::string &tag) {
+void Logger::RemoveDebugTag(const std::string& tag) {
std::unique_lock lock(log_queue_mutex_);
debug_tags_.erase(tag);
}
-void Logger::LoadDebugTagFromEnv(const char *env_var, std::string sep) {
- auto env = std::getenv(env_var);
- if (env != nullptr) {
- for (auto tag : cru::string::Split(std::string(env), sep)) {
- AddDebugTag(std::move(tag));
- }
- }
+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());
}
void Logger::Log(LogInfo log_info) {
@@ -113,7 +131,7 @@ void Logger::LogThreadRun() {
bool stop = false;
while (true) {
- std::vector<ILogTarget *> target_list;
+ std::vector<ILogTarget*> target_list;
{
std::unique_lock lock(log_queue_mutex_);
@@ -126,16 +144,16 @@ void Logger::LogThreadRun() {
{
std::lock_guard<std::mutex> lock_guard(target_list_mutex_);
- for (const auto &target : target_list_) {
+ for (const auto& target : target_list_) {
target_list.push_back(target.get());
}
}
- for (const auto &target : target_list) {
- for (auto &log_info : queue) {
+ for (const auto& target : target_list) {
+ for (auto& log_info : queue) {
if (log_info.level == LogLevel::Debug &&
std::ranges::none_of(debug_tags_,
- [&log_info](const std::string &tag) {
+ [&log_info](const std::string& tag) {
return log_info.tag.starts_with(tag);
})) {
continue;
@@ -150,7 +168,7 @@ void Logger::LogThreadRun() {
}
}
-LoggerCppStream::LoggerCppStream(Logger *logger, LogLevel level,
+LoggerCppStream::LoggerCppStream(ILogger* logger, LogLevel level,
std::string tag)
: logger_(logger), level_(level), tag_(std::move(tag)) {}