-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlog.hpp
265 lines (235 loc) · 7.75 KB
/
log.hpp
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#pragma once
#include <string>
#include <string_view>
#include <format>
#include <cstdint>
#include <cassert>
#include <iostream>
#include <sstream>
#include <chrono>
#include <iomanip>
#include <source_location>
#include <mutex>
#include <atomic>
#include <array>
namespace ws_client
{
// 0 = disabled, 1 = error, 2 = warning, 3 = info, 4 = debug
enum class LogLevel : uint8_t
{
N = 0, // Disabled
E = 1, // Error
W = 2, // Warning
I = 3, // Info
D = 4 // Debug
};
static constexpr LogLevel log_level_from_int(int level)
{
assert((level >= 0 && level <= 4) && "log level out of bounds");
return static_cast<LogLevel>(level);
}
static constexpr std::string_view to_string(LogLevel level)
{
switch (level)
{
case LogLevel::I:
return "I";
case LogLevel::D:
return "D";
case LogLevel::E:
return "E";
case LogLevel::W:
return "W";
default:
return "?";
}
}
inline std::ostream& operator<<(std::ostream& os, LogLevel level)
{
return os << to_string(level);
}
enum class LogTopic : uint8_t
{
None = 0,
DNS = 1, // WS_CLIENT_LOG_DNS
TCP = 2, // WS_CLIENT_LOG_TCP
SSL = 3, // WS_CLIENT_LOG_SSL
Handshake = 4, // WS_CLIENT_LOG_HANDSHAKE
Compression = 5, // WS_CLIENT_LOG_COMPRESSION
SendFrame = 6, // WS_CLIENT_LOG_SEND_FRAME
SendFramePayload = 7, // WS_CLIENT_LOG_SEND_FRAME_PAYLOAD
RecvFrame = 8, // WS_CLIENT_LOG_RECV_FRAME
RecvFramePayload = 9, // WS_CLIENT_LOG_RECV_FRAME_PAYLOAD
User = 10 // WS_CLIENT_LOG_USER
};
constexpr size_t WS_LOG_TOPICS_COUNT = 11;
static constexpr std::string_view to_string(LogTopic topic)
{
switch (topic)
{
case LogTopic::None:
return "None";
case LogTopic::DNS:
return "DNS";
case LogTopic::TCP:
return "TCP";
case LogTopic::SSL:
return "SSL";
case LogTopic::Handshake:
return "Handshake";
case LogTopic::Compression:
return "Compression";
case LogTopic::SendFrame:
return "SendFrame";
case LogTopic::SendFramePayload:
return "SendFramePayload";
case LogTopic::RecvFrame:
return "RecvFrame";
case LogTopic::RecvFramePayload:
return "RecvFramePayload";
case LogTopic::User:
return "User";
default:
return "unknown";
}
}
inline std::ostream& operator<<(std::ostream& os, LogTopic topic)
{
return os << to_string(topic);
}
inline constexpr const char* extract_log_file_name(const char* path) noexcept
{
const char* last_separator = nullptr;
for (const char* p = path; *p; ++p)
{
if (*p == '/' || *p == '\\')
last_separator = p + 1;
}
if (last_separator)
return last_separator;
return path;
}
/**
* Default console logger.
* It supports logging by topics, and log levels per topic.
* The log level can be configured per topic at runtime, the operations are thread-safe.
*/
class ConsoleLogger
{
private:
std::mutex clog_mutex;
std::array<std::atomic<LogLevel>, WS_LOG_TOPICS_COUNT> topic_levels;
template <LogLevel level>
static constexpr std::string_view level_to_color() noexcept
{
if constexpr (level == LogLevel::E)
return "\033[1;91m";
if constexpr (level == LogLevel::W)
return "\033[0;93m";
if constexpr (level == LogLevel::I)
return "\033[0;37m";
if constexpr (level == LogLevel::D)
return "\033[0;30m";
return "";
}
public:
ConsoleLogger() noexcept //
: ConsoleLogger(LogLevel::D)
{
}
ConsoleLogger(LogLevel min_level) noexcept
{
// Configure defaults for log topics.
// Defaults can be overridden using compile-time defines.
set_level(LogTopic::DNS, log_level_from_int(WS_CLIENT_LOG_DNS));
set_level(LogTopic::TCP, log_level_from_int(WS_CLIENT_LOG_TCP));
set_level(LogTopic::SSL, log_level_from_int(WS_CLIENT_LOG_SSL));
set_level(LogTopic::Handshake, log_level_from_int(WS_CLIENT_LOG_HANDSHAKE));
set_level(LogTopic::Compression, log_level_from_int(WS_CLIENT_LOG_COMPRESSION));
set_level(LogTopic::SendFrame, log_level_from_int(WS_CLIENT_LOG_SEND_FRAME));
set_level(LogTopic::SendFramePayload, log_level_from_int(WS_CLIENT_LOG_SEND_FRAME_PAYLOAD));
set_level(LogTopic::RecvFrame, log_level_from_int(WS_CLIENT_LOG_RECV_FRAME));
set_level(LogTopic::RecvFramePayload, log_level_from_int(WS_CLIENT_LOG_RECV_FRAME_PAYLOAD));
set_level(LogTopic::User, log_level_from_int(WS_CLIENT_LOG_USER));
// set minimum log level
set_min_level(min_level);
}
// disable copy
ConsoleLogger(const ConsoleLogger&) = delete;
ConsoleLogger& operator=(const ConsoleLogger&) = delete;
// // enable move
// ConsoleLogger(ConsoleLogger&&) noexcept = default;
// ConsoleLogger& operator=(ConsoleLogger&&) noexcept = default;
// TODO move
ConsoleLogger(ConsoleLogger&&) = delete;
ConsoleLogger& operator=(ConsoleLogger&&) = delete;
/**
* Check if the logger is enabled for the given log level and topic.
* This function is thread-safe.
*/
template <LogLevel level, LogTopic topic>
bool is_enabled() const noexcept
{
size_t topic_ix = static_cast<size_t>(topic);
LogLevel topic_level = topic_levels[topic_ix].load(std::memory_order::relaxed);
return static_cast<int>(level) <= static_cast<int>(topic_level);
}
/**
* Sets the log level for a given topic.
* This method is thread-safe.
*/
void set_level(LogTopic topic, LogLevel level) noexcept
{
topic_levels[static_cast<size_t>(topic)].store(level, std::memory_order::relaxed);
}
/**
* Set the minimum log level for all log topics.
* This overrides the compile-time defines per topic
* if the passed minimum log level is stricter.
*/
void set_min_level(LogLevel min_level) noexcept
{
for (size_t i = 0; i < topic_levels.size(); ++i)
{
auto topic_level = topic_levels[i].load(std::memory_order::relaxed);
if (static_cast<int>(topic_level) > static_cast<int>(min_level))
topic_levels[i].store(min_level, std::memory_order::relaxed);
}
}
/**
* Log a message with the given log level and topic.
*/
template <LogLevel level, LogTopic topic>
void log(
std::string_view message, const std::source_location loc = std::source_location::current()
) noexcept
{
if (!is_enabled<level, topic>())
return;
// time of day
auto now = std::chrono::system_clock::now();
auto now_us = std::chrono::floor<std::chrono::microseconds>(now);
// get color for log level
constexpr auto color = level_to_color<level>();
std::ostringstream msg;
// write time
msg << color << std::format("{:%Y-%m-%d %H:%M:%S}", now_us);
// write log level
msg << ' ' << level;
// write log topic
msg << ' ' << std::left << std::setw(17) << topic;
// write file name and line number
std::string filename_loc = std::format(
"{}:{}", extract_log_file_name(loc.file_name()), loc.line()
);
msg << ' ' << std::left << std::setw(20) << filename_loc;
// write message
msg << ": " << color << message << "\x1b[0m" << '\n';
{
// synchronize clog output
std::lock_guard<std::mutex> lock(clog_mutex);
std::clog << msg.str() << std::flush;
}
}
};
} // namespace ws_client