-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexample.cpp
147 lines (109 loc) · 3.17 KB
/
example.cpp
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
#include "minilog.h"
#include <thread>
#if defined(__GNUC__)
# define FUNC_NAME __PRETTY_FUNCTION__
#else
# define FUNC_NAME __FUNCTION__
#endif
void testTXT()
{
minilog::initialize("log.txt", {});
minilog::callstackPushProc("testTXT()->");
minilog::log(minilog::Log, "Hello world!");
minilog::log(minilog::Warning, "Warning!!!");
minilog::callstackPopProc();
minilog::deinitialize();
}
void testHTML()
{
minilog::initialize("log.html", { .htmlLog = true });
minilog::callstackPushProc("testHTML()->");
minilog::log(minilog::Log, "Hello world!");
minilog::log(minilog::Warning, "Warning!!!");
minilog::callstackPopProc();
minilog::deinitialize();
}
void testThread()
{
minilog::initialize("log_thread.html", { .htmlLog = true });
std::thread t([]()
{
minilog::threadNameSet("OtherThread");
minilog::callstackPushProc("std::thread->");
minilog::log(minilog::Log, "Hello from another thread!");
minilog::log(minilog::Warning, "Warning from another thread!!!");
minilog::callstackPopProc();
});
minilog::callstackPushProc("testThread()->");
minilog::log(minilog::Log, "Hello world!");
minilog::log(minilog::Warning, "Warning!!!");
minilog::callstackPopProc();
t.join();
minilog::deinitialize();
}
void testCallstack()
{
minilog::initialize("log_callstack.txt", {});
{
minilog::CallstackScope scope(FUNC_NAME);
minilog::log(minilog::Log, "Hello world!");
minilog::log(minilog::Warning, "Warning!!!");
}
minilog::deinitialize();
}
void testCallstackMacros()
{
minilog::initialize("log_callstack_macros.txt", {});
const unsigned int i = 32167;
{
minilog::CallstackScope scope(FUNC_NAME);
LLOGL("Hello world!");
LLOGW("Warning!!! i = %u", i);
}
minilog::deinitialize();
}
void testCallbacks()
{
minilog::initialize("log_callbacks.txt", {});
// intercept formatted messages
minilog::LogCallback cb = { .userData = nullptr };
cb.funcs[minilog::Warning] = [](void* userData, const char* msg) { printf(">>> CALLBACK Warning: %s\n", msg); };
minilog::callbackAdd(cb);
const unsigned int i = 32167;
{
minilog::CallstackScope scope(FUNC_NAME);
LLOGL("Hello world!");
LLOGW("Warning!!! i = %u", i);
}
minilog::deinitialize();
}
char* writeCustomTimeStamp(char* buffer, const char* bufferEnd)
{
time_t tempTime;
time(&tempTime);
::tm tmTime = *localtime(&tempTime);
const char* monthName[12] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
const int n = snprintf(
buffer, uint32_t(bufferEnd - buffer), "%04d.%s.%02d-%02d:%02d:%02d.%03d ", tmTime.tm_year + 1900, monthName[tmTime.tm_mon],
tmTime.tm_mday, tmTime.tm_hour, tmTime.tm_min, tmTime.tm_sec, minilog::getCurrentMilliseconds());
return buffer + n;
}
void testCustomTimestamp() {
minilog::initialize("log_timestamps.txt", { .writeTimeStamp = &writeCustomTimeStamp });
minilog::callstackPushProc("testCustomTimeStamp()->");
minilog::log(minilog::Log, "New Time Stamp!");
minilog::log(minilog::Warning, "Another Time Stamp!!!");
minilog::callstackPopProc();
minilog::deinitialize();
}
int main()
{
testTXT();
testHTML();
testThread();
testCallstack();
testCallstackMacros();
testCallbacks();
testCustomTimestamp();
return 0;
}