blob: 1ccbc319a02b6948e7e92034c621581a97c03cf4 [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
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080027#include <boost/test/unit_test.hpp>
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -060028#include <boost/algorithm/string.hpp>
29#include <boost/algorithm/string/classification.hpp>
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060030
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080031#include <iostream>
32
Junxiao Shid9ee45c2014-02-27 15:38:11 -070033#include "tests/test-common.hpp"
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080034
35namespace nfd {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070036namespace tests {
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080037
Junxiao Shid9ee45c2014-02-27 15:38:11 -070038BOOST_FIXTURE_TEST_SUITE(CoreLogger, BaseFixture)
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080039
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060040class LoggerFixture : protected BaseFixture
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080041{
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060042public:
Alexander Afanasyev66886812014-01-31 14:48:48 -080043 LoggerFixture()
Steve DiBenedetto4d43a452014-04-06 18:55:29 -060044 : m_savedBuf(std::clog.rdbuf())
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060045 , m_savedLevel(LoggerFactory::getInstance().getDefaultLevel())
Alexander Afanasyev66886812014-01-31 14:48:48 -080046 {
Steve DiBenedetto4d43a452014-04-06 18:55:29 -060047 std::clog.rdbuf(m_buffer.rdbuf());
Alexander Afanasyev66886812014-01-31 14:48:48 -080048 }
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080049
Alexander Afanasyev66886812014-01-31 14:48:48 -080050 ~LoggerFixture()
51 {
Steve DiBenedetto4d43a452014-04-06 18:55:29 -060052 std::clog.rdbuf(m_savedBuf);
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060053 LoggerFactory::getInstance().setDefaultLevel(m_savedLevel);
Alexander Afanasyev66886812014-01-31 14:48:48 -080054 }
Alexander Afanasyevb84cc922014-02-24 17:52:58 -080055
Alexander Afanasyev66886812014-01-31 14:48:48 -080056 std::stringstream m_buffer;
57 std::streambuf* m_savedBuf;
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060058 LogLevel m_savedLevel;
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -060059
Alexander Afanasyev66886812014-01-31 14:48:48 -080060};
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080061
Alexander Afanasyev66886812014-01-31 14:48:48 -080062BOOST_FIXTURE_TEST_CASE(Basic, LoggerFixture)
63{
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -060064 using namespace ndn::time;
65 using std::string;
66
67 const ndn::time::microseconds::rep ONE_SECOND = 1000000;
68
Alexander Afanasyev66886812014-01-31 14:48:48 -080069 NFD_LOG_INIT("BasicTests");
Alexander Afanasyevb84cc922014-02-24 17:52:58 -080070 g_logger.setLogLevel(LOG_ALL);
71
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -060072 const string EXPECTED[] =
73 {
74 "TRACE:", "[BasicTests]", "trace-message-JHGFDSR^1\n",
75 "DEBUG:", "[BasicTests]", "debug-message-IGg2474fdksd-fo-151617\n",
76 "WARNING:", "[BasicTests]", "warning-message-XXXhdhd111x\n",
77 "INFO:", "[BasicTests]", "info-message-Jjxjshj13\n",
78 "ERROR:", "[BasicTests]", "error-message-!#$&^%$#@\n",
79 "FATAL:", "[BasicTests]", "fatal-message-JJSjaamcng\n",
80 };
Alexander Afanasyevb84cc922014-02-24 17:52:58 -080081
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -060082 const size_t N_EXPECTED = sizeof(EXPECTED) / sizeof(string);
83
84 microseconds::rep before =
85 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
86
87 NFD_LOG_TRACE("trace-message-JHGFDSR^1");
88 NFD_LOG_DEBUG("debug-message-IGg2474fdksd-fo-" << 15 << 16 << 17);
89 NFD_LOG_WARN("warning-message-XXXhdhd11" << 1 <<"x");
90 NFD_LOG_INFO("info-message-Jjxjshj13");
91 NFD_LOG_ERROR("error-message-!#$&^%$#@");
92 NFD_LOG_FATAL("fatal-message-JJSjaamcng");
93
94 microseconds::rep after =
95 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
96
97 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
131 // std::cout << "before=" << before << " extracted=" << extractedTime << " after=" << after << std::endl;
132
133 BOOST_REQUIRE(before <= extractedTime);
134 BOOST_REQUIRE(extractedTime <= after);
135
136 // LOG_LEVEL:
137 BOOST_REQUIRE_EQUAL(*componentIter, EXPECTED[i]);
138 ++componentIter;
139 ++i;
140
141 // [ModuleName]
142 BOOST_REQUIRE_EQUAL(*componentIter, EXPECTED[i]);
143 ++componentIter;
144 ++i;
145
146 const string& message = *componentIter;
147
148 // std::cout << "message = " << message << std::endl;
149
150 // add back the newline that we split on
151 BOOST_REQUIRE_EQUAL(message + "\n", EXPECTED[i]);
152 ++componentIter;
153 }
154
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800155}
156
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600157
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600158BOOST_FIXTURE_TEST_CASE(ConfigureFactory, LoggerFixture)
159{
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600160 using namespace ndn::time;
161 using std::string;
162
163 const ndn::time::microseconds::rep ONE_SECOND = 1000000;
164
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600165 NFD_LOG_INIT("ConfigureFactoryTests");
166
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600167 const string LOG_CONFIG =
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600168 "log\n"
169 "{\n"
170 " default_level INFO\n"
171 "}\n";
172
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600173 LoggerFactory::getInstance().setDefaultLevel(LOG_ALL);
174
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600175 ConfigFile config;
176 LoggerFactory::getInstance().setConfigFile(config);
177
178 config.parse(LOG_CONFIG, false, "LOG_CONFIG");
179
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600180 BOOST_REQUIRE_EQUAL(LoggerFactory::getInstance().getDefaultLevel(), LOG_INFO);
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600181
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600182 const std::string EXPECTED[] =
183 {
184 "WARNING:", "[ConfigureFactoryTests]", "warning-message-XXXhdhd111x\n",
185 "INFO:", "[ConfigureFactoryTests]", "info-message-Jjxjshj13\n",
186 "ERROR:", "[ConfigureFactoryTests]", "error-message-!#$&^%$#@\n",
187 "FATAL:", "[ConfigureFactoryTests]", "fatal-message-JJSjaamcng\n",
188 };
189
190 const size_t N_EXPECTED = sizeof(EXPECTED) / sizeof(std::string);
191
192 microseconds::rep before =
193 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
194
195 NFD_LOG_TRACE("trace-message-JHGFDSR^1");
196 NFD_LOG_DEBUG("debug-message-IGg2474fdksd-fo-" << 15 << 16 << 17);
197 NFD_LOG_WARN("warning-message-XXXhdhd11" << 1 <<"x");
198 NFD_LOG_INFO("info-message-Jjxjshj13");
199 NFD_LOG_ERROR("error-message-!#$&^%$#@");
200 NFD_LOG_FATAL("fatal-message-JJSjaamcng");
201
202 microseconds::rep after =
203 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
204
205 const string buffer = m_buffer.str();
206
207 std::vector<string> components;
208 boost::split(components, buffer, boost::is_any_of(" ,\n"));
209
210 // std::cout << components.size() << " for " << moduleName << std::endl;
211 // for (size_t i = 0; i < components.size(); ++i)
212 // {
213 // std::cout << "-> " << components[i] << std::endl;
214 // }
215
216 // expected + number of timestamps (one per log statement) + trailing newline of last statement
217 BOOST_REQUIRE_EQUAL(components.size(), N_EXPECTED + 4 + 1);
218
219 std::vector<std::string>::const_iterator componentIter = components.begin();
220 for (size_t i = 0; i < N_EXPECTED; ++i)
221 {
222 // timestamp LOG_LEVEL: [ModuleName] message\n
223
224 const string& timestamp = *componentIter;
225 // std::cout << "timestamp = " << timestamp << std::endl;
226 ++componentIter;
227
228 size_t timeDelimiterPosition = timestamp.find(".");
229
230 BOOST_REQUIRE_NE(string::npos, timeDelimiterPosition);
231
232 string secondsString = timestamp.substr(0, timeDelimiterPosition);
233 string usecondsString = timestamp.substr(timeDelimiterPosition + 1);
234
235 microseconds::rep extractedTime =
236 ONE_SECOND * boost::lexical_cast<microseconds::rep>(secondsString) +
237 boost::lexical_cast<microseconds::rep>(usecondsString);
238
239 // std::cout << "before=" << before << " extracted=" << extractedTime << " after=" << after << std::endl;
240
241 BOOST_REQUIRE(before <= extractedTime);
242 BOOST_REQUIRE(extractedTime <= after);
243
244 // LOG_LEVEL:
245 BOOST_REQUIRE_EQUAL(*componentIter, EXPECTED[i]);
246 ++componentIter;
247 ++i;
248
249 // [ModuleName]
250 BOOST_REQUIRE_EQUAL(*componentIter, EXPECTED[i]);
251 ++componentIter;
252 ++i;
253
254 const string& message = *componentIter;
255
256 // std::cout << "message = " << message << std::endl;
257
258 // add back the newline that we split on
259 BOOST_REQUIRE_EQUAL(message + "\n", EXPECTED[i]);
260 ++componentIter;
261 }
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600262}
263
264BOOST_FIXTURE_TEST_CASE(TestNumberLevel, LoggerFixture)
265{
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600266 const std::string LOG_CONFIG =
267 "log\n"
268 "{\n"
269 " default_level 2\n" // equivalent of WARN
270 "}\n";
271
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600272 LoggerFactory::getInstance().setDefaultLevel(LOG_ALL);
273
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600274 ConfigFile config;
275 LoggerFactory::getInstance().setConfigFile(config);
276
277 config.parse(LOG_CONFIG, false, "LOG_CONFIG");
278
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600279 BOOST_REQUIRE_EQUAL(LoggerFactory::getInstance().getDefaultLevel(), LOG_WARN);
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600280}
281
282static void
283testModuleBPrint()
284{
285 NFD_LOG_INIT("TestModuleB");
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600286 NFD_LOG_DEBUG("debug-message-IGg2474fdksd-fo-" << 15 << 16 << 17);
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600287}
288
289BOOST_FIXTURE_TEST_CASE(LimitModules, LoggerFixture)
290{
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600291 using namespace ndn::time;
292 using std::string;
293
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600294 NFD_LOG_INIT("TestModuleA");
295
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600296 const ndn::time::microseconds::rep ONE_SECOND = 1000000;
297
298 const std::string EXPECTED[] =
299 {
300 "WARNING:", "[TestModuleA]", "warning-message-XXXhdhd111x\n",
301 };
302
303 const size_t N_EXPECTED = sizeof(EXPECTED) / sizeof(std::string);
304
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600305 const std::string LOG_CONFIG =
306 "log\n"
307 "{\n"
308 " default_level WARN\n"
309 "}\n";
310
311 ConfigFile config;
312 LoggerFactory::getInstance().setConfigFile(config);
313
314 config.parse(LOG_CONFIG, false, "LOG_CONFIG");
315
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600316 microseconds::rep before =
317 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
318
319 // this should print
320 NFD_LOG_WARN("warning-message-XXXhdhd11" << 1 << "x");
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600321
322 // this should not because it's level is < WARN
323 testModuleBPrint();
324
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600325 microseconds::rep after =
326 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
327
328 const string buffer = m_buffer.str();
329
330 std::vector<string> components;
331 boost::split(components, buffer, boost::is_any_of(" ,\n"));
332
333 // expected + number of timestamps (one per log statement) + trailing newline of last statement
334 BOOST_REQUIRE_EQUAL(components.size(), N_EXPECTED + 1 + 1);
335
336 std::vector<std::string>::const_iterator componentIter = components.begin();
337 for (size_t i = 0; i < N_EXPECTED; ++i)
338 {
339 // timestamp LOG_LEVEL: [ModuleName] message\n
340
341 const string& timestamp = *componentIter;
342 // std::cout << "timestamp = " << timestamp << std::endl;
343 ++componentIter;
344
345 size_t timeDelimiterPosition = timestamp.find(".");
346
347 BOOST_REQUIRE_NE(string::npos, timeDelimiterPosition);
348
349 string secondsString = timestamp.substr(0, timeDelimiterPosition);
350 string usecondsString = timestamp.substr(timeDelimiterPosition + 1);
351
352 microseconds::rep extractedTime =
353 ONE_SECOND * boost::lexical_cast<microseconds::rep>(secondsString) +
354 boost::lexical_cast<microseconds::rep>(usecondsString);
355
356 // std::cout << "before=" << before << " extracted=" << extractedTime << " after=" << after << std::endl;
357
358 BOOST_REQUIRE(before <= extractedTime);
359 BOOST_REQUIRE(extractedTime <= after);
360
361 // LOG_LEVEL:
362 BOOST_REQUIRE_EQUAL(*componentIter, EXPECTED[i]);
363 ++componentIter;
364 ++i;
365
366 // [ModuleName]
367 BOOST_REQUIRE_EQUAL(*componentIter, EXPECTED[i]);
368 ++componentIter;
369 ++i;
370
371 const string& message = *componentIter;
372
373 // std::cout << "message = " << message << std::endl;
374
375 // add back the newline that we split on
376 BOOST_REQUIRE_EQUAL(message + "\n", EXPECTED[i]);
377 ++componentIter;
378 }
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600379}
380
381BOOST_FIXTURE_TEST_CASE(ExplicitlySetModule, LoggerFixture)
382{
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600383 using namespace ndn::time;
384 using std::string;
385
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600386 NFD_LOG_INIT("TestModuleA");
387
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600388 const ndn::time::microseconds::rep ONE_SECOND = 1000000;
389
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600390 const std::string LOG_CONFIG =
391 "log\n"
392 "{\n"
393 " default_level WARN\n"
394 " TestModuleB DEBUG\n"
395 "}\n";
396
397 ConfigFile config;
398 LoggerFactory::getInstance().setConfigFile(config);
399
400 config.parse(LOG_CONFIG, false, "LOG_CONFIG");
401
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600402 microseconds::rep before =
403 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
404
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600405 // this should print
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600406 NFD_LOG_WARN("warning-message-XXXhdhd11" << 1 << "x");
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600407
408 // this too because its level is explicitly set to DEBUG
409 testModuleBPrint();
410
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600411 microseconds::rep after =
412 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
413
414 const std::string EXPECTED[] =
415 {
416 "WARNING:", "[TestModuleA]", "warning-message-XXXhdhd111x\n",
417 "DEBUG:", "[TestModuleB]", "debug-message-IGg2474fdksd-fo-151617\n",
418 };
419
420 const size_t N_EXPECTED = sizeof(EXPECTED) / sizeof(std::string);
421
422 const string buffer = m_buffer.str();
423
424 std::vector<string> components;
425 boost::split(components, buffer, boost::is_any_of(" ,\n"));
426
427 // for (size_t i = 0; i < components.size(); ++i)
428 // {
429 // std::cout << "-> " << components[i] << std::endl;
430 // }
431
432 // expected + number of timestamps (one per log statement) + trailing newline of last statement
433 BOOST_REQUIRE_EQUAL(components.size(), N_EXPECTED + 2 + 1);
434
435 std::vector<std::string>::const_iterator componentIter = components.begin();
436 for (size_t i = 0; i < N_EXPECTED; ++i)
437 {
438 // timestamp LOG_LEVEL: [ModuleName] message\n
439
440 const string& timestamp = *componentIter;
441 // std::cout << "timestamp = " << timestamp << std::endl;
442 ++componentIter;
443
444 size_t timeDelimiterPosition = timestamp.find(".");
445
446 BOOST_REQUIRE_NE(string::npos, timeDelimiterPosition);
447
448 string secondsString = timestamp.substr(0, timeDelimiterPosition);
449 string usecondsString = timestamp.substr(timeDelimiterPosition + 1);
450
451 microseconds::rep extractedTime =
452 ONE_SECOND * boost::lexical_cast<microseconds::rep>(secondsString) +
453 boost::lexical_cast<microseconds::rep>(usecondsString);
454
455 // std::cout << "before=" << before << " extracted=" << extractedTime << " after=" << after << std::endl;
456
457 BOOST_REQUIRE(before <= extractedTime);
458 BOOST_REQUIRE(extractedTime <= after);
459
460 // LOG_LEVEL:
461 BOOST_REQUIRE_EQUAL(*componentIter, EXPECTED[i]);
462 ++componentIter;
463 ++i;
464
465 // [ModuleName]
466 BOOST_REQUIRE_EQUAL(*componentIter, EXPECTED[i]);
467 ++componentIter;
468 ++i;
469
470 const string& message = *componentIter;
471
472 // std::cout << "message = " << message << std::endl;
473
474 // add back the newline that we split on
475 BOOST_REQUIRE_EQUAL(message + "\n", EXPECTED[i]);
476 ++componentIter;
477 }
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600478}
479
480static bool
481checkError(const LoggerFactory::Error& error, const std::string& expected)
482{
483 return error.what() == expected;
484}
485
486BOOST_FIXTURE_TEST_CASE(UnknownLevelString, LoggerFixture)
487{
488 const std::string LOG_CONFIG =
489 "log\n"
490 "{\n"
491 " default_level TestMadeUpLevel\n"
492 "}\n";
493
494 ConfigFile config;
495 LoggerFactory::getInstance().setConfigFile(config);
496
497 BOOST_REQUIRE_EXCEPTION(config.parse(LOG_CONFIG, false, "LOG_CONFIG"),
498 LoggerFactory::Error,
499 bind(&checkError,
500 _1,
501 "Unsupported logging level \"TestMadeUpLevel\""));
502}
503
504BOOST_FIXTURE_TEST_CASE(UnknownOption, LoggerFixture)
505{
506 const std::string LOG_CONFIG =
507 "log\n"
508 "{\n"
509 " TestMadeUpOption\n"
510 "}\n";
511
512 ConfigFile config;
513 LoggerFactory::getInstance().setConfigFile(config);
514
515 BOOST_REQUIRE_EXCEPTION(config.parse(LOG_CONFIG, false, "LOG_CONFIG"),
516 LoggerFactory::Error,
517 bind(&checkError,
518 _1,
519 "No logging level found for option \"TestMadeUpOption\""));
520}
521
Alexander Afanasyev66886812014-01-31 14:48:48 -0800522class InClassLogger : public LoggerFixture
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800523{
Alexander Afanasyev66886812014-01-31 14:48:48 -0800524public:
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600525
Alexander Afanasyevb84cc922014-02-24 17:52:58 -0800526 InClassLogger()
527 {
528 g_logger.setLogLevel(LOG_ALL);
529 }
530
Alexander Afanasyev66886812014-01-31 14:48:48 -0800531 void
532 writeLogs()
533 {
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600534 NFD_LOG_TRACE("trace-message-JHGFDSR^1");
535 NFD_LOG_DEBUG("debug-message-IGg2474fdksd-fo-" << 15 << 16 << 17);
536 NFD_LOG_WARN("warning-message-XXXhdhd11" << 1 <<"x");
537 NFD_LOG_INFO("info-message-Jjxjshj13");
538 NFD_LOG_ERROR("error-message-!#$&^%$#@");
539 NFD_LOG_FATAL("fatal-message-JJSjaamcng");
Alexander Afanasyev66886812014-01-31 14:48:48 -0800540 }
Alexander Afanasyevb84cc922014-02-24 17:52:58 -0800541
Alexander Afanasyev66886812014-01-31 14:48:48 -0800542private:
543 NFD_LOG_INCLASS_DECLARE();
544};
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800545
Alexander Afanasyev66886812014-01-31 14:48:48 -0800546NFD_LOG_INCLASS_DEFINE(InClassLogger, "InClassLogger");
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800547
Alexander Afanasyev66886812014-01-31 14:48:48 -0800548BOOST_FIXTURE_TEST_CASE(InClass, InClassLogger)
549{
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600550 using namespace ndn::time;
551 using std::string;
552
553 const ndn::time::microseconds::rep ONE_SECOND = 1000000;
554
555 microseconds::rep before =
556 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
557
Alexander Afanasyev66886812014-01-31 14:48:48 -0800558 writeLogs();
Alexander Afanasyevb84cc922014-02-24 17:52:58 -0800559
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600560 microseconds::rep after =
561 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
562
563 const string EXPECTED[] =
564 {
565 "TRACE:", "[InClassLogger]", "trace-message-JHGFDSR^1\n",
566 "DEBUG:", "[InClassLogger]", "debug-message-IGg2474fdksd-fo-151617\n",
567 "WARNING:", "[InClassLogger]", "warning-message-XXXhdhd111x\n",
568 "INFO:", "[InClassLogger]", "info-message-Jjxjshj13\n",
569 "ERROR:", "[InClassLogger]", "error-message-!#$&^%$#@\n",
570 "FATAL:", "[InClassLogger]", "fatal-message-JJSjaamcng\n",
571 };
572
573 const size_t N_EXPECTED = sizeof(EXPECTED) / sizeof(string);
574
575 const string buffer = m_buffer.str();
576
577 std::vector<string> components;
578 boost::split(components, buffer, boost::is_any_of(" ,\n"));
579
580 // expected + number of timestamps (one per log statement) + trailing newline of last statement
581 BOOST_REQUIRE_EQUAL(components.size(), N_EXPECTED + 6 + 1);
582
583 std::vector<std::string>::const_iterator componentIter = components.begin();
584 for (size_t i = 0; i < N_EXPECTED; ++i)
585 {
586 // timestamp LOG_LEVEL: [ModuleName] message\n
587
588 const string& timestamp = *componentIter;
589 // std::cout << "timestamp = " << timestamp << std::endl;
590 ++componentIter;
591
592 size_t timeDelimiterPosition = timestamp.find(".");
593
594 BOOST_REQUIRE_NE(string::npos, timeDelimiterPosition);
595
596 string secondsString = timestamp.substr(0, timeDelimiterPosition);
597 string usecondsString = timestamp.substr(timeDelimiterPosition + 1);
598
599 microseconds::rep extractedTime =
600 ONE_SECOND * boost::lexical_cast<microseconds::rep>(secondsString) +
601 boost::lexical_cast<microseconds::rep>(usecondsString);
602
603 // std::cout << "before=" << before << " extracted=" << extractedTime << " after=" << after << std::endl;
604
605 BOOST_REQUIRE(before <= extractedTime);
606 BOOST_REQUIRE(extractedTime <= after);
607
608 // LOG_LEVEL:
609 BOOST_REQUIRE_EQUAL(*componentIter, EXPECTED[i]);
610 ++componentIter;
611 ++i;
612
613 // [ModuleName]
614 BOOST_REQUIRE_EQUAL(*componentIter, EXPECTED[i]);
615 ++componentIter;
616 ++i;
617
618 const string& message = *componentIter;
619
620 // std::cout << "message = " << message << std::endl;
621
622 // add back the newline that we split on
623 BOOST_REQUIRE_EQUAL(message + "\n", EXPECTED[i]);
624 ++componentIter;
625 }
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800626}
627
Alexander Afanasyev66886812014-01-31 14:48:48 -0800628
629template<class T>
630class InClassTemplateLogger : public LoggerFixture
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800631{
Alexander Afanasyev66886812014-01-31 14:48:48 -0800632public:
Alexander Afanasyevb84cc922014-02-24 17:52:58 -0800633 InClassTemplateLogger()
634 {
635 g_logger.setLogLevel(LOG_ALL);
636 }
637
Alexander Afanasyev66886812014-01-31 14:48:48 -0800638 void
639 writeLogs()
640 {
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600641 NFD_LOG_TRACE("trace-message-JHGFDSR^1");
642 NFD_LOG_DEBUG("debug-message-IGg2474fdksd-fo-" << 15 << 16 << 17);
643 NFD_LOG_WARN("warning-message-XXXhdhd11" << 1 <<"x");
644 NFD_LOG_INFO("info-message-Jjxjshj13");
645 NFD_LOG_ERROR("error-message-!#$&^%$#@");
646 NFD_LOG_FATAL("fatal-message-JJSjaamcng");
Alexander Afanasyev66886812014-01-31 14:48:48 -0800647 }
Alexander Afanasyevb84cc922014-02-24 17:52:58 -0800648
Alexander Afanasyev66886812014-01-31 14:48:48 -0800649private:
650 NFD_LOG_INCLASS_DECLARE();
651};
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800652
Alexander Afanasyev66886812014-01-31 14:48:48 -0800653NFD_LOG_INCLASS_TEMPLATE_DEFINE(InClassTemplateLogger, "GenericInClassTemplateLogger");
654NFD_LOG_INCLASS_TEMPLATE_SPECIALIZATION_DEFINE(InClassTemplateLogger, int, "IntInClassLogger");
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800655
Alexander Afanasyev66886812014-01-31 14:48:48 -0800656BOOST_FIXTURE_TEST_CASE(GenericInTemplatedClass, InClassTemplateLogger<bool>)
657{
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600658 using namespace ndn::time;
659 using std::string;
660
661 const ndn::time::microseconds::rep ONE_SECOND = 1000000;
662
663 microseconds::rep before =
664 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
665
Alexander Afanasyev66886812014-01-31 14:48:48 -0800666 writeLogs();
Alexander Afanasyevb84cc922014-02-24 17:52:58 -0800667
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600668 microseconds::rep after =
669 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
670
671 const string EXPECTED[] =
672 {
673 "TRACE:", "[GenericInClassTemplateLogger]", "trace-message-JHGFDSR^1\n",
674 "DEBUG:", "[GenericInClassTemplateLogger]", "debug-message-IGg2474fdksd-fo-151617\n",
675 "WARNING:", "[GenericInClassTemplateLogger]", "warning-message-XXXhdhd111x\n",
676 "INFO:", "[GenericInClassTemplateLogger]", "info-message-Jjxjshj13\n",
677 "ERROR:", "[GenericInClassTemplateLogger]", "error-message-!#$&^%$#@\n",
678 "FATAL:", "[GenericInClassTemplateLogger]", "fatal-message-JJSjaamcng\n",
679 };
680
681 const size_t N_EXPECTED = sizeof(EXPECTED) / sizeof(string);
682
683 const string buffer = m_buffer.str();
684
685 std::vector<string> components;
686 boost::split(components, buffer, boost::is_any_of(" ,\n"));
687
688 // expected + number of timestamps (one per log statement) + trailing newline of last statement
689 BOOST_REQUIRE_EQUAL(components.size(), N_EXPECTED + 6 + 1);
690
691 std::vector<std::string>::const_iterator componentIter = components.begin();
692 for (size_t i = 0; i < N_EXPECTED; ++i)
693 {
694 // timestamp LOG_LEVEL: [ModuleName] message\n
695
696 const string& timestamp = *componentIter;
697 // std::cout << "timestamp = " << timestamp << std::endl;
698 ++componentIter;
699
700 size_t timeDelimiterPosition = timestamp.find(".");
701
702 BOOST_REQUIRE_NE(string::npos, timeDelimiterPosition);
703
704 string secondsString = timestamp.substr(0, timeDelimiterPosition);
705 string usecondsString = timestamp.substr(timeDelimiterPosition + 1);
706
707 microseconds::rep extractedTime =
708 ONE_SECOND * boost::lexical_cast<microseconds::rep>(secondsString) +
709 boost::lexical_cast<microseconds::rep>(usecondsString);
710
711 // std::cout << "before=" << before << " extracted=" << extractedTime << " after=" << after << std::endl;
712
713 BOOST_REQUIRE(before <= extractedTime);
714 BOOST_REQUIRE(extractedTime <= after);
715
716 // LOG_LEVEL:
717 BOOST_REQUIRE_EQUAL(*componentIter, EXPECTED[i]);
718 ++componentIter;
719 ++i;
720
721 // [ModuleName]
722 BOOST_REQUIRE_EQUAL(*componentIter, EXPECTED[i]);
723 ++componentIter;
724 ++i;
725
726 const string& message = *componentIter;
727
728 // std::cout << "message = " << message << std::endl;
729
730 // add back the newline that we split on
731 BOOST_REQUIRE_EQUAL(message + "\n", EXPECTED[i]);
732 ++componentIter;
733 }
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800734}
735
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600736
Alexander Afanasyev66886812014-01-31 14:48:48 -0800737BOOST_FIXTURE_TEST_CASE(SpecializedInTemplatedClass, InClassTemplateLogger<int>)
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800738{
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600739 using namespace ndn::time;
740 using std::string;
741
742 const ndn::time::microseconds::rep ONE_SECOND = 1000000;
743
744 microseconds::rep before =
745 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
746
Alexander Afanasyev66886812014-01-31 14:48:48 -0800747 writeLogs();
Alexander Afanasyevb84cc922014-02-24 17:52:58 -0800748
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600749 microseconds::rep after =
750 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
751
752 const string EXPECTED[] =
753 {
754 "TRACE:", "[IntInClassLogger]", "trace-message-JHGFDSR^1\n",
755 "DEBUG:", "[IntInClassLogger]", "debug-message-IGg2474fdksd-fo-151617\n",
756 "WARNING:", "[IntInClassLogger]", "warning-message-XXXhdhd111x\n",
757 "INFO:", "[IntInClassLogger]", "info-message-Jjxjshj13\n",
758 "ERROR:", "[IntInClassLogger]", "error-message-!#$&^%$#@\n",
759 "FATAL:", "[IntInClassLogger]", "fatal-message-JJSjaamcng\n",
760 };
761
762 const size_t N_EXPECTED = sizeof(EXPECTED) / sizeof(string);
763
764 const string buffer = m_buffer.str();
765
766 std::vector<string> components;
767 boost::split(components, buffer, boost::is_any_of(" ,\n"));
768
769 // expected + number of timestamps (one per log statement) + trailing newline of last statement
770 BOOST_REQUIRE_EQUAL(components.size(), N_EXPECTED + 6 + 1);
771
772 std::vector<std::string>::const_iterator componentIter = components.begin();
773 for (size_t i = 0; i < N_EXPECTED; ++i)
774 {
775 // timestamp LOG_LEVEL: [ModuleName] message\n
776
777 const string& timestamp = *componentIter;
778 // std::cout << "timestamp = " << timestamp << std::endl;
779 ++componentIter;
780
781 size_t timeDelimiterPosition = timestamp.find(".");
782
783 BOOST_REQUIRE_NE(string::npos, timeDelimiterPosition);
784
785 string secondsString = timestamp.substr(0, timeDelimiterPosition);
786 string usecondsString = timestamp.substr(timeDelimiterPosition + 1);
787
788 microseconds::rep extractedTime =
789 ONE_SECOND * boost::lexical_cast<microseconds::rep>(secondsString) +
790 boost::lexical_cast<microseconds::rep>(usecondsString);
791
792 // std::cout << "before=" << before << " extracted=" << extractedTime << " after=" << after << std::endl;
793
794 BOOST_REQUIRE(before <= extractedTime);
795 BOOST_REQUIRE(extractedTime <= after);
796
797 // LOG_LEVEL:
798 BOOST_REQUIRE_EQUAL(*componentIter, EXPECTED[i]);
799 ++componentIter;
800 ++i;
801
802 // [ModuleName]
803 BOOST_REQUIRE_EQUAL(*componentIter, EXPECTED[i]);
804 ++componentIter;
805 ++i;
806
807 const string& message = *componentIter;
808
809 // std::cout << "message = " << message << std::endl;
810
811 // add back the newline that we split on
812 BOOST_REQUIRE_EQUAL(message + "\n", EXPECTED[i]);
813 ++componentIter;
814 }
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800815}
816
817BOOST_AUTO_TEST_SUITE_END()
818
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700819} // namespace tests
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800820} // namespace nfd