|
1 | 1 | #pragma once
|
2 | 2 |
|
3 | 3 | /**
|
4 |
| - minilog v1.0.2 |
| 4 | + minilog v1.1.0 |
5 | 5 |
|
6 | 6 | MIT License
|
7 |
| - Copyright (c) 2021-2022 Sergey Kosarevsky |
| 7 | + Copyright (c) 2021-2023 Sergey Kosarevsky |
| 8 | + https://github.com/corporateshark/minilog |
8 | 9 | **/
|
9 | 10 |
|
10 | 11 | #if defined(MINILOG_ENABLE_VA_LIST)
|
|
13 | 14 |
|
14 | 15 | namespace minilog
|
15 | 16 | {
|
16 |
| - enum eLogLevel { |
17 |
| - Paranoid = 0, |
18 |
| - Debug = 1, |
19 |
| - Log = 2, |
20 |
| - Warning = 3, |
21 |
| - FatalError = 4 |
22 |
| - }; |
23 |
| - struct LogConfig { |
24 |
| - eLogLevel logLevel = minilog::Debug; // everything >= this level goes to the log file |
25 |
| - eLogLevel logLevelPrintToConsole = minilog::Log; // everything >= this level is printed to the console (cannot be lower than logLevel) |
26 |
| - bool forceFlush = true; // call fflush() after every log() and logRaw() |
27 |
| - bool writeIntro = true; |
28 |
| - bool writeOutro = true; |
29 |
| - bool coloredConsole = true; // apply colors to console output (Windows, escape sequences) |
30 |
| - bool htmlLog = false; // output everything as HTML instead of plain text |
31 |
| - const char* htmlPageTitle = "Minilog"; // just the title of the resulting HTML page |
32 |
| - const char* htmlPageHeader = nullptr; // override default HTML header |
33 |
| - const char* htmlPageFooter = nullptr; // override default HTML footer |
34 |
| - const char* mainThreadName = "MainThread"; // just the name of the thread which calls minilog::initialize() |
35 |
| - }; |
36 | 17 |
|
37 |
| - bool initialize(const char* fileName, const LogConfig& cfg); // non-thread-safe |
38 |
| - void deinitialize(); // non-thread-safe |
| 18 | +enum eLogLevel { |
| 19 | + Paranoid = 0, |
| 20 | + Debug = 1, |
| 21 | + Log = 2, |
| 22 | + Warning = 3, |
| 23 | + FatalError = 4 |
| 24 | +}; |
| 25 | +struct LogConfig { |
| 26 | + eLogLevel logLevel = minilog::Debug; // everything >= this level goes to the log file |
| 27 | + eLogLevel logLevelPrintToConsole = minilog::Log; // everything >= this level is printed to the console (cannot be lower than logLevel) |
| 28 | + bool forceFlush = true; // call fflush() after every log() and logRaw() |
| 29 | + bool writeIntro = true; |
| 30 | + bool writeOutro = true; |
| 31 | + bool coloredConsole = true; // apply colors to console output (Windows, escape sequences) |
| 32 | + bool htmlLog = false; // output everything as HTML instead of plain text |
| 33 | + bool threadNames = true; // prefix log messages with thread names |
| 34 | + const char* htmlPageTitle = "Minilog"; // just the title of the resulting HTML page |
| 35 | + const char* htmlPageHeader = nullptr; // override default HTML header |
| 36 | + const char* htmlPageFooter = nullptr; // override default HTML footer |
| 37 | + const char* mainThreadName = "MainThread"; // just the name of the thread which calls minilog::initialize() |
| 38 | +}; |
39 | 39 |
|
40 |
| - void log(eLogLevel level, const char* format, ...); // thread-safe |
41 |
| - void logRaw(eLogLevel level, const char* format, ...); // thread-safe |
| 40 | +bool initialize(const char* fileName, const LogConfig& cfg); // non-thread-safe |
| 41 | +void deinitialize(); // non-thread-safe |
| 42 | + |
| 43 | +void log(eLogLevel level, const char* format, ...); // thread-safe |
| 44 | +void logRaw(eLogLevel level, const char* format, ...); // thread-safe |
42 | 45 | #if defined(MINILOG_ENABLE_VA_LIST)
|
43 |
| - void log(eLogLevel level, const char* format, va_list args); // thread-safe |
44 |
| - void logRaw(eLogLevel level, const char* format, va_list args); // thread-safe |
45 |
| -#endif // MINILOG_ENABLE_VA_LIST |
| 46 | +void log(eLogLevel level, const char* format, va_list args); // thread-safe |
| 47 | +void logRaw(eLogLevel level, const char* format, va_list args); // thread-safe |
| 48 | +#endif // MINILOG_ENABLE_VA_LIST |
46 | 49 |
|
47 |
| - /// threads management |
48 |
| - void threadNameSet(const char* name); // thread-safe |
49 |
| - const char* threadNameGet(); // thread-safe |
50 |
| - |
51 |
| - /// callstack management |
52 |
| - bool callstackPushProc(const char* name); // thread-safe |
53 |
| - void callstackPopProc(); // thread-safe |
54 |
| - unsigned int callstackGetNumProcs(); // thread-safe |
55 |
| - const char* callstackGetProc(unsigned int i); // thread-safe |
56 |
| - |
57 |
| - /// set up custom callbacks |
58 |
| - struct LogCallback { |
59 |
| - typedef void (*callback_t)(void*, const char*); |
60 |
| - callback_t funcs[minilog::FatalError + 1] = {}; |
61 |
| - void* userData = nullptr; |
62 |
| - }; |
63 |
| - bool callbackAdd(const LogCallback& cb); // non-thread-safe |
64 |
| - void callbackRemove(void* userData); // non-thread-safe |
65 |
| - |
66 |
| - /// RAII wrapper around callstackPushProc() and callstackPopProc() |
67 |
| - class CallstackScope { |
68 |
| - enum { kBufferSize = 256 }; |
69 |
| - public: |
70 |
| - explicit CallstackScope(const char* funcName); |
71 |
| - explicit CallstackScope(const char* funcName, const char* format, ...); |
72 |
| - inline ~CallstackScope() { minilog::callstackPopProc(); } |
73 |
| - private: |
74 |
| - char buffer_[kBufferSize]; |
| 50 | +/// threads management |
| 51 | +void threadNameSet(const char* name); // thread-safe |
| 52 | +const char* threadNameGet(); // thread-safe |
| 53 | + |
| 54 | +/// callstack management |
| 55 | +bool callstackPushProc(const char* name); // thread-safe |
| 56 | +void callstackPopProc(); // thread-safe |
| 57 | +unsigned int callstackGetNumProcs(); // thread-safe |
| 58 | +const char* callstackGetProc(unsigned int i); // thread-safe |
| 59 | + |
| 60 | +/// set up custom callbacks |
| 61 | +struct LogCallback { |
| 62 | + typedef void (*callback_t)(void*, const char*); |
| 63 | + callback_t funcs[minilog::FatalError + 1] = {}; |
| 64 | + void* userData = nullptr; |
| 65 | +}; |
| 66 | +bool callbackAdd(const LogCallback& cb); // non-thread-safe |
| 67 | +void callbackRemove(void* userData); // non-thread-safe |
| 68 | + |
| 69 | +/// RAII wrapper around callstackPushProc() and callstackPopProc() |
| 70 | +class CallstackScope |
| 71 | +{ |
| 72 | + enum { |
| 73 | + kBufferSize = 256 |
75 | 74 | };
|
| 75 | + |
| 76 | + public: |
| 77 | + explicit CallstackScope(const char* funcName); |
| 78 | + explicit CallstackScope(const char* funcName, const char* format, ...); |
| 79 | + inline ~CallstackScope() { minilog::callstackPopProc(); } |
| 80 | + |
| 81 | + private: |
| 82 | + char buffer_[kBufferSize]; |
| 83 | +}; |
76 | 84 | } // namespace minilog
|
77 | 85 |
|
| 86 | +// clang-format off |
| 87 | +#if defined(MINILOG_RAW_OUTPUT) |
| 88 | +# define MINILOG_LOG_PROC minilog::logRaw |
| 89 | +#else |
| 90 | +# define MINILOG_LOG_PROC minilog::log |
| 91 | +#endif // MINILOG_RAW_OUTPUT |
| 92 | + |
78 | 93 | #if !defined(MINILOG_DISABLE_HELPER_MACROS)
|
79 | 94 |
|
80 | 95 | #if defined(__GNUC__) && !defined(EMSCRIPTEN) && !defined(__clang__)
|
81 |
| -# define LLOGP(...) minilog::log(minilog::Paranoid, ##__VA_ARGS__) |
82 |
| -# define LLOGD(...) minilog::log(minilog::Debug, ##__VA_ARGS__) |
83 |
| -# define LLOGL(...) minilog::log(minilog::Log, ##__VA_ARGS__) |
84 |
| -# define LLOGW(...) minilog::log(minilog::Warning, ##__VA_ARGS__) |
| 96 | +# define LLOGP(...) MINILOG_LOG_PROC(minilog::Paranoid, ##__VA_ARGS__) |
| 97 | +# define LLOGD(...) MINILOG_LOG_PROC(minilog::Debug, ##__VA_ARGS__) |
| 98 | +# define LLOGL(...) MINILOG_LOG_PROC(minilog::Log, ##__VA_ARGS__) |
| 99 | +# define LLOGW(...) MINILOG_LOG_PROC(minilog::Warning, ##__VA_ARGS__) |
85 | 100 | #else
|
86 |
| -# define LLOGP(...) minilog::log(minilog::Paranoid, ## __VA_ARGS__) |
87 |
| -# define LLOGD(...) minilog::log(minilog::Debug, ## __VA_ARGS__) |
88 |
| -# define LLOGL(...) minilog::log(minilog::Log, ## __VA_ARGS__) |
89 |
| -# define LLOGW(...) minilog::log(minilog::Warning, ## __VA_ARGS__) |
| 101 | +# define LLOGP(...) MINILOG_LOG_PROC(minilog::Paranoid, ## __VA_ARGS__) |
| 102 | +# define LLOGD(...) MINILOG_LOG_PROC(minilog::Debug, ## __VA_ARGS__) |
| 103 | +# define LLOGL(...) MINILOG_LOG_PROC(minilog::Log, ## __VA_ARGS__) |
| 104 | +# define LLOGW(...) MINILOG_LOG_PROC(minilog::Warning, ## __VA_ARGS__) |
90 | 105 | #endif
|
91 | 106 |
|
92 | 107 | #endif // MINILOG_DISABLE_HELPER_MACROS
|
| 108 | +// clang-format on |
0 commit comments