blob: 11dcfa410039d07135d6efef9f315e459a1787db [file] [log] [blame]
Ilya Moiseenkoa807e652014-01-28 11:51:01 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Yumin Xiaab497452016-05-10 20:23:24 +08003 * Copyright (c) 2014-2016, 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
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080033namespace nfd {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070034namespace tests {
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080035
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -080036BOOST_FIXTURE_TEST_SUITE(TestLogger, BaseFixture)
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080037
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060038class LoggerFixture : protected BaseFixture
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080039{
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060040public:
Alexander Afanasyev66886812014-01-31 14:48:48 -080041 LoggerFixture()
Steve DiBenedetto4d43a452014-04-06 18:55:29 -060042 : m_savedBuf(std::clog.rdbuf())
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060043 , m_savedLevel(LoggerFactory::getInstance().getDefaultLevel())
Alexander Afanasyev66886812014-01-31 14:48:48 -080044 {
Steve DiBenedetto4d43a452014-04-06 18:55:29 -060045 std::clog.rdbuf(m_buffer.rdbuf());
Alexander Afanasyev66886812014-01-31 14:48:48 -080046 }
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080047
Alexander Afanasyev66886812014-01-31 14:48:48 -080048 ~LoggerFixture()
49 {
Steve DiBenedetto4d43a452014-04-06 18:55:29 -060050 std::clog.rdbuf(m_savedBuf);
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060051 LoggerFactory::getInstance().setDefaultLevel(m_savedLevel);
Alexander Afanasyev66886812014-01-31 14:48:48 -080052 }
Alexander Afanasyevb84cc922014-02-24 17:52:58 -080053
Alexander Afanasyev66886812014-01-31 14:48:48 -080054 std::stringstream m_buffer;
55 std::streambuf* m_savedBuf;
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060056 LogLevel m_savedLevel;
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -060057
Alexander Afanasyev66886812014-01-31 14:48:48 -080058};
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080059
Alexander Afanasyev66886812014-01-31 14:48:48 -080060BOOST_FIXTURE_TEST_CASE(Basic, LoggerFixture)
61{
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -060062 using namespace ndn::time;
63 using std::string;
64
65 const ndn::time::microseconds::rep ONE_SECOND = 1000000;
66
Alexander Afanasyev66886812014-01-31 14:48:48 -080067 NFD_LOG_INIT("BasicTests");
Alexander Afanasyevb84cc922014-02-24 17:52:58 -080068 g_logger.setLogLevel(LOG_ALL);
69
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -060070 const string EXPECTED[] =
71 {
72 "TRACE:", "[BasicTests]", "trace-message-JHGFDSR^1\n",
73 "DEBUG:", "[BasicTests]", "debug-message-IGg2474fdksd-fo-151617\n",
74 "WARNING:", "[BasicTests]", "warning-message-XXXhdhd111x\n",
75 "INFO:", "[BasicTests]", "info-message-Jjxjshj13\n",
76 "ERROR:", "[BasicTests]", "error-message-!#$&^%$#@\n",
77 "FATAL:", "[BasicTests]", "fatal-message-JJSjaamcng\n",
78 };
Alexander Afanasyevb84cc922014-02-24 17:52:58 -080079
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -060080 const size_t N_EXPECTED = sizeof(EXPECTED) / sizeof(string);
81
82 microseconds::rep before =
83 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
84
85 NFD_LOG_TRACE("trace-message-JHGFDSR^1");
86 NFD_LOG_DEBUG("debug-message-IGg2474fdksd-fo-" << 15 << 16 << 17);
87 NFD_LOG_WARN("warning-message-XXXhdhd11" << 1 <<"x");
88 NFD_LOG_INFO("info-message-Jjxjshj13");
89 NFD_LOG_ERROR("error-message-!#$&^%$#@");
90 NFD_LOG_FATAL("fatal-message-JJSjaamcng");
91
92 microseconds::rep after =
93 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
94
Yumin Xiaab497452016-05-10 20:23:24 +080095 LoggerFactory::getInstance().flushBackend();
96
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -060097 const string buffer = m_buffer.str();
98
99 std::vector<string> components;
100 boost::split(components, buffer, boost::is_any_of(" ,\n"));
101
102 // std::cout << components.size() << " for " << moduleName << std::endl;
103 // for (size_t i = 0; i < components.size(); ++i)
104 // {
105 // std::cout << "-> " << components[i] << std::endl;
106 // }
107
108 // expected + number of timestamps (one per log statement) + trailing newline of last statement
109 BOOST_REQUIRE_EQUAL(components.size(), N_EXPECTED + 6 + 1);
110
111 std::vector<std::string>::const_iterator componentIter = components.begin();
112 for (size_t i = 0; i < N_EXPECTED; ++i)
113 {
114 // timestamp LOG_LEVEL: [ModuleName] message\n
115
116 const string& timestamp = *componentIter;
117 // std::cout << "timestamp = " << timestamp << std::endl;
118 ++componentIter;
119
120 size_t timeDelimiterPosition = timestamp.find(".");
121
122 BOOST_REQUIRE_NE(string::npos, timeDelimiterPosition);
123
124 string secondsString = timestamp.substr(0, timeDelimiterPosition);
125 string usecondsString = timestamp.substr(timeDelimiterPosition + 1);
126
127 microseconds::rep extractedTime =
128 ONE_SECOND * boost::lexical_cast<microseconds::rep>(secondsString) +
129 boost::lexical_cast<microseconds::rep>(usecondsString);
130
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600131 // std::cout << "before=" << before
132 // << " extracted=" << extractedTime
133 // << " after=" << after << std::endl;
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600134
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600135
136 BOOST_CHECK_LE(before, extractedTime);
137 BOOST_CHECK_LE(extractedTime, after);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600138
139 // LOG_LEVEL:
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600140 BOOST_CHECK_EQUAL(*componentIter, EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600141 ++componentIter;
142 ++i;
143
144 // [ModuleName]
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600145 BOOST_CHECK_EQUAL(*componentIter, EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600146 ++componentIter;
147 ++i;
148
149 const string& message = *componentIter;
150
151 // std::cout << "message = " << message << std::endl;
152
153 // add back the newline that we split on
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600154 BOOST_CHECK_EQUAL(message + "\n", EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600155 ++componentIter;
156 }
157
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800158}
159
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600160
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600161BOOST_FIXTURE_TEST_CASE(ConfigureFactory, LoggerFixture)
162{
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600163 using namespace ndn::time;
164 using std::string;
165
166 const ndn::time::microseconds::rep ONE_SECOND = 1000000;
167
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600168 NFD_LOG_INIT("ConfigureFactoryTests");
169
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600170 const string LOG_CONFIG =
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600171 "log\n"
172 "{\n"
173 " default_level INFO\n"
174 "}\n";
175
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600176 LoggerFactory::getInstance().setDefaultLevel(LOG_ALL);
177
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600178 ConfigFile config;
179 LoggerFactory::getInstance().setConfigFile(config);
180
181 config.parse(LOG_CONFIG, false, "LOG_CONFIG");
182
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600183 BOOST_REQUIRE_EQUAL(LoggerFactory::getInstance().getDefaultLevel(), LOG_INFO);
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600184
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600185 const std::string EXPECTED[] =
186 {
187 "WARNING:", "[ConfigureFactoryTests]", "warning-message-XXXhdhd111x\n",
188 "INFO:", "[ConfigureFactoryTests]", "info-message-Jjxjshj13\n",
189 "ERROR:", "[ConfigureFactoryTests]", "error-message-!#$&^%$#@\n",
190 "FATAL:", "[ConfigureFactoryTests]", "fatal-message-JJSjaamcng\n",
191 };
192
193 const size_t N_EXPECTED = sizeof(EXPECTED) / sizeof(std::string);
194
195 microseconds::rep before =
196 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
197
198 NFD_LOG_TRACE("trace-message-JHGFDSR^1");
199 NFD_LOG_DEBUG("debug-message-IGg2474fdksd-fo-" << 15 << 16 << 17);
200 NFD_LOG_WARN("warning-message-XXXhdhd11" << 1 <<"x");
201 NFD_LOG_INFO("info-message-Jjxjshj13");
202 NFD_LOG_ERROR("error-message-!#$&^%$#@");
203 NFD_LOG_FATAL("fatal-message-JJSjaamcng");
204
205 microseconds::rep after =
206 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
207
Yumin Xiaab497452016-05-10 20:23:24 +0800208 LoggerFactory::getInstance().flushBackend();
209
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600210 const string buffer = m_buffer.str();
211
212 std::vector<string> components;
213 boost::split(components, buffer, boost::is_any_of(" ,\n"));
214
215 // std::cout << components.size() << " for " << moduleName << std::endl;
216 // for (size_t i = 0; i < components.size(); ++i)
217 // {
218 // std::cout << "-> " << components[i] << std::endl;
219 // }
220
221 // expected + number of timestamps (one per log statement) + trailing newline of last statement
222 BOOST_REQUIRE_EQUAL(components.size(), N_EXPECTED + 4 + 1);
223
224 std::vector<std::string>::const_iterator componentIter = components.begin();
225 for (size_t i = 0; i < N_EXPECTED; ++i)
226 {
227 // timestamp LOG_LEVEL: [ModuleName] message\n
228
229 const string& timestamp = *componentIter;
230 // std::cout << "timestamp = " << timestamp << std::endl;
231 ++componentIter;
232
233 size_t timeDelimiterPosition = timestamp.find(".");
234
235 BOOST_REQUIRE_NE(string::npos, timeDelimiterPosition);
236
237 string secondsString = timestamp.substr(0, timeDelimiterPosition);
238 string usecondsString = timestamp.substr(timeDelimiterPosition + 1);
239
240 microseconds::rep extractedTime =
241 ONE_SECOND * boost::lexical_cast<microseconds::rep>(secondsString) +
242 boost::lexical_cast<microseconds::rep>(usecondsString);
243
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600244 // std::cout << "before=" << before
245 // << " extracted=" << extractedTime
246 // << " after=" << after << std::endl;
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600247
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600248 BOOST_CHECK_LE(before, extractedTime);
249 BOOST_CHECK_LE(extractedTime, after);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600250
251 // LOG_LEVEL:
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600252 BOOST_CHECK_EQUAL(*componentIter, EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600253 ++componentIter;
254 ++i;
255
256 // [ModuleName]
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600257 BOOST_CHECK_EQUAL(*componentIter, EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600258 ++componentIter;
259 ++i;
260
261 const string& message = *componentIter;
262
263 // std::cout << "message = " << message << std::endl;
264
265 // add back the newline that we split on
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600266 BOOST_CHECK_EQUAL(message + "\n", EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600267 ++componentIter;
268 }
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600269}
270
271BOOST_FIXTURE_TEST_CASE(TestNumberLevel, LoggerFixture)
272{
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600273 const std::string LOG_CONFIG =
274 "log\n"
275 "{\n"
276 " default_level 2\n" // equivalent of WARN
277 "}\n";
278
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600279 LoggerFactory::getInstance().setDefaultLevel(LOG_ALL);
280
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600281 ConfigFile config;
282 LoggerFactory::getInstance().setConfigFile(config);
283
284 config.parse(LOG_CONFIG, false, "LOG_CONFIG");
285
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600286 BOOST_REQUIRE_EQUAL(LoggerFactory::getInstance().getDefaultLevel(), LOG_WARN);
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600287}
288
289static void
290testModuleBPrint()
291{
292 NFD_LOG_INIT("TestModuleB");
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600293 NFD_LOG_DEBUG("debug-message-IGg2474fdksd-fo-" << 15 << 16 << 17);
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600294}
295
296BOOST_FIXTURE_TEST_CASE(LimitModules, LoggerFixture)
297{
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600298 using namespace ndn::time;
299 using std::string;
300
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600301 NFD_LOG_INIT("TestModuleA");
302
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600303 const ndn::time::microseconds::rep ONE_SECOND = 1000000;
304
305 const std::string EXPECTED[] =
306 {
307 "WARNING:", "[TestModuleA]", "warning-message-XXXhdhd111x\n",
308 };
309
310 const size_t N_EXPECTED = sizeof(EXPECTED) / sizeof(std::string);
311
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600312 const std::string LOG_CONFIG =
313 "log\n"
314 "{\n"
315 " default_level WARN\n"
316 "}\n";
317
318 ConfigFile config;
319 LoggerFactory::getInstance().setConfigFile(config);
320
321 config.parse(LOG_CONFIG, false, "LOG_CONFIG");
322
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600323 microseconds::rep before =
324 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
325
326 // this should print
327 NFD_LOG_WARN("warning-message-XXXhdhd11" << 1 << "x");
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600328
329 // this should not because it's level is < WARN
330 testModuleBPrint();
331
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600332 microseconds::rep after =
333 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
334
Yumin Xiaab497452016-05-10 20:23:24 +0800335 LoggerFactory::getInstance().flushBackend();
336
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600337 const string buffer = m_buffer.str();
338
339 std::vector<string> components;
340 boost::split(components, buffer, boost::is_any_of(" ,\n"));
341
342 // expected + number of timestamps (one per log statement) + trailing newline of last statement
343 BOOST_REQUIRE_EQUAL(components.size(), N_EXPECTED + 1 + 1);
344
345 std::vector<std::string>::const_iterator componentIter = components.begin();
346 for (size_t i = 0; i < N_EXPECTED; ++i)
347 {
348 // timestamp LOG_LEVEL: [ModuleName] message\n
349
350 const string& timestamp = *componentIter;
351 // std::cout << "timestamp = " << timestamp << std::endl;
352 ++componentIter;
353
354 size_t timeDelimiterPosition = timestamp.find(".");
355
356 BOOST_REQUIRE_NE(string::npos, timeDelimiterPosition);
357
358 string secondsString = timestamp.substr(0, timeDelimiterPosition);
359 string usecondsString = timestamp.substr(timeDelimiterPosition + 1);
360
361 microseconds::rep extractedTime =
362 ONE_SECOND * boost::lexical_cast<microseconds::rep>(secondsString) +
363 boost::lexical_cast<microseconds::rep>(usecondsString);
364
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600365 // std::cout << "before=" << before
366 // << " extracted=" << extractedTime
367 // << " after=" << after << std::endl;
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600368
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600369 BOOST_CHECK_LE(before, extractedTime);
370 BOOST_CHECK_LE(extractedTime, after);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600371
372 // LOG_LEVEL:
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600373 BOOST_CHECK_EQUAL(*componentIter, EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600374 ++componentIter;
375 ++i;
376
377 // [ModuleName]
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600378 BOOST_CHECK_EQUAL(*componentIter, EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600379 ++componentIter;
380 ++i;
381
382 const string& message = *componentIter;
383
384 // std::cout << "message = " << message << std::endl;
385
386 // add back the newline that we split on
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600387 BOOST_CHECK_EQUAL(message + "\n", EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600388 ++componentIter;
389 }
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600390}
391
392BOOST_FIXTURE_TEST_CASE(ExplicitlySetModule, LoggerFixture)
393{
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600394 using namespace ndn::time;
395 using std::string;
396
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600397 NFD_LOG_INIT("TestModuleA");
398
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600399 const ndn::time::microseconds::rep ONE_SECOND = 1000000;
400
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600401 const std::string LOG_CONFIG =
402 "log\n"
403 "{\n"
404 " default_level WARN\n"
405 " TestModuleB DEBUG\n"
406 "}\n";
407
408 ConfigFile config;
409 LoggerFactory::getInstance().setConfigFile(config);
410
411 config.parse(LOG_CONFIG, false, "LOG_CONFIG");
412
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600413 microseconds::rep before =
414 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
415
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600416 // this should print
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600417 NFD_LOG_WARN("warning-message-XXXhdhd11" << 1 << "x");
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600418
419 // this too because its level is explicitly set to DEBUG
420 testModuleBPrint();
421
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600422 microseconds::rep after =
423 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
424
425 const std::string EXPECTED[] =
426 {
427 "WARNING:", "[TestModuleA]", "warning-message-XXXhdhd111x\n",
428 "DEBUG:", "[TestModuleB]", "debug-message-IGg2474fdksd-fo-151617\n",
429 };
430
431 const size_t N_EXPECTED = sizeof(EXPECTED) / sizeof(std::string);
432
Yumin Xiaab497452016-05-10 20:23:24 +0800433 LoggerFactory::getInstance().flushBackend();
434
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600435 const string buffer = m_buffer.str();
436
437 std::vector<string> components;
438 boost::split(components, buffer, boost::is_any_of(" ,\n"));
439
440 // for (size_t i = 0; i < components.size(); ++i)
441 // {
442 // std::cout << "-> " << components[i] << std::endl;
443 // }
444
445 // expected + number of timestamps (one per log statement) + trailing newline of last statement
446 BOOST_REQUIRE_EQUAL(components.size(), N_EXPECTED + 2 + 1);
447
448 std::vector<std::string>::const_iterator componentIter = components.begin();
449 for (size_t i = 0; i < N_EXPECTED; ++i)
450 {
451 // timestamp LOG_LEVEL: [ModuleName] message\n
452
453 const string& timestamp = *componentIter;
454 // std::cout << "timestamp = " << timestamp << std::endl;
455 ++componentIter;
456
457 size_t timeDelimiterPosition = timestamp.find(".");
458
459 BOOST_REQUIRE_NE(string::npos, timeDelimiterPosition);
460
461 string secondsString = timestamp.substr(0, timeDelimiterPosition);
462 string usecondsString = timestamp.substr(timeDelimiterPosition + 1);
463
464 microseconds::rep extractedTime =
465 ONE_SECOND * boost::lexical_cast<microseconds::rep>(secondsString) +
466 boost::lexical_cast<microseconds::rep>(usecondsString);
467
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600468 // std::cout << "before=" << before
469 // << " extracted=" << extractedTime
470 // << " after=" << after << std::endl;
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600471
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600472 BOOST_CHECK_LE(before, extractedTime);
473 BOOST_CHECK_LE(extractedTime, after);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600474
475 // LOG_LEVEL:
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600476 BOOST_CHECK_EQUAL(*componentIter, EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600477 ++componentIter;
478 ++i;
479
480 // [ModuleName]
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600481 BOOST_CHECK_EQUAL(*componentIter, EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600482 ++componentIter;
483 ++i;
484
485 const string& message = *componentIter;
486
487 // std::cout << "message = " << message << std::endl;
488
489 // add back the newline that we split on
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600490 BOOST_CHECK_EQUAL(message + "\n", EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600491 ++componentIter;
492 }
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600493}
494
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600495BOOST_FIXTURE_TEST_CASE(UnknownModule, LoggerFixture)
496{
497 using namespace ndn::time;
498 using std::string;
499
500 const ndn::time::microseconds::rep ONE_SECOND = 1000000;
501
502 const std::string LOG_CONFIG =
503 "log\n"
504 "{\n"
Alexander Afanasyev5959b012014-06-02 19:18:12 +0300505 " default_level DEBUG\n"
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600506 " TestMadeUpModule INFO\n"
507 "}\n";
508
509 ConfigFile config;
510 LoggerFactory::getInstance().setDefaultLevel(LOG_ALL);
511 LoggerFactory::getInstance().setConfigFile(config);
512
513 const std::string EXPECTED = "DEBUG: [LoggerFactory] "
514 "Failed to configure logging level for module \"TestMadeUpModule\" (module not found)\n";
515
516 microseconds::rep before =
517 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
518
519 config.parse(LOG_CONFIG, false, "LOG_CONFIG");
520
521 microseconds::rep after =
522 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
523
Yumin Xiaab497452016-05-10 20:23:24 +0800524 LoggerFactory::getInstance().flushBackend();
525
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600526 const string buffer = m_buffer.str();
527
528 const size_t firstSpace = buffer.find(" ");
529 BOOST_REQUIRE(firstSpace != string::npos);
530
531 const string timestamp = buffer.substr(0, firstSpace);
532 const string message = buffer.substr(firstSpace + 1);
533
534 size_t timeDelimiterPosition = timestamp.find(".");
535
536 BOOST_REQUIRE_NE(string::npos, timeDelimiterPosition);
537
538 string secondsString = timestamp.substr(0, timeDelimiterPosition);
539 string usecondsString = timestamp.substr(timeDelimiterPosition + 1);
540
541 microseconds::rep extractedTime =
542 ONE_SECOND * boost::lexical_cast<microseconds::rep>(secondsString) +
543 boost::lexical_cast<microseconds::rep>(usecondsString);
544
545 // std::cout << "before=" << before
546 // << " extracted=" << extractedTime
547 // << " after=" << after << std::endl;
548
549 BOOST_CHECK_LE(before, extractedTime);
550 BOOST_CHECK_LE(extractedTime, after);
551
552 BOOST_CHECK_EQUAL(message, EXPECTED);
553}
554
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600555static bool
556checkError(const LoggerFactory::Error& error, const std::string& expected)
557{
558 return error.what() == expected;
559}
560
561BOOST_FIXTURE_TEST_CASE(UnknownLevelString, LoggerFixture)
562{
563 const std::string LOG_CONFIG =
564 "log\n"
565 "{\n"
566 " default_level TestMadeUpLevel\n"
567 "}\n";
568
569 ConfigFile config;
570 LoggerFactory::getInstance().setConfigFile(config);
571
572 BOOST_REQUIRE_EXCEPTION(config.parse(LOG_CONFIG, false, "LOG_CONFIG"),
573 LoggerFactory::Error,
574 bind(&checkError,
575 _1,
576 "Unsupported logging level \"TestMadeUpLevel\""));
577}
578
Alexander Afanasyev66886812014-01-31 14:48:48 -0800579class InClassLogger : public LoggerFixture
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800580{
Alexander Afanasyev66886812014-01-31 14:48:48 -0800581public:
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600582
Alexander Afanasyevb84cc922014-02-24 17:52:58 -0800583 InClassLogger()
584 {
585 g_logger.setLogLevel(LOG_ALL);
586 }
587
Alexander Afanasyev66886812014-01-31 14:48:48 -0800588 void
589 writeLogs()
590 {
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600591 NFD_LOG_TRACE("trace-message-JHGFDSR^1");
592 NFD_LOG_DEBUG("debug-message-IGg2474fdksd-fo-" << 15 << 16 << 17);
593 NFD_LOG_WARN("warning-message-XXXhdhd11" << 1 <<"x");
594 NFD_LOG_INFO("info-message-Jjxjshj13");
595 NFD_LOG_ERROR("error-message-!#$&^%$#@");
596 NFD_LOG_FATAL("fatal-message-JJSjaamcng");
Alexander Afanasyev66886812014-01-31 14:48:48 -0800597 }
Alexander Afanasyevb84cc922014-02-24 17:52:58 -0800598
Alexander Afanasyev66886812014-01-31 14:48:48 -0800599private:
600 NFD_LOG_INCLASS_DECLARE();
601};
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800602
Alexander Afanasyev66886812014-01-31 14:48:48 -0800603NFD_LOG_INCLASS_DEFINE(InClassLogger, "InClassLogger");
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800604
Alexander Afanasyev66886812014-01-31 14:48:48 -0800605BOOST_FIXTURE_TEST_CASE(InClass, InClassLogger)
606{
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600607 using namespace ndn::time;
608 using std::string;
609
610 const ndn::time::microseconds::rep ONE_SECOND = 1000000;
611
612 microseconds::rep before =
613 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
614
Alexander Afanasyev66886812014-01-31 14:48:48 -0800615 writeLogs();
Alexander Afanasyevb84cc922014-02-24 17:52:58 -0800616
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600617 microseconds::rep after =
618 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
619
620 const string EXPECTED[] =
621 {
622 "TRACE:", "[InClassLogger]", "trace-message-JHGFDSR^1\n",
623 "DEBUG:", "[InClassLogger]", "debug-message-IGg2474fdksd-fo-151617\n",
624 "WARNING:", "[InClassLogger]", "warning-message-XXXhdhd111x\n",
625 "INFO:", "[InClassLogger]", "info-message-Jjxjshj13\n",
626 "ERROR:", "[InClassLogger]", "error-message-!#$&^%$#@\n",
627 "FATAL:", "[InClassLogger]", "fatal-message-JJSjaamcng\n",
628 };
629
630 const size_t N_EXPECTED = sizeof(EXPECTED) / sizeof(string);
631
Yumin Xiaab497452016-05-10 20:23:24 +0800632 LoggerFactory::getInstance().flushBackend();
633
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600634 const string buffer = m_buffer.str();
635
636 std::vector<string> components;
637 boost::split(components, buffer, boost::is_any_of(" ,\n"));
638
639 // expected + number of timestamps (one per log statement) + trailing newline of last statement
640 BOOST_REQUIRE_EQUAL(components.size(), N_EXPECTED + 6 + 1);
641
642 std::vector<std::string>::const_iterator componentIter = components.begin();
643 for (size_t i = 0; i < N_EXPECTED; ++i)
644 {
645 // timestamp LOG_LEVEL: [ModuleName] message\n
646
647 const string& timestamp = *componentIter;
648 // std::cout << "timestamp = " << timestamp << std::endl;
649 ++componentIter;
650
651 size_t timeDelimiterPosition = timestamp.find(".");
652
653 BOOST_REQUIRE_NE(string::npos, timeDelimiterPosition);
654
655 string secondsString = timestamp.substr(0, timeDelimiterPosition);
656 string usecondsString = timestamp.substr(timeDelimiterPosition + 1);
657
658 microseconds::rep extractedTime =
659 ONE_SECOND * boost::lexical_cast<microseconds::rep>(secondsString) +
660 boost::lexical_cast<microseconds::rep>(usecondsString);
661
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600662 // std::cout << "before=" << before
663 // << " extracted=" << extractedTime
664 // << " after=" << after << std::endl;
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600665
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600666 BOOST_CHECK_LE(before, extractedTime);
667 BOOST_CHECK_LE(extractedTime, after);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600668
669 // LOG_LEVEL:
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600670 BOOST_CHECK_EQUAL(*componentIter, EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600671 ++componentIter;
672 ++i;
673
674 // [ModuleName]
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600675 BOOST_CHECK_EQUAL(*componentIter, EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600676 ++componentIter;
677 ++i;
678
679 const string& message = *componentIter;
680
681 // std::cout << "message = " << message << std::endl;
682
683 // add back the newline that we split on
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600684 BOOST_CHECK_EQUAL(message + "\n", EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600685 ++componentIter;
686 }
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800687}
688
Alexander Afanasyev66886812014-01-31 14:48:48 -0800689
690template<class T>
691class InClassTemplateLogger : public LoggerFixture
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800692{
Alexander Afanasyev66886812014-01-31 14:48:48 -0800693public:
Alexander Afanasyevb84cc922014-02-24 17:52:58 -0800694 InClassTemplateLogger()
695 {
696 g_logger.setLogLevel(LOG_ALL);
697 }
698
Alexander Afanasyev66886812014-01-31 14:48:48 -0800699 void
700 writeLogs()
701 {
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600702 NFD_LOG_TRACE("trace-message-JHGFDSR^1");
703 NFD_LOG_DEBUG("debug-message-IGg2474fdksd-fo-" << 15 << 16 << 17);
704 NFD_LOG_WARN("warning-message-XXXhdhd11" << 1 <<"x");
705 NFD_LOG_INFO("info-message-Jjxjshj13");
706 NFD_LOG_ERROR("error-message-!#$&^%$#@");
707 NFD_LOG_FATAL("fatal-message-JJSjaamcng");
Alexander Afanasyev66886812014-01-31 14:48:48 -0800708 }
Alexander Afanasyevb84cc922014-02-24 17:52:58 -0800709
Alexander Afanasyev66886812014-01-31 14:48:48 -0800710private:
711 NFD_LOG_INCLASS_DECLARE();
712};
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800713
Alexander Afanasyev66886812014-01-31 14:48:48 -0800714NFD_LOG_INCLASS_TEMPLATE_DEFINE(InClassTemplateLogger, "GenericInClassTemplateLogger");
715NFD_LOG_INCLASS_TEMPLATE_SPECIALIZATION_DEFINE(InClassTemplateLogger, int, "IntInClassLogger");
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800716
Alexander Afanasyev66886812014-01-31 14:48:48 -0800717BOOST_FIXTURE_TEST_CASE(GenericInTemplatedClass, InClassTemplateLogger<bool>)
718{
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600719 using namespace ndn::time;
720 using std::string;
721
722 const ndn::time::microseconds::rep ONE_SECOND = 1000000;
723
724 microseconds::rep before =
725 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
726
Alexander Afanasyev66886812014-01-31 14:48:48 -0800727 writeLogs();
Alexander Afanasyevb84cc922014-02-24 17:52:58 -0800728
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600729 microseconds::rep after =
730 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
731
732 const string EXPECTED[] =
733 {
734 "TRACE:", "[GenericInClassTemplateLogger]", "trace-message-JHGFDSR^1\n",
735 "DEBUG:", "[GenericInClassTemplateLogger]", "debug-message-IGg2474fdksd-fo-151617\n",
736 "WARNING:", "[GenericInClassTemplateLogger]", "warning-message-XXXhdhd111x\n",
737 "INFO:", "[GenericInClassTemplateLogger]", "info-message-Jjxjshj13\n",
738 "ERROR:", "[GenericInClassTemplateLogger]", "error-message-!#$&^%$#@\n",
739 "FATAL:", "[GenericInClassTemplateLogger]", "fatal-message-JJSjaamcng\n",
740 };
741
742 const size_t N_EXPECTED = sizeof(EXPECTED) / sizeof(string);
743
Yumin Xiaab497452016-05-10 20:23:24 +0800744 LoggerFactory::getInstance().flushBackend();
745
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600746 const string buffer = m_buffer.str();
747
748 std::vector<string> components;
749 boost::split(components, buffer, boost::is_any_of(" ,\n"));
750
751 // expected + number of timestamps (one per log statement) + trailing newline of last statement
752 BOOST_REQUIRE_EQUAL(components.size(), N_EXPECTED + 6 + 1);
753
754 std::vector<std::string>::const_iterator componentIter = components.begin();
755 for (size_t i = 0; i < N_EXPECTED; ++i)
756 {
757 // timestamp LOG_LEVEL: [ModuleName] message\n
758
759 const string& timestamp = *componentIter;
760 // std::cout << "timestamp = " << timestamp << std::endl;
761 ++componentIter;
762
763 size_t timeDelimiterPosition = timestamp.find(".");
764
765 BOOST_REQUIRE_NE(string::npos, timeDelimiterPosition);
766
767 string secondsString = timestamp.substr(0, timeDelimiterPosition);
768 string usecondsString = timestamp.substr(timeDelimiterPosition + 1);
769
770 microseconds::rep extractedTime =
771 ONE_SECOND * boost::lexical_cast<microseconds::rep>(secondsString) +
772 boost::lexical_cast<microseconds::rep>(usecondsString);
773
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600774 // std::cout << "before=" << before
775 // << " extracted=" << extractedTime
776 // << " after=" << after << std::endl;
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600777
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600778 BOOST_CHECK_LE(before, extractedTime);
779 BOOST_CHECK_LE(extractedTime, after);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600780
781 // LOG_LEVEL:
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600782 BOOST_CHECK_EQUAL(*componentIter, EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600783 ++componentIter;
784 ++i;
785
786 // [ModuleName]
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600787 BOOST_CHECK_EQUAL(*componentIter, EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600788 ++componentIter;
789 ++i;
790
791 const string& message = *componentIter;
792
793 // std::cout << "message = " << message << std::endl;
794
795 // add back the newline that we split on
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600796 BOOST_CHECK_EQUAL(message + "\n", EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600797 ++componentIter;
798 }
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800799}
800
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600801
Alexander Afanasyev66886812014-01-31 14:48:48 -0800802BOOST_FIXTURE_TEST_CASE(SpecializedInTemplatedClass, InClassTemplateLogger<int>)
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800803{
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600804 using namespace ndn::time;
805 using std::string;
806
807 const ndn::time::microseconds::rep ONE_SECOND = 1000000;
808
809 microseconds::rep before =
810 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
811
Alexander Afanasyev66886812014-01-31 14:48:48 -0800812 writeLogs();
Alexander Afanasyevb84cc922014-02-24 17:52:58 -0800813
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600814 microseconds::rep after =
815 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
816
817 const string EXPECTED[] =
818 {
819 "TRACE:", "[IntInClassLogger]", "trace-message-JHGFDSR^1\n",
820 "DEBUG:", "[IntInClassLogger]", "debug-message-IGg2474fdksd-fo-151617\n",
821 "WARNING:", "[IntInClassLogger]", "warning-message-XXXhdhd111x\n",
822 "INFO:", "[IntInClassLogger]", "info-message-Jjxjshj13\n",
823 "ERROR:", "[IntInClassLogger]", "error-message-!#$&^%$#@\n",
824 "FATAL:", "[IntInClassLogger]", "fatal-message-JJSjaamcng\n",
825 };
826
827 const size_t N_EXPECTED = sizeof(EXPECTED) / sizeof(string);
828
Yumin Xiaab497452016-05-10 20:23:24 +0800829 LoggerFactory::getInstance().flushBackend();
830
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600831 const string buffer = m_buffer.str();
832
833 std::vector<string> components;
834 boost::split(components, buffer, boost::is_any_of(" ,\n"));
835
836 // expected + number of timestamps (one per log statement) + trailing newline of last statement
837 BOOST_REQUIRE_EQUAL(components.size(), N_EXPECTED + 6 + 1);
838
839 std::vector<std::string>::const_iterator componentIter = components.begin();
840 for (size_t i = 0; i < N_EXPECTED; ++i)
841 {
842 // timestamp LOG_LEVEL: [ModuleName] message\n
843
844 const string& timestamp = *componentIter;
845 // std::cout << "timestamp = " << timestamp << std::endl;
846 ++componentIter;
847
848 size_t timeDelimiterPosition = timestamp.find(".");
849
850 BOOST_REQUIRE_NE(string::npos, timeDelimiterPosition);
851
852 string secondsString = timestamp.substr(0, timeDelimiterPosition);
853 string usecondsString = timestamp.substr(timeDelimiterPosition + 1);
854
855 microseconds::rep extractedTime =
856 ONE_SECOND * boost::lexical_cast<microseconds::rep>(secondsString) +
857 boost::lexical_cast<microseconds::rep>(usecondsString);
858
859 // std::cout << "before=" << before << " extracted=" << extractedTime << " after=" << after << std::endl;
860
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600861 BOOST_CHECK_LE(before, extractedTime);
862 BOOST_CHECK_LE(extractedTime, after);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600863
864 // LOG_LEVEL:
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600865 BOOST_CHECK_EQUAL(*componentIter, EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600866 ++componentIter;
867 ++i;
868
869 // [ModuleName]
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600870 BOOST_CHECK_EQUAL(*componentIter, EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600871 ++componentIter;
872 ++i;
873
874 const string& message = *componentIter;
875
876 // std::cout << "message = " << message << std::endl;
877
878 // add back the newline that we split on
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600879 BOOST_CHECK_EQUAL(message + "\n", EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600880 ++componentIter;
881 }
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800882}
883
Alexander Afanasyev6b34ab92015-02-11 21:22:46 -0800884BOOST_FIXTURE_TEST_CASE(LoggerFactoryListModules, LoggerFixture)
885{
886 std::set<std::string> testCaseLoggers{"LoggerFactoryListModules1", "LoggerFactoryListModules2"};
887
888 for (const auto& name : testCaseLoggers) {
889 LoggerFactory::create(name);
890 }
891
892 auto&& modules = LoggerFactory::getInstance().getModules();
893 BOOST_CHECK_GE(modules.size(), 2);
894
895 for (const auto& name : modules) {
896 testCaseLoggers.erase(name);
897 }
898
899 BOOST_CHECK_EQUAL(testCaseLoggers.size(), 0);
900}
901
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800902BOOST_AUTO_TEST_SUITE_END()
903
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700904} // namespace tests
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800905} // namespace nfd
Alexander Afanasyev6ca6cf72016-06-15 13:22:16 -0700906
907// Testing compilation of the logger outside ::nfd namespace
908
909namespace test_logger { // another root namespace
910
911void
912Test1()
913{
914 NFD_LOG_INIT("Test");
915
916 NFD_LOG_TRACE("Trace");
917 NFD_LOG_DEBUG("Debug");
918 NFD_LOG_INFO("Info");
919 NFD_LOG_WARN("Warn");
920 NFD_LOG_ERROR("Error");
921 NFD_LOG_FATAL("Fatal");
922}
923
924class Test2
925{
926 NFD_LOG_INCLASS_DECLARE();
927};
928
929NFD_LOG_INCLASS_DEFINE(Test2, "Test2");
930
931template<class T>
932class Test3
933{
934 NFD_LOG_INCLASS_DECLARE();
935};
936
937NFD_LOG_INCLASS_TEMPLATE_DEFINE(Test3, "Test3");
938
939NFD_LOG_INCLASS_TEMPLATE_SPECIALIZATION_DEFINE(Test3, int, "Test3Int");
940
941template<class X, class Y>
942class Test4
943{
944 NFD_LOG_INCLASS_DECLARE();
945};
946
947NFD_LOG_INCLASS_2TEMPLATE_SPECIALIZATION_DEFINE(Test4, int, int, "Test4IntInt");
948
949namespace nfd { // nested nfd namespace, different from ::nfd
950
951void
952Test1()
953{
954 NFD_LOG_INIT("Test");
955
956 NFD_LOG_TRACE("Trace");
957 NFD_LOG_DEBUG("Debug");
958 NFD_LOG_INFO("Info");
959 NFD_LOG_WARN("Warn");
960 NFD_LOG_ERROR("Error");
961 NFD_LOG_FATAL("Fatal");
962}
963
964class Test2
965{
966 NFD_LOG_INCLASS_DECLARE();
967};
968
969NFD_LOG_INCLASS_DEFINE(Test2, "Test2");
970
971template<class T>
972class Test3
973{
974 NFD_LOG_INCLASS_DECLARE();
975};
976
977NFD_LOG_INCLASS_TEMPLATE_DEFINE(Test3, "Test3");
978
979NFD_LOG_INCLASS_TEMPLATE_SPECIALIZATION_DEFINE(Test3, int, "Test3Int");
980
981template<class X, class Y>
982class Test4
983{
984 NFD_LOG_INCLASS_DECLARE();
985};
986
987NFD_LOG_INCLASS_2TEMPLATE_SPECIALIZATION_DEFINE(Test4, int, int, "Test4IntInt");
988
989} // namespace nfd
990} // namespace test_logger