forked from KjellKod/g3log
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_message.cpp
651 lines (503 loc) · 21.4 KB
/
test_message.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
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
639
640
641
642
643
644
645
646
647
648
649
650
/** ==========================================================================
* 2016 by KjellKod.cc. This is PUBLIC DOMAIN to use at your own risk and comes
* with no warranties. This code is yours to share, use and modify with no
* strings attached and no restrictions or obligations.
*
* For more information see g3log/LICENSE or refer refer to http://unlicense.org
* ============================================================================*/
#include <gtest/gtest.h>
#include <g3log/g3log.hpp>
#include <g3log/time.hpp>
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <g3log/generated_definitions.hpp>
#include <testing_helpers.h>
#include <g3log/filesink.hpp>
namespace {
// https://www.epochconverter.com/
// epoc value for: Thu, 27 Apr 2017 06:22:49 GMT
time_t k2017_April_27th = 1493274147;
auto kTimePoint_2017_April_27th = std::chrono::system_clock::from_time_t(k2017_April_27th);
std::chrono::time_point<std::chrono::system_clock> k1970_January_1st = {};
const std::string kFile = __FILE__;
const int kLine = 123;
const std::string kFunction = "MyTest::Foo";
const LEVELS kLevel = INFO;
const std::string testdirectory = "./";
}
TEST(Message, DefaultLogDetals_toString) {
using namespace g3;
LogMessage msg{kFile, kLine, kFunction, kLevel};
auto details = LogMessage::DefaultLogDetailsToString(msg);
auto details2 = msg._logDetailsToStringFunc(msg);
EXPECT_EQ(details, details2);
}
TEST(Message, Default_toString) {
using namespace g3;
LogMessage msg{kFile, kLine, kFunction, kLevel};
auto details = LogMessage::DefaultLogDetailsToString(msg);
auto output = msg.toString();
testing_helpers::verifyContent(output, details);
}
TEST(Message, UseOverride_4_DetailsWithThreadID_toString) {
using namespace g3;
LogMessage msg{kFile, kLine, kFunction, kLevel};
msg.overrideLogDetailsFunc(&LogMessage::FullLogDetailsToString);
auto output = msg.toString();
std::ostringstream thread_id_oss;
thread_id_oss << std::this_thread::get_id();
testing_helpers::verifyContent(output, thread_id_oss.str());
testing_helpers::verifyContent(output, kFile);
testing_helpers::verifyContent(output, kLevel.text);
testing_helpers::verifyContent(output, kFunction);
testing_helpers::verifyContent(output, std::to_string(kLine));
std::cout << output << std::endl;
}
TEST(Message, UseLogCall_4_DetailsWithThreadID_toString) {
using namespace g3;
LogMessage msg{kFile, kLine, kFunction, kLevel};
auto output = msg.toString(&LogMessage::FullLogDetailsToString);
std::ostringstream thread_id_oss;
thread_id_oss << std::this_thread::get_id();
testing_helpers::verifyContent(output, thread_id_oss.str());
testing_helpers::verifyContent(output, kFile);
testing_helpers::verifyContent(output, kLevel.text);
testing_helpers::verifyContent(output, kFunction);
testing_helpers::verifyContent(output, std::to_string(kLine));
std::cout << output << std::endl;
}
TEST(Message, DefaultFormattingToLogFile) {
using namespace g3;
std::string file_content;
{
testing_helpers::RestoreFileLogger logger(testdirectory);
LOG(WARNING) << "testing";
logger.reset(); // force flush of logger (which will trigger a shutdown)
file_content = testing_helpers::readFileToText(logger.logFile()); // logger is already reset
}
std::ostringstream thread_id_oss;
thread_id_oss << " [" << std::this_thread::get_id() << " ";
EXPECT_FALSE(testing_helpers::verifyContent(file_content, thread_id_oss.str()));
}
TEST(Message, FullFormattingToLogFile) {
using namespace g3;
std::string file_content;
{
testing_helpers::RestoreFileLogger logger(testdirectory);
logger._handle->call(&FileSink::overrideLogDetails, &LogMessage::FullLogDetailsToString);
LOG(WARNING) << "testing";
logger.reset(); // force flush of logger (which will trigger a shutdown)
file_content = testing_helpers::readFileToText(logger.logFile()); // logger is already reset
}
std::ostringstream thread_id_oss;
thread_id_oss << " [" << std::this_thread::get_id() << " ";
EXPECT_TRUE(testing_helpers::verifyContent(file_content, thread_id_oss.str()));
}
TEST(Message, CppSupport) {
// ref: http://www.cplusplus.com/reference/clibrary/ctime/strftime/
// ref: http://en.cppreference.com/w/cpp/io/manip/put_time
// Day Month Date Time Year: is written as "%a %b %d %H:%M:%S %Y" and formatted output as : Wed Sep 19 08:28:16 2012
// --- WARNING: The try/catch setup does NOT work,. but for fun and for fake-clarity I leave it
// --- For formatting options to std::put_time that are NOT YET implemented on Windows fatal errors/assert will occurr
// --- the last example is such an example.
try {
std::cout << g3::localtime_formatted(std::chrono::system_clock::now(), "%a %b %d %H:%M:%S %Y") << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(1));
std::cout << g3::localtime_formatted(std::chrono::system_clock::now(), "%%Y/%%m/%%d %%H:%%M:%%S = %Y/%m/%d %H:%M:%S") << std::endl;
#if (defined(WIN32) || defined(_WIN32) || defined(__WIN32__))
std::cerr << "Formatting options skipped due to VS2012, C++11 non-conformance for" << std::endl;
std::cerr << " some formatting options. The skipped code was:\n\t\t %EX %Ec, \n(see http://en.cppreference.com/w/cpp/io/manip/put_time for details)" << std::endl;
#else
std::cout << "C++11 new formatting options:\n" << g3::localtime_formatted(std::chrono::system_clock::now(), "%%EX: %EX\n%%z: %z\n%%Ec: %Ec") << std::endl;
#endif
}
// This does not work. Other kinds of fatal exits (on Windows) seems to be used instead of exceptions
// Maybe a signal handler catch would be better? --- TODO: Make it better, both failing and correct
catch (...) {
ADD_FAILURE() << "On this platform the library does not support given (C++11?) specifiers";
return;
}
ASSERT_TRUE(true); // no exception. all good
}
TEST(Message, GetFractional_Empty_buffer_ExpectDefaults) {
auto fractional = g3::internal::getFractional("", 0);
const auto expected = g3::internal::Fractional::NanosecondDefault;
EXPECT_EQ(fractional, expected);
fractional = g3::internal::getFractional("", 100);
EXPECT_EQ(fractional, expected);
}
TEST(Message, GetFractional_MilliSeconds) {
auto fractional = g3::internal::getFractional("%f3", 0);
const auto expected = g3::internal::Fractional::Millisecond;
EXPECT_EQ(fractional, expected);
}
TEST(Message, GetFractional_Microsecond) {
auto fractional = g3::internal::getFractional("%f6", 0);
const auto expected = g3::internal::Fractional::Microsecond;
EXPECT_EQ(fractional, expected);
}
TEST(Message, GetFractional_Nanosecond) {
auto fractional = g3::internal::getFractional("%f9", 0);
const auto expected = g3::internal::Fractional::Nanosecond;
EXPECT_EQ(fractional, expected);
}
TEST(Message, GetFractional_NanosecondDefault) {
auto fractional = g3::internal::getFractional("%f", 0);
const auto expected = g3::internal::Fractional::NanosecondDefault;
EXPECT_EQ(fractional, expected);
}
TEST(Message, GetFractional_All) {
std::string formatted = "%f, %f9, %f6, %f3";
auto fractional = g3::internal::getFractional(formatted, 0);
auto expected = g3::internal::Fractional::NanosecondDefault;
EXPECT_EQ(fractional, expected);
// ns
fractional = g3::internal::getFractional(formatted, 4);
expected = g3::internal::Fractional::Nanosecond;
EXPECT_EQ(fractional, expected);
// us
fractional = g3::internal::getFractional(formatted, 9);
expected = g3::internal::Fractional::Microsecond;
EXPECT_EQ(fractional, expected);
// ms
fractional = g3::internal::getFractional(formatted, 14);
expected = g3::internal::Fractional::Millisecond;
EXPECT_EQ(fractional, expected);
}
TEST(Message, FractionalToString_SizeCheck) {
auto value = g3::internal::to_string(kTimePoint_2017_April_27th, g3::internal::Fractional::Nanosecond);
EXPECT_EQ("000000000", value);
value = g3::internal::to_string(kTimePoint_2017_April_27th, g3::internal::Fractional::NanosecondDefault);
EXPECT_EQ("000000000", value);
// us
value = g3::internal::to_string(kTimePoint_2017_April_27th, g3::internal::Fractional::Microsecond);
EXPECT_EQ("000000", value);
// ms
value = g3::internal::to_string(kTimePoint_2017_April_27th, g3::internal::Fractional::Millisecond);
EXPECT_EQ("000", value);
}
TEST(Message, FractionalToStringNanoPadded) {
auto value = g3::internal::to_string(k1970_January_1st, g3::internal::Fractional::Nanosecond);
EXPECT_EQ("000000000", value);
// 0000000012
value = g3::internal::to_string(k1970_January_1st, g3::internal::Fractional::NanosecondDefault);
EXPECT_EQ("000000000", value);
}
TEST(Message, FractionalToString12NanoPadded) {
auto value = g3::internal::to_string(k1970_January_1st, g3::internal::Fractional::Nanosecond);
EXPECT_EQ("000000000", value);
// 0000000012
value = g3::internal::to_string(k1970_January_1st, g3::internal::Fractional::NanosecondDefault);
EXPECT_EQ("000000000", value);
}
TEST(Message, FractionalToStringMicroPadded) {
auto value = g3::internal::to_string(k1970_January_1st, g3::internal::Fractional::Microsecond);
EXPECT_EQ("000000", value);
value = g3::internal::to_string(k1970_January_1st, g3::internal::Fractional::Microsecond);
EXPECT_EQ("000000", value);
}
TEST(Message, FractionalToStringMilliPadded) {
auto value = g3::internal::to_string(k1970_January_1st, g3::internal::Fractional::Millisecond);
EXPECT_EQ("000", value);
value = g3::internal::to_string(k1970_January_1st, g3::internal::Fractional::Millisecond);
EXPECT_EQ("000", value);
}
#if !(defined(WIN32) || defined(_WIN32) || defined(__WIN32__))
TEST(Message, localtime_formatted) {
char* tz = nullptr;
std::shared_ptr<void> RaiiTimeZoneReset(nullptr, [&](void*) {
if (tz)
setenv("TZ", tz, 1);
else
unsetenv("TZ");
tzset();
});
tz = getenv("TZ");
setenv("TZ", "", 1);
tzset();
auto time_point = std::chrono::system_clock::from_time_t(k2017_April_27th);
auto format = g3::localtime_formatted(time_point, "%Y-%m-%d %H:%M:%S"); // %Y/%m/%d
std::string expected = {"2017-04-27 06:22:27"};
EXPECT_EQ(expected, format);
auto us_format = g3::localtime_formatted(time_point, g3::internal::time_formatted); // "%H:%M:%S %f6";
EXPECT_EQ("06:22:27 000000", us_format);
auto ns_format = g3::localtime_formatted(time_point, "%H:%M:%S %f");
EXPECT_EQ("06:22:27 000000000", ns_format);
auto ms_format = g3::localtime_formatted(time_point, "%H:%M:%S %f3");
EXPECT_EQ("06:22:27 000", ms_format);
}
#endif // timezone
#if defined(CHANGE_G3LOG_DEBUG_TO_DBUG)
TEST(Level, G3LogDebug_is_DBUG) {
LOG(DBUG) << "DBUG equals G3LOG_DEBUG";
LOG(G3LOG_DEBUG) << "G3LOG_DEBUG equals DBUG";
}
#else
TEST(Level, G3LogDebug_is_DEBUG) {
LOG(DEBUG) << "DEBUG equals G3LOG_DEBUG";
LOG(G3LOG_DEBUG) << "G3LOG_DEBUG equals DEBUG";
}
#endif
#ifdef G3_DYNAMIC_LOGGING
namespace {
using LevelsContainer = std::map<int, g3::LoggingLevel>;
const LevelsContainer g_test_log_level_defaults = {
{G3LOG_DEBUG.value, {G3LOG_DEBUG}},
{INFO.value, {INFO}},
{WARNING.value, {WARNING}},
{FATAL.value, {FATAL}}
};
const LevelsContainer g_test_all_disabled = {
{G3LOG_DEBUG.value, {G3LOG_DEBUG,false}},
{INFO.value, {INFO, false}},
{WARNING.value, {WARNING, false}},
{FATAL.value, {FATAL, false}}
};
bool mapCompare (LevelsContainer const& lhs, LevelsContainer const& rhs) {
auto pred = [] (auto a, auto b) {
return (a.first == b.first) &&
(a.second == b.second);
};
return lhs.size() == rhs.size()
&& std::equal(lhs.begin(), lhs.end(), rhs.begin(), pred);
}
} // anonymous
TEST(Level, Default) {
g3::only_change_at_initialization::reset();
auto defaults = g3::log_levels::getAll();
EXPECT_EQ(defaults.size(), g_test_log_level_defaults.size());
EXPECT_TRUE(mapCompare(defaults, g_test_log_level_defaults));
}
TEST(Level, DefaultChanged_only_change_at_initialization) {
g3::only_change_at_initialization::reset();
std::shared_ptr<void> RaiiLeveReset(nullptr, [&](void*) {
g3::only_change_at_initialization::reset();
});
g3::only_change_at_initialization::addLogLevel(INFO, false);
auto defaults = g3::log_levels::getAll();
EXPECT_EQ(defaults.size(), g_test_log_level_defaults.size());
EXPECT_FALSE(mapCompare(defaults, g_test_log_level_defaults));
const LevelsContainer defaultsWithInfoChangged = {
{G3LOG_DEBUG.value, {G3LOG_DEBUG, true}},
{INFO.value, {INFO, false}},
{WARNING.value, {WARNING, true}},
{FATAL.value, {FATAL, true}}
};
EXPECT_TRUE(mapCompare(defaults, defaultsWithInfoChangged));
}
TEST(Level, DefaultChanged_log_levels) {
g3::only_change_at_initialization::reset();
std::shared_ptr<void> RaiiLeveReset(nullptr, [&](void*) {
g3::only_change_at_initialization::reset();
});
g3::log_levels::disable(INFO);
auto defaults = g3::log_levels::getAll();
EXPECT_EQ(defaults.size(), g_test_log_level_defaults.size());
EXPECT_FALSE(mapCompare(defaults, g_test_log_level_defaults));
const LevelsContainer defaultsWithInfoChangged = {
{G3LOG_DEBUG.value, {G3LOG_DEBUG, true}},
{INFO.value, {INFO, false}},
{WARNING.value, {WARNING, true}},
{FATAL.value, {FATAL, true}}
};
EXPECT_TRUE(mapCompare(defaults, defaultsWithInfoChangged));
}
TEST(Level, Reset) {
std::shared_ptr<void> RaiiLeveReset(nullptr, [&](void*) {
g3::only_change_at_initialization::reset();
});
g3::log_levels::disableAll();
auto all_levels = g3::log_levels::getAll();
EXPECT_TRUE(mapCompare(all_levels, g_test_all_disabled));
g3::only_change_at_initialization::reset();
all_levels = g3::log_levels::getAll();
EXPECT_TRUE(mapCompare(all_levels, g_test_log_level_defaults));
}
TEST(Level, AllDisabled) {
g3::only_change_at_initialization::reset();
std::shared_ptr<void> RaiiLeveReset(nullptr, [&](void*) {
g3::only_change_at_initialization::reset();
});
auto all_levels = g3::log_levels::getAll();
EXPECT_EQ(all_levels.size(), g_test_all_disabled.size());
EXPECT_FALSE(mapCompare(all_levels, g_test_all_disabled));
g3::log_levels::disableAll();
all_levels = g3::log_levels::getAll();
EXPECT_TRUE(mapCompare(all_levels, g_test_all_disabled));
}
TEST(Level, setHighestLogLevel_high_end) {
g3::only_change_at_initialization::reset();
std::shared_ptr<void> RaiiLeveReset(nullptr, [&](void*) {
g3::only_change_at_initialization::reset();
});
g3::log_levels::enableAll();
g3::log_levels::disable(FATAL);
g3::log_levels::setHighest(FATAL);
LevelsContainer expected = {
{G3LOG_DEBUG.value, {G3LOG_DEBUG, false}},
{INFO.value, {INFO, false}},
{WARNING.value, {WARNING, false}},
{FATAL.value, {FATAL, true}}
};
auto all_levels = g3::log_levels::getAll();
EXPECT_TRUE(mapCompare(all_levels, expected)) << g3::log_levels::to_string();
}
TEST(Level, setHighestLogLevel_low_end) {
g3::only_change_at_initialization::reset();
std::shared_ptr<void> RaiiLeveReset(nullptr, [&](void*) {
g3::only_change_at_initialization::reset();
});
g3::log_levels::disableAll();
g3::log_levels::setHighest(G3LOG_DEBUG);
LevelsContainer expected = {
{G3LOG_DEBUG.value,{G3LOG_DEBUG, true}},
{INFO.value, {INFO, true}},
{WARNING.value, {WARNING, true}},
{FATAL.value, {FATAL, true}}
};
auto all_levels = g3::log_levels::getAll();
EXPECT_TRUE(mapCompare(all_levels, expected)) << g3::log_levels::to_string();
}
TEST(Level, setHighestLogLevel_middle) {
g3::only_change_at_initialization::reset();
std::shared_ptr<void> RaiiLeveReset(nullptr, [&](void*) {
g3::only_change_at_initialization::reset();
});
g3::log_levels::enableAll();
g3::log_levels::setHighest(WARNING);
LevelsContainer expected = {
{G3LOG_DEBUG.value, {G3LOG_DEBUG, false}},
{INFO.value, {INFO, false}},
{WARNING.value, {WARNING, true}},
{FATAL.value, {FATAL, true}}
};
auto all_levels = g3::log_levels::getAll();
EXPECT_TRUE(mapCompare(all_levels, expected));
}
TEST(Level, setHighestLogLevel_StepWiseDisableAll) {
g3::only_change_at_initialization::reset();
std::shared_ptr<void> RaiiLeveReset(nullptr, [&](void*) {
g3::only_change_at_initialization::reset();
});
LevelsContainer changing_levels = {
{G3LOG_DEBUG.value, {G3LOG_DEBUG, true}},
{INFO.value, {INFO, true}},
{WARNING.value, {WARNING, true}},
{FATAL.value, {FATAL, true}}
};
auto all_levels = g3::log_levels::getAll();
EXPECT_TRUE(mapCompare(all_levels, g_test_log_level_defaults));
size_t counter = 0;
for (auto& lvl : changing_levels) {
g3::log_levels::setHighest(lvl.second.level);
all_levels = g3::log_levels::getAll();
ASSERT_TRUE(mapCompare(all_levels, changing_levels)) <<
"counter: " << counter << "\nsystem:\n" <<
g3::log_levels::to_string(all_levels) <<
"\nexpected:\n" <<
g3::log_levels::to_string(changing_levels);
++counter;
if (counter != changing_levels.size()) {
// for next round this level will be disabled
lvl.second.status = false;
}
}
// in the end all except the last should be disabled
auto mostly_disabled = g_test_all_disabled;
mostly_disabled[FATAL.value].status = true;
EXPECT_TRUE(mapCompare(changing_levels, mostly_disabled));
all_levels = g3::log_levels::getAll();
EXPECT_TRUE(mapCompare(all_levels, mostly_disabled)) <<
"\nsystem:\n" <<
g3::log_levels::to_string(all_levels) <<
"\nexpected:\n" <<
g3::log_levels::to_string(mostly_disabled);
}
TEST(Level, Print) {
g3::only_change_at_initialization::reset();
std::string expected = std::string{"name: DEBUG level: 100 status: 1\n"}
+ "name: INFO level: 300 status: 1\n"
+ "name: WARNING level: 500 status: 1\n"
+ "name: FATAL level: 1000 status: 1\n";
EXPECT_EQ(g3::log_levels::to_string(), expected);
}
TEST(Level, AddOneEnabled_option1) {
std::shared_ptr<void> RaiiLeveReset(nullptr, [&](void*) {
g3::only_change_at_initialization::reset();
});
LEVELS MYINFO {WARNING.value + 1, "MyInfoLevel"};
g3::only_change_at_initialization::addLogLevel(MYINFO, true);
auto modified = g_test_log_level_defaults;
modified[MYINFO.value] = MYINFO;
auto all_levels = g3::log_levels::getAll();
EXPECT_TRUE(mapCompare(modified, all_levels)) << "\nsystem:\n" <<
g3::log_levels::to_string(all_levels) <<
"\nexpected:\n" <<
g3::log_levels::to_string(modified);
}
TEST(Level, AddOneEnabled_option2) {
std::shared_ptr<void> RaiiLeveReset(nullptr, [&](void*) {
g3::only_change_at_initialization::reset();
});
LEVELS MYINFO {WARNING.value + 1, "MyInfoLevel"};
g3::only_change_at_initialization::addLogLevel(MYINFO);
auto modified = g_test_log_level_defaults;
modified[MYINFO.value] = MYINFO;
auto all_levels = g3::log_levels::getAll();
EXPECT_TRUE(mapCompare(modified, all_levels)) << "\nsystem:\n" <<
g3::log_levels::to_string(all_levels) <<
"\nexpected:\n" <<
g3::log_levels::to_string(modified);
}
TEST(Level, Addlevel_using_addLevel) {
std::shared_ptr<void> RaiiLeveReset(nullptr, [&](void*) {
g3::only_change_at_initialization::reset();
});
LEVELS MYINFO {WARNING.value + 1, "MyInfoLevel"};
auto status = g3::log_levels::getStatus(MYINFO);
EXPECT_EQ(status, g3::log_levels::status::Absent);
g3::only_change_at_initialization::addLogLevel(MYINFO);
status = g3::log_levels::getStatus(MYINFO);
EXPECT_EQ(status, g3::log_levels::status::Enabled);
}
TEST(Level, Addlevel_using_addLogLevel_disabled) {
std::shared_ptr<void> RaiiLeveReset(nullptr, [&](void*) {
g3::only_change_at_initialization::reset();
});
LEVELS MYINFO {WARNING.value + 1, "MyInfoLevel"};
auto status = g3::log_levels::getStatus(MYINFO);
EXPECT_EQ(status, g3::log_levels::status::Absent);
g3::only_change_at_initialization::addLogLevel(MYINFO, false);
status = g3::log_levels::getStatus(MYINFO);
EXPECT_EQ(status, g3::log_levels::status::Disabled);
}
TEST(Level, Addlevel__disabled) {
std::shared_ptr<void> RaiiLeveReset(nullptr, [&](void*) {
g3::only_change_at_initialization::reset();
});
LEVELS MYINFO {WARNING.value + 1, "MyInfoLevel"};
auto status = g3::log_levels::getStatus(MYINFO);
EXPECT_EQ(status, g3::log_levels::status::Absent);
g3::log_levels::enable(MYINFO);
status = g3::log_levels::getStatus(MYINFO);
EXPECT_EQ(status, g3::log_levels::status::Absent);
g3::log_levels::set(MYINFO, true);
status = g3::log_levels::getStatus(MYINFO);
EXPECT_EQ(status, g3::log_levels::status::Absent);
g3::only_change_at_initialization::addLogLevel(MYINFO, false);
status = g3::log_levels::getStatus(MYINFO);
EXPECT_EQ(status, g3::log_levels::status::Disabled);
}
TEST(Level, Addlevel__enabled) {
std::shared_ptr<void> RaiiLeveReset(nullptr, [&](void*) {
g3::only_change_at_initialization::reset();
});
LEVELS MYINFO {WARNING.value + 1, "MyInfoLevel"};
auto status = g3::log_levels::getStatus(MYINFO);
EXPECT_EQ(status, g3::log_levels::status::Absent);
g3::only_change_at_initialization::addLogLevel(MYINFO);
status = g3::log_levels::getStatus(MYINFO);
EXPECT_EQ(status, g3::log_levels::status::Enabled);
}
#endif // G3_DYNAMIC_LOGGING