forked from andrew-d/cpplog
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
546 lines (438 loc) · 16.1 KB
/
main.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
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
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <sstream>
#include <strstream>
#include "cpplog.hpp"
#ifndef CPPLOG_NO_SYSTEM_IDS
#include <boost/interprocess/detail/os_thread_functions.hpp>
using namespace boost::interprocess::detail;
#endif
using namespace cpplog;
using namespace std;
// Helper function - gets the header string
void getLogHeader(string& outString, loglevel_t level, const char* file, unsigned int line)
{
ostringstream outStream;
#ifndef CPPLOG_NO_SYSTEM_IDS
unsigned long processId = get_current_process_id();
unsigned long threadId = (unsigned long)get_current_thread_id();
outStream << "["
<< right << setfill('0') << setw(8) << hex
<< processId << "."
<< setfill('0') << setw(8) << hex
<< threadId << "] ";
#endif
outStream << setfill(' ') << setw(5) << left << dec
<< LogMessage::getLevelName(level) << " - "
<< cpplog::helpers::fileNameFromPath(file) << "(" << line << "): ";
outString = outStream.str();
}
int TestLogLevels()
{
int failed = 0;
string expectedValue;
StringLogger log;
cout << "Testing logging macros... ";
#define TEST_EXPECTED(str, level) \
if( level >= CPPLOG_FILTER_LEVEL ) \
{ \
expectedValue.clear(); \
getLogHeader(expectedValue, level, __FILE__, __LINE__); \
expectedValue += str; \
expectedValue += "\n"; \
} else \
{ \
expectedValue = ""; \
} \
if( expectedValue != log.getString() ) \
{ \
cerr << "Mismatch detected at " << cpplog::helpers::fileNameFromPath(__FILE__) \
<< "(" << __LINE__ << "): \"" << log.getString() << "\" != \"" \
<< expectedValue << "\"" << endl; \
failed++; \
} \
log.clear()
// Note: All these are on the same line so that the __LINE__ macro will match.
LOG_TRACE(log) << "Trace message"; TEST_EXPECTED("Trace message", LL_TRACE);
LOG_DEBUG(log) << "Debug message"; TEST_EXPECTED("Debug message", LL_DEBUG);
LOG_INFO(log) << "Info message"; TEST_EXPECTED("Info message", LL_INFO);
LOG_WARN(log) << "Warning message"; TEST_EXPECTED("Warning message", LL_WARN);
LOG_ERROR(log) << "Error message"; TEST_EXPECTED("Error message", LL_ERROR);
LOG_FATAL(log) << "Fatal message"; TEST_EXPECTED("Fatal message", LL_FATAL);
LOG_LEVEL(LL_WARN, log) << "Specified warning message"; TEST_EXPECTED("Specified warning message", LL_WARN);
LOG(LL_DEBUG, log) << "Short specified debug message"; TEST_EXPECTED("Short specified debug message", LL_DEBUG);
#undef TEST_EXPECTED
cout << "done!" << endl;
return failed;
}
int TestDebugLogLevels()
{
int failed = 0;
string expectedValue;
StringLogger log;
cout << "Testing debug-logging macros... ";
#ifdef _DEBUG
#define TEST_EXPECTED(str, level) \
if( level >= CPPLOG_FILTER_LEVEL ) \
{ \
expectedValue.clear(); \
getLogHeader(expectedValue, level, __FILE__, __LINE__); \
expectedValue += str; \
expectedValue += "\n"; \
} else \
{ \
expectedValue = ""; \
} \
if( expectedValue != log.getString() ) \
{ \
cerr << "Mismatch detected at " << cpplog::helpers::fileNameFromPath(__FILE__) \
<< "(" << __LINE__ << "): \"" << log.getString() << "\" != \"" \
<< expectedValue << "\"" << endl; \
failed++; \
} \
log.clear()
#else
#define TEST_EXPECTED(str, level) \
expectedValue = ""; \
if( expectedValue != log.getString() ) \
{ \
cerr << "Mismatch detected at " << cpplog::helpers::fileNameFromPath(__FILE__) \
<< "(" << __LINE__ << "): \"" << log.getString() << "\" != \"" \
<< expectedValue << "\"" << endl; \
failed++; \
} \
log.clear()
#endif
// Note: All these are on the same line so that the __LINE__ macro will match.
DLOG_TRACE(log) << "Trace message"; TEST_EXPECTED("Trace message", LL_TRACE);
DLOG_DEBUG(log) << "Debug message"; TEST_EXPECTED("Debug message", LL_DEBUG);
DLOG_INFO(log) << "Info message"; TEST_EXPECTED("Info message", LL_INFO);
DLOG_WARN(log) << "Warning message"; TEST_EXPECTED("Warning message", LL_WARN);
DLOG_ERROR(log) << "Error message"; TEST_EXPECTED("Error message", LL_ERROR);
DLOG(LL_DEBUG, log) << "Short specified debug message"; TEST_EXPECTED("Short specified debug message", LL_DEBUG);
// Fatal messages are always logged. We have to handle this manually.
DLOG_FATAL(log) << "Fatal message"; int line = __LINE__;
expectedValue.clear();
getLogHeader(expectedValue, LL_FATAL, __FILE__, line);
expectedValue += "Fatal message\n";
if( expectedValue != log.getString() )
{
cerr << "Mismatch detected at " << cpplog::helpers::fileNameFromPath(__FILE__)
<< "(" << line << "): \"" << log.getString() << "\" != \""
<< expectedValue << "\"" << endl;
failed++;
}
log.clear();
#undef TEST_EXPECTED
cout << "done!" << endl;
return failed;
}
int TestConditionMacros()
{
int failed = 0;
string expectedValue;
StringLogger log;
cout << "Testing conditional macros... ";
#define TEST_EXPECTED(str, level, logged) \
if( level >= CPPLOG_FILTER_LEVEL && logged ) \
{ \
expectedValue.clear(); \
getLogHeader(expectedValue, level, __FILE__, __LINE__); \
expectedValue += str; \
expectedValue += "\n"; \
} else \
{ \
expectedValue = ""; \
} \
if( expectedValue != log.getString() ) \
{ \
cerr << "Mismatch detected at " << cpplog::helpers::fileNameFromPath(__FILE__) \
<< "(" << __LINE__ << "): \"" << log.getString() << "\" != \"" \
<< expectedValue << "\"" << endl; \
failed++; \
} \
log.clear()
LOG_IF(LL_WARN, log, 1 == 2) << "This should not be logged"; TEST_EXPECTED("This should not be logged", LL_WARN, false);
LOG_IF(LL_WARN, log, 'a' == 'a') << "This should be logged"; TEST_EXPECTED("This should be logged", LL_WARN, true);
LOG_IF_NOT(LL_WARN, log, 1 == 2) << "This should be logged"; TEST_EXPECTED("This should be logged", LL_WARN, true);
LOG_IF_NOT(LL_WARN, log, 1 == 1) << "This should not be logged"; TEST_EXPECTED("This should not be logged", LL_WARN, false);
#undef TEST_EXPECTED
cout << "done!" << endl;
return failed;
}
int TestCheckMacros()
{
int failed = 0;
StringLogger log;
cout << "Testing conditional macros... ";
#define TEST_EXPECTED(logged) \
if( logged ) \
{ \
if( log.getString().find("Check failed: ") == string::npos ) \
{ \
cerr << "Mismatch detected at " << cpplog::helpers::fileNameFromPath(__FILE__) \
<< "(" << __LINE__ << ")" << endl; \
failed++; \
} \
} else \
{ \
if( log.getString().length() > 0 ) \
{ \
cerr << "Mismatch detected at " << cpplog::helpers::fileNameFromPath(__FILE__) \
<< "(" << __LINE__ << ")" << endl; \
failed++; \
} \
} \
log.clear();
CHECK(log, 1 == 1) << "Should not log"; TEST_EXPECTED(false);
CHECK(log, 1 == 2) << "Should log"; TEST_EXPECTED(true);
CHECK_EQUAL(log, 1, 1) << "Should not log"; TEST_EXPECTED(false);
CHECK_EQUAL(log, 1, 2) << "Should log"; TEST_EXPECTED(true);
CHECK_LT(log, 1, 2) << "Should not log"; TEST_EXPECTED(false);
CHECK_LT(log, 2, 1) << "Should log"; TEST_EXPECTED(true);
CHECK_LT(log, 3, 3) << "Should log"; TEST_EXPECTED(true);
CHECK_GT(log, 4, 2) << "Should not log"; TEST_EXPECTED(false);
CHECK_GT(log, 2, 4) << "Should log"; TEST_EXPECTED(true);
CHECK_GT(log, 4, 4) << "Should log"; TEST_EXPECTED(true);
CHECK_LE(log, 1, 2) << "Should not log"; TEST_EXPECTED(false);
CHECK_LE(log, 1, 1) << "Should not log"; TEST_EXPECTED(false);
CHECK_LE(log, 2, 1) << "Should log"; TEST_EXPECTED(true);
CHECK_GE(log, 3, 1) << "Should not log"; TEST_EXPECTED(false);
CHECK_GE(log, 3, 3) << "Should not log"; TEST_EXPECTED(false);
CHECK_GE(log, 1, 3) << "Should log"; TEST_EXPECTED(true);
CHECK_NE(log, 1, 2) << "Should not log"; TEST_EXPECTED(false);
CHECK_NE(log, 1, 1) << "Should log"; TEST_EXPECTED(true);
CHECK_NOT_EQUAL(log, 1, 2) << "Should not log"; TEST_EXPECTED(false);
CHECK_NOT_EQUAL(log, 1, 1) << "Should log"; TEST_EXPECTED(true);
CHECK_STREQ(log, "ab", "ab") << "Should not log"; TEST_EXPECTED(false);
CHECK_STREQ(log, "ab", "cd") << "Should log"; TEST_EXPECTED(true);
CHECK_STRNE(log, "ab", "cd") << "Should not log"; TEST_EXPECTED(false);
CHECK_STRNE(log, "qq", "qq") << "Should log"; TEST_EXPECTED(true);
CHECK_NULL(log, NULL) << "Should not log"; TEST_EXPECTED(false);
CHECK_NULL(log, (void*)(1)) << "Should log"; TEST_EXPECTED(true);
CHECK_NOT_NULL(log, (void*)(1)) << "Should not log"; TEST_EXPECTED(false);
CHECK_NOT_NULL(log, NULL) << "Should log"; TEST_EXPECTED(true);
#undef TEST_EXPECTED
cout << "done!" << endl;
return failed;
}
int TestTeeLogger()
{
int failed = 0;
StringLogger logger1;
StringLogger logger2;
TeeLogger tlog(logger1, logger2);
string expectedValue;
cout << "Testing TeeLogger... ";
LOG_WARN(tlog) << "Some message here" << endl; int line = __LINE__;
getLogHeader(expectedValue, LL_WARN, __FILE__, line);
expectedValue += "Some message here\n";
// Compare.
if( expectedValue != logger1.getString() )
{
cerr << "Mismatch (1) detected at " << cpplog::helpers::fileNameFromPath(__FILE__)
<< "(" << line << ")" << endl;
failed++;
}
if( expectedValue != logger2.getString() )
{
cerr << "Mismatch (2) detected at " << cpplog::helpers::fileNameFromPath(__FILE__)
<< "(" << line << ")" << endl;
failed++;
}
cout << "done!" << endl;
return failed;
}
#ifndef CPPLOG_NO_THREADING
int TestBackgroundLogger()
{
int failed = 0, line;
StringLogger slogger;
string expectedValue;
cout << "Testing BackgroundLogger... ";
// Scoped, such that when the object is destructed we wait for the log
// messages to be flushed to the StringLogger.
{
BackgroundLogger blog(slogger);
LOG_WARN(blog) << "Background message here." << endl; line = __LINE__;
}
getLogHeader(expectedValue, LL_WARN, __FILE__, line);
expectedValue += "Background message here.\n";
if( expectedValue != slogger.getString() )
{
cerr << "Mismatch detected at " << cpplog::helpers::fileNameFromPath(__FILE__)
<< "(" << line << ")" << endl;
failed++;
}
cout << "done!" << endl;
return failed;
}
class CountingLogger : public BaseLogger
{
private:
int m_logMessageCount;
public:
CountingLogger()
: m_logMessageCount(0)
{ }
virtual bool sendLogMessage(LogData* /* logData */)
{
m_logMessageCount++;
return true;
}
int getCount()
{
return m_logMessageCount;
}
};
int TestBackgroundLoggerConcurrency()
{
int failed = 0;
CountingLogger clog;
const int numMessages = 100000;
cout << "Testing BackgroundLogger for consistency... " << flush;
// Scoped!
{
BackgroundLogger blog(clog);
for( int i = 0; i < numMessages; i++ )
{
LOG_INFO(blog) << "Message " << i << std::endl;
}
}
// Validate the number of messages sent.
if( clog.getCount() != numMessages )
{
cerr << "Mismatch detected! Sent: " << numMessages << ", Received: " << clog.getCount() << endl;
failed++;
}
cout << "done!" << endl;
return failed;
}
#endif // CPPLOG_NO_THREADING
void SizeNameFunc(unsigned long logNumber, std::string& newFileName, void* context)
{
std::ostringstream fileName;
fileName << "SizeRotateFileLogger_test_" << logNumber << ".log";
newFileName = fileName.str();
}
void TimeNameFunc(::tm* time, unsigned long logNumber,
std::string& newFileName, void* context)
{
std::ostringstream fileName;
fileName << "TimeRotateFileLogger_test_";
fileName << setfill('0');
fileName << setw(4) << (time->tm_year + 1900) << "-"
<< setw(2) << time->tm_mon << "-"
<< setw(2) << time->tm_mday
<< "_"
<< setw(2) << time->tm_hour << "-"
<< setw(2) << time->tm_min << "-"
<< setw(2) << time->tm_sec
<< ".log";
newFileName = fileName.str();
}
int TestRotatingLoggers()
{
int failed = 0;
cout << "Testing auto-rotating loggers... ";
// We rotate at 100 bytes.
cpplog::SizeRotateFileLogger srlog(SizeNameFunc, 200);
// Log two messages...
LOG_INFO(srlog) << "Size rotated 1";
LOG_INFO(srlog) << "Size rotated 2";
// ... and force a rotation.
LOG_INFO(srlog) << "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; // 200 * 'a'
// Log to the new file.
LOG_INFO(srlog) << "After size rotation 1";
LOG_INFO(srlog) << "After size rotation 2";
// Rotate every 10 seconds.
cpplog::TimeRotateFileLogger trlog(TimeNameFunc, 10);
// Log two messages...
LOG_INFO(trlog) << "Time rotated 1";
LOG_INFO(trlog) << "Time rotated 2";
// ... and wait for 11 seconds (to eliminate the rare edge case).
cout << "(waiting) " << flush;
time_t startTime = time(NULL);
while( difftime(time(NULL), startTime) < 11.0 )
{
;
}
// Log another two now.
LOG_WARN(trlog) << "After rotation 1";
LOG_WARN(trlog) << "After rotation 2";
// TODO: Verify that files exist with correct data.
cout << "done!" << endl;
return failed;
}
int TestOtherLogging()
{
int failed = 0;
StringLogger log;
#ifdef _DEBUG
bool debug = true;
#else
bool debug = false;
#endif
cout << "Testing other logging macros... ";
#define TEST_EXPECTED(logged) \
if( logged ) \
{ \
if( log.getString().find("Assertion failed: ") == string::npos ) \
{ \
cerr << "Mismatch detected at " << cpplog::helpers::fileNameFromPath(__FILE__) \
<< "(" << __LINE__ << ")" << endl; \
failed++; \
} \
} else \
{ \
if( log.getString().length() > 0 ) \
{ \
cerr << "Mismatch detected at " << cpplog::helpers::fileNameFromPath(__FILE__) \
<< "(" << __LINE__ << ")" << endl; \
failed++; \
} \
} \
log.clear();
LOG_ASSERT(log, 1 == 1); TEST_EXPECTED(false);
LOG_ASSERT(log, 1 == 2); TEST_EXPECTED(true);
DLOG_ASSERT(log, 1 == 1); TEST_EXPECTED(false);
DLOG_ASSERT(log, 1 == 2); TEST_EXPECTED(true && debug);
// Windows-only tests.
#ifdef _WIN32
OutputDebugStringLogger dlog;
LOG_INFO(dlog) << "Test log to debug output" << endl;
#endif
cout << "done!" << endl;
return failed;
}
int TestLogging()
{
int totalFailures = 0;
totalFailures += TestLogLevels();
totalFailures += TestDebugLogLevels();
totalFailures += TestConditionMacros();
totalFailures += TestCheckMacros();
totalFailures += TestTeeLogger();
totalFailures += TestRotatingLoggers();
totalFailures += TestOtherLogging();
#ifndef CPPLOG_NO_THREADING
totalFailures += TestBackgroundLogger();
totalFailures += TestBackgroundLoggerConcurrency();
#endif
return totalFailures;
}
int main(void)
{
int totalFailures;
totalFailures = TestLogging();
cout << "\n------------------------------" << endl;
if( 0 == totalFailures )
cout << "All tests passed!" << endl;
else
cerr << totalFailures << " tests failed :-(" << endl;
return totalFailures;
}