./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
65 / 66
98.5%
Branches:
30 / 33
90.9%
Functions:
11 / 11
100.0%

src/util/log.cpp
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 #include "terraingen/util/log.h"
4
5 #include "util/text.h"
6
7 #include <array>
8 #include <atomic>
9 #include <cstdio>
10 #include <iostream>
11 #include <mutex>
12 #include <utility>
13
14 namespace terraingen {
15
16 namespace {
17
18 /// The default sink: one line per record on standard error.
19 425 LogSink StandardErrorLogSink() {
20 460 return [](const LogRecord& record) {
21 35 std::cerr << "[terraingen] " << LogLevelName(record.level) << ' ' << record.category << ": "
22 70 << record.message << '\n';
23 460 };
24 }
25
26 struct LogState {
27 std::mutex mutex;
28 LogSink sink = StandardErrorLogSink();
29 std::atomic<LogLevel> level{LogLevel::Warning};
30 };
31
32 1002506 LogState& State() {
33
3/4
✓ Branch 0 taken 395 times.
✓ Branch 1 taken 1002111 times.
✓ Branch 3 taken 395 times.
✗ Branch 4 not taken.
1002506 static LogState state;
34 1002506 return state;
35 }
36
37 thread_local bool inSink = false;
38
39 /// Marks the current thread as inside the sink, so records the sink logs are dropped.
40 class SinkGuard {
41 public:
42 8261 SinkGuard() {
43 8261 inSink = true;
44 }
45 8261 ~SinkGuard() {
46 ✗ inSink = false;
47 }
48 SinkGuard(const SinkGuard&) = delete;
49 SinkGuard& operator=(const SinkGuard&) = delete;
50 };
51
52 } // namespace
53
54 183 std::string_view LogLevelName(LogLevel level) {
55
7/7
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 30 times.
✓ Branch 2 taken 62 times.
✓ Branch 3 taken 25 times.
✓ Branch 4 taken 18 times.
✓ Branch 5 taken 17 times.
✓ Branch 6 taken 1 time.
183 switch (level) {
56 30 case LogLevel::Trace:
57 30 return "trace";
58 30 case LogLevel::Debug:
59 30 return "debug";
60 62 case LogLevel::Info:
61 62 return "info";
62 25 case LogLevel::Warning:
63 25 return "warning";
64 18 case LogLevel::Error:
65 18 return "error";
66 17 case LogLevel::Off:
67 17 return "off";
68 }
69 1 return "unknown";
70 }
71
72 22 std::optional<LogLevel> ParseLogLevel(std::string_view name) {
73 22 const std::string lower = Lower(name);
74
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 21 times.
22 if (lower == "warn") {
75 1 return LogLevel::Warning;
76 }
77
2/2
✓ Branch 0 taken 89 times.
✓ Branch 1 taken 4 times.
93 for (const LogLevel level : kLogLevels) {
78
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 72 times.
89 if (lower == LogLevelName(level)) {
79 17 return level;
80 }
81 }
82 4 return std::nullopt;
83 22 }
84
85 66 void SetLogSink(LogSink sink) {
86 66 LogState& state = State();
87 66 const std::lock_guard lock(state.mutex);
88
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 30 times.
102 state.sink = sink ? std::move(sink) : StandardErrorLogSink();
89 66 }
90
91 73 void SetLogLevel(LogLevel level) {
92 73 State().level.store(level, std::memory_order_relaxed);
93 73 }
94
95 994831 LogLevel GetLogLevel() {
96 994831 return State().level.load(std::memory_order_relaxed);
97 }
98
99 994409 bool LogEnabled(LogLevel level) {
100
3/4
✓ Branch 0 taken 994818 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 978645 times.
✓ Branch 4 taken 16126 times.
994409 return level != LogLevel::Off && level >= GetLogLevel();
101 }
102
103 8504 void Log(LogLevel level, std::string_view category, std::string message) {
104
3/4
✓ Branch 1 taken 8239 times.
✓ Branch 2 taken 258 times.
✓ Branch 3 taken 8243 times.
✗ Branch 4 not taken.
8504 if (!LogEnabled(level) || inSink) {
105 254 return;
106 }
107 8243 const LogRecord record{level, std::string(category), std::move(message),
108 8243 std::chrono::system_clock::now()};
109 8208 LogState& state = State();
110
1/1
✓ Branch 1 taken 8261 times.
8178 const std::lock_guard lock(state.mutex);
111 8261 const SinkGuard guard;
112
1/1
✓ Branch 1 taken 8261 times.
8261 state.sink(record);
113 16513 }
114
115 1702 std::string FormatDuration(double milliseconds) {
116 1702 std::array<char, 32> buffer{};
117
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 1674 times.
1702 if (milliseconds >= 1000.0) {
118 28 std::snprintf(buffer.data(), buffer.size(), "%.2f s", milliseconds / 1000.0);
119
2/2
✓ Branch 0 taken 333 times.
✓ Branch 1 taken 1341 times.
1674 } else if (milliseconds >= 10.0) {
120 333 std::snprintf(buffer.data(), buffer.size(), "%.1f ms", milliseconds);
121 } else {
122 1341 std::snprintf(buffer.data(), buffer.size(), "%.2f ms", milliseconds);
123 }
124 1702 return buffer.data();
125 }
126
127 } // namespace terraingen
128