blob: 172d58c247edb64e6dd17c6e5ed2c2c913378c6a [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 +000034using boost::test_tools::output_test_stream;
35
36void
37logFromModule1();
38
39void
40logFromModule2();
41
42void
dmcoomese062a182017-06-12 11:10:31 -050043logFromFilterModule();
44
45static void
Junxiao Shi7d054272016-08-04 17:00:41 +000046logFromNewLogger(const std::string& moduleName)
47{
Davide Pesavento0c145ec2018-04-10 21:33:57 -040048 Logger logger(moduleName);
49 auto ndn_cxx_getLogger = [&logger] () -> Logger& { return logger; };
Junxiao Shi7d054272016-08-04 17:00:41 +000050
Junxiao Shi1fe7ce52016-08-08 05:48:02 +000051 NDN_LOG_TRACE("trace" << moduleName);
52 NDN_LOG_DEBUG("debug" << moduleName);
53 NDN_LOG_INFO("info" << moduleName);
54 NDN_LOG_WARN("warn" << moduleName);
55 NDN_LOG_ERROR("error" << moduleName);
56 NDN_LOG_FATAL("fatal" << moduleName);
Junxiao Shi7d054272016-08-04 17:00:41 +000057
58 BOOST_CHECK(Logging::get().removeLogger(logger));
59}
60
Davide Pesavento0c145ec2018-04-10 21:33:57 -040061namespace ns1 {
Junxiao Shi7d054272016-08-04 17:00:41 +000062
Davide Pesavento0c145ec2018-04-10 21:33:57 -040063NDN_LOG_INIT(ndn.util.tests.ns1);
64
65static void
66logFromNamespace1()
67{
68 NDN_LOG_INFO("hello world from ns1");
69}
70
71} // namespace ns1
72
73namespace ns2 {
74
75NDN_LOG_INIT(ndn.util.tests.ns2);
76
77static void
78logFromNamespace2()
79{
80 NDN_LOG_INFO("hi there from ns2");
81}
82
83} // namespace ns2
84
85class ClassWithLogger
86{
87public:
88 void
89 logFromConstMemberFunction() const
90 {
91 NDN_LOG_INFO("const member function");
92 }
93
94 static void
95 logFromStaticMemberFunction()
96 {
97 NDN_LOG_INFO("static member function");
98 }
99
100private:
101 NDN_LOG_MEMBER_INIT(ndn.util.tests.ClassWithLogger);
102};
103
104template<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:
153 output_test_stream os;
154
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{
165 NDN_LOG_TRACE("GetLoggerNames"); // to avoid unused function warning
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400166 auto names = Logging::getLoggerNames();
167 BOOST_CHECK(!names.empty());
Alexander Afanasyev354f3822017-03-27 15:26:41 -0500168 BOOST_CHECK_EQUAL(names.count("ndn.util.tests.Logging"), 1);
169}
170
Junxiao Shi7d054272016-08-04 17:00:41 +0000171BOOST_AUTO_TEST_SUITE(Severity)
172
173BOOST_AUTO_TEST_CASE(None)
174{
175 Logging::setLevel("Module1", LogLevel::NONE);
176 logFromModule1();
177
178 Logging::flush();
179 BOOST_CHECK(os.is_equal(
180 LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n"
181 ));
182}
183
184BOOST_AUTO_TEST_CASE(Error)
185{
186 Logging::setLevel("Module1", LogLevel::ERROR);
187 logFromModule1();
188
189 Logging::flush();
190 BOOST_CHECK(os.is_equal(
191 LOG_SYSTIME_STR + " ERROR: [Module1] error1\n" +
192 LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n"
193 ));
194}
195
196BOOST_AUTO_TEST_CASE(Warn)
197{
198 Logging::setLevel("Module1", LogLevel::WARN);
199 logFromModule1();
200
201 Logging::flush();
202 BOOST_CHECK(os.is_equal(
203 LOG_SYSTIME_STR + " WARNING: [Module1] warn1\n" +
204 LOG_SYSTIME_STR + " ERROR: [Module1] error1\n" +
205 LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n"
206 ));
207}
208
209BOOST_AUTO_TEST_CASE(Info)
210{
211 Logging::setLevel("Module1", LogLevel::INFO);
212 logFromModule1();
213
214 Logging::flush();
215 BOOST_CHECK(os.is_equal(
216 LOG_SYSTIME_STR + " INFO: [Module1] info1\n" +
217 LOG_SYSTIME_STR + " WARNING: [Module1] warn1\n" +
218 LOG_SYSTIME_STR + " ERROR: [Module1] error1\n" +
219 LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n"
220 ));
221}
222
223BOOST_AUTO_TEST_CASE(Debug)
224{
225 Logging::setLevel("Module1", LogLevel::DEBUG);
226 logFromModule1();
227
228 Logging::flush();
229 BOOST_CHECK(os.is_equal(
230 LOG_SYSTIME_STR + " DEBUG: [Module1] debug1\n" +
231 LOG_SYSTIME_STR + " INFO: [Module1] info1\n" +
232 LOG_SYSTIME_STR + " WARNING: [Module1] warn1\n" +
233 LOG_SYSTIME_STR + " ERROR: [Module1] error1\n" +
234 LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n"
235 ));
236}
237
238BOOST_AUTO_TEST_CASE(Trace)
239{
240 Logging::setLevel("Module1", LogLevel::TRACE);
241 logFromModule1();
242
243 Logging::flush();
244 BOOST_CHECK(os.is_equal(
245 LOG_SYSTIME_STR + " TRACE: [Module1] trace1\n" +
246 LOG_SYSTIME_STR + " DEBUG: [Module1] debug1\n" +
247 LOG_SYSTIME_STR + " INFO: [Module1] info1\n" +
248 LOG_SYSTIME_STR + " WARNING: [Module1] warn1\n" +
249 LOG_SYSTIME_STR + " ERROR: [Module1] error1\n" +
250 LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n"
251 ));
252}
253
254BOOST_AUTO_TEST_CASE(All)
255{
256 Logging::setLevel("Module1", LogLevel::ALL);
257 logFromModule1();
258
259 Logging::flush();
260 BOOST_CHECK(os.is_equal(
261 LOG_SYSTIME_STR + " TRACE: [Module1] trace1\n" +
262 LOG_SYSTIME_STR + " DEBUG: [Module1] debug1\n" +
263 LOG_SYSTIME_STR + " INFO: [Module1] info1\n" +
264 LOG_SYSTIME_STR + " WARNING: [Module1] warn1\n" +
265 LOG_SYSTIME_STR + " ERROR: [Module1] error1\n" +
266 LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n"
267 ));
268}
269
270BOOST_AUTO_TEST_SUITE_END() // Severity
271
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400272BOOST_AUTO_TEST_CASE(NamespaceLogger)
273{
274 Logging::setLevel("ndn.util.tests.ns1", LogLevel::INFO);
275 Logging::setLevel("ndn.util.tests.ns2", LogLevel::DEBUG);
276
277 const auto& levels = Logging::get().getLevels();
278 BOOST_CHECK_EQUAL(levels.size(), 2);
279 BOOST_CHECK_EQUAL(levels.at("ndn.util.tests.ns1"), LogLevel::INFO);
280 BOOST_CHECK_EQUAL(levels.at("ndn.util.tests.ns2"), LogLevel::DEBUG);
281
282 ns1::logFromNamespace1();
283 ns2::logFromNamespace2();
284
285 Logging::flush();
286 BOOST_CHECK(os.is_equal(
287 LOG_SYSTIME_STR + " INFO: [ndn.util.tests.ns1] hello world from ns1\n" +
288 LOG_SYSTIME_STR + " INFO: [ndn.util.tests.ns2] hi there from ns2\n"
289 ));
290}
291
292BOOST_AUTO_TEST_CASE(MemberLogger)
293{
294 Logging::setLevel("ndn.util.tests.ClassWithLogger", LogLevel::INFO);
295 Logging::setLevel("ndn.util.tests.Specialized1", LogLevel::INFO);
296 // ndn.util.tests.Specialized2 is not enabled
297
298 ClassWithLogger::logFromStaticMemberFunction();
299 ClassWithLogger{}.logFromConstMemberFunction();
300
301 Logging::flush();
302 BOOST_CHECK(os.is_equal(
303 LOG_SYSTIME_STR + " INFO: [ndn.util.tests.ClassWithLogger] static member function\n" +
304 LOG_SYSTIME_STR + " INFO: [ndn.util.tests.ClassWithLogger] const member function\n"
305 ));
306
307 ClassTemplateWithLogger<int, double>::logFromStaticMemberFunction();
308 ClassTemplateWithLogger<int, double>{}.logFromMemberFunction();
309 ClassTemplateWithLogger<int, std::string>::logFromStaticMemberFunction();
310 ClassTemplateWithLogger<int, std::string>{}.logFromMemberFunction();
311
312 Logging::flush();
313 BOOST_CHECK(os.is_equal(
314 LOG_SYSTIME_STR + " INFO: [ndn.util.tests.Specialized1] class template static member function\n" +
315 LOG_SYSTIME_STR + " INFO: [ndn.util.tests.Specialized1] class template non-static member function\n"
316 ));
317}
318
Junxiao Shi7d054272016-08-04 17:00:41 +0000319BOOST_AUTO_TEST_CASE(SameNameLoggers)
320{
321 Logging::setLevel("Module1", LogLevel::WARN);
322 logFromModule1();
323 logFromNewLogger("Module1");
324
325 Logging::flush();
326 BOOST_CHECK(os.is_equal(
327 LOG_SYSTIME_STR + " WARNING: [Module1] warn1\n" +
328 LOG_SYSTIME_STR + " ERROR: [Module1] error1\n" +
329 LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" +
330 LOG_SYSTIME_STR + " WARNING: [Module1] warnModule1\n" +
331 LOG_SYSTIME_STR + " ERROR: [Module1] errorModule1\n" +
332 LOG_SYSTIME_STR + " FATAL: [Module1] fatalModule1\n"
333 ));
334}
335
336BOOST_AUTO_TEST_CASE(LateRegistration)
337{
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400338 Logging::setLevel("Module3", LogLevel::DEBUG);
Junxiao Shi7d054272016-08-04 17:00:41 +0000339 logFromNewLogger("Module3");
340
341 Logging::flush();
342 BOOST_CHECK(os.is_equal(
343 LOG_SYSTIME_STR + " DEBUG: [Module3] debugModule3\n" +
344 LOG_SYSTIME_STR + " INFO: [Module3] infoModule3\n" +
345 LOG_SYSTIME_STR + " WARNING: [Module3] warnModule3\n" +
346 LOG_SYSTIME_STR + " ERROR: [Module3] errorModule3\n" +
347 LOG_SYSTIME_STR + " FATAL: [Module3] fatalModule3\n"
348 ));
349}
350
351BOOST_AUTO_TEST_SUITE(DefaultSeverity)
352
353BOOST_AUTO_TEST_CASE(Unset)
354{
355 logFromModule1();
356 logFromModule2();
357
358 Logging::flush();
359 BOOST_CHECK(os.is_equal(
360 LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" +
361 LOG_SYSTIME_STR + " FATAL: [Module2] fatal2\n"
362 ));
363}
364
365BOOST_AUTO_TEST_CASE(NoOverride)
366{
367 Logging::setLevel("*", LogLevel::WARN);
368 logFromModule1();
369 logFromModule2();
370
371 Logging::flush();
372 BOOST_CHECK(os.is_equal(
373 LOG_SYSTIME_STR + " WARNING: [Module1] warn1\n" +
374 LOG_SYSTIME_STR + " ERROR: [Module1] error1\n" +
375 LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" +
376 LOG_SYSTIME_STR + " WARNING: [Module2] warn2\n" +
377 LOG_SYSTIME_STR + " ERROR: [Module2] error2\n" +
378 LOG_SYSTIME_STR + " FATAL: [Module2] fatal2\n"
379 ));
380}
381
382BOOST_AUTO_TEST_CASE(Override)
383{
384 Logging::setLevel("*", LogLevel::WARN);
385 Logging::setLevel("Module2", LogLevel::DEBUG);
386 logFromModule1();
387 logFromModule2();
388
389 Logging::flush();
390 BOOST_CHECK(os.is_equal(
391 LOG_SYSTIME_STR + " WARNING: [Module1] warn1\n" +
392 LOG_SYSTIME_STR + " ERROR: [Module1] error1\n" +
393 LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" +
394 LOG_SYSTIME_STR + " DEBUG: [Module2] debug2\n" +
395 LOG_SYSTIME_STR + " INFO: [Module2] info2\n" +
396 LOG_SYSTIME_STR + " WARNING: [Module2] warn2\n" +
397 LOG_SYSTIME_STR + " ERROR: [Module2] error2\n" +
398 LOG_SYSTIME_STR + " FATAL: [Module2] fatal2\n"
399 ));
400}
401
402BOOST_AUTO_TEST_SUITE_END() // DefaultSeverity
403
404BOOST_AUTO_TEST_SUITE(SeverityConfig)
405
406BOOST_AUTO_TEST_CASE(SetEmpty)
407{
408 Logging::setLevel("");
dmcoomese062a182017-06-12 11:10:31 -0500409 const auto& prefixMap = Logging::get().getLevels();
410 BOOST_CHECK_EQUAL(prefixMap.size(), 0);
Junxiao Shi7d054272016-08-04 17:00:41 +0000411 logFromModule1();
412 logFromModule2();
413
414 Logging::flush();
415 BOOST_CHECK(os.is_equal(
416 LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" +
417 LOG_SYSTIME_STR + " FATAL: [Module2] fatal2\n"
418 ));
419}
420
421BOOST_AUTO_TEST_CASE(SetDefault)
422{
423 Logging::setLevel("*=WARN");
dmcoomese062a182017-06-12 11:10:31 -0500424 const auto& prefixMap = Logging::get().getLevels();
425 // "*" is treated as "" internally
426 BOOST_CHECK_EQUAL(prefixMap.size(), 1);
427 BOOST_CHECK_EQUAL(prefixMap.at(""), LogLevel::WARN);
Junxiao Shi7d054272016-08-04 17:00:41 +0000428 logFromModule1();
429 logFromModule2();
430
431 Logging::flush();
432 BOOST_CHECK(os.is_equal(
433 LOG_SYSTIME_STR + " WARNING: [Module1] warn1\n" +
434 LOG_SYSTIME_STR + " ERROR: [Module1] error1\n" +
435 LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" +
436 LOG_SYSTIME_STR + " WARNING: [Module2] warn2\n" +
437 LOG_SYSTIME_STR + " ERROR: [Module2] error2\n" +
438 LOG_SYSTIME_STR + " FATAL: [Module2] fatal2\n"
439 ));
440}
441
442BOOST_AUTO_TEST_CASE(SetModule)
443{
444 Logging::setLevel("Module1=ERROR");
dmcoomese062a182017-06-12 11:10:31 -0500445 const auto& prefixMap = Logging::get().getLevels();
446 BOOST_CHECK_EQUAL(prefixMap.size(), 1);
447 BOOST_CHECK_EQUAL(prefixMap.at("Module1"), LogLevel::ERROR);
Junxiao Shi7d054272016-08-04 17:00:41 +0000448 logFromModule1();
449 logFromModule2();
450
451 Logging::flush();
452 BOOST_CHECK(os.is_equal(
453 LOG_SYSTIME_STR + " ERROR: [Module1] error1\n" +
454 LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" +
455 LOG_SYSTIME_STR + " FATAL: [Module2] fatal2\n"
456 ));
457}
458
459BOOST_AUTO_TEST_CASE(SetOverride)
460{
461 Logging::setLevel("*=WARN:Module2=DEBUG");
dmcoomese062a182017-06-12 11:10:31 -0500462 const auto& prefixMap = Logging::get().getLevels();
463 BOOST_CHECK_EQUAL(prefixMap.size(), 2);
464 // "*" is treated as "" internally
465 BOOST_CHECK_EQUAL(prefixMap.at(""), LogLevel::WARN);
466 BOOST_CHECK_EQUAL(prefixMap.at("Module2"), LogLevel::DEBUG);
Junxiao Shi7d054272016-08-04 17:00:41 +0000467 logFromModule1();
468 logFromModule2();
469
470 Logging::flush();
471 BOOST_CHECK(os.is_equal(
472 LOG_SYSTIME_STR + " WARNING: [Module1] warn1\n" +
473 LOG_SYSTIME_STR + " ERROR: [Module1] error1\n" +
474 LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" +
475 LOG_SYSTIME_STR + " DEBUG: [Module2] debug2\n" +
476 LOG_SYSTIME_STR + " INFO: [Module2] info2\n" +
477 LOG_SYSTIME_STR + " WARNING: [Module2] warn2\n" +
478 LOG_SYSTIME_STR + " ERROR: [Module2] error2\n" +
479 LOG_SYSTIME_STR + " FATAL: [Module2] fatal2\n"
480 ));
481}
482
483BOOST_AUTO_TEST_CASE(SetTwice)
484{
485 Logging::setLevel("*=WARN");
486 Logging::setLevel("Module2=DEBUG");
dmcoomese062a182017-06-12 11:10:31 -0500487 const auto& prefixMap = Logging::get().getLevels();
488 BOOST_CHECK_EQUAL(prefixMap.size(), 2);
489 // "*" is treated as "" internally
490 BOOST_CHECK_EQUAL(prefixMap.at(""), LogLevel::WARN);
491 BOOST_CHECK_EQUAL(prefixMap.at("Module2"), LogLevel::DEBUG);
Junxiao Shi7d054272016-08-04 17:00:41 +0000492 logFromModule1();
493 logFromModule2();
494
495 Logging::flush();
496 BOOST_CHECK(os.is_equal(
497 LOG_SYSTIME_STR + " WARNING: [Module1] warn1\n" +
498 LOG_SYSTIME_STR + " ERROR: [Module1] error1\n" +
499 LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" +
500 LOG_SYSTIME_STR + " DEBUG: [Module2] debug2\n" +
501 LOG_SYSTIME_STR + " INFO: [Module2] info2\n" +
502 LOG_SYSTIME_STR + " WARNING: [Module2] warn2\n" +
503 LOG_SYSTIME_STR + " ERROR: [Module2] error2\n" +
504 LOG_SYSTIME_STR + " FATAL: [Module2] fatal2\n"
505 ));
506}
507
508BOOST_AUTO_TEST_CASE(Reset)
509{
510 Logging::setLevel("Module2=DEBUG");
511 Logging::setLevel("*=ERROR");
dmcoomese062a182017-06-12 11:10:31 -0500512 const auto& prefixMap = Logging::get().getLevels();
513 BOOST_CHECK_EQUAL(prefixMap.size(), 1);
514 // "*" is treated as "" internally
515 BOOST_CHECK_EQUAL(prefixMap.at(""), LogLevel::ERROR);
Junxiao Shi7d054272016-08-04 17:00:41 +0000516 logFromModule1();
517 logFromModule2();
518
519 Logging::flush();
520 BOOST_CHECK(os.is_equal(
521 LOG_SYSTIME_STR + " ERROR: [Module1] error1\n" +
522 LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" +
523 LOG_SYSTIME_STR + " ERROR: [Module2] error2\n" +
524 LOG_SYSTIME_STR + " FATAL: [Module2] fatal2\n"
525 ));
526}
527
528BOOST_AUTO_TEST_CASE(Malformed)
529{
530 BOOST_CHECK_THROW(Logging::setLevel("Module1=INVALID-LEVEL"), std::invalid_argument);
531 BOOST_CHECK_THROW(Logging::setLevel("Module1-MISSING-EQUAL-SIGN"), std::invalid_argument);
532}
533
dmcoomese062a182017-06-12 11:10:31 -0500534BOOST_AUTO_TEST_CASE(SetFilter)
535{
536 Logging::setLevel("*=FATAL:fm.*=DEBUG");
537 const auto& prefixMap = Logging::get().getLevels();
538 BOOST_CHECK_EQUAL(prefixMap.size(), 2);
539 // "*" is treated as "" internally
540 BOOST_CHECK_EQUAL(prefixMap.at(""), LogLevel::FATAL);
541 // "name.*" is treated as "name." internally
542 BOOST_CHECK_EQUAL(prefixMap.at("fm."), LogLevel::DEBUG);
543 logFromModule1();
544 logFromFilterModule();
545
546 Logging::flush();
547 BOOST_CHECK(os.is_equal(
548 LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" +
549 LOG_SYSTIME_STR + " DEBUG: [fm.FilterModule] debugFM\n" +
550 LOG_SYSTIME_STR + " INFO: [fm.FilterModule] infoFM\n" +
551 LOG_SYSTIME_STR + " WARNING: [fm.FilterModule] warnFM\n" +
552 LOG_SYSTIME_STR + " ERROR: [fm.FilterModule] errorFM\n" +
553 LOG_SYSTIME_STR + " FATAL: [fm.FilterModule] fatalFM\n"
554 ));
555}
556
557BOOST_AUTO_TEST_CASE(SetOverrideFilter)
558{
559 Logging::setLevel("*=FATAL:fm.FilterModule=DEBUG");
560 Logging::setLevel("fm.*", LogLevel::INFO);
561 const auto& prefixMap = Logging::get().getLevels();
562 BOOST_CHECK_EQUAL(prefixMap.size(), 2);
563 // "*" is treated as "" internally
564 BOOST_CHECK_EQUAL(prefixMap.at(""), LogLevel::FATAL);
565 // "name.*" is treated as "name." internally
566 BOOST_CHECK_EQUAL(prefixMap.at("fm."), LogLevel::INFO);
567 logFromModule1();
568 logFromFilterModule();
569
570 Logging::flush();
571 BOOST_CHECK(os.is_equal(
572 LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" +
573 LOG_SYSTIME_STR + " INFO: [fm.FilterModule] infoFM\n" +
574 LOG_SYSTIME_STR + " WARNING: [fm.FilterModule] warnFM\n" +
575 LOG_SYSTIME_STR + " ERROR: [fm.FilterModule] errorFM\n" +
576 LOG_SYSTIME_STR + " FATAL: [fm.FilterModule] fatalFM\n"
577 ));
578}
579
580BOOST_AUTO_TEST_CASE(FindPrefixRule)
581{
582 Logging::setLevel("*=FATAL:fm.a.*=ERROR:fm.a.b=INFO");
583 logFromNewLogger("fm.a.b");
584 logFromNewLogger("fm.a.b.c");
585 logFromNewLogger("fm.a.b.d");
586 logFromNewLogger("fm.b");
587
588 Logging::flush();
589 BOOST_CHECK(os.is_equal(
590 LOG_SYSTIME_STR + " INFO: [fm.a.b] infofm.a.b\n" +
591 LOG_SYSTIME_STR + " WARNING: [fm.a.b] warnfm.a.b\n" +
592 LOG_SYSTIME_STR + " ERROR: [fm.a.b] errorfm.a.b\n" +
593 LOG_SYSTIME_STR + " FATAL: [fm.a.b] fatalfm.a.b\n" +
594 LOG_SYSTIME_STR + " ERROR: [fm.a.b.c] errorfm.a.b.c\n" +
595 LOG_SYSTIME_STR + " FATAL: [fm.a.b.c] fatalfm.a.b.c\n" +
596 LOG_SYSTIME_STR + " ERROR: [fm.a.b.d] errorfm.a.b.d\n" +
597 LOG_SYSTIME_STR + " FATAL: [fm.a.b.d] fatalfm.a.b.d\n" +
598 LOG_SYSTIME_STR + " FATAL: [fm.b] fatalfm.b\n"
599 ));
600}
601
Junxiao Shi7d054272016-08-04 17:00:41 +0000602BOOST_AUTO_TEST_SUITE_END() // SeverityConfig
603
604BOOST_AUTO_TEST_CASE(ChangeDestination)
605{
606 logFromModule1();
607
608 auto os2 = make_shared<output_test_stream>();
609 Logging::setDestination(os2);
610 weak_ptr<output_test_stream> os2weak(os2);
611 os2.reset();
612
613 logFromModule2();
614
615 Logging::flush();
616 os2 = os2weak.lock();
617 BOOST_REQUIRE(os2 != nullptr);
618
619 BOOST_CHECK(os.is_equal(
620 LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n"
621 ));
622 BOOST_CHECK(os2->is_equal(
623 LOG_SYSTIME_STR + " FATAL: [Module2] fatal2\n"
624 ));
625
626 os2.reset();
627 Logging::setDestination(os);
628 BOOST_CHECK(os2weak.expired());
629}
630
631BOOST_AUTO_TEST_SUITE_END() // TestLogging
632BOOST_AUTO_TEST_SUITE_END() // Util
633
634} // namespace tests
635} // namespace util
636} // namespace ndn