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
|
#include "cru/common/Logger.hpp"
#include <array>
#include <cstdlib>
#include <ctime>
#include <memory>
#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 {
std::u16string_view 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();
}
}
std::u16string GetLogTime() {
auto time = std::time(nullptr);
auto calendar = std::localtime(&time);
return fmt::format(u"{}:{}:{}", calendar->tm_hour, calendar->tm_min,
calendar->tm_sec);
}
} // namespace
void Logger::Log(LogLevel level, std::u16string_view s) {
#ifndef CRU_DEBUG
if (level == LogLevel::Debug) {
return;
}
#endif
for (const auto &source : sources_) {
source->Write(level, fmt::format(u"[{}] {}: {}\n", GetLogTime(),
LogLevelToString(level), s));
}
}
void Logger::Log(LogLevel level, std::u16string_view tag,
std::u16string_view s) {
#ifndef CRU_DEBUG
if (level == LogLevel::Debug) {
return;
}
#endif
for (const auto &source : sources_) {
source->Write(level, fmt::format(u"[{}] {} {}: {}\n", GetLogTime(),
LogLevelToString(level), tag, s));
}
}
} // namespace cru::log
|