blob: b2fb4c74c045e14467f1a9d2eed6e40199d058ca [file] [log] [blame]
Ilya Moiseenkoa807e652014-01-28 11:51:01 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventoa997d292017-08-24 20:16:59 -04002/*
3 * Copyright (c) 2014-2017, Regents of the University of California,
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -08004 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis.
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080010 *
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070011 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -080024 */
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080025
26#include "core/logger.hpp"
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060027
Davide Pesavento52a18f92014-04-10 00:55:01 +020028#include "tests/test-common.hpp"
29
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -060030#include <boost/algorithm/string/classification.hpp>
Davide Pesavento89567d32016-11-19 16:39:45 +010031#include <boost/algorithm/string/split.hpp>
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060032
Davide Pesaventoa997d292017-08-24 20:16:59 -040033#include <iostream>
34
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080035namespace nfd {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070036namespace tests {
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080037
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -080038BOOST_FIXTURE_TEST_SUITE(TestLogger, BaseFixture)
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080039
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060040class LoggerFixture : protected BaseFixture
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080041{
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060042public:
Alexander Afanasyev66886812014-01-31 14:48:48 -080043 LoggerFixture()
Steve DiBenedetto4d43a452014-04-06 18:55:29 -060044 : m_savedBuf(std::clog.rdbuf())
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060045 , m_savedLevel(LoggerFactory::getInstance().getDefaultLevel())
Alexander Afanasyev66886812014-01-31 14:48:48 -080046 {
Steve DiBenedetto4d43a452014-04-06 18:55:29 -060047 std::clog.rdbuf(m_buffer.rdbuf());
Alexander Afanasyev66886812014-01-31 14:48:48 -080048 }
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080049
Alexander Afanasyev66886812014-01-31 14:48:48 -080050 ~LoggerFixture()
51 {
Steve DiBenedetto4d43a452014-04-06 18:55:29 -060052 std::clog.rdbuf(m_savedBuf);
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060053 LoggerFactory::getInstance().setDefaultLevel(m_savedLevel);
Alexander Afanasyev66886812014-01-31 14:48:48 -080054 }
Alexander Afanasyevb84cc922014-02-24 17:52:58 -080055
Alexander Afanasyev66886812014-01-31 14:48:48 -080056 std::stringstream m_buffer;
57 std::streambuf* m_savedBuf;
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060058 LogLevel m_savedLevel;
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -060059
Alexander Afanasyev66886812014-01-31 14:48:48 -080060};
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080061
Alexander Afanasyev66886812014-01-31 14:48:48 -080062BOOST_FIXTURE_TEST_CASE(Basic, LoggerFixture)
63{
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -060064 using namespace ndn::time;
65 using std::string;
66
67 const ndn::time::microseconds::rep ONE_SECOND = 1000000;
68
Alexander Afanasyev66886812014-01-31 14:48:48 -080069 NFD_LOG_INIT("BasicTests");
Alexander Afanasyevb84cc922014-02-24 17:52:58 -080070 g_logger.setLogLevel(LOG_ALL);
71
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -060072 const string EXPECTED[] =
73 {
74 "TRACE:", "[BasicTests]", "trace-message-JHGFDSR^1\n",
75 "DEBUG:", "[BasicTests]", "debug-message-IGg2474fdksd-fo-151617\n",
76 "WARNING:", "[BasicTests]", "warning-message-XXXhdhd111x\n",
77 "INFO:", "[BasicTests]", "info-message-Jjxjshj13\n",
78 "ERROR:", "[BasicTests]", "error-message-!#$&^%$#@\n",
79 "FATAL:", "[BasicTests]", "fatal-message-JJSjaamcng\n",
80 };
Alexander Afanasyevb84cc922014-02-24 17:52:58 -080081
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -060082 const size_t N_EXPECTED = sizeof(EXPECTED) / sizeof(string);
83
84 microseconds::rep before =
85 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
86
87 NFD_LOG_TRACE("trace-message-JHGFDSR^1");
88 NFD_LOG_DEBUG("debug-message-IGg2474fdksd-fo-" << 15 << 16 << 17);
89 NFD_LOG_WARN("warning-message-XXXhdhd11" << 1 <<"x");
90 NFD_LOG_INFO("info-message-Jjxjshj13");
91 NFD_LOG_ERROR("error-message-!#$&^%$#@");
92 NFD_LOG_FATAL("fatal-message-JJSjaamcng");
93
94 microseconds::rep after =
95 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
96
Yumin Xiaab497452016-05-10 20:23:24 +080097 LoggerFactory::getInstance().flushBackend();
98
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -060099 const string buffer = m_buffer.str();
100
101 std::vector<string> components;
102 boost::split(components, buffer, boost::is_any_of(" ,\n"));
103
104 // std::cout << components.size() << " for " << moduleName << std::endl;
105 // for (size_t i = 0; i < components.size(); ++i)
106 // {
107 // std::cout << "-> " << components[i] << std::endl;
108 // }
109
110 // expected + number of timestamps (one per log statement) + trailing newline of last statement
111 BOOST_REQUIRE_EQUAL(components.size(), N_EXPECTED + 6 + 1);
112
113 std::vector<std::string>::const_iterator componentIter = components.begin();
114 for (size_t i = 0; i < N_EXPECTED; ++i)
115 {
116 // timestamp LOG_LEVEL: [ModuleName] message\n
117
118 const string& timestamp = *componentIter;
119 // std::cout << "timestamp = " << timestamp << std::endl;
120 ++componentIter;
121
122 size_t timeDelimiterPosition = timestamp.find(".");
123
124 BOOST_REQUIRE_NE(string::npos, timeDelimiterPosition);
125
126 string secondsString = timestamp.substr(0, timeDelimiterPosition);
127 string usecondsString = timestamp.substr(timeDelimiterPosition + 1);
128
129 microseconds::rep extractedTime =
130 ONE_SECOND * boost::lexical_cast<microseconds::rep>(secondsString) +
131 boost::lexical_cast<microseconds::rep>(usecondsString);
132
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600133 // std::cout << "before=" << before
134 // << " extracted=" << extractedTime
135 // << " after=" << after << std::endl;
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600136
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600137
138 BOOST_CHECK_LE(before, extractedTime);
139 BOOST_CHECK_LE(extractedTime, after);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600140
141 // LOG_LEVEL:
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600142 BOOST_CHECK_EQUAL(*componentIter, EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600143 ++componentIter;
144 ++i;
145
146 // [ModuleName]
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600147 BOOST_CHECK_EQUAL(*componentIter, EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600148 ++componentIter;
149 ++i;
150
151 const string& message = *componentIter;
152
153 // std::cout << "message = " << message << std::endl;
154
155 // add back the newline that we split on
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600156 BOOST_CHECK_EQUAL(message + "\n", EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600157 ++componentIter;
158 }
159
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800160}
161
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600162
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600163BOOST_FIXTURE_TEST_CASE(ConfigureFactory, LoggerFixture)
164{
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600165 using namespace ndn::time;
166 using std::string;
167
168 const ndn::time::microseconds::rep ONE_SECOND = 1000000;
169
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600170 NFD_LOG_INIT("ConfigureFactoryTests");
171
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600172 const string LOG_CONFIG =
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600173 "log\n"
174 "{\n"
175 " default_level INFO\n"
176 "}\n";
177
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600178 LoggerFactory::getInstance().setDefaultLevel(LOG_ALL);
179
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600180 ConfigFile config;
181 LoggerFactory::getInstance().setConfigFile(config);
182
183 config.parse(LOG_CONFIG, false, "LOG_CONFIG");
184
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600185 BOOST_REQUIRE_EQUAL(LoggerFactory::getInstance().getDefaultLevel(), LOG_INFO);
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600186
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600187 const std::string EXPECTED[] =
188 {
189 "WARNING:", "[ConfigureFactoryTests]", "warning-message-XXXhdhd111x\n",
190 "INFO:", "[ConfigureFactoryTests]", "info-message-Jjxjshj13\n",
191 "ERROR:", "[ConfigureFactoryTests]", "error-message-!#$&^%$#@\n",
192 "FATAL:", "[ConfigureFactoryTests]", "fatal-message-JJSjaamcng\n",
193 };
194
195 const size_t N_EXPECTED = sizeof(EXPECTED) / sizeof(std::string);
196
197 microseconds::rep before =
198 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
199
200 NFD_LOG_TRACE("trace-message-JHGFDSR^1");
201 NFD_LOG_DEBUG("debug-message-IGg2474fdksd-fo-" << 15 << 16 << 17);
202 NFD_LOG_WARN("warning-message-XXXhdhd11" << 1 <<"x");
203 NFD_LOG_INFO("info-message-Jjxjshj13");
204 NFD_LOG_ERROR("error-message-!#$&^%$#@");
205 NFD_LOG_FATAL("fatal-message-JJSjaamcng");
206
207 microseconds::rep after =
208 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
209
Yumin Xiaab497452016-05-10 20:23:24 +0800210 LoggerFactory::getInstance().flushBackend();
211
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600212 const string buffer = m_buffer.str();
213
214 std::vector<string> components;
215 boost::split(components, buffer, boost::is_any_of(" ,\n"));
216
217 // std::cout << components.size() << " for " << moduleName << std::endl;
218 // for (size_t i = 0; i < components.size(); ++i)
219 // {
220 // std::cout << "-> " << components[i] << std::endl;
221 // }
222
223 // expected + number of timestamps (one per log statement) + trailing newline of last statement
224 BOOST_REQUIRE_EQUAL(components.size(), N_EXPECTED + 4 + 1);
225
226 std::vector<std::string>::const_iterator componentIter = components.begin();
227 for (size_t i = 0; i < N_EXPECTED; ++i)
228 {
229 // timestamp LOG_LEVEL: [ModuleName] message\n
230
231 const string& timestamp = *componentIter;
232 // std::cout << "timestamp = " << timestamp << std::endl;
233 ++componentIter;
234
235 size_t timeDelimiterPosition = timestamp.find(".");
236
237 BOOST_REQUIRE_NE(string::npos, timeDelimiterPosition);
238
239 string secondsString = timestamp.substr(0, timeDelimiterPosition);
240 string usecondsString = timestamp.substr(timeDelimiterPosition + 1);
241
242 microseconds::rep extractedTime =
243 ONE_SECOND * boost::lexical_cast<microseconds::rep>(secondsString) +
244 boost::lexical_cast<microseconds::rep>(usecondsString);
245
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600246 // std::cout << "before=" << before
247 // << " extracted=" << extractedTime
248 // << " after=" << after << std::endl;
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600249
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600250 BOOST_CHECK_LE(before, extractedTime);
251 BOOST_CHECK_LE(extractedTime, after);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600252
253 // LOG_LEVEL:
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600254 BOOST_CHECK_EQUAL(*componentIter, EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600255 ++componentIter;
256 ++i;
257
258 // [ModuleName]
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600259 BOOST_CHECK_EQUAL(*componentIter, EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600260 ++componentIter;
261 ++i;
262
263 const string& message = *componentIter;
264
265 // std::cout << "message = " << message << std::endl;
266
267 // add back the newline that we split on
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600268 BOOST_CHECK_EQUAL(message + "\n", EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600269 ++componentIter;
270 }
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600271}
272
273BOOST_FIXTURE_TEST_CASE(TestNumberLevel, LoggerFixture)
274{
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600275 const std::string LOG_CONFIG =
276 "log\n"
277 "{\n"
278 " default_level 2\n" // equivalent of WARN
279 "}\n";
280
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600281 LoggerFactory::getInstance().setDefaultLevel(LOG_ALL);
282
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600283 ConfigFile config;
284 LoggerFactory::getInstance().setConfigFile(config);
285
286 config.parse(LOG_CONFIG, false, "LOG_CONFIG");
287
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600288 BOOST_REQUIRE_EQUAL(LoggerFactory::getInstance().getDefaultLevel(), LOG_WARN);
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600289}
290
291static void
292testModuleBPrint()
293{
294 NFD_LOG_INIT("TestModuleB");
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600295 NFD_LOG_DEBUG("debug-message-IGg2474fdksd-fo-" << 15 << 16 << 17);
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600296}
297
298BOOST_FIXTURE_TEST_CASE(LimitModules, LoggerFixture)
299{
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600300 using namespace ndn::time;
301 using std::string;
302
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600303 NFD_LOG_INIT("TestModuleA");
304
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600305 const ndn::time::microseconds::rep ONE_SECOND = 1000000;
306
307 const std::string EXPECTED[] =
308 {
309 "WARNING:", "[TestModuleA]", "warning-message-XXXhdhd111x\n",
310 };
311
312 const size_t N_EXPECTED = sizeof(EXPECTED) / sizeof(std::string);
313
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600314 const std::string LOG_CONFIG =
315 "log\n"
316 "{\n"
317 " default_level WARN\n"
318 "}\n";
319
320 ConfigFile config;
321 LoggerFactory::getInstance().setConfigFile(config);
322
323 config.parse(LOG_CONFIG, false, "LOG_CONFIG");
324
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600325 microseconds::rep before =
326 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
327
328 // this should print
329 NFD_LOG_WARN("warning-message-XXXhdhd11" << 1 << "x");
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600330
331 // this should not because it's level is < WARN
332 testModuleBPrint();
333
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600334 microseconds::rep after =
335 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
336
Yumin Xiaab497452016-05-10 20:23:24 +0800337 LoggerFactory::getInstance().flushBackend();
338
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600339 const string buffer = m_buffer.str();
340
341 std::vector<string> components;
342 boost::split(components, buffer, boost::is_any_of(" ,\n"));
343
344 // expected + number of timestamps (one per log statement) + trailing newline of last statement
345 BOOST_REQUIRE_EQUAL(components.size(), N_EXPECTED + 1 + 1);
346
347 std::vector<std::string>::const_iterator componentIter = components.begin();
348 for (size_t i = 0; i < N_EXPECTED; ++i)
349 {
350 // timestamp LOG_LEVEL: [ModuleName] message\n
351
352 const string& timestamp = *componentIter;
353 // std::cout << "timestamp = " << timestamp << std::endl;
354 ++componentIter;
355
356 size_t timeDelimiterPosition = timestamp.find(".");
357
358 BOOST_REQUIRE_NE(string::npos, timeDelimiterPosition);
359
360 string secondsString = timestamp.substr(0, timeDelimiterPosition);
361 string usecondsString = timestamp.substr(timeDelimiterPosition + 1);
362
363 microseconds::rep extractedTime =
364 ONE_SECOND * boost::lexical_cast<microseconds::rep>(secondsString) +
365 boost::lexical_cast<microseconds::rep>(usecondsString);
366
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600367 // std::cout << "before=" << before
368 // << " extracted=" << extractedTime
369 // << " after=" << after << std::endl;
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600370
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600371 BOOST_CHECK_LE(before, extractedTime);
372 BOOST_CHECK_LE(extractedTime, after);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600373
374 // LOG_LEVEL:
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600375 BOOST_CHECK_EQUAL(*componentIter, EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600376 ++componentIter;
377 ++i;
378
379 // [ModuleName]
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600380 BOOST_CHECK_EQUAL(*componentIter, EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600381 ++componentIter;
382 ++i;
383
384 const string& message = *componentIter;
385
386 // std::cout << "message = " << message << std::endl;
387
388 // add back the newline that we split on
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600389 BOOST_CHECK_EQUAL(message + "\n", EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600390 ++componentIter;
391 }
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600392}
393
394BOOST_FIXTURE_TEST_CASE(ExplicitlySetModule, LoggerFixture)
395{
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600396 using namespace ndn::time;
397 using std::string;
398
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600399 NFD_LOG_INIT("TestModuleA");
400
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600401 const ndn::time::microseconds::rep ONE_SECOND = 1000000;
402
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600403 const std::string LOG_CONFIG =
404 "log\n"
405 "{\n"
406 " default_level WARN\n"
407 " TestModuleB DEBUG\n"
408 "}\n";
409
410 ConfigFile config;
411 LoggerFactory::getInstance().setConfigFile(config);
412
413 config.parse(LOG_CONFIG, false, "LOG_CONFIG");
414
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600415 microseconds::rep before =
416 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
417
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600418 // this should print
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600419 NFD_LOG_WARN("warning-message-XXXhdhd11" << 1 << "x");
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600420
421 // this too because its level is explicitly set to DEBUG
422 testModuleBPrint();
423
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600424 microseconds::rep after =
425 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
426
427 const std::string EXPECTED[] =
428 {
429 "WARNING:", "[TestModuleA]", "warning-message-XXXhdhd111x\n",
430 "DEBUG:", "[TestModuleB]", "debug-message-IGg2474fdksd-fo-151617\n",
431 };
432
433 const size_t N_EXPECTED = sizeof(EXPECTED) / sizeof(std::string);
434
Yumin Xiaab497452016-05-10 20:23:24 +0800435 LoggerFactory::getInstance().flushBackend();
436
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600437 const string buffer = m_buffer.str();
438
439 std::vector<string> components;
440 boost::split(components, buffer, boost::is_any_of(" ,\n"));
441
442 // for (size_t i = 0; i < components.size(); ++i)
443 // {
444 // std::cout << "-> " << components[i] << std::endl;
445 // }
446
447 // expected + number of timestamps (one per log statement) + trailing newline of last statement
448 BOOST_REQUIRE_EQUAL(components.size(), N_EXPECTED + 2 + 1);
449
450 std::vector<std::string>::const_iterator componentIter = components.begin();
451 for (size_t i = 0; i < N_EXPECTED; ++i)
452 {
453 // timestamp LOG_LEVEL: [ModuleName] message\n
454
455 const string& timestamp = *componentIter;
456 // std::cout << "timestamp = " << timestamp << std::endl;
457 ++componentIter;
458
459 size_t timeDelimiterPosition = timestamp.find(".");
460
461 BOOST_REQUIRE_NE(string::npos, timeDelimiterPosition);
462
463 string secondsString = timestamp.substr(0, timeDelimiterPosition);
464 string usecondsString = timestamp.substr(timeDelimiterPosition + 1);
465
466 microseconds::rep extractedTime =
467 ONE_SECOND * boost::lexical_cast<microseconds::rep>(secondsString) +
468 boost::lexical_cast<microseconds::rep>(usecondsString);
469
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600470 // std::cout << "before=" << before
471 // << " extracted=" << extractedTime
472 // << " after=" << after << std::endl;
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600473
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600474 BOOST_CHECK_LE(before, extractedTime);
475 BOOST_CHECK_LE(extractedTime, after);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600476
477 // LOG_LEVEL:
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600478 BOOST_CHECK_EQUAL(*componentIter, EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600479 ++componentIter;
480 ++i;
481
482 // [ModuleName]
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600483 BOOST_CHECK_EQUAL(*componentIter, EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600484 ++componentIter;
485 ++i;
486
487 const string& message = *componentIter;
488
489 // std::cout << "message = " << message << std::endl;
490
491 // add back the newline that we split on
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600492 BOOST_CHECK_EQUAL(message + "\n", EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600493 ++componentIter;
494 }
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600495}
496
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600497BOOST_FIXTURE_TEST_CASE(UnknownModule, LoggerFixture)
498{
499 using namespace ndn::time;
500 using std::string;
501
502 const ndn::time::microseconds::rep ONE_SECOND = 1000000;
503
504 const std::string LOG_CONFIG =
505 "log\n"
506 "{\n"
Alexander Afanasyev5959b012014-06-02 19:18:12 +0300507 " default_level DEBUG\n"
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600508 " TestMadeUpModule INFO\n"
509 "}\n";
510
511 ConfigFile config;
512 LoggerFactory::getInstance().setDefaultLevel(LOG_ALL);
513 LoggerFactory::getInstance().setConfigFile(config);
514
515 const std::string EXPECTED = "DEBUG: [LoggerFactory] "
516 "Failed to configure logging level for module \"TestMadeUpModule\" (module not found)\n";
517
518 microseconds::rep before =
519 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
520
521 config.parse(LOG_CONFIG, false, "LOG_CONFIG");
522
523 microseconds::rep after =
524 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
525
Yumin Xiaab497452016-05-10 20:23:24 +0800526 LoggerFactory::getInstance().flushBackend();
527
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600528 const string buffer = m_buffer.str();
529
530 const size_t firstSpace = buffer.find(" ");
531 BOOST_REQUIRE(firstSpace != string::npos);
532
533 const string timestamp = buffer.substr(0, firstSpace);
534 const string message = buffer.substr(firstSpace + 1);
535
536 size_t timeDelimiterPosition = timestamp.find(".");
537
538 BOOST_REQUIRE_NE(string::npos, timeDelimiterPosition);
539
540 string secondsString = timestamp.substr(0, timeDelimiterPosition);
541 string usecondsString = timestamp.substr(timeDelimiterPosition + 1);
542
543 microseconds::rep extractedTime =
544 ONE_SECOND * boost::lexical_cast<microseconds::rep>(secondsString) +
545 boost::lexical_cast<microseconds::rep>(usecondsString);
546
547 // std::cout << "before=" << before
548 // << " extracted=" << extractedTime
549 // << " after=" << after << std::endl;
550
551 BOOST_CHECK_LE(before, extractedTime);
552 BOOST_CHECK_LE(extractedTime, after);
553
554 BOOST_CHECK_EQUAL(message, EXPECTED);
555}
556
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600557static bool
558checkError(const LoggerFactory::Error& error, const std::string& expected)
559{
560 return error.what() == expected;
561}
562
563BOOST_FIXTURE_TEST_CASE(UnknownLevelString, LoggerFixture)
564{
565 const std::string LOG_CONFIG =
566 "log\n"
567 "{\n"
568 " default_level TestMadeUpLevel\n"
569 "}\n";
570
571 ConfigFile config;
572 LoggerFactory::getInstance().setConfigFile(config);
573
574 BOOST_REQUIRE_EXCEPTION(config.parse(LOG_CONFIG, false, "LOG_CONFIG"),
575 LoggerFactory::Error,
576 bind(&checkError,
577 _1,
578 "Unsupported logging level \"TestMadeUpLevel\""));
579}
580
Alexander Afanasyev66886812014-01-31 14:48:48 -0800581class InClassLogger : public LoggerFixture
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800582{
Alexander Afanasyev66886812014-01-31 14:48:48 -0800583public:
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600584
Alexander Afanasyevb84cc922014-02-24 17:52:58 -0800585 InClassLogger()
586 {
587 g_logger.setLogLevel(LOG_ALL);
588 }
589
Alexander Afanasyev66886812014-01-31 14:48:48 -0800590 void
591 writeLogs()
592 {
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600593 NFD_LOG_TRACE("trace-message-JHGFDSR^1");
594 NFD_LOG_DEBUG("debug-message-IGg2474fdksd-fo-" << 15 << 16 << 17);
595 NFD_LOG_WARN("warning-message-XXXhdhd11" << 1 <<"x");
596 NFD_LOG_INFO("info-message-Jjxjshj13");
597 NFD_LOG_ERROR("error-message-!#$&^%$#@");
598 NFD_LOG_FATAL("fatal-message-JJSjaamcng");
Alexander Afanasyev66886812014-01-31 14:48:48 -0800599 }
Alexander Afanasyevb84cc922014-02-24 17:52:58 -0800600
Alexander Afanasyev66886812014-01-31 14:48:48 -0800601private:
602 NFD_LOG_INCLASS_DECLARE();
603};
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800604
Alexander Afanasyev66886812014-01-31 14:48:48 -0800605NFD_LOG_INCLASS_DEFINE(InClassLogger, "InClassLogger");
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800606
Alexander Afanasyev66886812014-01-31 14:48:48 -0800607BOOST_FIXTURE_TEST_CASE(InClass, InClassLogger)
608{
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600609 using namespace ndn::time;
610 using std::string;
611
612 const ndn::time::microseconds::rep ONE_SECOND = 1000000;
613
614 microseconds::rep before =
615 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
616
Alexander Afanasyev66886812014-01-31 14:48:48 -0800617 writeLogs();
Alexander Afanasyevb84cc922014-02-24 17:52:58 -0800618
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600619 microseconds::rep after =
620 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
621
622 const string EXPECTED[] =
623 {
624 "TRACE:", "[InClassLogger]", "trace-message-JHGFDSR^1\n",
625 "DEBUG:", "[InClassLogger]", "debug-message-IGg2474fdksd-fo-151617\n",
626 "WARNING:", "[InClassLogger]", "warning-message-XXXhdhd111x\n",
627 "INFO:", "[InClassLogger]", "info-message-Jjxjshj13\n",
628 "ERROR:", "[InClassLogger]", "error-message-!#$&^%$#@\n",
629 "FATAL:", "[InClassLogger]", "fatal-message-JJSjaamcng\n",
630 };
631
632 const size_t N_EXPECTED = sizeof(EXPECTED) / sizeof(string);
633
Yumin Xiaab497452016-05-10 20:23:24 +0800634 LoggerFactory::getInstance().flushBackend();
635
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600636 const string buffer = m_buffer.str();
637
638 std::vector<string> components;
639 boost::split(components, buffer, boost::is_any_of(" ,\n"));
640
641 // expected + number of timestamps (one per log statement) + trailing newline of last statement
642 BOOST_REQUIRE_EQUAL(components.size(), N_EXPECTED + 6 + 1);
643
644 std::vector<std::string>::const_iterator componentIter = components.begin();
645 for (size_t i = 0; i < N_EXPECTED; ++i)
646 {
647 // timestamp LOG_LEVEL: [ModuleName] message\n
648
649 const string& timestamp = *componentIter;
650 // std::cout << "timestamp = " << timestamp << std::endl;
651 ++componentIter;
652
653 size_t timeDelimiterPosition = timestamp.find(".");
654
655 BOOST_REQUIRE_NE(string::npos, timeDelimiterPosition);
656
657 string secondsString = timestamp.substr(0, timeDelimiterPosition);
658 string usecondsString = timestamp.substr(timeDelimiterPosition + 1);
659
660 microseconds::rep extractedTime =
661 ONE_SECOND * boost::lexical_cast<microseconds::rep>(secondsString) +
662 boost::lexical_cast<microseconds::rep>(usecondsString);
663
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600664 // std::cout << "before=" << before
665 // << " extracted=" << extractedTime
666 // << " after=" << after << std::endl;
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600667
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600668 BOOST_CHECK_LE(before, extractedTime);
669 BOOST_CHECK_LE(extractedTime, after);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600670
671 // LOG_LEVEL:
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600672 BOOST_CHECK_EQUAL(*componentIter, EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600673 ++componentIter;
674 ++i;
675
676 // [ModuleName]
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600677 BOOST_CHECK_EQUAL(*componentIter, EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600678 ++componentIter;
679 ++i;
680
681 const string& message = *componentIter;
682
683 // std::cout << "message = " << message << std::endl;
684
685 // add back the newline that we split on
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600686 BOOST_CHECK_EQUAL(message + "\n", EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600687 ++componentIter;
688 }
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800689}
690
Alexander Afanasyev66886812014-01-31 14:48:48 -0800691
692template<class T>
693class InClassTemplateLogger : public LoggerFixture
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800694{
Alexander Afanasyev66886812014-01-31 14:48:48 -0800695public:
Alexander Afanasyevb84cc922014-02-24 17:52:58 -0800696 InClassTemplateLogger()
697 {
698 g_logger.setLogLevel(LOG_ALL);
699 }
700
Alexander Afanasyev66886812014-01-31 14:48:48 -0800701 void
702 writeLogs()
703 {
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600704 NFD_LOG_TRACE("trace-message-JHGFDSR^1");
705 NFD_LOG_DEBUG("debug-message-IGg2474fdksd-fo-" << 15 << 16 << 17);
706 NFD_LOG_WARN("warning-message-XXXhdhd11" << 1 <<"x");
707 NFD_LOG_INFO("info-message-Jjxjshj13");
708 NFD_LOG_ERROR("error-message-!#$&^%$#@");
709 NFD_LOG_FATAL("fatal-message-JJSjaamcng");
Alexander Afanasyev66886812014-01-31 14:48:48 -0800710 }
Alexander Afanasyevb84cc922014-02-24 17:52:58 -0800711
Alexander Afanasyev66886812014-01-31 14:48:48 -0800712private:
713 NFD_LOG_INCLASS_DECLARE();
714};
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800715
Alexander Afanasyev66886812014-01-31 14:48:48 -0800716NFD_LOG_INCLASS_TEMPLATE_DEFINE(InClassTemplateLogger, "GenericInClassTemplateLogger");
717NFD_LOG_INCLASS_TEMPLATE_SPECIALIZATION_DEFINE(InClassTemplateLogger, int, "IntInClassLogger");
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800718
Alexander Afanasyev66886812014-01-31 14:48:48 -0800719BOOST_FIXTURE_TEST_CASE(GenericInTemplatedClass, InClassTemplateLogger<bool>)
720{
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600721 using namespace ndn::time;
722 using std::string;
723
724 const ndn::time::microseconds::rep ONE_SECOND = 1000000;
725
726 microseconds::rep before =
727 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
728
Alexander Afanasyev66886812014-01-31 14:48:48 -0800729 writeLogs();
Alexander Afanasyevb84cc922014-02-24 17:52:58 -0800730
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600731 microseconds::rep after =
732 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
733
734 const string EXPECTED[] =
735 {
736 "TRACE:", "[GenericInClassTemplateLogger]", "trace-message-JHGFDSR^1\n",
737 "DEBUG:", "[GenericInClassTemplateLogger]", "debug-message-IGg2474fdksd-fo-151617\n",
738 "WARNING:", "[GenericInClassTemplateLogger]", "warning-message-XXXhdhd111x\n",
739 "INFO:", "[GenericInClassTemplateLogger]", "info-message-Jjxjshj13\n",
740 "ERROR:", "[GenericInClassTemplateLogger]", "error-message-!#$&^%$#@\n",
741 "FATAL:", "[GenericInClassTemplateLogger]", "fatal-message-JJSjaamcng\n",
742 };
743
744 const size_t N_EXPECTED = sizeof(EXPECTED) / sizeof(string);
745
Yumin Xiaab497452016-05-10 20:23:24 +0800746 LoggerFactory::getInstance().flushBackend();
747
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600748 const string buffer = m_buffer.str();
749
750 std::vector<string> components;
751 boost::split(components, buffer, boost::is_any_of(" ,\n"));
752
753 // expected + number of timestamps (one per log statement) + trailing newline of last statement
754 BOOST_REQUIRE_EQUAL(components.size(), N_EXPECTED + 6 + 1);
755
756 std::vector<std::string>::const_iterator componentIter = components.begin();
757 for (size_t i = 0; i < N_EXPECTED; ++i)
758 {
759 // timestamp LOG_LEVEL: [ModuleName] message\n
760
761 const string& timestamp = *componentIter;
762 // std::cout << "timestamp = " << timestamp << std::endl;
763 ++componentIter;
764
765 size_t timeDelimiterPosition = timestamp.find(".");
766
767 BOOST_REQUIRE_NE(string::npos, timeDelimiterPosition);
768
769 string secondsString = timestamp.substr(0, timeDelimiterPosition);
770 string usecondsString = timestamp.substr(timeDelimiterPosition + 1);
771
772 microseconds::rep extractedTime =
773 ONE_SECOND * boost::lexical_cast<microseconds::rep>(secondsString) +
774 boost::lexical_cast<microseconds::rep>(usecondsString);
775
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600776 // std::cout << "before=" << before
777 // << " extracted=" << extractedTime
778 // << " after=" << after << std::endl;
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600779
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600780 BOOST_CHECK_LE(before, extractedTime);
781 BOOST_CHECK_LE(extractedTime, after);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600782
783 // LOG_LEVEL:
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600784 BOOST_CHECK_EQUAL(*componentIter, EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600785 ++componentIter;
786 ++i;
787
788 // [ModuleName]
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600789 BOOST_CHECK_EQUAL(*componentIter, EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600790 ++componentIter;
791 ++i;
792
793 const string& message = *componentIter;
794
795 // std::cout << "message = " << message << std::endl;
796
797 // add back the newline that we split on
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600798 BOOST_CHECK_EQUAL(message + "\n", EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600799 ++componentIter;
800 }
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800801}
802
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600803
Alexander Afanasyev66886812014-01-31 14:48:48 -0800804BOOST_FIXTURE_TEST_CASE(SpecializedInTemplatedClass, InClassTemplateLogger<int>)
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800805{
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600806 using namespace ndn::time;
807 using std::string;
808
809 const ndn::time::microseconds::rep ONE_SECOND = 1000000;
810
811 microseconds::rep before =
812 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
813
Alexander Afanasyev66886812014-01-31 14:48:48 -0800814 writeLogs();
Alexander Afanasyevb84cc922014-02-24 17:52:58 -0800815
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600816 microseconds::rep after =
817 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
818
819 const string EXPECTED[] =
820 {
821 "TRACE:", "[IntInClassLogger]", "trace-message-JHGFDSR^1\n",
822 "DEBUG:", "[IntInClassLogger]", "debug-message-IGg2474fdksd-fo-151617\n",
823 "WARNING:", "[IntInClassLogger]", "warning-message-XXXhdhd111x\n",
824 "INFO:", "[IntInClassLogger]", "info-message-Jjxjshj13\n",
825 "ERROR:", "[IntInClassLogger]", "error-message-!#$&^%$#@\n",
826 "FATAL:", "[IntInClassLogger]", "fatal-message-JJSjaamcng\n",
827 };
828
829 const size_t N_EXPECTED = sizeof(EXPECTED) / sizeof(string);
830
Yumin Xiaab497452016-05-10 20:23:24 +0800831 LoggerFactory::getInstance().flushBackend();
832
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600833 const string buffer = m_buffer.str();
834
835 std::vector<string> components;
836 boost::split(components, buffer, boost::is_any_of(" ,\n"));
837
838 // expected + number of timestamps (one per log statement) + trailing newline of last statement
839 BOOST_REQUIRE_EQUAL(components.size(), N_EXPECTED + 6 + 1);
840
841 std::vector<std::string>::const_iterator componentIter = components.begin();
842 for (size_t i = 0; i < N_EXPECTED; ++i)
843 {
844 // timestamp LOG_LEVEL: [ModuleName] message\n
845
846 const string& timestamp = *componentIter;
847 // std::cout << "timestamp = " << timestamp << std::endl;
848 ++componentIter;
849
850 size_t timeDelimiterPosition = timestamp.find(".");
851
852 BOOST_REQUIRE_NE(string::npos, timeDelimiterPosition);
853
854 string secondsString = timestamp.substr(0, timeDelimiterPosition);
855 string usecondsString = timestamp.substr(timeDelimiterPosition + 1);
856
857 microseconds::rep extractedTime =
858 ONE_SECOND * boost::lexical_cast<microseconds::rep>(secondsString) +
859 boost::lexical_cast<microseconds::rep>(usecondsString);
860
861 // std::cout << "before=" << before << " extracted=" << extractedTime << " after=" << after << std::endl;
862
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600863 BOOST_CHECK_LE(before, extractedTime);
864 BOOST_CHECK_LE(extractedTime, after);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600865
866 // LOG_LEVEL:
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600867 BOOST_CHECK_EQUAL(*componentIter, EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600868 ++componentIter;
869 ++i;
870
871 // [ModuleName]
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600872 BOOST_CHECK_EQUAL(*componentIter, EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600873 ++componentIter;
874 ++i;
875
876 const string& message = *componentIter;
877
878 // std::cout << "message = " << message << std::endl;
879
880 // add back the newline that we split on
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600881 BOOST_CHECK_EQUAL(message + "\n", EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600882 ++componentIter;
883 }
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800884}
885
Alexander Afanasyev6b34ab92015-02-11 21:22:46 -0800886BOOST_FIXTURE_TEST_CASE(LoggerFactoryListModules, LoggerFixture)
887{
888 std::set<std::string> testCaseLoggers{"LoggerFactoryListModules1", "LoggerFactoryListModules2"};
889
890 for (const auto& name : testCaseLoggers) {
891 LoggerFactory::create(name);
892 }
893
894 auto&& modules = LoggerFactory::getInstance().getModules();
895 BOOST_CHECK_GE(modules.size(), 2);
896
897 for (const auto& name : modules) {
898 testCaseLoggers.erase(name);
899 }
900
901 BOOST_CHECK_EQUAL(testCaseLoggers.size(), 0);
902}
903
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800904BOOST_AUTO_TEST_SUITE_END()
905
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700906} // namespace tests
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800907} // namespace nfd
Alexander Afanasyev6ca6cf72016-06-15 13:22:16 -0700908
909// Testing compilation of the logger outside ::nfd namespace
910
911namespace test_logger { // another root namespace
912
913void
914Test1()
915{
916 NFD_LOG_INIT("Test");
917
918 NFD_LOG_TRACE("Trace");
919 NFD_LOG_DEBUG("Debug");
920 NFD_LOG_INFO("Info");
921 NFD_LOG_WARN("Warn");
922 NFD_LOG_ERROR("Error");
923 NFD_LOG_FATAL("Fatal");
924}
925
926class Test2
927{
928 NFD_LOG_INCLASS_DECLARE();
929};
930
931NFD_LOG_INCLASS_DEFINE(Test2, "Test2");
932
933template<class T>
934class Test3
935{
936 NFD_LOG_INCLASS_DECLARE();
937};
938
939NFD_LOG_INCLASS_TEMPLATE_DEFINE(Test3, "Test3");
940
941NFD_LOG_INCLASS_TEMPLATE_SPECIALIZATION_DEFINE(Test3, int, "Test3Int");
942
943template<class X, class Y>
944class Test4
945{
946 NFD_LOG_INCLASS_DECLARE();
947};
948
949NFD_LOG_INCLASS_2TEMPLATE_SPECIALIZATION_DEFINE(Test4, int, int, "Test4IntInt");
950
951namespace nfd { // nested nfd namespace, different from ::nfd
952
953void
954Test1()
955{
956 NFD_LOG_INIT("Test");
957
958 NFD_LOG_TRACE("Trace");
959 NFD_LOG_DEBUG("Debug");
960 NFD_LOG_INFO("Info");
961 NFD_LOG_WARN("Warn");
962 NFD_LOG_ERROR("Error");
963 NFD_LOG_FATAL("Fatal");
964}
965
966class Test2
967{
968 NFD_LOG_INCLASS_DECLARE();
969};
970
971NFD_LOG_INCLASS_DEFINE(Test2, "Test2");
972
973template<class T>
974class Test3
975{
976 NFD_LOG_INCLASS_DECLARE();
977};
978
979NFD_LOG_INCLASS_TEMPLATE_DEFINE(Test3, "Test3");
980
981NFD_LOG_INCLASS_TEMPLATE_SPECIALIZATION_DEFINE(Test3, int, "Test3Int");
982
983template<class X, class Y>
984class Test4
985{
986 NFD_LOG_INCLASS_DECLARE();
987};
988
989NFD_LOG_INCLASS_2TEMPLATE_SPECIALIZATION_DEFINE(Test4, int, int, "Test4IntInt");
990
991} // namespace nfd
992} // namespace test_logger