-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils_logger.hpp
638 lines (554 loc) · 27 KB
/
utils_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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
#ifndef UTILS_LOGGER_HPP
#define UTILS_LOGGER_HPP
#include "utils_string.hpp"
#include "utils_print.hpp"
#include "utils_os.hpp"
#include "utils_time.hpp"
#include "utils_test.hpp"
#include "utils_threading.hpp"
#include "utils_algorithm.hpp"
#include <iostream>
#include <ostream>
#include <fstream>
#include <mutex>
#ifdef LOG_ERROR_TRACE
#undef LOG_ERROR_TRACE
#endif
/**
* Macro to print an error trace to Logger.
*/
#define LOG_ERROR_TRACE(E) utils::Logger::ErrorTrace(UTILS_TRACE_LOCATION, E);
namespace utils {
/**
* \brief A class with static methods for logging to std::cout and a log file.
* Create the Loging instance with Logger::Create(),
* and destroy it by calling Logger::Destroy()
*
* Use https://github.com/gabime/spdlog for a more extensive logger.
*/
class Logger {
public:
/**
* \brief Log level enum.
* Note: Prefixed with `LOG_` to mitigate MACRO
* clash in windows related headers...
*/
enum class Level {
LOG_EMERGENCY = 0, // Emergency - A "panic" condition usually affecting multiple apps/servers/sites. At this level it would usually notify all tech staff on call.
LOG_ALERT = 1, // Alert - Should be corrected immediately, therefore notify staff who can fix the problem. An example would be the loss of a primary ISP connection.
LOG_CRITICAL = 2, // Critical - Should be corrected immediately, but indicates failure in a secondary system, an example is a loss of a backup ISP connection.
LOG_ERROR = 3, // Error - Non-urgent failures, these should be relayed to developers or admins; each item must be resolved within a given time.
LOG_WARNING = 4, // Warning - Warning messages, not an error, but indication that an error will occur if action is not taken, e.g. file system 85% full - each item must be resolved within a given time.
LOG_NOTICE = 5, // Notice - Events that are unusual but not error conditions - might be summarized in an email to developers or admins to spot potential problems - no immediate action required.
LOG_INFO = 6, // Informational - Normal operational messages - may be harvested for reporting, measuring throughput, etc. - no action required.
LOG_DEBUG = 7 // Debug - Info useful to developers for debugging the application, not useful during operations.
};
private:
bool screen_enabled;
bool screen_paused;
bool file_enabled;
bool file_paused;
bool file_timestamp;
std::ostream& screen_output;
std::ofstream log_file;
Logger::Level level_screen;
Logger::Level level_file;
std::mutex logger_mutex;
std::mutex file_mutex;
std::mutex screen_mutex;
static /*inline*/ Logger& get() {
static Logger instance;
return instance;
}
inline bool canLogScreen(const Logger::Level level = Logger::Level::LOG_EMERGENCY) const {
return this->screen_enabled && !this->screen_paused
&& level <= this->level_screen;
}
inline bool canLogFile(const Logger::Level level = Logger::Level::LOG_EMERGENCY) const {
return this->file_enabled && !this->file_paused
&& level <= this->level_file;
}
inline bool canLog(const Logger::Level level = Logger::Level::LOG_EMERGENCY) const {
return this->canLogScreen(level) || this->canLogFile(level);
}
inline void write_to_screen(const std::string_view text) {
LOCK_BLOCK(utils::Logger::get().screen_mutex);
if (HEDLEY_LIKELY(this->canLogScreen())) {
this->screen_output << text;
}
}
inline void write_to_file(const std::string_view text) {
LOCK_BLOCK(utils::Logger::get().file_mutex);
if (HEDLEY_LIKELY(this->canLogFile())) {
try {
if (HEDLEY_LIKELY(this->IsFileTimestampEnabled())) {
this->log_file << utils::time::Timestamp("[%Y-%m-%d %H:%M:%S] ");
}
this->log_file << text;
} catch (std::exception const& e) {
std::cerr << "[Logger][ERROR] " << e.what() << '\n';
utils::Logger::DestroyFile();
}
}
}
template<typename ...Type>
void hdr_colour_format(const Logger::Level level,
const utils::os::command_t hdr_colour,
const std::string_view hdr_str,
const std::string_view format,
const Type& ...args)
{
LOCK_BLOCK(utils::Logger::get().logger_mutex);
if (HEDLEY_LIKELY(this->canLog(level))) {
const bool stamp = this->IsFileTimestampEnabled();
this->Command( utils::os::Console::FG
| utils::os::Console::BOLD
| hdr_colour);
this->Write("[" + std::string(hdr_str) + "]", true);
this->Command(utils::os::Console::RESET
| utils::os::Console::WHITE);
this->SetFileTimestamp(false);
this->Writef(" " + std::string(format) + utils::Logger::CRLF, args...);
this->SetFileTimestamp(stamp);
this->Command(utils::os::Console::RESET);
}
}
Logger()
: screen_enabled(true)
, screen_paused(false)
, file_enabled(false)
, file_paused(false)
, file_timestamp(true)
, screen_output(std::cout)
, level_screen(Level::LOG_INFO)
, level_file(Level::LOG_INFO)
{
utils::os::EnableVirtualConsole();
if (HEDLEY_UNLIKELY(this->screen_output.bad())) {
this->screen_enabled = false;
std::cerr << "[Logger][ERROR] Bad screen output stream!\n";
}
}
/**
* Dtor: write line to outputs and close streams.
*/
~Logger() {
const std::string end_line =
utils::Logger::CRLF
+ utils::Logger::LINE<>
+ utils::Logger::CRLF
+ utils::Logger::CRLF;
if (this->file_enabled) {
this->SetFileTimestamp(false);
this->write_to_file(end_line);
this->log_file.close();
this->file_enabled = false;
}
this->write_to_screen(end_line);
}
public:
Logger(Logger const&) = delete;
Logger(Logger&&) = delete;
void operator=(Logger const&) = delete;
Logger& operator=(Logger&&) = delete;
static void InitFile(const std::string& fileName = "",
const utils::Logger::Level level = utils::Logger::Level::LOG_INFO)
{
utils::Logger::DestroyFile();
utils::Logger::SetFileLogLevel(level);
LOCK_BLOCK(utils::Logger::get().file_mutex);
bool enable_file = false;
if (HEDLEY_LIKELY(fileName.length() > 0)) {
try {
utils::Logger::get().log_file.open(fileName, std::ios_base::app | std::ios_base::out);
enable_file = true;
} catch (std::exception const& e) {
std::cerr << "[Logger][ERROR] " << e.what() << '\n';
enable_file = false;
}
}
utils::Logger::get().file_enabled = enable_file;
}
static void InitScreen(std::ostream& console_stream = std::cout,
const utils::Logger::Level level = utils::Logger::Level::LOG_INFO)
{
utils::Logger::DestroyScreen();
utils::Logger::SetScreenLogLevel(level);
LOCK_BLOCK(utils::Logger::get().screen_mutex);
if (HEDLEY_UNLIKELY(console_stream.bad())) {
utils::Logger::get().screen_output.tie(&std::cerr);
utils::Logger::get().screen_output << "[Logger][ERROR] Bad screen output stream!\n";
} else {
utils::Logger::get().screen_output.tie(&console_stream);
}
utils::os::EnableVirtualConsole();
utils::Logger::get().screen_enabled = true;
}
/**
* @brief Create the Logger instance from a filename.
* If the file does not exist, it will be created, text will be appended otherwise.
*
* Disable logging if fileName is an empty string or on error.
*
* Writes version and author info to the log on start.
*
* @param fileName
* The log file to use.
*/
static void Create(const std::string& fileName = "",
const utils::Logger::Level level = utils::Logger::Level::LOG_INFO,
std::ostream& console_stream = std::cout)
{
utils::Logger::InitScreen(console_stream, level);
utils::Logger::InitFile(fileName, level);
}
/**
* \brief Destroy the file Logging instance by closing the output file.
*/
static void DestroyFile() {
LOCK_BLOCK(utils::Logger::get().file_mutex);
if (utils::Logger::get().file_enabled) {
utils::Logger::get().log_file.close();
utils::Logger::get().file_enabled = false;
}
}
/**
* \brief Destroy the screen Logging instance.
*/
static void DestroyScreen() {
LOCK_BLOCK(utils::Logger::get().screen_mutex);
if (utils::Logger::get().screen_enabled) {
utils::os::Command(utils::os::Console::RESET, utils::Logger::get().screen_output);
utils::Logger::get().screen_output.flush();
utils::Logger::get().screen_enabled = false;
}
}
static inline std::ostream& GetConsoleStream() {
return utils::Logger::get().screen_output;
}
static inline std::ofstream& GetFileStream() {
return utils::Logger::get().log_file;
}
/**
* \brief Write a separator (line) to the stream.
*/
static void Separator(void) {
utils::Logger::WriteLn(utils::Logger::LINE<>);
}
/**
* @brief Format the given args and write to the streams.
*
* @param format
* The text to format in and write.
* @param args
*/
template<typename ...Type>
static void Writef(const std::string_view format, const Type& ...args) {
if constexpr (sizeof...(args) > 0) {
utils::Logger::Write(utils::string::format(format, args...), utils::Logger::IsFileTimestampEnabled());
} else {
utils::Logger::Write(format, utils::Logger::IsFileTimestampEnabled());
}
}
/**
* @brief Write the text <text> to the console and the log file.
*
* @param text
* The text to write.
*/
static void Write(const std::string_view text, const bool timestamp = false) {
if (HEDLEY_LIKELY(utils::Logger::get().canLog())) {
const bool stamp = utils::Logger::IsFileTimestampEnabled();
utils::Logger::SetFileTimestamp(timestamp);
utils::Logger::get().write_to_screen(text);
utils::Logger::get().write_to_file(text);
utils::Logger::SetFileTimestamp(stamp);
}
}
static inline void WriteLn(const std::string_view text, const bool timestamp = false) {
utils::Logger::Write(text, timestamp);
utils::Logger::Write(utils::Logger::CRLF, false);
}
template<typename ...Type>
static void Debug(const std::string_view format, const Type& ...args) {
utils::Logger::get().hdr_colour_format(
utils::Logger::Level::LOG_DEBUG,
utils::os::Console::BG | utils::os::Console::BLUE, "DEBUG",
format, args...
);
}
template<typename ...Type>
static void Success(const std::string_view format, const Type& ...args) {
utils::Logger::get().hdr_colour_format(
utils::Logger::Level::LOG_INFO,
utils::os::Console::GREEN, "Success",
format, args...
);
}
template<typename ...Type>
static void Info(const std::string_view format, const Type& ...args) {
utils::Logger::get().hdr_colour_format(
utils::Logger::Level::LOG_INFO,
utils::os::Console::CYAN, "Info",
format, args...
);
}
template<typename ...Type>
static void Notice(const std::string_view format, const Type& ...args) {
utils::Logger::get().hdr_colour_format(
utils::Logger::Level::LOG_NOTICE,
utils::os::Console::BG |
utils::os::Console::BRIGHT |
utils::os::Console::CYAN,
"Notice",
format, args...
);
}
template<typename ...Type>
static void Warn(const std::string_view format, const Type& ...args) {
utils::Logger::get().hdr_colour_format(
utils::Logger::Level::LOG_WARNING,
utils::os::Console::YELLOW, "Warning",
format, args...
);
}
template<typename ...Type>
static void Error(const std::string_view format, const Type& ...args) {
utils::Logger::get().hdr_colour_format(
utils::Logger::Level::LOG_ERROR,
utils::os::Console::RED, "Error",
format, args...
);
}
template<typename ...Type>
static void Critical(const std::string_view format, const Type& ...args) {
utils::Logger::get().hdr_colour_format(
utils::Logger::Level::LOG_CRITICAL,
utils::os::Console::BG | utils::os::Console::RED, "Critical",
format, args...
);
}
template<typename ...Type>
static void Alert(const std::string_view format, const Type& ...args) {
utils::Logger::get().hdr_colour_format(
utils::Logger::Level::LOG_ALERT,
utils::os::Console::MAGENTA, "Alert",
format, args...
);
}
template<typename ...Type>
static void Emergency(const std::string_view format, const Type& ...args) {
utils::Logger::get().hdr_colour_format(
utils::Logger::Level::LOG_EMERGENCY,
utils::os::Console::BG |
utils::os::Console::BRIGHT |
utils::os::Console::MAGENTA,
"Emergency",
format, args...
);
}
HEDLEY_NON_NULL(1,3)
static void ErrorTrace(const char* file, const int line, const char* function, const std::exception& e) {
LOCK_BLOCK(utils::Logger::get().logger_mutex);
if (HEDLEY_LIKELY(utils::Logger::get().canLog())) {
utils::Logger::Command( utils::os::Console::BG
| utils::os::Console::BRIGHT
| utils::os::Console::RED);
utils::Logger::Command( utils::os::Console::FG
| utils::os::Console::BRIGHT
| utils::os::Console::WHITE);
utils::Logger::Write( utils::Logger::CRLF
+ utils::Logger::LINE<'-'>, false);
utils::Logger::Command( utils::os::Console::RESET
| utils::os::Console::FG
| utils::os::Console::BOLD
| utils::os::Console::RED);
utils::Logger::WriteLn("");
utils::Logger::Write("[ERROR] Exception thrown:\n ", true);
utils::Logger::Command( utils::os::Console::FG
| utils::os::Console::YELLOW);
utils::Logger::Write(e.what());
utils::Logger::Command( utils::os::Console::RESET);
utils::Logger::Write("\n at ");
utils::Logger::Command( utils::os::Console::FG
| utils::os::Console::BRIGHT
| utils::os::Console::CYAN);
utils::Logger::Write(file);
utils::Logger::Command( utils::os::Console::RESET);
utils::Logger::Write(":");
utils::Logger::Command( utils::os::Console::FG
| utils::os::Console::BRIGHT
| utils::os::Console::CYAN);
utils::Logger::Write(std::to_string(line));
utils::Logger::Command( utils::os::Console::RESET);
utils::Logger::Write("\n inside: ");
utils::Logger::Command( utils::os::Console::FG
| utils::os::Console::BRIGHT
| utils::os::Console::MAGENTA);
utils::Logger::Write(function);
utils::Logger::Command( utils::os::Console::BG
| utils::os::Console::BRIGHT
| utils::os::Console::RED);
utils::Logger::Command( utils::os::Console::FG
| utils::os::Console::BRIGHT
| utils::os::Console::WHITE);
utils::Logger::Write(utils::Logger::CRLF
+ utils::Logger::LINE<'-'>, false);
utils::Logger::Command( utils::os::Console::RESET);
utils::Logger::WriteLn(utils::Logger::CRLF);
} else {
std::cerr << ( utils::os::Console::RESET
| utils::os::Console::FG | utils::os::Console::BRIGHT
| utils::os::Console::RED)
<< "[ERROR] Exception thrown:\n "
<< ( utils::os::Console::FG
| utils::os::Console::YELLOW)
<< e.what()
<< utils::os::Console::RESET
<< "\n at "
<< ( utils::os::Console::FG | utils::os::Console::BRIGHT
| utils::os::Console::CYAN)
<< file
<< utils::os::Console::RESET
<< ":"
<< ( utils::os::Console::FG | utils::os::Console::BRIGHT
| utils::os::Console::CYAN)
<< line
<< utils::os::Console::RESET
<< "\n inside: "
<< ( utils::os::Console::FG | utils::os::Console::BRIGHT
| utils::os::Console::MAGENTA)
<< function
<< utils::os::Console::RESET << '\n';
}
}
template <typename Iterator, typename F>
static void WriteProgress(Iterator start, Iterator end, F&& f) {
if (HEDLEY_LIKELY(utils::Logger::get().canLogScreen())) {
utils::print::with_progressbar(start, end,
utils::Logger::GetConsoleStream(),
std::forward<F>(f));
} else {
utils::algorithm::for_each(start, end, std::forward<F>(f));
}
}
template <typename Container, typename F>
static void WriteProgress(const Container& cont, F&& f) {
if (HEDLEY_LIKELY(utils::Logger::get().canLogScreen())) {
utils::print::with_progressbar(cont,
utils::Logger::GetConsoleStream(),
std::forward<F>(f));
} else {
utils::algorithm::for_each(cont, std::forward<F>(f));
}
}
static const utils::Logger& Stream() {
return utils::Logger::get();
}
template<typename T>
static const utils::Logger& Stream(const T& arg) {
if (HEDLEY_LIKELY(utils::Logger::get().canLog())) {
std::stringstream ss;
ss << arg;
utils::Logger::Write(ss.str());
}
return utils::Logger::Stream();
}
template<typename T, typename ...Type>
static const utils::Logger& Stream(const T& arg, const Type& ...args) {
if (HEDLEY_LIKELY(utils::Logger::get().canLog())) {
std::stringstream ss;
ss << arg;
((ss << args), ...);
utils::Logger::Write(ss.str());
}
return utils::Logger::Stream();
}
template<typename ...Type>
friend const utils::Logger& operator<<(const utils::Logger&, const Type& ...args)
{
if constexpr (sizeof...(Type) > 0) {
utils::Logger::Stream(args...);
}
return utils::Logger::Stream();
}
static inline void Command(const utils::os::command_t cmd) {
LOCK_BLOCK(utils::Logger::get().screen_mutex);
if (HEDLEY_LIKELY(utils::Logger::get().canLogScreen())) {
utils::os::Command(cmd, utils::Logger::GetConsoleStream());
}
}
static inline void SetScreenTitle(const std::string_view title) {
LOCK_BLOCK(utils::Logger::get().screen_mutex);
utils::os::SetScreenTitle(title, utils::Logger::GetConsoleStream());
}
static inline void PauseScreen(void) {
LOCK_BLOCK(utils::Logger::get().screen_mutex);
utils::Logger::get().screen_paused = true;
}
static inline bool IsPausedScreen(void) {
LOCK_BLOCK(utils::Logger::get().screen_mutex);
return utils::Logger::get().screen_paused;
}
static inline void PauseFile(void) {
LOCK_BLOCK(utils::Logger::get().file_mutex);
utils::Logger::get().file_paused = true;
}
static inline bool IsPausedFile(void) {
LOCK_BLOCK(utils::Logger::get().file_mutex);
return utils::Logger::get().file_paused;
}
static inline void Pause(void) {
utils::Logger::get().PauseScreen();
utils::Logger::get().PauseFile();
}
static inline void ResumeScreen(void) {
LOCK_BLOCK(utils::Logger::get().screen_mutex);
utils::Logger::get().screen_paused = false;
}
static inline void ResumeFile(void) {
LOCK_BLOCK(utils::Logger::get().file_mutex);
utils::Logger::get().file_paused = false;
}
static inline void Resume(void) {
utils::Logger::get().ResumeScreen();
utils::Logger::get().ResumeFile();
}
static inline void SetFileTimestamp(const bool stamp) {
LOCK_BLOCK(utils::Logger::get().file_mutex);
utils::Logger::get().file_timestamp = stamp;
}
static inline bool IsFileTimestampEnabled(void) {
return utils::Logger::get().file_timestamp;
}
static inline void SetScreenLogLevel(const utils::Logger::Level level) {
LOCK_BLOCK(utils::Logger::get().screen_mutex);
utils::Logger::get().level_screen = level;
}
static inline utils::Logger::Level GetScreenLogLevel(void) {
return utils::Logger::get().level_screen;
}
static inline void SetFileLogLevel(const utils::Logger::Level level) {
LOCK_BLOCK(utils::Logger::get().file_mutex);
utils::Logger::get().level_file = level;
}
static inline utils::Logger::Level GetFileLogLevel(void) {
return utils::Logger::get().level_file;
}
/**
* Symbols for printing an image to the console.
*
* █
* std::string_format("%c", 219)
* 219
*/
static inline constexpr size_t CONSOLE_WIDTH = 79;
static inline const std::string FILL = "#";
static inline const std::string EMPTY = " ";
static inline const std::string CRLF = "\n";
template<char line_char = '-'>
static inline const std::string LINE
= std::string(utils::Logger::CONSOLE_WIDTH, line_char);
};
}
#endif // UTILS_LOGGER_HPP