1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
|
#include "cru/base/log/Logger.h"
#include "cru/base/StringUtil.h"
#include "cru/base/log/StdioLogTarget.h"
#include <algorithm>
#include <chrono>
#include <condition_variable>
#include <cstdlib>
#include <ctime>
#include <format>
#include <iostream>
#include <memory>
#include <mutex>
#ifdef _WIN32
#include "cru/base/platform/win/DebugLogTarget.h"
#endif
namespace cru::log {
ILogger* ILogger::GetInstance() {
static SimpleStdioLogger logger;
logger.LoadDebugTagFromEnv();
return &logger;
}
namespace {
const char* LogLevelToString(LogLevel level) {
switch (level) {
case LogLevel::Debug:
return "DEBUG";
case LogLevel::Info:
return "INFO";
case LogLevel::Warn:
return "WARN";
case LogLevel::Error:
return "ERROR";
default:
std::terminate();
}
}
std::string GetLogTime() {
auto time = std::time(nullptr);
auto calendar = std::localtime(&time);
return std::format("{}:{}:{}", calendar->tm_hour, calendar->tm_min,
calendar->tm_sec);
}
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) {
AddLogTarget(std::make_unique<StdioLogTarget>());
#ifdef _WIN32
AddLogTarget(std::make_unique<platform::win::WinDebugLogTarget>());
#endif
}
Logger::~Logger() {
{
std::unique_lock lock(log_queue_mutex_);
log_stop_ = true;
log_queue_condition_variable_.notify_one();
}
log_thread_.join();
}
void Logger::AddDebugTag(std::string tag) {
std::unique_lock lock(log_queue_mutex_);
debug_tags_.insert(std::move(tag));
}
void Logger::RemoveDebugTag(const std::string& tag) {
std::unique_lock lock(log_queue_mutex_);
debug_tags_.erase(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) {
std::unique_lock lock(log_queue_mutex_);
log_queue_.push_back(std::move(log_info));
log_queue_condition_variable_.notify_one();
}
void Logger::LogThreadRun() {
std::list<LogInfo> queue;
bool stop = false;
while (true) {
std::vector<ILogTarget*> target_list;
{
std::unique_lock lock(log_queue_mutex_);
log_queue_condition_variable_.wait(
lock, [this] { return !log_queue_.empty() || log_stop_; });
queue = std::move(log_queue_);
log_queue_ = {};
stop = log_stop_;
}
{
std::lock_guard<std::mutex> lock_guard(target_list_mutex_);
for (const auto& target : target_list_) {
target_list.push_back(target.get());
}
}
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) {
return log_info.tag.starts_with(tag);
})) {
continue;
}
target->Write(log_info.level, MakeLogFinalMessage(log_info));
}
queue.clear();
}
// TODO: Should still wait for queue to be cleared.
if (stop) return;
}
}
Guard MeasureTimeAndLog(std::string_view tag, std::string_view name) {
CruLogDebug(std::string(tag), "Start measure {}.", name);
auto start = std::chrono::high_resolution_clock::now();
return Guard([tag = std::string(tag), name = std::string(name), start] {
auto end = std::chrono::high_resolution_clock::now();
auto duration =
std::chrono::duration_cast<std::chrono::microseconds>(end - start);
CruLogDebug(tag, "End measure {}, time {} us.", name, duration.count());
});
}
} // namespace cru::log
|