-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlogger.hpp
512 lines (462 loc) · 19.5 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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
#pragma once
#include "fly/logger/log.hpp"
#include "fly/system/system.hpp"
#include "fly/types/string/format.hpp"
#include <atomic>
#include <chrono>
#include <cstdint>
#include <filesystem>
#include <memory>
#include <string>
/**
* Add a debug log point to the default logger with trace information.
*
* At minimum, a format string is required as the first argument. Subsequent arguments are used to
* format that string. For example:
*
* LOGD("This is a message");
* LOGD("This is message number {:d}", 10);
*/
#define LOGD(format, ...) \
do \
{ \
fly::logger::Logger::get_default_logger()->debug( \
{__FILE__, __FUNCTION__, static_cast<std::uint32_t>(__LINE__)}, \
format __VA_OPT__(, ) __VA_ARGS__); \
} while (0)
/**
* Add an informational log point to the default logger with trace information.
*
* At minimum, a format string is required as the first argument. Subsequent arguments are used to
* format that string. For example:
*
* LOGI("This is a message");
* LOGI("This is message number {:d}", 10);
*/
#define LOGI(format, ...) \
do \
{ \
fly::logger::Logger::get_default_logger()->info( \
{__FILE__, __FUNCTION__, static_cast<std::uint32_t>(__LINE__)}, \
format __VA_OPT__(, ) __VA_ARGS__); \
} while (0)
/**
* Add a warning log point to the default logger with trace information.
*
* At minimum, a format string is required as the first argument. Subsequent arguments are used to
* format that string. For example:
*
* LOGW("This is a message");
* LOGW("This is message number {:d}", 10);
*/
#define LOGW(format, ...) \
do \
{ \
fly::logger::Logger::get_default_logger()->warn( \
{__FILE__, __FUNCTION__, static_cast<std::uint32_t>(__LINE__)}, \
format __VA_OPT__(, ) __VA_ARGS__); \
} while (0)
/**
* Add a system warning log point to the default logger with trace information. The log point will
* include the system's last error code and message.
*
* At minimum, a format string is required as the first argument. Subsequent arguments are used to
* format that string. For example:
*
* LOGS("This is a message");
* LOGS("This is message number {:d}", 10);
*/
#define LOGS(format, ...) \
do \
{ \
fly::logger::Logger::get_default_logger()->warn( \
{__FILE__, __FUNCTION__, static_cast<std::uint32_t>(__LINE__)}, \
format ": {}" __VA_OPT__(, ) __VA_ARGS__, \
fly::system::get_error_string()); \
} while (0)
/**
* Add an error log point to the default logger with trace information.
*
* At minimum, a format string is required as the first argument. Subsequent arguments are used to
* format that string. For example:
*
* LOGE("This is a message");
* LOGE("This is message number {:d}", 10);
*/
#define LOGE(format, ...) \
do \
{ \
fly::logger::Logger::get_default_logger()->error( \
{__FILE__, __FUNCTION__, static_cast<std::uint32_t>(__LINE__)}, \
format __VA_OPT__(, ) __VA_ARGS__); \
} while (0)
namespace fly::coders {
class CoderConfig;
} // namespace fly::coders
namespace fly::task {
class SequencedTaskRunner;
} // namespace fly::task
namespace fly::logger {
class Logger;
class LoggerConfig;
class Sink;
namespace detail {
class Registry;
} // namespace detail
/**
* Create a synchronous file logger.
*
* @param name Name of the logger to create.
* @param logger_config Reference to the logger configuration.
* @param coder_config Reference to the coder configuration.
* @param logger_directory Path to store log files.
*
* @return The created logger, or null if the logger could not be initialized.
*/
std::shared_ptr<Logger> create_file_logger(
std::string name,
std::shared_ptr<LoggerConfig> logger_config,
std::shared_ptr<fly::coders::CoderConfig> coder_config,
std::filesystem::path logger_directory);
/**
* Create an asynchronous file logger.
*
* @param name Name of the logger to create.
* @param task_runner The sequence on which logs are streamed.
* @param logger_config Reference to the logger configuration.
* @param coder_config Reference to the coder configuration.
* @param logger_directory Path to store log files.
*
* @return The created logger, or null if the logger could not be initialized.
*/
std::shared_ptr<Logger> create_file_logger(
std::string name,
std::shared_ptr<fly::task::SequencedTaskRunner> task_runner,
std::shared_ptr<LoggerConfig> logger_config,
std::shared_ptr<fly::coders::CoderConfig> coder_config,
std::filesystem::path logger_directory);
/**
* Create a synchronous console logger.
*
* @param name Name of the logger to create.
* @param logger_config Reference to the logger configuration.
*
* @return The created logger, or null if the logger could not be initialized.
*/
std::shared_ptr<Logger>
create_console_logger(std::string name, std::shared_ptr<LoggerConfig> logger_config);
/**
* Create an asynchronous console logger.
*
* @param name Name of the logger to create.
* @param task_runner The sequence on which logs are streamed.
* @param logger_config Reference to the logger configuration.
*
* @return The created logger, or null if the logger could not be initialized.
*/
std::shared_ptr<Logger> create_console_logger(
std::string name,
std::shared_ptr<fly::task::SequencedTaskRunner> task_runner,
std::shared_ptr<LoggerConfig> logger_config);
/**
* Logging class to provide configurable instrumentation. There are 4 levels of instrumentation:
*
* 1. Debug = Common points.
* 2. Informational = Less common, event based points.
* 3. Warning = Something went wrong, but the system is OK.
* 4. Error = Something went wrong, and the sytem is not OK.
*
* This class manages creating log points, but delegates the streaming of those log points to a log
* sink. Sinks may stream log points however they wish, for example to the console or to a file.
*
* Loggers may be created as synchronous or asynchronous loggers. With synchronous loggers, the
* log sink receives the log points immediately on the same thread they are created. Asynchronous
* loggers defer handing the log point to the sink to a dedicated thread sequence.
*
* Any number of loggers may be created. By default, a synchronous console logger will be used, but
* callers may override the default logger.
*
* The logging macros above may be used to add log points to the default logger. They are useful for
* providing trace information about the log point (e.g. file name, line number).
*
* @author Timothy Flynn ([email protected])
* @version July 21, 2016
*/
class Logger : public std::enable_shared_from_this<Logger>
{
template <typename... ParameterTypes>
using FormatString = fly::string::FormatString<char, ParameterTypes...>;
public:
/**
* Destructor.
*/
~Logger();
/**
* Create a synchronous logger with the provided log sink.
*
* @param name Name of the logger to create.
* @param logger_config Reference to the logger configuration.
* @param sink The log sink to receive log points for streaming.
*
* @return The created logger, or null if the logger could not be initialized.
*/
static std::shared_ptr<Logger> create(
std::string name,
std::shared_ptr<LoggerConfig> logger_config,
std::unique_ptr<Sink> &&sink);
/**
* Create an asynchronous logger with the provided log sink.
*
* @param name Name of the logger to create.
* @param task_runner The sequence on which logs are streamed.
* @param logger_config Reference to the logger configuration.
* @param sink The log sink to receive log points for streaming.
*
* @return The created logger, or null if the logger could not be initialized.
*/
static std::shared_ptr<Logger> create(
std::string name,
std::shared_ptr<fly::task::SequencedTaskRunner> task_runner,
std::shared_ptr<LoggerConfig> logger_config,
std::unique_ptr<Sink> &&sink);
/**
* Set the default logger instance for the LOG* macro functions. If the provided logger is null,
* the default logger is reset to the initial synchronous console logger.
*
* The default logger is retained until it is replaced or reset.
*
* Warning: Setting the default logger is not thread-safe. Do not set the default logger on one
* thread while invoking a LOG* macro on another thread. The default logger should be set once
* during initialization.
*
* @param default_logger The logger instance.
*/
static void set_default_logger(std::shared_ptr<Logger> default_logger);
/**
* @return The default logger instance for the LOG* macro functions.
*/
static Logger *get_default_logger();
/**
* Retrieve a logger by name. If the logger is not found, or if the logger instance has been
* deleted, returns null.
*
* @param name Name of the logger to retrieve.
*
* @return The logger instance, or null.
*/
static std::shared_ptr<Logger> get(std::string const &name);
/**
* @return This logger's name.
*/
std::string const &name() const;
/**
* Add a debug log point to the logger.
*
* @tparam ParameterTypes Variadic format parameter types.
*
* @param format The format string for the log point.
* @param parameters The variadic list of arguments to augment the format string with.
*/
template <typename... ParameterTypes>
void debug(FormatString<ParameterTypes...> &&format, ParameterTypes &&...parameters);
/**
* Add a debug log point to the logger with trace information.
*
* @tparam ParameterTypes Variadic format parameter types.
*
* @param trace The trace information for the log point.
* @param format The format string for the log point.
* @param parameters The variadic list of arguments to augment the format string with.
*/
template <typename... ParameterTypes>
void
debug(Trace &&trace, FormatString<ParameterTypes...> &&format, ParameterTypes &&...parameters);
/**
* Add an informational log point to the logger.
*
* @tparam ParameterTypes Variadic format parameter types.
*
* @param format The format string for the log point.
* @param parameters The variadic list of arguments to augment the format string with.
*/
template <typename... ParameterTypes>
void info(FormatString<ParameterTypes...> &&format, ParameterTypes &&...parameters);
/**
* Add an informational log point to the logger with trace information.
*
* @tparam ParameterTypes Variadic format parameter types.
*
* @param trace The trace information for the log point.
* @param format The format string for the log point.
* @param parameters The variadic list of arguments to augment the format string with.
*/
template <typename... ParameterTypes>
void
info(Trace &&trace, FormatString<ParameterTypes...> &&format, ParameterTypes &&...parameters);
/**
* Add a warning log point to the logger.
*
* @tparam ParameterTypes Variadic format parameter types.
*
* @param format The format string for the log point.
* @param parameters The variadic list of arguments to augment the format string with.
*/
template <typename... ParameterTypes>
void warn(FormatString<ParameterTypes...> &&format, ParameterTypes &&...parameters);
/**
* Add a warning log point to the logger with trace information.
*
* @tparam ParameterTypes Variadic format parameter types.
*
* @param trace The trace information for the log point.
* @param format The format string for the log point.
* @param parameters The variadic list of arguments to augment the format string with.
*/
template <typename... ParameterTypes>
void
warn(Trace &&trace, FormatString<ParameterTypes...> &&format, ParameterTypes &&...parameters);
/**
* Add an error log point to the logger.
*
* @tparam ParameterTypes Variadic format parameter types.
*
* @param format The format string for the log point.
* @param parameters The variadic list of arguments to augment the format string with.
*/
template <typename... ParameterTypes>
void error(FormatString<ParameterTypes...> &&format, ParameterTypes &&...parameters);
/**
* Add an error log point to the logger with trace information.
*
* @tparam ParameterTypes Variadic format parameter types.
*
* @param trace The trace information for the log point.
* @param format The format string for the log point.
* @param parameters The variadic list of arguments to augment the format string with.
*/
template <typename... ParameterTypes>
void
error(Trace &&trace, FormatString<ParameterTypes...> &&format, ParameterTypes &&...parameters);
private:
friend class detail::Registry;
/**
* Constructor. Creates a synchronous or an asynchronous logger, depending on whether the given
* task runner is null.
*
* @param name Name of the logger.
* @param task_runner If not null, the sequence on which logs are streamed.
* @param config Reference to the logger configuration.
* @param sink The log sink to receive log points for streaming.
*/
Logger(
std::string name,
std::shared_ptr<fly::task::SequencedTaskRunner> task_runner,
std::shared_ptr<LoggerConfig> config,
std::unique_ptr<Sink> &&sink) noexcept;
/**
* Initialize the log sink.
*
* @return True if the logger could be initialized.
*/
bool initialize();
/**
* Add a log point to the logger, optionally with trace information.
*
* Synchronous loggers will forward the log to the log sink immediately. Asynchronous loggers
* will post a task to forward the log later.
*
* @param level The level of the log point.
* @param trace The trace information for the log point.
* @param message The message to log.
*/
void log(Level level, Trace &&trace, std::string &&message);
/**
* Forward a log point to the log sink.
*
* @param level The level of the log point.
* @param trace The trace information for the log point.
* @param message The message to log.
* @param time The time the log point was made.
*/
void log_to_sink(
Level level,
Trace &&trace,
std::string &&message,
std::chrono::steady_clock::time_point time);
std::string const m_name;
std::shared_ptr<LoggerConfig> m_config;
std::unique_ptr<Sink> m_sink;
std::shared_ptr<fly::task::SequencedTaskRunner> m_task_runner;
std::atomic_bool m_last_task_failed {true};
std::chrono::steady_clock::time_point const m_start_time;
std::uintmax_t m_index {0};
};
//==================================================================================================
template <typename... ParameterTypes>
inline void Logger::debug(FormatString<ParameterTypes...> &&format, ParameterTypes &&...parameters)
{
debug({}, std::move(format), std::forward<ParameterTypes>(parameters)...);
}
//==================================================================================================
template <typename... ParameterTypes>
inline void Logger::debug(
Trace &&trace,
FormatString<ParameterTypes...> &&format,
ParameterTypes &&...parameters)
{
log(Level::Debug,
std::move(trace),
fly::string::format(std::move(format), std::forward<ParameterTypes>(parameters)...));
}
//==================================================================================================
template <typename... ParameterTypes>
inline void Logger::info(FormatString<ParameterTypes...> &&format, ParameterTypes &&...parameters)
{
info({}, std::move(format), std::forward<ParameterTypes>(parameters)...);
}
//==================================================================================================
template <typename... ParameterTypes>
inline void Logger::info(
Trace &&trace,
FormatString<ParameterTypes...> &&format,
ParameterTypes &&...parameters)
{
log(Level::Info,
std::move(trace),
fly::string::format(std::move(format), std::forward<ParameterTypes>(parameters)...));
}
//==================================================================================================
template <typename... ParameterTypes>
inline void Logger::warn(FormatString<ParameterTypes...> &&format, ParameterTypes &&...parameters)
{
warn({}, std::move(format), std::forward<ParameterTypes>(parameters)...);
}
//==================================================================================================
template <typename... ParameterTypes>
inline void Logger::warn(
Trace &&trace,
FormatString<ParameterTypes...> &&format,
ParameterTypes &&...parameters)
{
log(Level::Warn,
std::move(trace),
fly::string::format(std::move(format), std::forward<ParameterTypes>(parameters)...));
}
//==================================================================================================
template <typename... ParameterTypes>
inline void Logger::error(FormatString<ParameterTypes...> &&format, ParameterTypes &&...parameters)
{
error({}, std::move(format), std::forward<ParameterTypes>(parameters)...);
}
//==================================================================================================
template <typename... ParameterTypes>
inline void Logger::error(
Trace &&trace,
FormatString<ParameterTypes...> &&format,
ParameterTypes &&...parameters)
{
log(Level::Error,
std::move(trace),
fly::string::format(std::move(format), std::forward<ParameterTypes>(parameters)...));
}
} // namespace fly::logger