| 70 | Log::Log(LogLevel level_) : level(level_), buffer{}, stream{buffer} { Init(); } |
| 71 | |
| 72 | void Log::Init() |
| 73 | { |
| 74 | std::lock_guard<std::mutex> lock(get_mutex()); |
| 75 | if (!LogPolicy::GetInstance().IsMute() && level <= LogPolicy::GetInstance().GetLevel()) |
| 76 | { |
| 77 | const bool is_terminal = IsStdoutATTY(); |
| 78 | |
| 79 | auto format = [is_terminal](const char *level, const char *color) |
| 80 | { |
| 81 | const auto now = std::chrono::system_clock::now(); |
| 82 | const auto timestamp = |
| 83 | std::format("{:%FT%T}", std::chrono::floor<std::chrono::seconds>(now)); |
| 84 | return std::format("{}[{}] [{}] ", is_terminal ? color : "", timestamp, level); |
| 85 | }; |
| 86 | |
| 87 | switch (level) |
| 88 | { |
| 89 | case logNONE: |
| 90 | break; |
| 91 | case logWARNING: |
| 92 | stream << format("warn", YELLOW); |
| 93 | break; |
| 94 | case logERROR: |
| 95 | stream << format("error", RED); |
| 96 | break; |
| 97 | case logDEBUG: |
| 98 | #ifdef ENABLE_DEBUG_LOGGING |
| 99 | stream << format("debug", MAGENTA); |
| 100 | #endif |
| 101 | break; |
| 102 | default: // logINFO: |
| 103 | stream << format("info", ""); |
| 104 | break; |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | std::mutex &Log::get_mutex() |
| 110 | { |
nothing calls this directly
no test coverage detected