blob: e051cce15516e655616ccfd6032720de0fb53337 [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/*
Alexander Afanasyev576b4ff2019-07-05 22:06:04 -04003 * Copyright (c) 2013-2019 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
Davide Pesavento7e780642018-11-24 15:51:34 -050022#include "ndn-cxx/util/logging.hpp"
23#include "ndn-cxx/util/logger.hpp"
Junxiao Shi7d054272016-08-04 17:00:41 +000024
Davide Pesavento7e780642018-11-24 15:51:34 -050025#include "tests/boost-test.hpp"
26#include "tests/unit/unit-test-time-fixture.hpp"
27
28#include <boost/test/output_test_stream.hpp>
Junxiao Shi7d054272016-08-04 17:00:41 +000029
30namespace ndn {
31namespace util {
32namespace tests {
33
Alexander Afanasyev354f3822017-03-27 15:26:41 -050034NDN_LOG_INIT(ndn.util.tests.Logging);
35
Junxiao Shi7d054272016-08-04 17:00:41 +000036void
37logFromModule1();
38
39void
40logFromModule2();
41
42void
dmcoomese062a182017-06-12 11:10:31 -050043logFromFilterModule();
44
45static void
Davide Pesavento1c9c96c2018-04-25 10:45:08 -040046logFromNewLogger(const char* moduleName)
Junxiao Shi7d054272016-08-04 17:00:41 +000047{
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:
Davide Pesavento1c9c96c2018-04-25 10:45:08 -0400101 NDN_LOG_MEMBER_DECL();
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400102};
103
Davide Pesavento1c9c96c2018-04-25 10:45:08 -0400104NDN_LOG_MEMBER_INIT(ClassWithLogger, ndn.util.tests.ClassWithLogger);
105
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400106template<class T, class U>
107class ClassTemplateWithLogger
108{
109public:
110 void
111 logFromMemberFunction()
112 {
113 NDN_LOG_INFO("class template non-static member function");
114 }
115
116 static void
117 logFromStaticMemberFunction()
118 {
119 NDN_LOG_INFO("class template static member function");
120 }
121
122private:
123 NDN_LOG_MEMBER_DECL();
124};
125
126// Technically this declaration is not necessary in this case,
127// but we want to test that the macro expands to well-formed code
128NDN_LOG_MEMBER_DECL_SPECIALIZED((ClassTemplateWithLogger<int, double>));
129
130NDN_LOG_MEMBER_INIT_SPECIALIZED((ClassTemplateWithLogger<int, double>), ndn.util.tests.Specialized1);
131NDN_LOG_MEMBER_INIT_SPECIALIZED((ClassTemplateWithLogger<int, std::string>), ndn.util.tests.Specialized2);
132
133const time::microseconds LOG_SYSTIME(1468108800311239LL);
134const std::string LOG_SYSTIME_STR("1468108800.311239");
135
136class LoggingFixture : public ndn::tests::UnitTestTimeFixture
Junxiao Shi7d054272016-08-04 17:00:41 +0000137{
138protected:
Junxiao Shi7d054272016-08-04 17:00:41 +0000139 LoggingFixture()
dmcoomese062a182017-06-12 11:10:31 -0500140 : m_oldEnabledLevel(Logging::get().getLevels())
Junxiao Shi7d054272016-08-04 17:00:41 +0000141 , m_oldDestination(Logging::get().getDestination())
142 {
143 this->systemClock->setNow(LOG_SYSTIME);
144 Logging::get().resetLevels();
145 Logging::setDestination(os);
146 }
147
148 ~LoggingFixture()
149 {
dmcoomese062a182017-06-12 11:10:31 -0500150 Logging::get().setLevelImpl(m_oldEnabledLevel);
Junxiao Shi7d054272016-08-04 17:00:41 +0000151 Logging::setDestination(m_oldDestination);
152 }
153
154protected:
Davide Pesavento1c9c96c2018-04-25 10:45:08 -0400155 boost::test_tools::output_test_stream os;
Junxiao Shi7d054272016-08-04 17:00:41 +0000156
157private:
dmcoomese062a182017-06-12 11:10:31 -0500158 std::unordered_map<std::string, LogLevel> m_oldEnabledLevel;
Alexander Afanasyev7b2f58c2019-07-13 11:50:19 -0400159 boost::shared_ptr<boost::log::sinks::sink> m_oldDestination;
Junxiao Shi7d054272016-08-04 17:00:41 +0000160};
161
162BOOST_AUTO_TEST_SUITE(Util)
163BOOST_FIXTURE_TEST_SUITE(TestLogging, LoggingFixture)
164
Alexander Afanasyev354f3822017-03-27 15:26:41 -0500165BOOST_AUTO_TEST_CASE(GetLoggerNames)
166{
Davide Pesavento1c9c96c2018-04-25 10:45:08 -0400167 // check that all names are registered from the start even if
168 // logger instances are lazily created on first use
169 auto n = Logging::getLoggerNames().size();
170 NDN_LOG_TRACE("GetLoggerNames");
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400171 auto names = Logging::getLoggerNames();
Davide Pesavento1c9c96c2018-04-25 10:45:08 -0400172 BOOST_CHECK_EQUAL(names.size(), n);
Alexander Afanasyev354f3822017-03-27 15:26:41 -0500173 BOOST_CHECK_EQUAL(names.count("ndn.util.tests.Logging"), 1);
174}
175
Junxiao Shi7d054272016-08-04 17:00:41 +0000176BOOST_AUTO_TEST_SUITE(Severity)
177
178BOOST_AUTO_TEST_CASE(None)
179{
180 Logging::setLevel("Module1", LogLevel::NONE);
181 logFromModule1();
182
183 Logging::flush();
184 BOOST_CHECK(os.is_equal(
185 LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n"
186 ));
187}
188
189BOOST_AUTO_TEST_CASE(Error)
190{
191 Logging::setLevel("Module1", LogLevel::ERROR);
192 logFromModule1();
193
194 Logging::flush();
195 BOOST_CHECK(os.is_equal(
196 LOG_SYSTIME_STR + " ERROR: [Module1] error1\n" +
197 LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n"
198 ));
199}
200
201BOOST_AUTO_TEST_CASE(Warn)
202{
203 Logging::setLevel("Module1", LogLevel::WARN);
204 logFromModule1();
205
206 Logging::flush();
207 BOOST_CHECK(os.is_equal(
Alexander Afanasyev576b4ff2019-07-05 22:06:04 -0400208 LOG_SYSTIME_STR + " WARN: [Module1] warn1\n" +
Junxiao Shi7d054272016-08-04 17:00:41 +0000209 LOG_SYSTIME_STR + " ERROR: [Module1] error1\n" +
210 LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n"
211 ));
212}
213
214BOOST_AUTO_TEST_CASE(Info)
215{
216 Logging::setLevel("Module1", LogLevel::INFO);
217 logFromModule1();
218
219 Logging::flush();
220 BOOST_CHECK(os.is_equal(
Alexander Afanasyev576b4ff2019-07-05 22:06:04 -0400221 LOG_SYSTIME_STR + " INFO: [Module1] info1\n" +
222 LOG_SYSTIME_STR + " WARN: [Module1] warn1\n" +
Junxiao Shi7d054272016-08-04 17:00:41 +0000223 LOG_SYSTIME_STR + " ERROR: [Module1] error1\n" +
224 LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n"
225 ));
226}
227
228BOOST_AUTO_TEST_CASE(Debug)
229{
230 Logging::setLevel("Module1", LogLevel::DEBUG);
231 logFromModule1();
232
233 Logging::flush();
234 BOOST_CHECK(os.is_equal(
235 LOG_SYSTIME_STR + " DEBUG: [Module1] debug1\n" +
Alexander Afanasyev576b4ff2019-07-05 22:06:04 -0400236 LOG_SYSTIME_STR + " INFO: [Module1] info1\n" +
237 LOG_SYSTIME_STR + " WARN: [Module1] warn1\n" +
Junxiao Shi7d054272016-08-04 17:00:41 +0000238 LOG_SYSTIME_STR + " ERROR: [Module1] error1\n" +
239 LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n"
240 ));
241}
242
243BOOST_AUTO_TEST_CASE(Trace)
244{
245 Logging::setLevel("Module1", LogLevel::TRACE);
246 logFromModule1();
247
248 Logging::flush();
249 BOOST_CHECK(os.is_equal(
250 LOG_SYSTIME_STR + " TRACE: [Module1] trace1\n" +
251 LOG_SYSTIME_STR + " DEBUG: [Module1] debug1\n" +
Alexander Afanasyev576b4ff2019-07-05 22:06:04 -0400252 LOG_SYSTIME_STR + " INFO: [Module1] info1\n" +
253 LOG_SYSTIME_STR + " WARN: [Module1] warn1\n" +
Junxiao Shi7d054272016-08-04 17:00:41 +0000254 LOG_SYSTIME_STR + " ERROR: [Module1] error1\n" +
255 LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n"
256 ));
257}
258
259BOOST_AUTO_TEST_CASE(All)
260{
261 Logging::setLevel("Module1", LogLevel::ALL);
262 logFromModule1();
263
264 Logging::flush();
265 BOOST_CHECK(os.is_equal(
266 LOG_SYSTIME_STR + " TRACE: [Module1] trace1\n" +
267 LOG_SYSTIME_STR + " DEBUG: [Module1] debug1\n" +
Alexander Afanasyev576b4ff2019-07-05 22:06:04 -0400268 LOG_SYSTIME_STR + " INFO: [Module1] info1\n" +
269 LOG_SYSTIME_STR + " WARN: [Module1] warn1\n" +
Junxiao Shi7d054272016-08-04 17:00:41 +0000270 LOG_SYSTIME_STR + " ERROR: [Module1] error1\n" +
271 LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n"
272 ));
273}
274
275BOOST_AUTO_TEST_SUITE_END() // Severity
276
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400277BOOST_AUTO_TEST_CASE(NamespaceLogger)
278{
279 Logging::setLevel("ndn.util.tests.ns1", LogLevel::INFO);
280 Logging::setLevel("ndn.util.tests.ns2", LogLevel::DEBUG);
281
282 const auto& levels = Logging::get().getLevels();
283 BOOST_CHECK_EQUAL(levels.size(), 2);
284 BOOST_CHECK_EQUAL(levels.at("ndn.util.tests.ns1"), LogLevel::INFO);
285 BOOST_CHECK_EQUAL(levels.at("ndn.util.tests.ns2"), LogLevel::DEBUG);
286
Davide Pesavento1c9c96c2018-04-25 10:45:08 -0400287 const auto& names = Logging::getLoggerNames();
288 BOOST_CHECK_EQUAL(names.count("ndn.util.tests.ns1"), 1);
289 BOOST_CHECK_EQUAL(names.count("ndn.util.tests.ns2"), 1);
290
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400291 ns1::logFromNamespace1();
292 ns2::logFromNamespace2();
293
294 Logging::flush();
295 BOOST_CHECK(os.is_equal(
Alexander Afanasyev576b4ff2019-07-05 22:06:04 -0400296 LOG_SYSTIME_STR + " INFO: [ndn.util.tests.ns1] hello world from ns1\n" +
297 LOG_SYSTIME_STR + " INFO: [ndn.util.tests.ns2] hi there from ns2\n"
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400298 ));
299}
300
301BOOST_AUTO_TEST_CASE(MemberLogger)
302{
303 Logging::setLevel("ndn.util.tests.ClassWithLogger", LogLevel::INFO);
304 Logging::setLevel("ndn.util.tests.Specialized1", LogLevel::INFO);
305 // ndn.util.tests.Specialized2 is not enabled
306
Davide Pesavento1c9c96c2018-04-25 10:45:08 -0400307 const auto& names = Logging::getLoggerNames();
308 BOOST_CHECK_EQUAL(names.count("ndn.util.tests.ClassWithLogger"), 1);
309 BOOST_CHECK_EQUAL(names.count("ndn.util.tests.Specialized1"), 1);
310 BOOST_CHECK_EQUAL(names.count("ndn.util.tests.Specialized2"), 1);
311
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400312 ClassWithLogger::logFromStaticMemberFunction();
313 ClassWithLogger{}.logFromConstMemberFunction();
314
315 Logging::flush();
316 BOOST_CHECK(os.is_equal(
Alexander Afanasyev576b4ff2019-07-05 22:06:04 -0400317 LOG_SYSTIME_STR + " INFO: [ndn.util.tests.ClassWithLogger] static member function\n" +
318 LOG_SYSTIME_STR + " INFO: [ndn.util.tests.ClassWithLogger] const member function\n"
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400319 ));
320
321 ClassTemplateWithLogger<int, double>::logFromStaticMemberFunction();
322 ClassTemplateWithLogger<int, double>{}.logFromMemberFunction();
323 ClassTemplateWithLogger<int, std::string>::logFromStaticMemberFunction();
324 ClassTemplateWithLogger<int, std::string>{}.logFromMemberFunction();
325
326 Logging::flush();
327 BOOST_CHECK(os.is_equal(
Alexander Afanasyev576b4ff2019-07-05 22:06:04 -0400328 LOG_SYSTIME_STR + " INFO: [ndn.util.tests.Specialized1] class template static member function\n" +
329 LOG_SYSTIME_STR + " INFO: [ndn.util.tests.Specialized1] class template non-static member function\n"
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400330 ));
331}
332
Junxiao Shi7d054272016-08-04 17:00:41 +0000333BOOST_AUTO_TEST_CASE(SameNameLoggers)
334{
335 Logging::setLevel("Module1", LogLevel::WARN);
336 logFromModule1();
337 logFromNewLogger("Module1");
338
Davide Pesavento1c9c96c2018-04-25 10:45:08 -0400339 BOOST_CHECK_EQUAL(Logging::getLoggerNames().count("Module1"), 1);
340
Junxiao Shi7d054272016-08-04 17:00:41 +0000341 Logging::flush();
342 BOOST_CHECK(os.is_equal(
Alexander Afanasyev576b4ff2019-07-05 22:06:04 -0400343 LOG_SYSTIME_STR + " WARN: [Module1] warn1\n" +
Junxiao Shi7d054272016-08-04 17:00:41 +0000344 LOG_SYSTIME_STR + " ERROR: [Module1] error1\n" +
345 LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" +
Alexander Afanasyev576b4ff2019-07-05 22:06:04 -0400346 LOG_SYSTIME_STR + " WARN: [Module1] warnModule1\n" +
Junxiao Shi7d054272016-08-04 17:00:41 +0000347 LOG_SYSTIME_STR + " ERROR: [Module1] errorModule1\n" +
348 LOG_SYSTIME_STR + " FATAL: [Module1] fatalModule1\n"
349 ));
350}
351
352BOOST_AUTO_TEST_CASE(LateRegistration)
353{
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400354 Logging::setLevel("Module3", LogLevel::DEBUG);
Junxiao Shi7d054272016-08-04 17:00:41 +0000355 logFromNewLogger("Module3");
356
357 Logging::flush();
358 BOOST_CHECK(os.is_equal(
359 LOG_SYSTIME_STR + " DEBUG: [Module3] debugModule3\n" +
Alexander Afanasyev576b4ff2019-07-05 22:06:04 -0400360 LOG_SYSTIME_STR + " INFO: [Module3] infoModule3\n" +
361 LOG_SYSTIME_STR + " WARN: [Module3] warnModule3\n" +
Junxiao Shi7d054272016-08-04 17:00:41 +0000362 LOG_SYSTIME_STR + " ERROR: [Module3] errorModule3\n" +
363 LOG_SYSTIME_STR + " FATAL: [Module3] fatalModule3\n"
364 ));
365}
366
367BOOST_AUTO_TEST_SUITE(DefaultSeverity)
368
369BOOST_AUTO_TEST_CASE(Unset)
370{
371 logFromModule1();
372 logFromModule2();
373
374 Logging::flush();
375 BOOST_CHECK(os.is_equal(
376 LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" +
377 LOG_SYSTIME_STR + " FATAL: [Module2] fatal2\n"
378 ));
379}
380
381BOOST_AUTO_TEST_CASE(NoOverride)
382{
383 Logging::setLevel("*", LogLevel::WARN);
384 logFromModule1();
385 logFromModule2();
386
387 Logging::flush();
388 BOOST_CHECK(os.is_equal(
Alexander Afanasyev576b4ff2019-07-05 22:06:04 -0400389 LOG_SYSTIME_STR + " WARN: [Module1] warn1\n" +
Junxiao Shi7d054272016-08-04 17:00:41 +0000390 LOG_SYSTIME_STR + " ERROR: [Module1] error1\n" +
391 LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" +
Alexander Afanasyev576b4ff2019-07-05 22:06:04 -0400392 LOG_SYSTIME_STR + " WARN: [Module2] warn2\n" +
Junxiao Shi7d054272016-08-04 17:00:41 +0000393 LOG_SYSTIME_STR + " ERROR: [Module2] error2\n" +
394 LOG_SYSTIME_STR + " FATAL: [Module2] fatal2\n"
395 ));
396}
397
398BOOST_AUTO_TEST_CASE(Override)
399{
400 Logging::setLevel("*", LogLevel::WARN);
401 Logging::setLevel("Module2", LogLevel::DEBUG);
402 logFromModule1();
403 logFromModule2();
404
405 Logging::flush();
406 BOOST_CHECK(os.is_equal(
Alexander Afanasyev576b4ff2019-07-05 22:06:04 -0400407 LOG_SYSTIME_STR + " WARN: [Module1] warn1\n" +
Junxiao Shi7d054272016-08-04 17:00:41 +0000408 LOG_SYSTIME_STR + " ERROR: [Module1] error1\n" +
409 LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" +
410 LOG_SYSTIME_STR + " DEBUG: [Module2] debug2\n" +
Alexander Afanasyev576b4ff2019-07-05 22:06:04 -0400411 LOG_SYSTIME_STR + " INFO: [Module2] info2\n" +
412 LOG_SYSTIME_STR + " WARN: [Module2] warn2\n" +
Junxiao Shi7d054272016-08-04 17:00:41 +0000413 LOG_SYSTIME_STR + " ERROR: [Module2] error2\n" +
414 LOG_SYSTIME_STR + " FATAL: [Module2] fatal2\n"
415 ));
416}
417
418BOOST_AUTO_TEST_SUITE_END() // DefaultSeverity
419
420BOOST_AUTO_TEST_SUITE(SeverityConfig)
421
422BOOST_AUTO_TEST_CASE(SetEmpty)
423{
424 Logging::setLevel("");
dmcoomese062a182017-06-12 11:10:31 -0500425 const auto& prefixMap = Logging::get().getLevels();
426 BOOST_CHECK_EQUAL(prefixMap.size(), 0);
Junxiao Shi7d054272016-08-04 17:00:41 +0000427 logFromModule1();
428 logFromModule2();
429
430 Logging::flush();
431 BOOST_CHECK(os.is_equal(
432 LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" +
433 LOG_SYSTIME_STR + " FATAL: [Module2] fatal2\n"
434 ));
435}
436
437BOOST_AUTO_TEST_CASE(SetDefault)
438{
439 Logging::setLevel("*=WARN");
dmcoomese062a182017-06-12 11:10:31 -0500440 const auto& prefixMap = Logging::get().getLevels();
441 // "*" is treated as "" internally
442 BOOST_CHECK_EQUAL(prefixMap.size(), 1);
443 BOOST_CHECK_EQUAL(prefixMap.at(""), LogLevel::WARN);
Junxiao Shi7d054272016-08-04 17:00:41 +0000444 logFromModule1();
445 logFromModule2();
446
447 Logging::flush();
448 BOOST_CHECK(os.is_equal(
Alexander Afanasyev576b4ff2019-07-05 22:06:04 -0400449 LOG_SYSTIME_STR + " WARN: [Module1] warn1\n" +
Junxiao Shi7d054272016-08-04 17:00:41 +0000450 LOG_SYSTIME_STR + " ERROR: [Module1] error1\n" +
451 LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" +
Alexander Afanasyev576b4ff2019-07-05 22:06:04 -0400452 LOG_SYSTIME_STR + " WARN: [Module2] warn2\n" +
Junxiao Shi7d054272016-08-04 17:00:41 +0000453 LOG_SYSTIME_STR + " ERROR: [Module2] error2\n" +
454 LOG_SYSTIME_STR + " FATAL: [Module2] fatal2\n"
455 ));
456}
457
458BOOST_AUTO_TEST_CASE(SetModule)
459{
460 Logging::setLevel("Module1=ERROR");
dmcoomese062a182017-06-12 11:10:31 -0500461 const auto& prefixMap = Logging::get().getLevels();
462 BOOST_CHECK_EQUAL(prefixMap.size(), 1);
463 BOOST_CHECK_EQUAL(prefixMap.at("Module1"), LogLevel::ERROR);
Junxiao Shi7d054272016-08-04 17:00:41 +0000464 logFromModule1();
465 logFromModule2();
466
467 Logging::flush();
468 BOOST_CHECK(os.is_equal(
469 LOG_SYSTIME_STR + " ERROR: [Module1] error1\n" +
470 LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" +
471 LOG_SYSTIME_STR + " FATAL: [Module2] fatal2\n"
472 ));
473}
474
475BOOST_AUTO_TEST_CASE(SetOverride)
476{
477 Logging::setLevel("*=WARN:Module2=DEBUG");
dmcoomese062a182017-06-12 11:10:31 -0500478 const auto& prefixMap = Logging::get().getLevels();
479 BOOST_CHECK_EQUAL(prefixMap.size(), 2);
480 // "*" is treated as "" internally
481 BOOST_CHECK_EQUAL(prefixMap.at(""), LogLevel::WARN);
482 BOOST_CHECK_EQUAL(prefixMap.at("Module2"), LogLevel::DEBUG);
Junxiao Shi7d054272016-08-04 17:00:41 +0000483 logFromModule1();
484 logFromModule2();
485
486 Logging::flush();
487 BOOST_CHECK(os.is_equal(
Alexander Afanasyev576b4ff2019-07-05 22:06:04 -0400488 LOG_SYSTIME_STR + " WARN: [Module1] warn1\n" +
Junxiao Shi7d054272016-08-04 17:00:41 +0000489 LOG_SYSTIME_STR + " ERROR: [Module1] error1\n" +
490 LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" +
491 LOG_SYSTIME_STR + " DEBUG: [Module2] debug2\n" +
Alexander Afanasyev576b4ff2019-07-05 22:06:04 -0400492 LOG_SYSTIME_STR + " INFO: [Module2] info2\n" +
493 LOG_SYSTIME_STR + " WARN: [Module2] warn2\n" +
Junxiao Shi7d054272016-08-04 17:00:41 +0000494 LOG_SYSTIME_STR + " ERROR: [Module2] error2\n" +
495 LOG_SYSTIME_STR + " FATAL: [Module2] fatal2\n"
496 ));
497}
498
499BOOST_AUTO_TEST_CASE(SetTwice)
500{
501 Logging::setLevel("*=WARN");
502 Logging::setLevel("Module2=DEBUG");
dmcoomese062a182017-06-12 11:10:31 -0500503 const auto& prefixMap = Logging::get().getLevels();
504 BOOST_CHECK_EQUAL(prefixMap.size(), 2);
505 // "*" is treated as "" internally
506 BOOST_CHECK_EQUAL(prefixMap.at(""), LogLevel::WARN);
507 BOOST_CHECK_EQUAL(prefixMap.at("Module2"), LogLevel::DEBUG);
Junxiao Shi7d054272016-08-04 17:00:41 +0000508 logFromModule1();
509 logFromModule2();
510
511 Logging::flush();
512 BOOST_CHECK(os.is_equal(
Alexander Afanasyev576b4ff2019-07-05 22:06:04 -0400513 LOG_SYSTIME_STR + " WARN: [Module1] warn1\n" +
Junxiao Shi7d054272016-08-04 17:00:41 +0000514 LOG_SYSTIME_STR + " ERROR: [Module1] error1\n" +
515 LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" +
516 LOG_SYSTIME_STR + " DEBUG: [Module2] debug2\n" +
Alexander Afanasyev576b4ff2019-07-05 22:06:04 -0400517 LOG_SYSTIME_STR + " INFO: [Module2] info2\n" +
518 LOG_SYSTIME_STR + " WARN: [Module2] warn2\n" +
Junxiao Shi7d054272016-08-04 17:00:41 +0000519 LOG_SYSTIME_STR + " ERROR: [Module2] error2\n" +
520 LOG_SYSTIME_STR + " FATAL: [Module2] fatal2\n"
521 ));
522}
523
524BOOST_AUTO_TEST_CASE(Reset)
525{
526 Logging::setLevel("Module2=DEBUG");
527 Logging::setLevel("*=ERROR");
dmcoomese062a182017-06-12 11:10:31 -0500528 const auto& prefixMap = Logging::get().getLevels();
529 BOOST_CHECK_EQUAL(prefixMap.size(), 1);
530 // "*" is treated as "" internally
531 BOOST_CHECK_EQUAL(prefixMap.at(""), LogLevel::ERROR);
Junxiao Shi7d054272016-08-04 17:00:41 +0000532 logFromModule1();
533 logFromModule2();
534
535 Logging::flush();
536 BOOST_CHECK(os.is_equal(
537 LOG_SYSTIME_STR + " ERROR: [Module1] error1\n" +
538 LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" +
539 LOG_SYSTIME_STR + " ERROR: [Module2] error2\n" +
540 LOG_SYSTIME_STR + " FATAL: [Module2] fatal2\n"
541 ));
542}
543
544BOOST_AUTO_TEST_CASE(Malformed)
545{
546 BOOST_CHECK_THROW(Logging::setLevel("Module1=INVALID-LEVEL"), std::invalid_argument);
547 BOOST_CHECK_THROW(Logging::setLevel("Module1-MISSING-EQUAL-SIGN"), std::invalid_argument);
548}
549
dmcoomese062a182017-06-12 11:10:31 -0500550BOOST_AUTO_TEST_CASE(SetFilter)
551{
552 Logging::setLevel("*=FATAL:fm.*=DEBUG");
553 const auto& prefixMap = Logging::get().getLevels();
554 BOOST_CHECK_EQUAL(prefixMap.size(), 2);
555 // "*" is treated as "" internally
556 BOOST_CHECK_EQUAL(prefixMap.at(""), LogLevel::FATAL);
557 // "name.*" is treated as "name." internally
558 BOOST_CHECK_EQUAL(prefixMap.at("fm."), LogLevel::DEBUG);
559 logFromModule1();
560 logFromFilterModule();
561
562 Logging::flush();
563 BOOST_CHECK(os.is_equal(
564 LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" +
565 LOG_SYSTIME_STR + " DEBUG: [fm.FilterModule] debugFM\n" +
Alexander Afanasyev576b4ff2019-07-05 22:06:04 -0400566 LOG_SYSTIME_STR + " INFO: [fm.FilterModule] infoFM\n" +
567 LOG_SYSTIME_STR + " WARN: [fm.FilterModule] warnFM\n" +
dmcoomese062a182017-06-12 11:10:31 -0500568 LOG_SYSTIME_STR + " ERROR: [fm.FilterModule] errorFM\n" +
569 LOG_SYSTIME_STR + " FATAL: [fm.FilterModule] fatalFM\n"
570 ));
571}
572
573BOOST_AUTO_TEST_CASE(SetOverrideFilter)
574{
575 Logging::setLevel("*=FATAL:fm.FilterModule=DEBUG");
576 Logging::setLevel("fm.*", LogLevel::INFO);
577 const auto& prefixMap = Logging::get().getLevels();
578 BOOST_CHECK_EQUAL(prefixMap.size(), 2);
579 // "*" is treated as "" internally
580 BOOST_CHECK_EQUAL(prefixMap.at(""), LogLevel::FATAL);
581 // "name.*" is treated as "name." internally
582 BOOST_CHECK_EQUAL(prefixMap.at("fm."), LogLevel::INFO);
583 logFromModule1();
584 logFromFilterModule();
585
586 Logging::flush();
587 BOOST_CHECK(os.is_equal(
588 LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" +
Alexander Afanasyev576b4ff2019-07-05 22:06:04 -0400589 LOG_SYSTIME_STR + " INFO: [fm.FilterModule] infoFM\n" +
590 LOG_SYSTIME_STR + " WARN: [fm.FilterModule] warnFM\n" +
dmcoomese062a182017-06-12 11:10:31 -0500591 LOG_SYSTIME_STR + " ERROR: [fm.FilterModule] errorFM\n" +
592 LOG_SYSTIME_STR + " FATAL: [fm.FilterModule] fatalFM\n"
593 ));
594}
595
596BOOST_AUTO_TEST_CASE(FindPrefixRule)
597{
598 Logging::setLevel("*=FATAL:fm.a.*=ERROR:fm.a.b=INFO");
599 logFromNewLogger("fm.a.b");
600 logFromNewLogger("fm.a.b.c");
601 logFromNewLogger("fm.a.b.d");
602 logFromNewLogger("fm.b");
603
604 Logging::flush();
605 BOOST_CHECK(os.is_equal(
Alexander Afanasyev576b4ff2019-07-05 22:06:04 -0400606 LOG_SYSTIME_STR + " INFO: [fm.a.b] infofm.a.b\n" +
607 LOG_SYSTIME_STR + " WARN: [fm.a.b] warnfm.a.b\n" +
dmcoomese062a182017-06-12 11:10:31 -0500608 LOG_SYSTIME_STR + " ERROR: [fm.a.b] errorfm.a.b\n" +
609 LOG_SYSTIME_STR + " FATAL: [fm.a.b] fatalfm.a.b\n" +
610 LOG_SYSTIME_STR + " ERROR: [fm.a.b.c] errorfm.a.b.c\n" +
611 LOG_SYSTIME_STR + " FATAL: [fm.a.b.c] fatalfm.a.b.c\n" +
612 LOG_SYSTIME_STR + " ERROR: [fm.a.b.d] errorfm.a.b.d\n" +
613 LOG_SYSTIME_STR + " FATAL: [fm.a.b.d] fatalfm.a.b.d\n" +
614 LOG_SYSTIME_STR + " FATAL: [fm.b] fatalfm.b\n"
615 ));
616}
617
Junxiao Shi7d054272016-08-04 17:00:41 +0000618BOOST_AUTO_TEST_SUITE_END() // SeverityConfig
619
620BOOST_AUTO_TEST_CASE(ChangeDestination)
621{
Davide Pesavento1c9c96c2018-04-25 10:45:08 -0400622 using boost::test_tools::output_test_stream;
623
Junxiao Shi7d054272016-08-04 17:00:41 +0000624 logFromModule1();
625
626 auto os2 = make_shared<output_test_stream>();
Alexander Afanasyev7b2f58c2019-07-13 11:50:19 -0400627 Logging::setDestination(Logging::makeDefaultStreamDestination(os2));
Junxiao Shi7d054272016-08-04 17:00:41 +0000628 weak_ptr<output_test_stream> os2weak(os2);
629 os2.reset();
630
631 logFromModule2();
632
633 Logging::flush();
634 os2 = os2weak.lock();
635 BOOST_REQUIRE(os2 != nullptr);
636
637 BOOST_CHECK(os.is_equal(
638 LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n"
639 ));
640 BOOST_CHECK(os2->is_equal(
641 LOG_SYSTIME_STR + " FATAL: [Module2] fatal2\n"
642 ));
643
644 os2.reset();
645 Logging::setDestination(os);
646 BOOST_CHECK(os2weak.expired());
647}
648
Alexander Afanasyev7b2f58c2019-07-13 11:50:19 -0400649BOOST_AUTO_TEST_CASE(SetNullptrDestination)
650{
651 Logging::setDestination(nullptr);
652 logFromModule1();
653
654 Logging::flush();
655 BOOST_CHECK(os.is_equal(""));
656 // The default Boost.Log output is still expected
657}
658
Junxiao Shi7d054272016-08-04 17:00:41 +0000659BOOST_AUTO_TEST_SUITE_END() // TestLogging
660BOOST_AUTO_TEST_SUITE_END() // Util
661
662} // namespace tests
663} // namespace util
664} // namespace ndn