blob: 5a85d452a17a882fe0b66da284aa757dd6fed956 [file] [log] [blame]
Ilya Moiseenkoa807e652014-01-28 11:51:01 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -07003 * Copyright (c) 2014 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
Ilya Moiseenkoa807e652014-01-28 11:51:01 -08009 *
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 * This file is part of NFD (Named Data Networking Forwarding Daemon).
11 * See AUTHORS.md for complete list of NFD authors and contributors.
12 *
13 * NFD is free software: you can redistribute it and/or modify it under the terms
14 * of the GNU General Public License as published by the Free Software Foundation,
15 * either version 3 of the License, or (at your option) any later version.
16 *
17 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
18 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
19 * PURPOSE. See the GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along with
22 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
23 **/
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080024
25#include "core/logger.hpp"
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060026
Davide Pesavento52a18f92014-04-10 00:55:01 +020027#include "tests/test-common.hpp"
28
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -060029#include <boost/algorithm/string.hpp>
30#include <boost/algorithm/string/classification.hpp>
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060031
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080032namespace nfd {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070033namespace tests {
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080034
Junxiao Shid9ee45c2014-02-27 15:38:11 -070035BOOST_FIXTURE_TEST_SUITE(CoreLogger, BaseFixture)
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080036
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060037class LoggerFixture : protected BaseFixture
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080038{
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060039public:
Alexander Afanasyev66886812014-01-31 14:48:48 -080040 LoggerFixture()
Steve DiBenedetto4d43a452014-04-06 18:55:29 -060041 : m_savedBuf(std::clog.rdbuf())
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060042 , m_savedLevel(LoggerFactory::getInstance().getDefaultLevel())
Alexander Afanasyev66886812014-01-31 14:48:48 -080043 {
Steve DiBenedetto4d43a452014-04-06 18:55:29 -060044 std::clog.rdbuf(m_buffer.rdbuf());
Alexander Afanasyev66886812014-01-31 14:48:48 -080045 }
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080046
Alexander Afanasyev66886812014-01-31 14:48:48 -080047 ~LoggerFixture()
48 {
Steve DiBenedetto4d43a452014-04-06 18:55:29 -060049 std::clog.rdbuf(m_savedBuf);
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060050 LoggerFactory::getInstance().setDefaultLevel(m_savedLevel);
Alexander Afanasyev66886812014-01-31 14:48:48 -080051 }
Alexander Afanasyevb84cc922014-02-24 17:52:58 -080052
Alexander Afanasyev66886812014-01-31 14:48:48 -080053 std::stringstream m_buffer;
54 std::streambuf* m_savedBuf;
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060055 LogLevel m_savedLevel;
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -060056
Alexander Afanasyev66886812014-01-31 14:48:48 -080057};
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080058
Alexander Afanasyev66886812014-01-31 14:48:48 -080059BOOST_FIXTURE_TEST_CASE(Basic, LoggerFixture)
60{
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -060061 using namespace ndn::time;
62 using std::string;
63
64 const ndn::time::microseconds::rep ONE_SECOND = 1000000;
65
Alexander Afanasyev66886812014-01-31 14:48:48 -080066 NFD_LOG_INIT("BasicTests");
Alexander Afanasyevb84cc922014-02-24 17:52:58 -080067 g_logger.setLogLevel(LOG_ALL);
68
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -060069 const string EXPECTED[] =
70 {
71 "TRACE:", "[BasicTests]", "trace-message-JHGFDSR^1\n",
72 "DEBUG:", "[BasicTests]", "debug-message-IGg2474fdksd-fo-151617\n",
73 "WARNING:", "[BasicTests]", "warning-message-XXXhdhd111x\n",
74 "INFO:", "[BasicTests]", "info-message-Jjxjshj13\n",
75 "ERROR:", "[BasicTests]", "error-message-!#$&^%$#@\n",
76 "FATAL:", "[BasicTests]", "fatal-message-JJSjaamcng\n",
77 };
Alexander Afanasyevb84cc922014-02-24 17:52:58 -080078
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -060079 const size_t N_EXPECTED = sizeof(EXPECTED) / sizeof(string);
80
81 microseconds::rep before =
82 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
83
84 NFD_LOG_TRACE("trace-message-JHGFDSR^1");
85 NFD_LOG_DEBUG("debug-message-IGg2474fdksd-fo-" << 15 << 16 << 17);
86 NFD_LOG_WARN("warning-message-XXXhdhd11" << 1 <<"x");
87 NFD_LOG_INFO("info-message-Jjxjshj13");
88 NFD_LOG_ERROR("error-message-!#$&^%$#@");
89 NFD_LOG_FATAL("fatal-message-JJSjaamcng");
90
91 microseconds::rep after =
92 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
93
94 const string buffer = m_buffer.str();
95
96 std::vector<string> components;
97 boost::split(components, buffer, boost::is_any_of(" ,\n"));
98
99 // std::cout << components.size() << " for " << moduleName << std::endl;
100 // for (size_t i = 0; i < components.size(); ++i)
101 // {
102 // std::cout << "-> " << components[i] << std::endl;
103 // }
104
105 // expected + number of timestamps (one per log statement) + trailing newline of last statement
106 BOOST_REQUIRE_EQUAL(components.size(), N_EXPECTED + 6 + 1);
107
108 std::vector<std::string>::const_iterator componentIter = components.begin();
109 for (size_t i = 0; i < N_EXPECTED; ++i)
110 {
111 // timestamp LOG_LEVEL: [ModuleName] message\n
112
113 const string& timestamp = *componentIter;
114 // std::cout << "timestamp = " << timestamp << std::endl;
115 ++componentIter;
116
117 size_t timeDelimiterPosition = timestamp.find(".");
118
119 BOOST_REQUIRE_NE(string::npos, timeDelimiterPosition);
120
121 string secondsString = timestamp.substr(0, timeDelimiterPosition);
122 string usecondsString = timestamp.substr(timeDelimiterPosition + 1);
123
124 microseconds::rep extractedTime =
125 ONE_SECOND * boost::lexical_cast<microseconds::rep>(secondsString) +
126 boost::lexical_cast<microseconds::rep>(usecondsString);
127
128 // std::cout << "before=" << before << " extracted=" << extractedTime << " after=" << after << std::endl;
129
130 BOOST_REQUIRE(before <= extractedTime);
131 BOOST_REQUIRE(extractedTime <= after);
132
133 // LOG_LEVEL:
134 BOOST_REQUIRE_EQUAL(*componentIter, EXPECTED[i]);
135 ++componentIter;
136 ++i;
137
138 // [ModuleName]
139 BOOST_REQUIRE_EQUAL(*componentIter, EXPECTED[i]);
140 ++componentIter;
141 ++i;
142
143 const string& message = *componentIter;
144
145 // std::cout << "message = " << message << std::endl;
146
147 // add back the newline that we split on
148 BOOST_REQUIRE_EQUAL(message + "\n", EXPECTED[i]);
149 ++componentIter;
150 }
151
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800152}
153
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600154
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600155BOOST_FIXTURE_TEST_CASE(ConfigureFactory, LoggerFixture)
156{
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600157 using namespace ndn::time;
158 using std::string;
159
160 const ndn::time::microseconds::rep ONE_SECOND = 1000000;
161
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600162 NFD_LOG_INIT("ConfigureFactoryTests");
163
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600164 const string LOG_CONFIG =
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600165 "log\n"
166 "{\n"
167 " default_level INFO\n"
168 "}\n";
169
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600170 LoggerFactory::getInstance().setDefaultLevel(LOG_ALL);
171
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600172 ConfigFile config;
173 LoggerFactory::getInstance().setConfigFile(config);
174
175 config.parse(LOG_CONFIG, false, "LOG_CONFIG");
176
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600177 BOOST_REQUIRE_EQUAL(LoggerFactory::getInstance().getDefaultLevel(), LOG_INFO);
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600178
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600179 const std::string EXPECTED[] =
180 {
181 "WARNING:", "[ConfigureFactoryTests]", "warning-message-XXXhdhd111x\n",
182 "INFO:", "[ConfigureFactoryTests]", "info-message-Jjxjshj13\n",
183 "ERROR:", "[ConfigureFactoryTests]", "error-message-!#$&^%$#@\n",
184 "FATAL:", "[ConfigureFactoryTests]", "fatal-message-JJSjaamcng\n",
185 };
186
187 const size_t N_EXPECTED = sizeof(EXPECTED) / sizeof(std::string);
188
189 microseconds::rep before =
190 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
191
192 NFD_LOG_TRACE("trace-message-JHGFDSR^1");
193 NFD_LOG_DEBUG("debug-message-IGg2474fdksd-fo-" << 15 << 16 << 17);
194 NFD_LOG_WARN("warning-message-XXXhdhd11" << 1 <<"x");
195 NFD_LOG_INFO("info-message-Jjxjshj13");
196 NFD_LOG_ERROR("error-message-!#$&^%$#@");
197 NFD_LOG_FATAL("fatal-message-JJSjaamcng");
198
199 microseconds::rep after =
200 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
201
202 const string buffer = m_buffer.str();
203
204 std::vector<string> components;
205 boost::split(components, buffer, boost::is_any_of(" ,\n"));
206
207 // std::cout << components.size() << " for " << moduleName << std::endl;
208 // for (size_t i = 0; i < components.size(); ++i)
209 // {
210 // std::cout << "-> " << components[i] << std::endl;
211 // }
212
213 // expected + number of timestamps (one per log statement) + trailing newline of last statement
214 BOOST_REQUIRE_EQUAL(components.size(), N_EXPECTED + 4 + 1);
215
216 std::vector<std::string>::const_iterator componentIter = components.begin();
217 for (size_t i = 0; i < N_EXPECTED; ++i)
218 {
219 // timestamp LOG_LEVEL: [ModuleName] message\n
220
221 const string& timestamp = *componentIter;
222 // std::cout << "timestamp = " << timestamp << std::endl;
223 ++componentIter;
224
225 size_t timeDelimiterPosition = timestamp.find(".");
226
227 BOOST_REQUIRE_NE(string::npos, timeDelimiterPosition);
228
229 string secondsString = timestamp.substr(0, timeDelimiterPosition);
230 string usecondsString = timestamp.substr(timeDelimiterPosition + 1);
231
232 microseconds::rep extractedTime =
233 ONE_SECOND * boost::lexical_cast<microseconds::rep>(secondsString) +
234 boost::lexical_cast<microseconds::rep>(usecondsString);
235
236 // std::cout << "before=" << before << " extracted=" << extractedTime << " after=" << after << std::endl;
237
238 BOOST_REQUIRE(before <= extractedTime);
239 BOOST_REQUIRE(extractedTime <= after);
240
241 // LOG_LEVEL:
242 BOOST_REQUIRE_EQUAL(*componentIter, EXPECTED[i]);
243 ++componentIter;
244 ++i;
245
246 // [ModuleName]
247 BOOST_REQUIRE_EQUAL(*componentIter, EXPECTED[i]);
248 ++componentIter;
249 ++i;
250
251 const string& message = *componentIter;
252
253 // std::cout << "message = " << message << std::endl;
254
255 // add back the newline that we split on
256 BOOST_REQUIRE_EQUAL(message + "\n", EXPECTED[i]);
257 ++componentIter;
258 }
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600259}
260
261BOOST_FIXTURE_TEST_CASE(TestNumberLevel, LoggerFixture)
262{
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600263 const std::string LOG_CONFIG =
264 "log\n"
265 "{\n"
266 " default_level 2\n" // equivalent of WARN
267 "}\n";
268
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600269 LoggerFactory::getInstance().setDefaultLevel(LOG_ALL);
270
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600271 ConfigFile config;
272 LoggerFactory::getInstance().setConfigFile(config);
273
274 config.parse(LOG_CONFIG, false, "LOG_CONFIG");
275
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600276 BOOST_REQUIRE_EQUAL(LoggerFactory::getInstance().getDefaultLevel(), LOG_WARN);
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600277}
278
279static void
280testModuleBPrint()
281{
282 NFD_LOG_INIT("TestModuleB");
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600283 NFD_LOG_DEBUG("debug-message-IGg2474fdksd-fo-" << 15 << 16 << 17);
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600284}
285
286BOOST_FIXTURE_TEST_CASE(LimitModules, LoggerFixture)
287{
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600288 using namespace ndn::time;
289 using std::string;
290
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600291 NFD_LOG_INIT("TestModuleA");
292
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600293 const ndn::time::microseconds::rep ONE_SECOND = 1000000;
294
295 const std::string EXPECTED[] =
296 {
297 "WARNING:", "[TestModuleA]", "warning-message-XXXhdhd111x\n",
298 };
299
300 const size_t N_EXPECTED = sizeof(EXPECTED) / sizeof(std::string);
301
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600302 const std::string LOG_CONFIG =
303 "log\n"
304 "{\n"
305 " default_level WARN\n"
306 "}\n";
307
308 ConfigFile config;
309 LoggerFactory::getInstance().setConfigFile(config);
310
311 config.parse(LOG_CONFIG, false, "LOG_CONFIG");
312
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600313 microseconds::rep before =
314 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
315
316 // this should print
317 NFD_LOG_WARN("warning-message-XXXhdhd11" << 1 << "x");
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600318
319 // this should not because it's level is < WARN
320 testModuleBPrint();
321
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600322 microseconds::rep after =
323 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
324
325 const string buffer = m_buffer.str();
326
327 std::vector<string> components;
328 boost::split(components, buffer, boost::is_any_of(" ,\n"));
329
330 // expected + number of timestamps (one per log statement) + trailing newline of last statement
331 BOOST_REQUIRE_EQUAL(components.size(), N_EXPECTED + 1 + 1);
332
333 std::vector<std::string>::const_iterator componentIter = components.begin();
334 for (size_t i = 0; i < N_EXPECTED; ++i)
335 {
336 // timestamp LOG_LEVEL: [ModuleName] message\n
337
338 const string& timestamp = *componentIter;
339 // std::cout << "timestamp = " << timestamp << std::endl;
340 ++componentIter;
341
342 size_t timeDelimiterPosition = timestamp.find(".");
343
344 BOOST_REQUIRE_NE(string::npos, timeDelimiterPosition);
345
346 string secondsString = timestamp.substr(0, timeDelimiterPosition);
347 string usecondsString = timestamp.substr(timeDelimiterPosition + 1);
348
349 microseconds::rep extractedTime =
350 ONE_SECOND * boost::lexical_cast<microseconds::rep>(secondsString) +
351 boost::lexical_cast<microseconds::rep>(usecondsString);
352
353 // std::cout << "before=" << before << " extracted=" << extractedTime << " after=" << after << std::endl;
354
355 BOOST_REQUIRE(before <= extractedTime);
356 BOOST_REQUIRE(extractedTime <= after);
357
358 // LOG_LEVEL:
359 BOOST_REQUIRE_EQUAL(*componentIter, EXPECTED[i]);
360 ++componentIter;
361 ++i;
362
363 // [ModuleName]
364 BOOST_REQUIRE_EQUAL(*componentIter, EXPECTED[i]);
365 ++componentIter;
366 ++i;
367
368 const string& message = *componentIter;
369
370 // std::cout << "message = " << message << std::endl;
371
372 // add back the newline that we split on
373 BOOST_REQUIRE_EQUAL(message + "\n", EXPECTED[i]);
374 ++componentIter;
375 }
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600376}
377
378BOOST_FIXTURE_TEST_CASE(ExplicitlySetModule, LoggerFixture)
379{
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600380 using namespace ndn::time;
381 using std::string;
382
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600383 NFD_LOG_INIT("TestModuleA");
384
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600385 const ndn::time::microseconds::rep ONE_SECOND = 1000000;
386
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600387 const std::string LOG_CONFIG =
388 "log\n"
389 "{\n"
390 " default_level WARN\n"
391 " TestModuleB DEBUG\n"
392 "}\n";
393
394 ConfigFile config;
395 LoggerFactory::getInstance().setConfigFile(config);
396
397 config.parse(LOG_CONFIG, false, "LOG_CONFIG");
398
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600399 microseconds::rep before =
400 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
401
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600402 // this should print
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600403 NFD_LOG_WARN("warning-message-XXXhdhd11" << 1 << "x");
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600404
405 // this too because its level is explicitly set to DEBUG
406 testModuleBPrint();
407
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600408 microseconds::rep after =
409 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
410
411 const std::string EXPECTED[] =
412 {
413 "WARNING:", "[TestModuleA]", "warning-message-XXXhdhd111x\n",
414 "DEBUG:", "[TestModuleB]", "debug-message-IGg2474fdksd-fo-151617\n",
415 };
416
417 const size_t N_EXPECTED = sizeof(EXPECTED) / sizeof(std::string);
418
419 const string buffer = m_buffer.str();
420
421 std::vector<string> components;
422 boost::split(components, buffer, boost::is_any_of(" ,\n"));
423
424 // for (size_t i = 0; i < components.size(); ++i)
425 // {
426 // std::cout << "-> " << components[i] << std::endl;
427 // }
428
429 // expected + number of timestamps (one per log statement) + trailing newline of last statement
430 BOOST_REQUIRE_EQUAL(components.size(), N_EXPECTED + 2 + 1);
431
432 std::vector<std::string>::const_iterator componentIter = components.begin();
433 for (size_t i = 0; i < N_EXPECTED; ++i)
434 {
435 // timestamp LOG_LEVEL: [ModuleName] message\n
436
437 const string& timestamp = *componentIter;
438 // std::cout << "timestamp = " << timestamp << std::endl;
439 ++componentIter;
440
441 size_t timeDelimiterPosition = timestamp.find(".");
442
443 BOOST_REQUIRE_NE(string::npos, timeDelimiterPosition);
444
445 string secondsString = timestamp.substr(0, timeDelimiterPosition);
446 string usecondsString = timestamp.substr(timeDelimiterPosition + 1);
447
448 microseconds::rep extractedTime =
449 ONE_SECOND * boost::lexical_cast<microseconds::rep>(secondsString) +
450 boost::lexical_cast<microseconds::rep>(usecondsString);
451
452 // std::cout << "before=" << before << " extracted=" << extractedTime << " after=" << after << std::endl;
453
454 BOOST_REQUIRE(before <= extractedTime);
455 BOOST_REQUIRE(extractedTime <= after);
456
457 // LOG_LEVEL:
458 BOOST_REQUIRE_EQUAL(*componentIter, EXPECTED[i]);
459 ++componentIter;
460 ++i;
461
462 // [ModuleName]
463 BOOST_REQUIRE_EQUAL(*componentIter, EXPECTED[i]);
464 ++componentIter;
465 ++i;
466
467 const string& message = *componentIter;
468
469 // std::cout << "message = " << message << std::endl;
470
471 // add back the newline that we split on
472 BOOST_REQUIRE_EQUAL(message + "\n", EXPECTED[i]);
473 ++componentIter;
474 }
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600475}
476
477static bool
478checkError(const LoggerFactory::Error& error, const std::string& expected)
479{
480 return error.what() == expected;
481}
482
483BOOST_FIXTURE_TEST_CASE(UnknownLevelString, LoggerFixture)
484{
485 const std::string LOG_CONFIG =
486 "log\n"
487 "{\n"
488 " default_level TestMadeUpLevel\n"
489 "}\n";
490
491 ConfigFile config;
492 LoggerFactory::getInstance().setConfigFile(config);
493
494 BOOST_REQUIRE_EXCEPTION(config.parse(LOG_CONFIG, false, "LOG_CONFIG"),
495 LoggerFactory::Error,
496 bind(&checkError,
497 _1,
498 "Unsupported logging level \"TestMadeUpLevel\""));
499}
500
501BOOST_FIXTURE_TEST_CASE(UnknownOption, LoggerFixture)
502{
503 const std::string LOG_CONFIG =
504 "log\n"
505 "{\n"
506 " TestMadeUpOption\n"
507 "}\n";
508
509 ConfigFile config;
510 LoggerFactory::getInstance().setConfigFile(config);
511
512 BOOST_REQUIRE_EXCEPTION(config.parse(LOG_CONFIG, false, "LOG_CONFIG"),
513 LoggerFactory::Error,
514 bind(&checkError,
515 _1,
516 "No logging level found for option \"TestMadeUpOption\""));
517}
518
Alexander Afanasyev66886812014-01-31 14:48:48 -0800519class InClassLogger : public LoggerFixture
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800520{
Alexander Afanasyev66886812014-01-31 14:48:48 -0800521public:
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600522
Alexander Afanasyevb84cc922014-02-24 17:52:58 -0800523 InClassLogger()
524 {
525 g_logger.setLogLevel(LOG_ALL);
526 }
527
Alexander Afanasyev66886812014-01-31 14:48:48 -0800528 void
529 writeLogs()
530 {
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600531 NFD_LOG_TRACE("trace-message-JHGFDSR^1");
532 NFD_LOG_DEBUG("debug-message-IGg2474fdksd-fo-" << 15 << 16 << 17);
533 NFD_LOG_WARN("warning-message-XXXhdhd11" << 1 <<"x");
534 NFD_LOG_INFO("info-message-Jjxjshj13");
535 NFD_LOG_ERROR("error-message-!#$&^%$#@");
536 NFD_LOG_FATAL("fatal-message-JJSjaamcng");
Alexander Afanasyev66886812014-01-31 14:48:48 -0800537 }
Alexander Afanasyevb84cc922014-02-24 17:52:58 -0800538
Alexander Afanasyev66886812014-01-31 14:48:48 -0800539private:
540 NFD_LOG_INCLASS_DECLARE();
541};
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800542
Alexander Afanasyev66886812014-01-31 14:48:48 -0800543NFD_LOG_INCLASS_DEFINE(InClassLogger, "InClassLogger");
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800544
Alexander Afanasyev66886812014-01-31 14:48:48 -0800545BOOST_FIXTURE_TEST_CASE(InClass, InClassLogger)
546{
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600547 using namespace ndn::time;
548 using std::string;
549
550 const ndn::time::microseconds::rep ONE_SECOND = 1000000;
551
552 microseconds::rep before =
553 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
554
Alexander Afanasyev66886812014-01-31 14:48:48 -0800555 writeLogs();
Alexander Afanasyevb84cc922014-02-24 17:52:58 -0800556
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600557 microseconds::rep after =
558 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
559
560 const string EXPECTED[] =
561 {
562 "TRACE:", "[InClassLogger]", "trace-message-JHGFDSR^1\n",
563 "DEBUG:", "[InClassLogger]", "debug-message-IGg2474fdksd-fo-151617\n",
564 "WARNING:", "[InClassLogger]", "warning-message-XXXhdhd111x\n",
565 "INFO:", "[InClassLogger]", "info-message-Jjxjshj13\n",
566 "ERROR:", "[InClassLogger]", "error-message-!#$&^%$#@\n",
567 "FATAL:", "[InClassLogger]", "fatal-message-JJSjaamcng\n",
568 };
569
570 const size_t N_EXPECTED = sizeof(EXPECTED) / sizeof(string);
571
572 const string buffer = m_buffer.str();
573
574 std::vector<string> components;
575 boost::split(components, buffer, boost::is_any_of(" ,\n"));
576
577 // expected + number of timestamps (one per log statement) + trailing newline of last statement
578 BOOST_REQUIRE_EQUAL(components.size(), N_EXPECTED + 6 + 1);
579
580 std::vector<std::string>::const_iterator componentIter = components.begin();
581 for (size_t i = 0; i < N_EXPECTED; ++i)
582 {
583 // timestamp LOG_LEVEL: [ModuleName] message\n
584
585 const string& timestamp = *componentIter;
586 // std::cout << "timestamp = " << timestamp << std::endl;
587 ++componentIter;
588
589 size_t timeDelimiterPosition = timestamp.find(".");
590
591 BOOST_REQUIRE_NE(string::npos, timeDelimiterPosition);
592
593 string secondsString = timestamp.substr(0, timeDelimiterPosition);
594 string usecondsString = timestamp.substr(timeDelimiterPosition + 1);
595
596 microseconds::rep extractedTime =
597 ONE_SECOND * boost::lexical_cast<microseconds::rep>(secondsString) +
598 boost::lexical_cast<microseconds::rep>(usecondsString);
599
600 // std::cout << "before=" << before << " extracted=" << extractedTime << " after=" << after << std::endl;
601
602 BOOST_REQUIRE(before <= extractedTime);
603 BOOST_REQUIRE(extractedTime <= after);
604
605 // LOG_LEVEL:
606 BOOST_REQUIRE_EQUAL(*componentIter, EXPECTED[i]);
607 ++componentIter;
608 ++i;
609
610 // [ModuleName]
611 BOOST_REQUIRE_EQUAL(*componentIter, EXPECTED[i]);
612 ++componentIter;
613 ++i;
614
615 const string& message = *componentIter;
616
617 // std::cout << "message = " << message << std::endl;
618
619 // add back the newline that we split on
620 BOOST_REQUIRE_EQUAL(message + "\n", EXPECTED[i]);
621 ++componentIter;
622 }
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800623}
624
Alexander Afanasyev66886812014-01-31 14:48:48 -0800625
626template<class T>
627class InClassTemplateLogger : public LoggerFixture
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800628{
Alexander Afanasyev66886812014-01-31 14:48:48 -0800629public:
Alexander Afanasyevb84cc922014-02-24 17:52:58 -0800630 InClassTemplateLogger()
631 {
632 g_logger.setLogLevel(LOG_ALL);
633 }
634
Alexander Afanasyev66886812014-01-31 14:48:48 -0800635 void
636 writeLogs()
637 {
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600638 NFD_LOG_TRACE("trace-message-JHGFDSR^1");
639 NFD_LOG_DEBUG("debug-message-IGg2474fdksd-fo-" << 15 << 16 << 17);
640 NFD_LOG_WARN("warning-message-XXXhdhd11" << 1 <<"x");
641 NFD_LOG_INFO("info-message-Jjxjshj13");
642 NFD_LOG_ERROR("error-message-!#$&^%$#@");
643 NFD_LOG_FATAL("fatal-message-JJSjaamcng");
Alexander Afanasyev66886812014-01-31 14:48:48 -0800644 }
Alexander Afanasyevb84cc922014-02-24 17:52:58 -0800645
Alexander Afanasyev66886812014-01-31 14:48:48 -0800646private:
647 NFD_LOG_INCLASS_DECLARE();
648};
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800649
Alexander Afanasyev66886812014-01-31 14:48:48 -0800650NFD_LOG_INCLASS_TEMPLATE_DEFINE(InClassTemplateLogger, "GenericInClassTemplateLogger");
651NFD_LOG_INCLASS_TEMPLATE_SPECIALIZATION_DEFINE(InClassTemplateLogger, int, "IntInClassLogger");
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800652
Alexander Afanasyev66886812014-01-31 14:48:48 -0800653BOOST_FIXTURE_TEST_CASE(GenericInTemplatedClass, InClassTemplateLogger<bool>)
654{
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600655 using namespace ndn::time;
656 using std::string;
657
658 const ndn::time::microseconds::rep ONE_SECOND = 1000000;
659
660 microseconds::rep before =
661 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
662
Alexander Afanasyev66886812014-01-31 14:48:48 -0800663 writeLogs();
Alexander Afanasyevb84cc922014-02-24 17:52:58 -0800664
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600665 microseconds::rep after =
666 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
667
668 const string EXPECTED[] =
669 {
670 "TRACE:", "[GenericInClassTemplateLogger]", "trace-message-JHGFDSR^1\n",
671 "DEBUG:", "[GenericInClassTemplateLogger]", "debug-message-IGg2474fdksd-fo-151617\n",
672 "WARNING:", "[GenericInClassTemplateLogger]", "warning-message-XXXhdhd111x\n",
673 "INFO:", "[GenericInClassTemplateLogger]", "info-message-Jjxjshj13\n",
674 "ERROR:", "[GenericInClassTemplateLogger]", "error-message-!#$&^%$#@\n",
675 "FATAL:", "[GenericInClassTemplateLogger]", "fatal-message-JJSjaamcng\n",
676 };
677
678 const size_t N_EXPECTED = sizeof(EXPECTED) / sizeof(string);
679
680 const string buffer = m_buffer.str();
681
682 std::vector<string> components;
683 boost::split(components, buffer, boost::is_any_of(" ,\n"));
684
685 // expected + number of timestamps (one per log statement) + trailing newline of last statement
686 BOOST_REQUIRE_EQUAL(components.size(), N_EXPECTED + 6 + 1);
687
688 std::vector<std::string>::const_iterator componentIter = components.begin();
689 for (size_t i = 0; i < N_EXPECTED; ++i)
690 {
691 // timestamp LOG_LEVEL: [ModuleName] message\n
692
693 const string& timestamp = *componentIter;
694 // std::cout << "timestamp = " << timestamp << std::endl;
695 ++componentIter;
696
697 size_t timeDelimiterPosition = timestamp.find(".");
698
699 BOOST_REQUIRE_NE(string::npos, timeDelimiterPosition);
700
701 string secondsString = timestamp.substr(0, timeDelimiterPosition);
702 string usecondsString = timestamp.substr(timeDelimiterPosition + 1);
703
704 microseconds::rep extractedTime =
705 ONE_SECOND * boost::lexical_cast<microseconds::rep>(secondsString) +
706 boost::lexical_cast<microseconds::rep>(usecondsString);
707
708 // std::cout << "before=" << before << " extracted=" << extractedTime << " after=" << after << std::endl;
709
710 BOOST_REQUIRE(before <= extractedTime);
711 BOOST_REQUIRE(extractedTime <= after);
712
713 // LOG_LEVEL:
714 BOOST_REQUIRE_EQUAL(*componentIter, EXPECTED[i]);
715 ++componentIter;
716 ++i;
717
718 // [ModuleName]
719 BOOST_REQUIRE_EQUAL(*componentIter, EXPECTED[i]);
720 ++componentIter;
721 ++i;
722
723 const string& message = *componentIter;
724
725 // std::cout << "message = " << message << std::endl;
726
727 // add back the newline that we split on
728 BOOST_REQUIRE_EQUAL(message + "\n", EXPECTED[i]);
729 ++componentIter;
730 }
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800731}
732
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600733
Alexander Afanasyev66886812014-01-31 14:48:48 -0800734BOOST_FIXTURE_TEST_CASE(SpecializedInTemplatedClass, InClassTemplateLogger<int>)
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800735{
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600736 using namespace ndn::time;
737 using std::string;
738
739 const ndn::time::microseconds::rep ONE_SECOND = 1000000;
740
741 microseconds::rep before =
742 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
743
Alexander Afanasyev66886812014-01-31 14:48:48 -0800744 writeLogs();
Alexander Afanasyevb84cc922014-02-24 17:52:58 -0800745
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600746 microseconds::rep after =
747 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
748
749 const string EXPECTED[] =
750 {
751 "TRACE:", "[IntInClassLogger]", "trace-message-JHGFDSR^1\n",
752 "DEBUG:", "[IntInClassLogger]", "debug-message-IGg2474fdksd-fo-151617\n",
753 "WARNING:", "[IntInClassLogger]", "warning-message-XXXhdhd111x\n",
754 "INFO:", "[IntInClassLogger]", "info-message-Jjxjshj13\n",
755 "ERROR:", "[IntInClassLogger]", "error-message-!#$&^%$#@\n",
756 "FATAL:", "[IntInClassLogger]", "fatal-message-JJSjaamcng\n",
757 };
758
759 const size_t N_EXPECTED = sizeof(EXPECTED) / sizeof(string);
760
761 const string buffer = m_buffer.str();
762
763 std::vector<string> components;
764 boost::split(components, buffer, boost::is_any_of(" ,\n"));
765
766 // expected + number of timestamps (one per log statement) + trailing newline of last statement
767 BOOST_REQUIRE_EQUAL(components.size(), N_EXPECTED + 6 + 1);
768
769 std::vector<std::string>::const_iterator componentIter = components.begin();
770 for (size_t i = 0; i < N_EXPECTED; ++i)
771 {
772 // timestamp LOG_LEVEL: [ModuleName] message\n
773
774 const string& timestamp = *componentIter;
775 // std::cout << "timestamp = " << timestamp << std::endl;
776 ++componentIter;
777
778 size_t timeDelimiterPosition = timestamp.find(".");
779
780 BOOST_REQUIRE_NE(string::npos, timeDelimiterPosition);
781
782 string secondsString = timestamp.substr(0, timeDelimiterPosition);
783 string usecondsString = timestamp.substr(timeDelimiterPosition + 1);
784
785 microseconds::rep extractedTime =
786 ONE_SECOND * boost::lexical_cast<microseconds::rep>(secondsString) +
787 boost::lexical_cast<microseconds::rep>(usecondsString);
788
789 // std::cout << "before=" << before << " extracted=" << extractedTime << " after=" << after << std::endl;
790
791 BOOST_REQUIRE(before <= extractedTime);
792 BOOST_REQUIRE(extractedTime <= after);
793
794 // LOG_LEVEL:
795 BOOST_REQUIRE_EQUAL(*componentIter, EXPECTED[i]);
796 ++componentIter;
797 ++i;
798
799 // [ModuleName]
800 BOOST_REQUIRE_EQUAL(*componentIter, EXPECTED[i]);
801 ++componentIter;
802 ++i;
803
804 const string& message = *componentIter;
805
806 // std::cout << "message = " << message << std::endl;
807
808 // add back the newline that we split on
809 BOOST_REQUIRE_EQUAL(message + "\n", EXPECTED[i]);
810 ++componentIter;
811 }
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800812}
813
814BOOST_AUTO_TEST_SUITE_END()
815
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700816} // namespace tests
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800817} // namespace nfd