blob: b431a058b2da40748f501ca124865e0789eae849 [file] [log] [blame]
Junxiao Shi7d054272016-08-04 17:00:41 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
dmcoomese062a182017-06-12 11:10:31 -05002/*
Davide Pesavento38a061d2018-02-15 22:45:48 -05003 * Copyright (c) 2013-2018 Regents of the University of California.
Junxiao Shi7d054272016-08-04 17:00:41 +00004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
7 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20 */
Junxiao Shi1fe7ce52016-08-08 05:48:02 +000021
Junxiao Shi7d054272016-08-04 17:00:41 +000022#include "util/logging.hpp"
23#include "util/logger.hpp"
24
Davide Pesavento0c145ec2018-04-10 21:33:57 -040025#include "../unit-test-time-fixture.hpp"
Junxiao Shi7d054272016-08-04 17:00:41 +000026#include "boost-test.hpp"
Junxiao Shi7d054272016-08-04 17:00:41 +000027
28namespace ndn {
29namespace util {
30namespace tests {
31
Alexander Afanasyev354f3822017-03-27 15:26:41 -050032NDN_LOG_INIT(ndn.util.tests.Logging);
33
Junxiao Shi7d054272016-08-04 17:00:41 +000034void
35logFromModule1();
36
37void
38logFromModule2();
39
40void
dmcoomese062a182017-06-12 11:10:31 -050041logFromFilterModule();
42
43static void
Davide Pesavento1c9c96c2018-04-25 10:45:08 -040044logFromNewLogger(const char* moduleName)
Junxiao Shi7d054272016-08-04 17:00:41 +000045{
Davide Pesavento0c145ec2018-04-10 21:33:57 -040046 Logger logger(moduleName);
47 auto ndn_cxx_getLogger = [&logger] () -> Logger& { return logger; };
Junxiao Shi7d054272016-08-04 17:00:41 +000048
Junxiao Shi1fe7ce52016-08-08 05:48:02 +000049 NDN_LOG_TRACE("trace" << moduleName);
50 NDN_LOG_DEBUG("debug" << moduleName);
51 NDN_LOG_INFO("info" << moduleName);
52 NDN_LOG_WARN("warn" << moduleName);
53 NDN_LOG_ERROR("error" << moduleName);
54 NDN_LOG_FATAL("fatal" << moduleName);
Junxiao Shi7d054272016-08-04 17:00:41 +000055
56 BOOST_CHECK(Logging::get().removeLogger(logger));
57}
58
Davide Pesavento0c145ec2018-04-10 21:33:57 -040059namespace ns1 {
Junxiao Shi7d054272016-08-04 17:00:41 +000060
Davide Pesavento0c145ec2018-04-10 21:33:57 -040061NDN_LOG_INIT(ndn.util.tests.ns1);
62
63static void
64logFromNamespace1()
65{
66 NDN_LOG_INFO("hello world from ns1");
67}
68
69} // namespace ns1
70
71namespace ns2 {
72
73NDN_LOG_INIT(ndn.util.tests.ns2);
74
75static void
76logFromNamespace2()
77{
78 NDN_LOG_INFO("hi there from ns2");
79}
80
81} // namespace ns2
82
83class ClassWithLogger
84{
85public:
86 void
87 logFromConstMemberFunction() const
88 {
89 NDN_LOG_INFO("const member function");
90 }
91
92 static void
93 logFromStaticMemberFunction()
94 {
95 NDN_LOG_INFO("static member function");
96 }
97
98private:
Davide Pesavento1c9c96c2018-04-25 10:45:08 -040099 NDN_LOG_MEMBER_DECL();
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400100};
101
Davide Pesavento1c9c96c2018-04-25 10:45:08 -0400102NDN_LOG_MEMBER_INIT(ClassWithLogger, ndn.util.tests.ClassWithLogger);
103
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400104template<class T, class U>
105class ClassTemplateWithLogger
106{
107public:
108 void
109 logFromMemberFunction()
110 {
111 NDN_LOG_INFO("class template non-static member function");
112 }
113
114 static void
115 logFromStaticMemberFunction()
116 {
117 NDN_LOG_INFO("class template static member function");
118 }
119
120private:
121 NDN_LOG_MEMBER_DECL();
122};
123
124// Technically this declaration is not necessary in this case,
125// but we want to test that the macro expands to well-formed code
126NDN_LOG_MEMBER_DECL_SPECIALIZED((ClassTemplateWithLogger<int, double>));
127
128NDN_LOG_MEMBER_INIT_SPECIALIZED((ClassTemplateWithLogger<int, double>), ndn.util.tests.Specialized1);
129NDN_LOG_MEMBER_INIT_SPECIALIZED((ClassTemplateWithLogger<int, std::string>), ndn.util.tests.Specialized2);
130
131const time::microseconds LOG_SYSTIME(1468108800311239LL);
132const std::string LOG_SYSTIME_STR("1468108800.311239");
133
134class LoggingFixture : public ndn::tests::UnitTestTimeFixture
Junxiao Shi7d054272016-08-04 17:00:41 +0000135{
136protected:
Junxiao Shi7d054272016-08-04 17:00:41 +0000137 LoggingFixture()
dmcoomese062a182017-06-12 11:10:31 -0500138 : m_oldEnabledLevel(Logging::get().getLevels())
Junxiao Shi7d054272016-08-04 17:00:41 +0000139 , m_oldDestination(Logging::get().getDestination())
140 {
141 this->systemClock->setNow(LOG_SYSTIME);
142 Logging::get().resetLevels();
143 Logging::setDestination(os);
144 }
145
146 ~LoggingFixture()
147 {
dmcoomese062a182017-06-12 11:10:31 -0500148 Logging::get().setLevelImpl(m_oldEnabledLevel);
Junxiao Shi7d054272016-08-04 17:00:41 +0000149 Logging::setDestination(m_oldDestination);
150 }
151
152protected:
Davide Pesavento1c9c96c2018-04-25 10:45:08 -0400153 boost::test_tools::output_test_stream os;
Junxiao Shi7d054272016-08-04 17:00:41 +0000154
155private:
dmcoomese062a182017-06-12 11:10:31 -0500156 std::unordered_map<std::string, LogLevel> m_oldEnabledLevel;
Junxiao Shi7d054272016-08-04 17:00:41 +0000157 shared_ptr<std::ostream> m_oldDestination;
158};
159
160BOOST_AUTO_TEST_SUITE(Util)
161BOOST_FIXTURE_TEST_SUITE(TestLogging, LoggingFixture)
162
Alexander Afanasyev354f3822017-03-27 15:26:41 -0500163BOOST_AUTO_TEST_CASE(GetLoggerNames)
164{
Davide Pesavento1c9c96c2018-04-25 10:45:08 -0400165 // check that all names are registered from the start even if
166 // logger instances are lazily created on first use
167 auto n = Logging::getLoggerNames().size();
168 NDN_LOG_TRACE("GetLoggerNames");
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400169 auto names = Logging::getLoggerNames();
Davide Pesavento1c9c96c2018-04-25 10:45:08 -0400170 BOOST_CHECK_EQUAL(names.size(), n);
Alexander Afanasyev354f3822017-03-27 15:26:41 -0500171 BOOST_CHECK_EQUAL(names.count("ndn.util.tests.Logging"), 1);
172}
173
Junxiao Shi7d054272016-08-04 17:00:41 +0000174BOOST_AUTO_TEST_SUITE(Severity)
175
176BOOST_AUTO_TEST_CASE(None)
177{
178 Logging::setLevel("Module1", LogLevel::NONE);
179 logFromModule1();
180
181 Logging::flush();
182 BOOST_CHECK(os.is_equal(
183 LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n"
184 ));
185}
186
187BOOST_AUTO_TEST_CASE(Error)
188{
189 Logging::setLevel("Module1", LogLevel::ERROR);
190 logFromModule1();
191
192 Logging::flush();
193 BOOST_CHECK(os.is_equal(
194 LOG_SYSTIME_STR + " ERROR: [Module1] error1\n" +
195 LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n"
196 ));
197}
198
199BOOST_AUTO_TEST_CASE(Warn)
200{
201 Logging::setLevel("Module1", LogLevel::WARN);
202 logFromModule1();
203
204 Logging::flush();
205 BOOST_CHECK(os.is_equal(
206 LOG_SYSTIME_STR + " WARNING: [Module1] warn1\n" +
207 LOG_SYSTIME_STR + " ERROR: [Module1] error1\n" +
208 LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n"
209 ));
210}
211
212BOOST_AUTO_TEST_CASE(Info)
213{
214 Logging::setLevel("Module1", LogLevel::INFO);
215 logFromModule1();
216
217 Logging::flush();
218 BOOST_CHECK(os.is_equal(
219 LOG_SYSTIME_STR + " INFO: [Module1] info1\n" +
220 LOG_SYSTIME_STR + " WARNING: [Module1] warn1\n" +
221 LOG_SYSTIME_STR + " ERROR: [Module1] error1\n" +
222 LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n"
223 ));
224}
225
226BOOST_AUTO_TEST_CASE(Debug)
227{
228 Logging::setLevel("Module1", LogLevel::DEBUG);
229 logFromModule1();
230
231 Logging::flush();
232 BOOST_CHECK(os.is_equal(
233 LOG_SYSTIME_STR + " DEBUG: [Module1] debug1\n" +
234 LOG_SYSTIME_STR + " INFO: [Module1] info1\n" +
235 LOG_SYSTIME_STR + " WARNING: [Module1] warn1\n" +
236 LOG_SYSTIME_STR + " ERROR: [Module1] error1\n" +
237 LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n"
238 ));
239}
240
241BOOST_AUTO_TEST_CASE(Trace)
242{
243 Logging::setLevel("Module1", LogLevel::TRACE);
244 logFromModule1();
245
246 Logging::flush();
247 BOOST_CHECK(os.is_equal(
248 LOG_SYSTIME_STR + " TRACE: [Module1] trace1\n" +
249 LOG_SYSTIME_STR + " DEBUG: [Module1] debug1\n" +
250 LOG_SYSTIME_STR + " INFO: [Module1] info1\n" +
251 LOG_SYSTIME_STR + " WARNING: [Module1] warn1\n" +
252 LOG_SYSTIME_STR + " ERROR: [Module1] error1\n" +
253 LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n"
254 ));
255}
256
257BOOST_AUTO_TEST_CASE(All)
258{
259 Logging::setLevel("Module1", LogLevel::ALL);
260 logFromModule1();
261
262 Logging::flush();
263 BOOST_CHECK(os.is_equal(
264 LOG_SYSTIME_STR + " TRACE: [Module1] trace1\n" +
265 LOG_SYSTIME_STR + " DEBUG: [Module1] debug1\n" +
266 LOG_SYSTIME_STR + " INFO: [Module1] info1\n" +
267 LOG_SYSTIME_STR + " WARNING: [Module1] warn1\n" +
268 LOG_SYSTIME_STR + " ERROR: [Module1] error1\n" +
269 LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n"
270 ));
271}
272
273BOOST_AUTO_TEST_SUITE_END() // Severity
274
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400275BOOST_AUTO_TEST_CASE(NamespaceLogger)
276{
277 Logging::setLevel("ndn.util.tests.ns1", LogLevel::INFO);
278 Logging::setLevel("ndn.util.tests.ns2", LogLevel::DEBUG);
279
280 const auto& levels = Logging::get().getLevels();
281 BOOST_CHECK_EQUAL(levels.size(), 2);
282 BOOST_CHECK_EQUAL(levels.at("ndn.util.tests.ns1"), LogLevel::INFO);
283 BOOST_CHECK_EQUAL(levels.at("ndn.util.tests.ns2"), LogLevel::DEBUG);
284
Davide Pesavento1c9c96c2018-04-25 10:45:08 -0400285 const auto& names = Logging::getLoggerNames();
286 BOOST_CHECK_EQUAL(names.count("ndn.util.tests.ns1"), 1);
287 BOOST_CHECK_EQUAL(names.count("ndn.util.tests.ns2"), 1);
288
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400289 ns1::logFromNamespace1();
290 ns2::logFromNamespace2();
291
292 Logging::flush();
293 BOOST_CHECK(os.is_equal(
294 LOG_SYSTIME_STR + " INFO: [ndn.util.tests.ns1] hello world from ns1\n" +
295 LOG_SYSTIME_STR + " INFO: [ndn.util.tests.ns2] hi there from ns2\n"
296 ));
297}
298
299BOOST_AUTO_TEST_CASE(MemberLogger)
300{
301 Logging::setLevel("ndn.util.tests.ClassWithLogger", LogLevel::INFO);
302 Logging::setLevel("ndn.util.tests.Specialized1", LogLevel::INFO);
303 // ndn.util.tests.Specialized2 is not enabled
304
Davide Pesavento1c9c96c2018-04-25 10:45:08 -0400305 const auto& names = Logging::getLoggerNames();
306 BOOST_CHECK_EQUAL(names.count("ndn.util.tests.ClassWithLogger"), 1);
307 BOOST_CHECK_EQUAL(names.count("ndn.util.tests.Specialized1"), 1);
308 BOOST_CHECK_EQUAL(names.count("ndn.util.tests.Specialized2"), 1);
309
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400310 ClassWithLogger::logFromStaticMemberFunction();
311 ClassWithLogger{}.logFromConstMemberFunction();
312
313 Logging::flush();
314 BOOST_CHECK(os.is_equal(
315 LOG_SYSTIME_STR + " INFO: [ndn.util.tests.ClassWithLogger] static member function\n" +
316 LOG_SYSTIME_STR + " INFO: [ndn.util.tests.ClassWithLogger] const member function\n"
317 ));
318
319 ClassTemplateWithLogger<int, double>::logFromStaticMemberFunction();
320 ClassTemplateWithLogger<int, double>{}.logFromMemberFunction();
321 ClassTemplateWithLogger<int, std::string>::logFromStaticMemberFunction();
322 ClassTemplateWithLogger<int, std::string>{}.logFromMemberFunction();
323
324 Logging::flush();
325 BOOST_CHECK(os.is_equal(
326 LOG_SYSTIME_STR + " INFO: [ndn.util.tests.Specialized1] class template static member function\n" +
327 LOG_SYSTIME_STR + " INFO: [ndn.util.tests.Specialized1] class template non-static member function\n"
328 ));
329}
330
Junxiao Shi7d054272016-08-04 17:00:41 +0000331BOOST_AUTO_TEST_CASE(SameNameLoggers)
332{
333 Logging::setLevel("Module1", LogLevel::WARN);
334 logFromModule1();
335 logFromNewLogger("Module1");
336
Davide Pesavento1c9c96c2018-04-25 10:45:08 -0400337 BOOST_CHECK_EQUAL(Logging::getLoggerNames().count("Module1"), 1);
338
Junxiao Shi7d054272016-08-04 17:00:41 +0000339 Logging::flush();
340 BOOST_CHECK(os.is_equal(
341 LOG_SYSTIME_STR + " WARNING: [Module1] warn1\n" +
342 LOG_SYSTIME_STR + " ERROR: [Module1] error1\n" +
343 LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" +
344 LOG_SYSTIME_STR + " WARNING: [Module1] warnModule1\n" +
345 LOG_SYSTIME_STR + " ERROR: [Module1] errorModule1\n" +
346 LOG_SYSTIME_STR + " FATAL: [Module1] fatalModule1\n"
347 ));
348}
349
350BOOST_AUTO_TEST_CASE(LateRegistration)
351{
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400352 Logging::setLevel("Module3", LogLevel::DEBUG);
Junxiao Shi7d054272016-08-04 17:00:41 +0000353 logFromNewLogger("Module3");
354
355 Logging::flush();
356 BOOST_CHECK(os.is_equal(
357 LOG_SYSTIME_STR + " DEBUG: [Module3] debugModule3\n" +
358 LOG_SYSTIME_STR + " INFO: [Module3] infoModule3\n" +
359 LOG_SYSTIME_STR + " WARNING: [Module3] warnModule3\n" +
360 LOG_SYSTIME_STR + " ERROR: [Module3] errorModule3\n" +
361 LOG_SYSTIME_STR + " FATAL: [Module3] fatalModule3\n"
362 ));
363}
364
365BOOST_AUTO_TEST_SUITE(DefaultSeverity)
366
367BOOST_AUTO_TEST_CASE(Unset)
368{
369 logFromModule1();
370 logFromModule2();
371
372 Logging::flush();
373 BOOST_CHECK(os.is_equal(
374 LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" +
375 LOG_SYSTIME_STR + " FATAL: [Module2] fatal2\n"
376 ));
377}
378
379BOOST_AUTO_TEST_CASE(NoOverride)
380{
381 Logging::setLevel("*", LogLevel::WARN);
382 logFromModule1();
383 logFromModule2();
384
385 Logging::flush();
386 BOOST_CHECK(os.is_equal(
387 LOG_SYSTIME_STR + " WARNING: [Module1] warn1\n" +
388 LOG_SYSTIME_STR + " ERROR: [Module1] error1\n" +
389 LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" +
390 LOG_SYSTIME_STR + " WARNING: [Module2] warn2\n" +
391 LOG_SYSTIME_STR + " ERROR: [Module2] error2\n" +
392 LOG_SYSTIME_STR + " FATAL: [Module2] fatal2\n"
393 ));
394}
395
396BOOST_AUTO_TEST_CASE(Override)
397{
398 Logging::setLevel("*", LogLevel::WARN);
399 Logging::setLevel("Module2", LogLevel::DEBUG);
400 logFromModule1();
401 logFromModule2();
402
403 Logging::flush();
404 BOOST_CHECK(os.is_equal(
405 LOG_SYSTIME_STR + " WARNING: [Module1] warn1\n" +
406 LOG_SYSTIME_STR + " ERROR: [Module1] error1\n" +
407 LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" +
408 LOG_SYSTIME_STR + " DEBUG: [Module2] debug2\n" +
409 LOG_SYSTIME_STR + " INFO: [Module2] info2\n" +
410 LOG_SYSTIME_STR + " WARNING: [Module2] warn2\n" +
411 LOG_SYSTIME_STR + " ERROR: [Module2] error2\n" +
412 LOG_SYSTIME_STR + " FATAL: [Module2] fatal2\n"
413 ));
414}
415
416BOOST_AUTO_TEST_SUITE_END() // DefaultSeverity
417
418BOOST_AUTO_TEST_SUITE(SeverityConfig)
419
420BOOST_AUTO_TEST_CASE(SetEmpty)
421{
422 Logging::setLevel("");
dmcoomese062a182017-06-12 11:10:31 -0500423 const auto& prefixMap = Logging::get().getLevels();
424 BOOST_CHECK_EQUAL(prefixMap.size(), 0);
Junxiao Shi7d054272016-08-04 17:00:41 +0000425 logFromModule1();
426 logFromModule2();
427
428 Logging::flush();
429 BOOST_CHECK(os.is_equal(
430 LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" +
431 LOG_SYSTIME_STR + " FATAL: [Module2] fatal2\n"
432 ));
433}
434
435BOOST_AUTO_TEST_CASE(SetDefault)
436{
437 Logging::setLevel("*=WARN");
dmcoomese062a182017-06-12 11:10:31 -0500438 const auto& prefixMap = Logging::get().getLevels();
439 // "*" is treated as "" internally
440 BOOST_CHECK_EQUAL(prefixMap.size(), 1);
441 BOOST_CHECK_EQUAL(prefixMap.at(""), LogLevel::WARN);
Junxiao Shi7d054272016-08-04 17:00:41 +0000442 logFromModule1();
443 logFromModule2();
444
445 Logging::flush();
446 BOOST_CHECK(os.is_equal(
447 LOG_SYSTIME_STR + " WARNING: [Module1] warn1\n" +
448 LOG_SYSTIME_STR + " ERROR: [Module1] error1\n" +
449 LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" +
450 LOG_SYSTIME_STR + " WARNING: [Module2] warn2\n" +
451 LOG_SYSTIME_STR + " ERROR: [Module2] error2\n" +
452 LOG_SYSTIME_STR + " FATAL: [Module2] fatal2\n"
453 ));
454}
455
456BOOST_AUTO_TEST_CASE(SetModule)
457{
458 Logging::setLevel("Module1=ERROR");
dmcoomese062a182017-06-12 11:10:31 -0500459 const auto& prefixMap = Logging::get().getLevels();
460 BOOST_CHECK_EQUAL(prefixMap.size(), 1);
461 BOOST_CHECK_EQUAL(prefixMap.at("Module1"), LogLevel::ERROR);
Junxiao Shi7d054272016-08-04 17:00:41 +0000462 logFromModule1();
463 logFromModule2();
464
465 Logging::flush();
466 BOOST_CHECK(os.is_equal(
467 LOG_SYSTIME_STR + " ERROR: [Module1] error1\n" +
468 LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" +
469 LOG_SYSTIME_STR + " FATAL: [Module2] fatal2\n"
470 ));
471}
472
473BOOST_AUTO_TEST_CASE(SetOverride)
474{
475 Logging::setLevel("*=WARN:Module2=DEBUG");
dmcoomese062a182017-06-12 11:10:31 -0500476 const auto& prefixMap = Logging::get().getLevels();
477 BOOST_CHECK_EQUAL(prefixMap.size(), 2);
478 // "*" is treated as "" internally
479 BOOST_CHECK_EQUAL(prefixMap.at(""), LogLevel::WARN);
480 BOOST_CHECK_EQUAL(prefixMap.at("Module2"), LogLevel::DEBUG);
Junxiao Shi7d054272016-08-04 17:00:41 +0000481 logFromModule1();
482 logFromModule2();
483
484 Logging::flush();
485 BOOST_CHECK(os.is_equal(
486 LOG_SYSTIME_STR + " WARNING: [Module1] warn1\n" +
487 LOG_SYSTIME_STR + " ERROR: [Module1] error1\n" +
488 LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" +
489 LOG_SYSTIME_STR + " DEBUG: [Module2] debug2\n" +
490 LOG_SYSTIME_STR + " INFO: [Module2] info2\n" +
491 LOG_SYSTIME_STR + " WARNING: [Module2] warn2\n" +
492 LOG_SYSTIME_STR + " ERROR: [Module2] error2\n" +
493 LOG_SYSTIME_STR + " FATAL: [Module2] fatal2\n"
494 ));
495}
496
497BOOST_AUTO_TEST_CASE(SetTwice)
498{
499 Logging::setLevel("*=WARN");
500 Logging::setLevel("Module2=DEBUG");
dmcoomese062a182017-06-12 11:10:31 -0500501 const auto& prefixMap = Logging::get().getLevels();
502 BOOST_CHECK_EQUAL(prefixMap.size(), 2);
503 // "*" is treated as "" internally
504 BOOST_CHECK_EQUAL(prefixMap.at(""), LogLevel::WARN);
505 BOOST_CHECK_EQUAL(prefixMap.at("Module2"), LogLevel::DEBUG);
Junxiao Shi7d054272016-08-04 17:00:41 +0000506 logFromModule1();
507 logFromModule2();
508
509 Logging::flush();
510 BOOST_CHECK(os.is_equal(
511 LOG_SYSTIME_STR + " WARNING: [Module1] warn1\n" +
512 LOG_SYSTIME_STR + " ERROR: [Module1] error1\n" +
513 LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" +
514 LOG_SYSTIME_STR + " DEBUG: [Module2] debug2\n" +
515 LOG_SYSTIME_STR + " INFO: [Module2] info2\n" +
516 LOG_SYSTIME_STR + " WARNING: [Module2] warn2\n" +
517 LOG_SYSTIME_STR + " ERROR: [Module2] error2\n" +
518 LOG_SYSTIME_STR + " FATAL: [Module2] fatal2\n"
519 ));
520}
521
522BOOST_AUTO_TEST_CASE(Reset)
523{
524 Logging::setLevel("Module2=DEBUG");
525 Logging::setLevel("*=ERROR");
dmcoomese062a182017-06-12 11:10:31 -0500526 const auto& prefixMap = Logging::get().getLevels();
527 BOOST_CHECK_EQUAL(prefixMap.size(), 1);
528 // "*" is treated as "" internally
529 BOOST_CHECK_EQUAL(prefixMap.at(""), LogLevel::ERROR);
Junxiao Shi7d054272016-08-04 17:00:41 +0000530 logFromModule1();
531 logFromModule2();
532
533 Logging::flush();
534 BOOST_CHECK(os.is_equal(
535 LOG_SYSTIME_STR + " ERROR: [Module1] error1\n" +
536 LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" +
537 LOG_SYSTIME_STR + " ERROR: [Module2] error2\n" +
538 LOG_SYSTIME_STR + " FATAL: [Module2] fatal2\n"
539 ));
540}
541
542BOOST_AUTO_TEST_CASE(Malformed)
543{
544 BOOST_CHECK_THROW(Logging::setLevel("Module1=INVALID-LEVEL"), std::invalid_argument);
545 BOOST_CHECK_THROW(Logging::setLevel("Module1-MISSING-EQUAL-SIGN"), std::invalid_argument);
546}
547
dmcoomese062a182017-06-12 11:10:31 -0500548BOOST_AUTO_TEST_CASE(SetFilter)
549{
550 Logging::setLevel("*=FATAL:fm.*=DEBUG");
551 const auto& prefixMap = Logging::get().getLevels();
552 BOOST_CHECK_EQUAL(prefixMap.size(), 2);
553 // "*" is treated as "" internally
554 BOOST_CHECK_EQUAL(prefixMap.at(""), LogLevel::FATAL);
555 // "name.*" is treated as "name." internally
556 BOOST_CHECK_EQUAL(prefixMap.at("fm."), LogLevel::DEBUG);
557 logFromModule1();
558 logFromFilterModule();
559
560 Logging::flush();
561 BOOST_CHECK(os.is_equal(
562 LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" +
563 LOG_SYSTIME_STR + " DEBUG: [fm.FilterModule] debugFM\n" +
564 LOG_SYSTIME_STR + " INFO: [fm.FilterModule] infoFM\n" +
565 LOG_SYSTIME_STR + " WARNING: [fm.FilterModule] warnFM\n" +
566 LOG_SYSTIME_STR + " ERROR: [fm.FilterModule] errorFM\n" +
567 LOG_SYSTIME_STR + " FATAL: [fm.FilterModule] fatalFM\n"
568 ));
569}
570
571BOOST_AUTO_TEST_CASE(SetOverrideFilter)
572{
573 Logging::setLevel("*=FATAL:fm.FilterModule=DEBUG");
574 Logging::setLevel("fm.*", LogLevel::INFO);
575 const auto& prefixMap = Logging::get().getLevels();
576 BOOST_CHECK_EQUAL(prefixMap.size(), 2);
577 // "*" is treated as "" internally
578 BOOST_CHECK_EQUAL(prefixMap.at(""), LogLevel::FATAL);
579 // "name.*" is treated as "name." internally
580 BOOST_CHECK_EQUAL(prefixMap.at("fm."), LogLevel::INFO);
581 logFromModule1();
582 logFromFilterModule();
583
584 Logging::flush();
585 BOOST_CHECK(os.is_equal(
586 LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" +
587 LOG_SYSTIME_STR + " INFO: [fm.FilterModule] infoFM\n" +
588 LOG_SYSTIME_STR + " WARNING: [fm.FilterModule] warnFM\n" +
589 LOG_SYSTIME_STR + " ERROR: [fm.FilterModule] errorFM\n" +
590 LOG_SYSTIME_STR + " FATAL: [fm.FilterModule] fatalFM\n"
591 ));
592}
593
594BOOST_AUTO_TEST_CASE(FindPrefixRule)
595{
596 Logging::setLevel("*=FATAL:fm.a.*=ERROR:fm.a.b=INFO");
597 logFromNewLogger("fm.a.b");
598 logFromNewLogger("fm.a.b.c");
599 logFromNewLogger("fm.a.b.d");
600 logFromNewLogger("fm.b");
601
602 Logging::flush();
603 BOOST_CHECK(os.is_equal(
604 LOG_SYSTIME_STR + " INFO: [fm.a.b] infofm.a.b\n" +
605 LOG_SYSTIME_STR + " WARNING: [fm.a.b] warnfm.a.b\n" +
606 LOG_SYSTIME_STR + " ERROR: [fm.a.b] errorfm.a.b\n" +
607 LOG_SYSTIME_STR + " FATAL: [fm.a.b] fatalfm.a.b\n" +
608 LOG_SYSTIME_STR + " ERROR: [fm.a.b.c] errorfm.a.b.c\n" +
609 LOG_SYSTIME_STR + " FATAL: [fm.a.b.c] fatalfm.a.b.c\n" +
610 LOG_SYSTIME_STR + " ERROR: [fm.a.b.d] errorfm.a.b.d\n" +
611 LOG_SYSTIME_STR + " FATAL: [fm.a.b.d] fatalfm.a.b.d\n" +
612 LOG_SYSTIME_STR + " FATAL: [fm.b] fatalfm.b\n"
613 ));
614}
615
Junxiao Shi7d054272016-08-04 17:00:41 +0000616BOOST_AUTO_TEST_SUITE_END() // SeverityConfig
617
618BOOST_AUTO_TEST_CASE(ChangeDestination)
619{
Davide Pesavento1c9c96c2018-04-25 10:45:08 -0400620 using boost::test_tools::output_test_stream;
621
Junxiao Shi7d054272016-08-04 17:00:41 +0000622 logFromModule1();
623
624 auto os2 = make_shared<output_test_stream>();
625 Logging::setDestination(os2);
626 weak_ptr<output_test_stream> os2weak(os2);
627 os2.reset();
628
629 logFromModule2();
630
631 Logging::flush();
632 os2 = os2weak.lock();
633 BOOST_REQUIRE(os2 != nullptr);
634
635 BOOST_CHECK(os.is_equal(
636 LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n"
637 ));
638 BOOST_CHECK(os2->is_equal(
639 LOG_SYSTIME_STR + " FATAL: [Module2] fatal2\n"
640 ));
641
642 os2.reset();
643 Logging::setDestination(os);
644 BOOST_CHECK(os2weak.expired());
645}
646
647BOOST_AUTO_TEST_SUITE_END() // TestLogging
648BOOST_AUTO_TEST_SUITE_END() // Util
649
650} // namespace tests
651} // namespace util
652} // namespace ndn