blob: 5dafadb61fb42dce1734b044033a7699cd17c8c9 [file] [log] [blame]
Ilya Moiseenkoa807e652014-01-28 11:51:01 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -08003 * Copyright (c) 2014-2015, Regents of the University of California,
4 * 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.hpp>
31#include <boost/algorithm/string/classification.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
95 const string buffer = m_buffer.str();
96
97 std::vector<string> components;
98 boost::split(components, buffer, boost::is_any_of(" ,\n"));
99
100 // std::cout << components.size() << " for " << moduleName << std::endl;
101 // for (size_t i = 0; i < components.size(); ++i)
102 // {
103 // std::cout << "-> " << components[i] << std::endl;
104 // }
105
106 // expected + number of timestamps (one per log statement) + trailing newline of last statement
107 BOOST_REQUIRE_EQUAL(components.size(), N_EXPECTED + 6 + 1);
108
109 std::vector<std::string>::const_iterator componentIter = components.begin();
110 for (size_t i = 0; i < N_EXPECTED; ++i)
111 {
112 // timestamp LOG_LEVEL: [ModuleName] message\n
113
114 const string& timestamp = *componentIter;
115 // std::cout << "timestamp = " << timestamp << std::endl;
116 ++componentIter;
117
118 size_t timeDelimiterPosition = timestamp.find(".");
119
120 BOOST_REQUIRE_NE(string::npos, timeDelimiterPosition);
121
122 string secondsString = timestamp.substr(0, timeDelimiterPosition);
123 string usecondsString = timestamp.substr(timeDelimiterPosition + 1);
124
125 microseconds::rep extractedTime =
126 ONE_SECOND * boost::lexical_cast<microseconds::rep>(secondsString) +
127 boost::lexical_cast<microseconds::rep>(usecondsString);
128
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600129 // std::cout << "before=" << before
130 // << " extracted=" << extractedTime
131 // << " after=" << after << std::endl;
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600132
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600133
134 BOOST_CHECK_LE(before, extractedTime);
135 BOOST_CHECK_LE(extractedTime, after);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600136
137 // LOG_LEVEL:
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600138 BOOST_CHECK_EQUAL(*componentIter, EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600139 ++componentIter;
140 ++i;
141
142 // [ModuleName]
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600143 BOOST_CHECK_EQUAL(*componentIter, EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600144 ++componentIter;
145 ++i;
146
147 const string& message = *componentIter;
148
149 // std::cout << "message = " << message << std::endl;
150
151 // add back the newline that we split on
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600152 BOOST_CHECK_EQUAL(message + "\n", EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600153 ++componentIter;
154 }
155
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800156}
157
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600158
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600159BOOST_FIXTURE_TEST_CASE(ConfigureFactory, LoggerFixture)
160{
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600161 using namespace ndn::time;
162 using std::string;
163
164 const ndn::time::microseconds::rep ONE_SECOND = 1000000;
165
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600166 NFD_LOG_INIT("ConfigureFactoryTests");
167
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600168 const string LOG_CONFIG =
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600169 "log\n"
170 "{\n"
171 " default_level INFO\n"
172 "}\n";
173
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600174 LoggerFactory::getInstance().setDefaultLevel(LOG_ALL);
175
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600176 ConfigFile config;
177 LoggerFactory::getInstance().setConfigFile(config);
178
179 config.parse(LOG_CONFIG, false, "LOG_CONFIG");
180
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600181 BOOST_REQUIRE_EQUAL(LoggerFactory::getInstance().getDefaultLevel(), LOG_INFO);
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600182
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600183 const std::string EXPECTED[] =
184 {
185 "WARNING:", "[ConfigureFactoryTests]", "warning-message-XXXhdhd111x\n",
186 "INFO:", "[ConfigureFactoryTests]", "info-message-Jjxjshj13\n",
187 "ERROR:", "[ConfigureFactoryTests]", "error-message-!#$&^%$#@\n",
188 "FATAL:", "[ConfigureFactoryTests]", "fatal-message-JJSjaamcng\n",
189 };
190
191 const size_t N_EXPECTED = sizeof(EXPECTED) / sizeof(std::string);
192
193 microseconds::rep before =
194 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
195
196 NFD_LOG_TRACE("trace-message-JHGFDSR^1");
197 NFD_LOG_DEBUG("debug-message-IGg2474fdksd-fo-" << 15 << 16 << 17);
198 NFD_LOG_WARN("warning-message-XXXhdhd11" << 1 <<"x");
199 NFD_LOG_INFO("info-message-Jjxjshj13");
200 NFD_LOG_ERROR("error-message-!#$&^%$#@");
201 NFD_LOG_FATAL("fatal-message-JJSjaamcng");
202
203 microseconds::rep after =
204 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
205
206 const string buffer = m_buffer.str();
207
208 std::vector<string> components;
209 boost::split(components, buffer, boost::is_any_of(" ,\n"));
210
211 // std::cout << components.size() << " for " << moduleName << std::endl;
212 // for (size_t i = 0; i < components.size(); ++i)
213 // {
214 // std::cout << "-> " << components[i] << std::endl;
215 // }
216
217 // expected + number of timestamps (one per log statement) + trailing newline of last statement
218 BOOST_REQUIRE_EQUAL(components.size(), N_EXPECTED + 4 + 1);
219
220 std::vector<std::string>::const_iterator componentIter = components.begin();
221 for (size_t i = 0; i < N_EXPECTED; ++i)
222 {
223 // timestamp LOG_LEVEL: [ModuleName] message\n
224
225 const string& timestamp = *componentIter;
226 // std::cout << "timestamp = " << timestamp << std::endl;
227 ++componentIter;
228
229 size_t timeDelimiterPosition = timestamp.find(".");
230
231 BOOST_REQUIRE_NE(string::npos, timeDelimiterPosition);
232
233 string secondsString = timestamp.substr(0, timeDelimiterPosition);
234 string usecondsString = timestamp.substr(timeDelimiterPosition + 1);
235
236 microseconds::rep extractedTime =
237 ONE_SECOND * boost::lexical_cast<microseconds::rep>(secondsString) +
238 boost::lexical_cast<microseconds::rep>(usecondsString);
239
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600240 // std::cout << "before=" << before
241 // << " extracted=" << extractedTime
242 // << " after=" << after << std::endl;
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600243
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600244 BOOST_CHECK_LE(before, extractedTime);
245 BOOST_CHECK_LE(extractedTime, after);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600246
247 // LOG_LEVEL:
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600248 BOOST_CHECK_EQUAL(*componentIter, EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600249 ++componentIter;
250 ++i;
251
252 // [ModuleName]
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600253 BOOST_CHECK_EQUAL(*componentIter, EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600254 ++componentIter;
255 ++i;
256
257 const string& message = *componentIter;
258
259 // std::cout << "message = " << message << std::endl;
260
261 // add back the newline that we split on
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600262 BOOST_CHECK_EQUAL(message + "\n", EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600263 ++componentIter;
264 }
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600265}
266
267BOOST_FIXTURE_TEST_CASE(TestNumberLevel, LoggerFixture)
268{
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600269 const std::string LOG_CONFIG =
270 "log\n"
271 "{\n"
272 " default_level 2\n" // equivalent of WARN
273 "}\n";
274
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600275 LoggerFactory::getInstance().setDefaultLevel(LOG_ALL);
276
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600277 ConfigFile config;
278 LoggerFactory::getInstance().setConfigFile(config);
279
280 config.parse(LOG_CONFIG, false, "LOG_CONFIG");
281
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600282 BOOST_REQUIRE_EQUAL(LoggerFactory::getInstance().getDefaultLevel(), LOG_WARN);
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600283}
284
285static void
286testModuleBPrint()
287{
288 NFD_LOG_INIT("TestModuleB");
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600289 NFD_LOG_DEBUG("debug-message-IGg2474fdksd-fo-" << 15 << 16 << 17);
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600290}
291
292BOOST_FIXTURE_TEST_CASE(LimitModules, LoggerFixture)
293{
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600294 using namespace ndn::time;
295 using std::string;
296
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600297 NFD_LOG_INIT("TestModuleA");
298
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600299 const ndn::time::microseconds::rep ONE_SECOND = 1000000;
300
301 const std::string EXPECTED[] =
302 {
303 "WARNING:", "[TestModuleA]", "warning-message-XXXhdhd111x\n",
304 };
305
306 const size_t N_EXPECTED = sizeof(EXPECTED) / sizeof(std::string);
307
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600308 const std::string LOG_CONFIG =
309 "log\n"
310 "{\n"
311 " default_level WARN\n"
312 "}\n";
313
314 ConfigFile config;
315 LoggerFactory::getInstance().setConfigFile(config);
316
317 config.parse(LOG_CONFIG, false, "LOG_CONFIG");
318
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600319 microseconds::rep before =
320 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
321
322 // this should print
323 NFD_LOG_WARN("warning-message-XXXhdhd11" << 1 << "x");
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600324
325 // this should not because it's level is < WARN
326 testModuleBPrint();
327
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600328 microseconds::rep after =
329 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
330
331 const string buffer = m_buffer.str();
332
333 std::vector<string> components;
334 boost::split(components, buffer, boost::is_any_of(" ,\n"));
335
336 // expected + number of timestamps (one per log statement) + trailing newline of last statement
337 BOOST_REQUIRE_EQUAL(components.size(), N_EXPECTED + 1 + 1);
338
339 std::vector<std::string>::const_iterator componentIter = components.begin();
340 for (size_t i = 0; i < N_EXPECTED; ++i)
341 {
342 // timestamp LOG_LEVEL: [ModuleName] message\n
343
344 const string& timestamp = *componentIter;
345 // std::cout << "timestamp = " << timestamp << std::endl;
346 ++componentIter;
347
348 size_t timeDelimiterPosition = timestamp.find(".");
349
350 BOOST_REQUIRE_NE(string::npos, timeDelimiterPosition);
351
352 string secondsString = timestamp.substr(0, timeDelimiterPosition);
353 string usecondsString = timestamp.substr(timeDelimiterPosition + 1);
354
355 microseconds::rep extractedTime =
356 ONE_SECOND * boost::lexical_cast<microseconds::rep>(secondsString) +
357 boost::lexical_cast<microseconds::rep>(usecondsString);
358
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600359 // std::cout << "before=" << before
360 // << " extracted=" << extractedTime
361 // << " after=" << after << std::endl;
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600362
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600363 BOOST_CHECK_LE(before, extractedTime);
364 BOOST_CHECK_LE(extractedTime, after);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600365
366 // LOG_LEVEL:
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600367 BOOST_CHECK_EQUAL(*componentIter, EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600368 ++componentIter;
369 ++i;
370
371 // [ModuleName]
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600372 BOOST_CHECK_EQUAL(*componentIter, EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600373 ++componentIter;
374 ++i;
375
376 const string& message = *componentIter;
377
378 // std::cout << "message = " << message << std::endl;
379
380 // add back the newline that we split on
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600381 BOOST_CHECK_EQUAL(message + "\n", EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600382 ++componentIter;
383 }
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600384}
385
386BOOST_FIXTURE_TEST_CASE(ExplicitlySetModule, LoggerFixture)
387{
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600388 using namespace ndn::time;
389 using std::string;
390
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600391 NFD_LOG_INIT("TestModuleA");
392
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600393 const ndn::time::microseconds::rep ONE_SECOND = 1000000;
394
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600395 const std::string LOG_CONFIG =
396 "log\n"
397 "{\n"
398 " default_level WARN\n"
399 " TestModuleB DEBUG\n"
400 "}\n";
401
402 ConfigFile config;
403 LoggerFactory::getInstance().setConfigFile(config);
404
405 config.parse(LOG_CONFIG, false, "LOG_CONFIG");
406
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600407 microseconds::rep before =
408 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
409
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600410 // this should print
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600411 NFD_LOG_WARN("warning-message-XXXhdhd11" << 1 << "x");
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600412
413 // this too because its level is explicitly set to DEBUG
414 testModuleBPrint();
415
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600416 microseconds::rep after =
417 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
418
419 const std::string EXPECTED[] =
420 {
421 "WARNING:", "[TestModuleA]", "warning-message-XXXhdhd111x\n",
422 "DEBUG:", "[TestModuleB]", "debug-message-IGg2474fdksd-fo-151617\n",
423 };
424
425 const size_t N_EXPECTED = sizeof(EXPECTED) / sizeof(std::string);
426
427 const string buffer = m_buffer.str();
428
429 std::vector<string> components;
430 boost::split(components, buffer, boost::is_any_of(" ,\n"));
431
432 // for (size_t i = 0; i < components.size(); ++i)
433 // {
434 // std::cout << "-> " << components[i] << std::endl;
435 // }
436
437 // expected + number of timestamps (one per log statement) + trailing newline of last statement
438 BOOST_REQUIRE_EQUAL(components.size(), N_EXPECTED + 2 + 1);
439
440 std::vector<std::string>::const_iterator componentIter = components.begin();
441 for (size_t i = 0; i < N_EXPECTED; ++i)
442 {
443 // timestamp LOG_LEVEL: [ModuleName] message\n
444
445 const string& timestamp = *componentIter;
446 // std::cout << "timestamp = " << timestamp << std::endl;
447 ++componentIter;
448
449 size_t timeDelimiterPosition = timestamp.find(".");
450
451 BOOST_REQUIRE_NE(string::npos, timeDelimiterPosition);
452
453 string secondsString = timestamp.substr(0, timeDelimiterPosition);
454 string usecondsString = timestamp.substr(timeDelimiterPosition + 1);
455
456 microseconds::rep extractedTime =
457 ONE_SECOND * boost::lexical_cast<microseconds::rep>(secondsString) +
458 boost::lexical_cast<microseconds::rep>(usecondsString);
459
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600460 // std::cout << "before=" << before
461 // << " extracted=" << extractedTime
462 // << " after=" << after << std::endl;
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600463
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600464 BOOST_CHECK_LE(before, extractedTime);
465 BOOST_CHECK_LE(extractedTime, after);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600466
467 // LOG_LEVEL:
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600468 BOOST_CHECK_EQUAL(*componentIter, EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600469 ++componentIter;
470 ++i;
471
472 // [ModuleName]
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600473 BOOST_CHECK_EQUAL(*componentIter, EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600474 ++componentIter;
475 ++i;
476
477 const string& message = *componentIter;
478
479 // std::cout << "message = " << message << std::endl;
480
481 // add back the newline that we split on
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600482 BOOST_CHECK_EQUAL(message + "\n", EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600483 ++componentIter;
484 }
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600485}
486
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600487BOOST_FIXTURE_TEST_CASE(UnknownModule, LoggerFixture)
488{
489 using namespace ndn::time;
490 using std::string;
491
492 const ndn::time::microseconds::rep ONE_SECOND = 1000000;
493
494 const std::string LOG_CONFIG =
495 "log\n"
496 "{\n"
Alexander Afanasyev5959b012014-06-02 19:18:12 +0300497 " default_level DEBUG\n"
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600498 " TestMadeUpModule INFO\n"
499 "}\n";
500
501 ConfigFile config;
502 LoggerFactory::getInstance().setDefaultLevel(LOG_ALL);
503 LoggerFactory::getInstance().setConfigFile(config);
504
505 const std::string EXPECTED = "DEBUG: [LoggerFactory] "
506 "Failed to configure logging level for module \"TestMadeUpModule\" (module not found)\n";
507
508 microseconds::rep before =
509 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
510
511 config.parse(LOG_CONFIG, false, "LOG_CONFIG");
512
513 microseconds::rep after =
514 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
515
516 const string buffer = m_buffer.str();
517
518 const size_t firstSpace = buffer.find(" ");
519 BOOST_REQUIRE(firstSpace != string::npos);
520
521 const string timestamp = buffer.substr(0, firstSpace);
522 const string message = buffer.substr(firstSpace + 1);
523
524 size_t timeDelimiterPosition = timestamp.find(".");
525
526 BOOST_REQUIRE_NE(string::npos, timeDelimiterPosition);
527
528 string secondsString = timestamp.substr(0, timeDelimiterPosition);
529 string usecondsString = timestamp.substr(timeDelimiterPosition + 1);
530
531 microseconds::rep extractedTime =
532 ONE_SECOND * boost::lexical_cast<microseconds::rep>(secondsString) +
533 boost::lexical_cast<microseconds::rep>(usecondsString);
534
535 // std::cout << "before=" << before
536 // << " extracted=" << extractedTime
537 // << " after=" << after << std::endl;
538
539 BOOST_CHECK_LE(before, extractedTime);
540 BOOST_CHECK_LE(extractedTime, after);
541
542 BOOST_CHECK_EQUAL(message, EXPECTED);
543}
544
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600545static bool
546checkError(const LoggerFactory::Error& error, const std::string& expected)
547{
548 return error.what() == expected;
549}
550
551BOOST_FIXTURE_TEST_CASE(UnknownLevelString, LoggerFixture)
552{
553 const std::string LOG_CONFIG =
554 "log\n"
555 "{\n"
556 " default_level TestMadeUpLevel\n"
557 "}\n";
558
559 ConfigFile config;
560 LoggerFactory::getInstance().setConfigFile(config);
561
562 BOOST_REQUIRE_EXCEPTION(config.parse(LOG_CONFIG, false, "LOG_CONFIG"),
563 LoggerFactory::Error,
564 bind(&checkError,
565 _1,
566 "Unsupported logging level \"TestMadeUpLevel\""));
567}
568
Alexander Afanasyev66886812014-01-31 14:48:48 -0800569class InClassLogger : public LoggerFixture
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800570{
Alexander Afanasyev66886812014-01-31 14:48:48 -0800571public:
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600572
Alexander Afanasyevb84cc922014-02-24 17:52:58 -0800573 InClassLogger()
574 {
575 g_logger.setLogLevel(LOG_ALL);
576 }
577
Alexander Afanasyev66886812014-01-31 14:48:48 -0800578 void
579 writeLogs()
580 {
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600581 NFD_LOG_TRACE("trace-message-JHGFDSR^1");
582 NFD_LOG_DEBUG("debug-message-IGg2474fdksd-fo-" << 15 << 16 << 17);
583 NFD_LOG_WARN("warning-message-XXXhdhd11" << 1 <<"x");
584 NFD_LOG_INFO("info-message-Jjxjshj13");
585 NFD_LOG_ERROR("error-message-!#$&^%$#@");
586 NFD_LOG_FATAL("fatal-message-JJSjaamcng");
Alexander Afanasyev66886812014-01-31 14:48:48 -0800587 }
Alexander Afanasyevb84cc922014-02-24 17:52:58 -0800588
Alexander Afanasyev66886812014-01-31 14:48:48 -0800589private:
590 NFD_LOG_INCLASS_DECLARE();
591};
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800592
Alexander Afanasyev66886812014-01-31 14:48:48 -0800593NFD_LOG_INCLASS_DEFINE(InClassLogger, "InClassLogger");
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800594
Alexander Afanasyev66886812014-01-31 14:48:48 -0800595BOOST_FIXTURE_TEST_CASE(InClass, InClassLogger)
596{
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600597 using namespace ndn::time;
598 using std::string;
599
600 const ndn::time::microseconds::rep ONE_SECOND = 1000000;
601
602 microseconds::rep before =
603 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
604
Alexander Afanasyev66886812014-01-31 14:48:48 -0800605 writeLogs();
Alexander Afanasyevb84cc922014-02-24 17:52:58 -0800606
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600607 microseconds::rep after =
608 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
609
610 const string EXPECTED[] =
611 {
612 "TRACE:", "[InClassLogger]", "trace-message-JHGFDSR^1\n",
613 "DEBUG:", "[InClassLogger]", "debug-message-IGg2474fdksd-fo-151617\n",
614 "WARNING:", "[InClassLogger]", "warning-message-XXXhdhd111x\n",
615 "INFO:", "[InClassLogger]", "info-message-Jjxjshj13\n",
616 "ERROR:", "[InClassLogger]", "error-message-!#$&^%$#@\n",
617 "FATAL:", "[InClassLogger]", "fatal-message-JJSjaamcng\n",
618 };
619
620 const size_t N_EXPECTED = sizeof(EXPECTED) / sizeof(string);
621
622 const string buffer = m_buffer.str();
623
624 std::vector<string> components;
625 boost::split(components, buffer, boost::is_any_of(" ,\n"));
626
627 // expected + number of timestamps (one per log statement) + trailing newline of last statement
628 BOOST_REQUIRE_EQUAL(components.size(), N_EXPECTED + 6 + 1);
629
630 std::vector<std::string>::const_iterator componentIter = components.begin();
631 for (size_t i = 0; i < N_EXPECTED; ++i)
632 {
633 // timestamp LOG_LEVEL: [ModuleName] message\n
634
635 const string& timestamp = *componentIter;
636 // std::cout << "timestamp = " << timestamp << std::endl;
637 ++componentIter;
638
639 size_t timeDelimiterPosition = timestamp.find(".");
640
641 BOOST_REQUIRE_NE(string::npos, timeDelimiterPosition);
642
643 string secondsString = timestamp.substr(0, timeDelimiterPosition);
644 string usecondsString = timestamp.substr(timeDelimiterPosition + 1);
645
646 microseconds::rep extractedTime =
647 ONE_SECOND * boost::lexical_cast<microseconds::rep>(secondsString) +
648 boost::lexical_cast<microseconds::rep>(usecondsString);
649
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600650 // std::cout << "before=" << before
651 // << " extracted=" << extractedTime
652 // << " after=" << after << std::endl;
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600653
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600654 BOOST_CHECK_LE(before, extractedTime);
655 BOOST_CHECK_LE(extractedTime, after);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600656
657 // LOG_LEVEL:
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600658 BOOST_CHECK_EQUAL(*componentIter, EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600659 ++componentIter;
660 ++i;
661
662 // [ModuleName]
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600663 BOOST_CHECK_EQUAL(*componentIter, EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600664 ++componentIter;
665 ++i;
666
667 const string& message = *componentIter;
668
669 // std::cout << "message = " << message << std::endl;
670
671 // add back the newline that we split on
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600672 BOOST_CHECK_EQUAL(message + "\n", EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600673 ++componentIter;
674 }
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800675}
676
Alexander Afanasyev66886812014-01-31 14:48:48 -0800677
678template<class T>
679class InClassTemplateLogger : public LoggerFixture
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800680{
Alexander Afanasyev66886812014-01-31 14:48:48 -0800681public:
Alexander Afanasyevb84cc922014-02-24 17:52:58 -0800682 InClassTemplateLogger()
683 {
684 g_logger.setLogLevel(LOG_ALL);
685 }
686
Alexander Afanasyev66886812014-01-31 14:48:48 -0800687 void
688 writeLogs()
689 {
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600690 NFD_LOG_TRACE("trace-message-JHGFDSR^1");
691 NFD_LOG_DEBUG("debug-message-IGg2474fdksd-fo-" << 15 << 16 << 17);
692 NFD_LOG_WARN("warning-message-XXXhdhd11" << 1 <<"x");
693 NFD_LOG_INFO("info-message-Jjxjshj13");
694 NFD_LOG_ERROR("error-message-!#$&^%$#@");
695 NFD_LOG_FATAL("fatal-message-JJSjaamcng");
Alexander Afanasyev66886812014-01-31 14:48:48 -0800696 }
Alexander Afanasyevb84cc922014-02-24 17:52:58 -0800697
Alexander Afanasyev66886812014-01-31 14:48:48 -0800698private:
699 NFD_LOG_INCLASS_DECLARE();
700};
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800701
Alexander Afanasyev66886812014-01-31 14:48:48 -0800702NFD_LOG_INCLASS_TEMPLATE_DEFINE(InClassTemplateLogger, "GenericInClassTemplateLogger");
703NFD_LOG_INCLASS_TEMPLATE_SPECIALIZATION_DEFINE(InClassTemplateLogger, int, "IntInClassLogger");
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800704
Alexander Afanasyev66886812014-01-31 14:48:48 -0800705BOOST_FIXTURE_TEST_CASE(GenericInTemplatedClass, InClassTemplateLogger<bool>)
706{
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600707 using namespace ndn::time;
708 using std::string;
709
710 const ndn::time::microseconds::rep ONE_SECOND = 1000000;
711
712 microseconds::rep before =
713 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
714
Alexander Afanasyev66886812014-01-31 14:48:48 -0800715 writeLogs();
Alexander Afanasyevb84cc922014-02-24 17:52:58 -0800716
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600717 microseconds::rep after =
718 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
719
720 const string EXPECTED[] =
721 {
722 "TRACE:", "[GenericInClassTemplateLogger]", "trace-message-JHGFDSR^1\n",
723 "DEBUG:", "[GenericInClassTemplateLogger]", "debug-message-IGg2474fdksd-fo-151617\n",
724 "WARNING:", "[GenericInClassTemplateLogger]", "warning-message-XXXhdhd111x\n",
725 "INFO:", "[GenericInClassTemplateLogger]", "info-message-Jjxjshj13\n",
726 "ERROR:", "[GenericInClassTemplateLogger]", "error-message-!#$&^%$#@\n",
727 "FATAL:", "[GenericInClassTemplateLogger]", "fatal-message-JJSjaamcng\n",
728 };
729
730 const size_t N_EXPECTED = sizeof(EXPECTED) / sizeof(string);
731
732 const string buffer = m_buffer.str();
733
734 std::vector<string> components;
735 boost::split(components, buffer, boost::is_any_of(" ,\n"));
736
737 // expected + number of timestamps (one per log statement) + trailing newline of last statement
738 BOOST_REQUIRE_EQUAL(components.size(), N_EXPECTED + 6 + 1);
739
740 std::vector<std::string>::const_iterator componentIter = components.begin();
741 for (size_t i = 0; i < N_EXPECTED; ++i)
742 {
743 // timestamp LOG_LEVEL: [ModuleName] message\n
744
745 const string& timestamp = *componentIter;
746 // std::cout << "timestamp = " << timestamp << std::endl;
747 ++componentIter;
748
749 size_t timeDelimiterPosition = timestamp.find(".");
750
751 BOOST_REQUIRE_NE(string::npos, timeDelimiterPosition);
752
753 string secondsString = timestamp.substr(0, timeDelimiterPosition);
754 string usecondsString = timestamp.substr(timeDelimiterPosition + 1);
755
756 microseconds::rep extractedTime =
757 ONE_SECOND * boost::lexical_cast<microseconds::rep>(secondsString) +
758 boost::lexical_cast<microseconds::rep>(usecondsString);
759
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600760 // std::cout << "before=" << before
761 // << " extracted=" << extractedTime
762 // << " after=" << after << std::endl;
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600763
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600764 BOOST_CHECK_LE(before, extractedTime);
765 BOOST_CHECK_LE(extractedTime, after);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600766
767 // LOG_LEVEL:
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600768 BOOST_CHECK_EQUAL(*componentIter, EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600769 ++componentIter;
770 ++i;
771
772 // [ModuleName]
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600773 BOOST_CHECK_EQUAL(*componentIter, EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600774 ++componentIter;
775 ++i;
776
777 const string& message = *componentIter;
778
779 // std::cout << "message = " << message << std::endl;
780
781 // add back the newline that we split on
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600782 BOOST_CHECK_EQUAL(message + "\n", EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600783 ++componentIter;
784 }
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800785}
786
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600787
Alexander Afanasyev66886812014-01-31 14:48:48 -0800788BOOST_FIXTURE_TEST_CASE(SpecializedInTemplatedClass, InClassTemplateLogger<int>)
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800789{
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600790 using namespace ndn::time;
791 using std::string;
792
793 const ndn::time::microseconds::rep ONE_SECOND = 1000000;
794
795 microseconds::rep before =
796 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
797
Alexander Afanasyev66886812014-01-31 14:48:48 -0800798 writeLogs();
Alexander Afanasyevb84cc922014-02-24 17:52:58 -0800799
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600800 microseconds::rep after =
801 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
802
803 const string EXPECTED[] =
804 {
805 "TRACE:", "[IntInClassLogger]", "trace-message-JHGFDSR^1\n",
806 "DEBUG:", "[IntInClassLogger]", "debug-message-IGg2474fdksd-fo-151617\n",
807 "WARNING:", "[IntInClassLogger]", "warning-message-XXXhdhd111x\n",
808 "INFO:", "[IntInClassLogger]", "info-message-Jjxjshj13\n",
809 "ERROR:", "[IntInClassLogger]", "error-message-!#$&^%$#@\n",
810 "FATAL:", "[IntInClassLogger]", "fatal-message-JJSjaamcng\n",
811 };
812
813 const size_t N_EXPECTED = sizeof(EXPECTED) / sizeof(string);
814
815 const string buffer = m_buffer.str();
816
817 std::vector<string> components;
818 boost::split(components, buffer, boost::is_any_of(" ,\n"));
819
820 // expected + number of timestamps (one per log statement) + trailing newline of last statement
821 BOOST_REQUIRE_EQUAL(components.size(), N_EXPECTED + 6 + 1);
822
823 std::vector<std::string>::const_iterator componentIter = components.begin();
824 for (size_t i = 0; i < N_EXPECTED; ++i)
825 {
826 // timestamp LOG_LEVEL: [ModuleName] message\n
827
828 const string& timestamp = *componentIter;
829 // std::cout << "timestamp = " << timestamp << std::endl;
830 ++componentIter;
831
832 size_t timeDelimiterPosition = timestamp.find(".");
833
834 BOOST_REQUIRE_NE(string::npos, timeDelimiterPosition);
835
836 string secondsString = timestamp.substr(0, timeDelimiterPosition);
837 string usecondsString = timestamp.substr(timeDelimiterPosition + 1);
838
839 microseconds::rep extractedTime =
840 ONE_SECOND * boost::lexical_cast<microseconds::rep>(secondsString) +
841 boost::lexical_cast<microseconds::rep>(usecondsString);
842
843 // std::cout << "before=" << before << " extracted=" << extractedTime << " after=" << after << std::endl;
844
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600845 BOOST_CHECK_LE(before, extractedTime);
846 BOOST_CHECK_LE(extractedTime, after);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600847
848 // LOG_LEVEL:
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600849 BOOST_CHECK_EQUAL(*componentIter, EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600850 ++componentIter;
851 ++i;
852
853 // [ModuleName]
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600854 BOOST_CHECK_EQUAL(*componentIter, EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600855 ++componentIter;
856 ++i;
857
858 const string& message = *componentIter;
859
860 // std::cout << "message = " << message << std::endl;
861
862 // add back the newline that we split on
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600863 BOOST_CHECK_EQUAL(message + "\n", EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600864 ++componentIter;
865 }
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800866}
867
Alexander Afanasyev6b34ab92015-02-11 21:22:46 -0800868BOOST_FIXTURE_TEST_CASE(LoggerFactoryListModules, LoggerFixture)
869{
870 std::set<std::string> testCaseLoggers{"LoggerFactoryListModules1", "LoggerFactoryListModules2"};
871
872 for (const auto& name : testCaseLoggers) {
873 LoggerFactory::create(name);
874 }
875
876 auto&& modules = LoggerFactory::getInstance().getModules();
877 BOOST_CHECK_GE(modules.size(), 2);
878
879 for (const auto& name : modules) {
880 testCaseLoggers.erase(name);
881 }
882
883 BOOST_CHECK_EQUAL(testCaseLoggers.size(), 0);
884}
885
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800886BOOST_AUTO_TEST_SUITE_END()
887
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700888} // namespace tests
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800889} // namespace nfd