-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathLogger.hpp
379 lines (344 loc) · 11 KB
/
Logger.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/**
* Logger facility.
*
* This include file defines logging macros for source files (.cpp). Each
* source file including Logger.hpp MUST define its own MS_CLASS macro. Include
* files (.h) MUST NOT include Logger.hpp.
*
* All the logging macros use the same format as printf(). The XXX_STD version
* of a macro logs to stdoud/stderr instead of using the Channel instance.
*
* If the macro MS_LOG_FILE_LINE is defied, all the logging macros print more
* verbose information, including current file and line.
*
* MS_TRACE()
*
* Logs the current method/function if MS_LOG_TRACE macro is defined and the
* current debug level is "debug".
*
* MS_HAS_DEBUG_TAG(tag)
* MS_HAS_WARN_TAG(tag)
*
* True if the current debug level is satisfied and the given tag is enabled
* (or if the current source file defines the MS_LOG_DEV macro).
*
* MS_DEBUG_TAG(tag, ...)
* MS_WARN_TAG(tag, ...)
*
* Logs if the current debug level is satisfied and the given tag is enabled
* (or if the current source file defines the MS_LOG_DEV macro).
* Example:
* MS_WARN_TAG(ice, "ICE failed");
*
* MS_DEBUG_2TAGS(tag1, tag2, ...)
* MS_WARN_2TAGS(tag1, tag2, ...)
*
* Logs if the current debug level is satisfied and any of the given two tags
* is enabled (or if the current source file defines the MS_LOG_DEV macro).
* Example:
* MS_DEBUG_2TAGS(ice, dtls, "media connection established");
*
* MS_DEBUG_DEV(...)
* MS_WARN_DEV(...)
*
* Logs if the current source file defines the MS_LOG_DEV macro.
* Example:
* MS_DEBUG_DEV("Room closed [roomId:%" PRIu32 "]", roomId);
*
* MS_DUMP(...)
*
* For Dump() methods.
*
* MS_ERROR(...)
*
* Logs an error. Must just be used for internal errors that should not
* happen.
*
* MS_ABORT(...)
*
* Logs the given error to stderr and aborts the process.
*
* MS_ASSERT(condition, ...)
*
* If the condition is not satisfied, it calls MS_ABORT().
*/
#ifndef MS_LOGGER_HPP
#define MS_LOGGER_HPP
#include "common.hpp"
#include "LogLevel.hpp"
#include "Settings.hpp"
#include "Channel/UnixStreamSocket.hpp"
#include <cstdio> // std::snprintf(), std::fprintf(), stdout, stderr
#include <cstdlib> // std::abort()
#include <cstring>
#include <string>
// clang-format off
#define _MS_TAG_ENABLED(tag) Settings::configuration.logTags.tag
#define _MS_TAG_ENABLED_2(tag1, tag2) (Settings::configuration.logTags.tag1 || Settings::configuration.logTags.tag2)
#ifdef MS_LOG_DEV
#define _MS_LOG_DEV_ENABLED true
#else
#define _MS_LOG_DEV_ENABLED false
#endif
// Usage:
// MS_DEBUG_DEV("Leading text "MS_UINT16_TO_BINARY_PATTERN, MS_UINT16_TO_BINARY(value));
#define MS_UINT16_TO_BINARY_PATTERN "%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c"
#define MS_UINT16_TO_BINARY(value) \
((value & 0x8000) ? '1' : '0'), \
((value & 0x4000) ? '1' : '0'), \
((value & 0x2000) ? '1' : '0'), \
((value & 0x1000) ? '1' : '0'), \
((value & 0x800) ? '1' : '0'), \
((value & 0x400) ? '1' : '0'), \
((value & 0x200) ? '1' : '0'), \
((value & 0x100) ? '1' : '0'), \
((value & 0x80) ? '1' : '0'), \
((value & 0x40) ? '1' : '0'), \
((value & 0x20) ? '1' : '0'), \
((value & 0x10) ? '1' : '0'), \
((value & 0x08) ? '1' : '0'), \
((value & 0x04) ? '1' : '0'), \
((value & 0x02) ? '1' : '0'), \
((value & 0x01) ? '1' : '0')
class Logger
{
public:
static void Init(const std::string &id, Channel::UnixStreamSocket* channel);
static void Init(const std::string &id);
public:
static std::string id;
static Channel::UnixStreamSocket* channel;
static const size_t bufferSize {10000};
static char buffer[];
};
/* Logging macros. */
#define _MS_LOG_SEPARATOR_CHAR_STD "\n"
#ifdef MS_LOG_FILE_LINE
#define _MS_LOG_STR "[%s] %s:%d | %s::%s()"
#define _MS_LOG_STR_DESC _MS_LOG_STR " | "
#define _MS_FILE (std::strchr(__FILE__, '/') ? std::strchr(__FILE__, '/') + 1 : __FILE__)
#define _MS_LOG_ARG ("id:" + Logger::id).c_str(), _MS_FILE, __LINE__, MS_CLASS, __FUNCTION__
#else
#define _MS_LOG_STR "[%s] %s::%s()"
#define _MS_LOG_STR_DESC _MS_LOG_STR " | "
#define _MS_LOG_ARG ("id:" + Logger::id).c_str(), MS_CLASS, __FUNCTION__
#endif
#ifdef MS_LOG_TRACE
#define MS_TRACE() \
do \
{ \
if (LogLevel::LOG_DEBUG == Settings::configuration.logLevel) \
{ \
int loggerWritten = std::snprintf(Logger::buffer, Logger::bufferSize, "D(trace) " _MS_LOG_STR, _MS_LOG_ARG); \
Logger::channel->SendLog(Logger::buffer, loggerWritten); \
} \
} \
while (false)
#define MS_TRACE_STD() \
do \
{ \
if (LogLevel::LOG_DEBUG == Settings::configuration.logLevel) \
{ \
std::fprintf(stdout, "(trace) " _MS_LOG_STR _MS_LOG_SEPARATOR_CHAR_STD, _MS_LOG_ARG); \
std::fflush(stdout); \
} \
} \
while (false)
#else
#define MS_TRACE() ;
#define MS_TRACE_STD() ;
#endif
#define MS_HAS_DEBUG_TAG(tag) \
(LogLevel::LOG_DEBUG == Settings::configuration.logLevel && (_MS_TAG_ENABLED(tag) || _MS_LOG_DEV_ENABLED))
#define MS_HAS_WARN_TAG(tag) \
(LogLevel::LOG_WARN <= Settings::configuration.logLevel && (_MS_TAG_ENABLED(tag) || _MS_LOG_DEV_ENABLED))
#define MS_DEBUG_TAG(tag, desc, ...) \
do \
{ \
if (LogLevel::LOG_DEBUG == Settings::configuration.logLevel && (_MS_TAG_ENABLED(tag) || _MS_LOG_DEV_ENABLED)) \
{ \
int loggerWritten = std::snprintf(Logger::buffer, Logger::bufferSize, "D" _MS_LOG_STR_DESC desc, _MS_LOG_ARG, ##__VA_ARGS__); \
Logger::channel->SendLog(Logger::buffer, loggerWritten); \
} \
} \
while (false)
#define MS_DEBUG_TAG_STD(tag, desc, ...) \
do \
{ \
if (LogLevel::LOG_DEBUG == Settings::configuration.logLevel && (_MS_TAG_ENABLED(tag) || _MS_LOG_DEV_ENABLED)) \
{ \
std::fprintf(stdout, _MS_LOG_STR_DESC desc _MS_LOG_SEPARATOR_CHAR_STD, _MS_LOG_ARG, ##__VA_ARGS__); \
std::fflush(stdout); \
} \
} \
while (false)
#define MS_WARN_TAG(tag, desc, ...) \
do \
{ \
if (LogLevel::LOG_WARN <= Settings::configuration.logLevel && (_MS_TAG_ENABLED(tag) || _MS_LOG_DEV_ENABLED)) \
{ \
int loggerWritten = std::snprintf(Logger::buffer, Logger::bufferSize, "W" _MS_LOG_STR_DESC desc, _MS_LOG_ARG, ##__VA_ARGS__); \
Logger::channel->SendLog(Logger::buffer, loggerWritten); \
} \
} \
while (false)
#define MS_WARN_TAG_STD(tag, desc, ...) \
do \
{ \
if (LogLevel::LOG_WARN <= Settings::configuration.logLevel && (_MS_TAG_ENABLED(tag) || _MS_LOG_DEV_ENABLED)) \
{ \
std::fprintf(stderr, _MS_LOG_STR_DESC desc _MS_LOG_SEPARATOR_CHAR_STD, _MS_LOG_ARG, ##__VA_ARGS__); \
std::fflush(stderr); \
} \
} \
while (false)
#define MS_DEBUG_2TAGS(tag1, tag2, desc, ...) \
do \
{ \
if (LogLevel::LOG_DEBUG == Settings::configuration.logLevel && (_MS_TAG_ENABLED_2(tag1, tag2) || _MS_LOG_DEV_ENABLED)) \
{ \
int loggerWritten = std::snprintf(Logger::buffer, Logger::bufferSize, "D" _MS_LOG_STR_DESC desc, _MS_LOG_ARG, ##__VA_ARGS__); \
Logger::channel->SendLog(Logger::buffer, loggerWritten); \
} \
} \
while (false)
#define MS_DEBUG_2TAGS_STD(tag1, tag2, desc, ...) \
do \
{ \
if (LogLevel::LOG_DEBUG == Settings::configuration.logLevel && (_MS_TAG_ENABLED_2(tag1, tag2) || _MS_LOG_DEV_ENABLED)) \
{ \
std::fprintf(stdout, _MS_LOG_STR_DESC desc _MS_LOG_SEPARATOR_CHAR_STD, _MS_LOG_ARG, ##__VA_ARGS__); \
std::fflush(stdout); \
} \
} \
while (false)
#define MS_WARN_2TAGS(tag1, tag2, desc, ...) \
do \
{ \
if (LogLevel::LOG_WARN <= Settings::configuration.logLevel && (_MS_TAG_ENABLED_2(tag1, tag2) || _MS_LOG_DEV_ENABLED)) \
{ \
int loggerWritten = std::snprintf(Logger::buffer, Logger::bufferSize, "W" _MS_LOG_STR_DESC desc, _MS_LOG_ARG, ##__VA_ARGS__); \
Logger::channel->SendLog(Logger::buffer, loggerWritten); \
} \
} \
while (false)
#define MS_WARN_2TAGS_STD(tag1, tag2, desc, ...) \
do \
{ \
if (LogLevel::LOG_WARN <= Settings::configuration.logLevel && (_MS_TAG_ENABLED_2(tag1, tag2) || _MS_LOG_DEV_ENABLED)) \
{ \
std::fprintf(stderr, _MS_LOG_STR_DESC desc _MS_LOG_SEPARATOR_CHAR_STD, _MS_LOG_ARG, ##__VA_ARGS__); \
std::fflush(stderr); \
} \
} \
while (false)
#ifdef MS_LOG_DEV
#define MS_DEBUG_DEV(desc, ...) \
do \
{ \
if (LogLevel::LOG_DEBUG == Settings::configuration.logLevel) \
{ \
int loggerWritten = std::snprintf(Logger::buffer, Logger::bufferSize, "D" _MS_LOG_STR_DESC desc, _MS_LOG_ARG, ##__VA_ARGS__); \
Logger::channel->SendLog(Logger::buffer, loggerWritten); \
} \
} \
while (false)
#define MS_DEBUG_DEV_STD(desc, ...) \
do \
{ \
if (LogLevel::LOG_DEBUG == Settings::configuration.logLevel) \
{ \
std::fprintf(stdout, _MS_LOG_STR_DESC desc _MS_LOG_SEPARATOR_CHAR_STD, _MS_LOG_ARG, ##__VA_ARGS__); \
std::fflush(stdout); \
} \
} \
while (false)
#define MS_WARN_DEV(desc, ...) \
do \
{ \
if (LogLevel::LOG_WARN <= Settings::configuration.logLevel) \
{ \
int loggerWritten = std::snprintf(Logger::buffer, Logger::bufferSize, "W" _MS_LOG_STR_DESC desc, _MS_LOG_ARG, ##__VA_ARGS__); \
Logger::channel->SendLog(Logger::buffer, loggerWritten); \
} \
} \
while (false)
#define MS_WARN_DEV_STD(desc, ...) \
do \
{ \
if (LogLevel::LOG_WARN <= Settings::configuration.logLevel) \
{ \
std::fprintf(stderr, _MS_LOG_STR_DESC desc _MS_LOG_SEPARATOR_CHAR_STD, _MS_LOG_ARG, ##__VA_ARGS__); \
std::fflush(stderr); \
} \
} \
while (false)
#else
#define MS_DEBUG_DEV(desc, ...) ;
#define MS_DEBUG_DEV_STD(desc, ...) ;
#define MS_WARN_DEV(desc, ...) ;
#define MS_WARN_DEV_STD(desc, ...) ;
#endif
#define MS_DUMP(desc, ...) \
do \
{ \
int loggerWritten = std::snprintf(Logger::buffer, Logger::bufferSize, "D" _MS_LOG_STR_DESC desc, _MS_LOG_ARG, ##__VA_ARGS__); \
Logger::channel->SendLog(Logger::buffer, loggerWritten); \
} \
while (false)
#define MS_DUMP_STD(desc, ...) \
do \
{ \
std::fprintf(stdout, _MS_LOG_STR_DESC desc _MS_LOG_SEPARATOR_CHAR_STD, _MS_LOG_ARG, ##__VA_ARGS__); \
std::fflush(stdout); \
} \
while (false)
#define MS_ERROR(desc, ...) \
do \
{ \
int loggerWritten = std::snprintf(Logger::buffer, Logger::bufferSize, "E" _MS_LOG_STR_DESC desc, _MS_LOG_ARG, ##__VA_ARGS__); \
Logger::channel->SendLog(Logger::buffer, loggerWritten); \
} \
while (false)
#define MS_ERROR_STD(desc, ...) \
do \
{ \
std::fprintf(stderr, _MS_LOG_STR_DESC desc _MS_LOG_SEPARATOR_CHAR_STD, _MS_LOG_ARG, ##__VA_ARGS__); \
std::fflush(stderr); \
} \
while (false)
#define MS_ABORT(desc, ...) \
do \
{ \
std::fprintf(stderr, "ABORT" _MS_LOG_STR_DESC desc _MS_LOG_SEPARATOR_CHAR_STD, _MS_LOG_ARG, ##__VA_ARGS__); \
std::fflush(stderr); \
std::abort(); \
} \
while (false)
#define MS_ASSERT(condition, desc, ...) \
if (!(condition)) \
{ \
MS_ABORT("failed assertion `%s': " desc, #condition, ##__VA_ARGS__); \
}
#ifdef MS_LOG_STD
#undef MS_TRACE
#define MS_TRACE MS_TRACE_STD
#undef MS_DEBUG_TAG
#define MS_DEBUG_TAG MS_DEBUG_TAG_STD
#undef MS_WARN_TAG
#define MS_WARN_TAG MS_WARN_TAG_STD
#undef MS_DEBUG_2TAGS
#define MS_DEBUG_2TAGS MS_DEBUG_2TAGS_STD
#undef MS_WARN_2TAGS
#define MS_WARN_2TAGS MS_WARN_2TAGS_STD
#undef MS_DEBUG_DEV
#define MS_DEBUG_DEV MS_DEBUG_DEV_STD
#undef MS_WARN_DEV
#define MS_WARN_DEV MS_WARN_DEV_STD
#undef MS_DUMP
#define MS_DUMP MS_DUMP_STD
#undef MS_ERROR
#define MS_ERROR MS_ERROR_STD
#endif
// clang-format on
#endif