-
Notifications
You must be signed in to change notification settings - Fork 2
/
minilog.h
117 lines (96 loc) · 4.36 KB
/
minilog.h
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
#pragma once
/**
minilog v1.2.0
MIT License
Copyright (c) 2021-2024 Sergey Kosarevsky
https://github.com/corporateshark/minilog
**/
#if defined(MINILOG_ENABLE_VA_LIST)
# include <stdarg.h>
#endif // MINILOG_ENABLE_VA_LIST
namespace minilog
{
enum eLogLevel {
Paranoid = 0,
Debug = 1,
Log = 2,
Warning = 3,
FatalError = 4
};
// A user function to write a time stamp into a buffer `buffer`; it should not write past the pointer `bufferEnd`.
// It returns a pointer to the end of the written data.
using writeTimeStampFn = char* (*)(char* buffer, const char* bufferEnd);
struct LogConfig {
eLogLevel logLevel = minilog::Debug; // everything >= this level goes to the log file
eLogLevel logLevelPrintToConsole = minilog::Log; // everything >= this level is printed to the console (cannot be lower than logLevel)
bool forceFlush = true; // call fflush() after every log() and logRaw()
bool writeIntro = true;
bool writeOutro = true;
bool coloredConsole = true; // apply colors to console output (Windows, macOS, escape sequences)
bool htmlLog = false; // output everything as HTML instead of plain text
bool threadNames = true; // prefix log messages with thread names
const char* htmlPageTitle = "Minilog"; // just the title of the resulting HTML page
const char* htmlPageHeader = nullptr; // override default HTML header
const char* htmlPageFooter = nullptr; // override default HTML footer
const char* mainThreadName = "MainThread"; // just the name of the thread which calls minilog::initialize()
writeTimeStampFn writeTimeStamp = nullptr; // override default time stamp function
};
bool initialize(const char* fileName, const LogConfig& cfg); // non-thread-safe
void deinitialize(); // non-thread-safe
void log(eLogLevel level, const char* format, ...); // thread-safe
void logRaw(eLogLevel level, const char* format, ...); // thread-safe
#if defined(MINILOG_ENABLE_VA_LIST)
void log(eLogLevel level, const char* format, va_list args); // thread-safe
void logRaw(eLogLevel level, const char* format, va_list args); // thread-safe
#endif // MINILOG_ENABLE_VA_LIST
/// threads management
void threadNameSet(const char* name); // thread-safe
const char* threadNameGet(); // thread-safe
/// callstack management
bool callstackPushProc(const char* name); // thread-safe
void callstackPopProc(); // thread-safe
unsigned int callstackGetNumProcs(); // thread-safe
const char* callstackGetProc(unsigned int i); // thread-safe
/// set up custom callbacks
struct LogCallback {
typedef void (*callback_t)(void*, const char*);
callback_t funcs[minilog::FatalError + 1] = {};
void* userData = nullptr;
};
bool callbackAdd(const LogCallback& cb); // non-thread-safe
void callbackRemove(void* userData); // non-thread-safe
/// RAII wrapper around callstackPushProc() and callstackPopProc()
class CallstackScope
{
enum {
kBufferSize = 256
};
public:
explicit CallstackScope(const char* funcName);
explicit CallstackScope(const char* funcName, const char* format, ...);
inline ~CallstackScope() { minilog::callstackPopProc(); }
private:
char buffer_[kBufferSize];
};
unsigned int getCurrentMilliseconds();
} // namespace minilog
// clang-format off
#if defined(MINILOG_RAW_OUTPUT)
# define MINILOG_LOG_PROC minilog::logRaw
#else
# define MINILOG_LOG_PROC minilog::log
#endif // MINILOG_RAW_OUTPUT
#if !defined(MINILOG_DISABLE_HELPER_MACROS)
#if defined(__GNUC__) && !defined(EMSCRIPTEN) && !defined(__clang__)
# define LLOGP(...) MINILOG_LOG_PROC(minilog::Paranoid, ##__VA_ARGS__)
# define LLOGD(...) MINILOG_LOG_PROC(minilog::Debug, ##__VA_ARGS__)
# define LLOGL(...) MINILOG_LOG_PROC(minilog::Log, ##__VA_ARGS__)
# define LLOGW(...) MINILOG_LOG_PROC(minilog::Warning, ##__VA_ARGS__)
#else
# define LLOGP(...) MINILOG_LOG_PROC(minilog::Paranoid, ## __VA_ARGS__)
# define LLOGD(...) MINILOG_LOG_PROC(minilog::Debug, ## __VA_ARGS__)
# define LLOGL(...) MINILOG_LOG_PROC(minilog::Log, ## __VA_ARGS__)
# define LLOGW(...) MINILOG_LOG_PROC(minilog::Warning, ## __VA_ARGS__)
#endif
#endif // MINILOG_DISABLE_HELPER_MACROS
// clang-format on