blob: 628c1cefc8344988783c0877ff0a4d2d87757029 [file] [log] [blame]
Ilya Moiseenkoa807e652014-01-28 11:51:01 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (C) 2014 Named Data Networking Project
4 * See COPYING for copyright and distribution information.
5 *
6 * Author: Ilya Moiseenko <iliamo@ucla.edu>
7 */
8
9#include "core/logger.hpp"
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060010
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080011#include <boost/test/unit_test.hpp>
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -060012#include <boost/algorithm/string.hpp>
13#include <boost/algorithm/string/classification.hpp>
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060014
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080015#include <iostream>
16
Junxiao Shid9ee45c2014-02-27 15:38:11 -070017#include "tests/test-common.hpp"
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080018
19namespace nfd {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070020namespace tests {
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080021
Junxiao Shid9ee45c2014-02-27 15:38:11 -070022BOOST_FIXTURE_TEST_SUITE(CoreLogger, BaseFixture)
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080023
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060024class LoggerFixture : protected BaseFixture
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080025{
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060026public:
Alexander Afanasyev66886812014-01-31 14:48:48 -080027 LoggerFixture()
Steve DiBenedetto4d43a452014-04-06 18:55:29 -060028 : m_savedBuf(std::clog.rdbuf())
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060029 , m_savedLevel(LoggerFactory::getInstance().getDefaultLevel())
Alexander Afanasyev66886812014-01-31 14:48:48 -080030 {
Steve DiBenedetto4d43a452014-04-06 18:55:29 -060031 std::clog.rdbuf(m_buffer.rdbuf());
Alexander Afanasyev66886812014-01-31 14:48:48 -080032 }
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080033
Alexander Afanasyev66886812014-01-31 14:48:48 -080034 ~LoggerFixture()
35 {
Steve DiBenedetto4d43a452014-04-06 18:55:29 -060036 std::clog.rdbuf(m_savedBuf);
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060037 LoggerFactory::getInstance().setDefaultLevel(m_savedLevel);
Alexander Afanasyev66886812014-01-31 14:48:48 -080038 }
Alexander Afanasyevb84cc922014-02-24 17:52:58 -080039
Alexander Afanasyev66886812014-01-31 14:48:48 -080040 std::stringstream m_buffer;
41 std::streambuf* m_savedBuf;
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060042 LogLevel m_savedLevel;
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -060043
Alexander Afanasyev66886812014-01-31 14:48:48 -080044};
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080045
Alexander Afanasyev66886812014-01-31 14:48:48 -080046BOOST_FIXTURE_TEST_CASE(Basic, LoggerFixture)
47{
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -060048 using namespace ndn::time;
49 using std::string;
50
51 const ndn::time::microseconds::rep ONE_SECOND = 1000000;
52
Alexander Afanasyev66886812014-01-31 14:48:48 -080053 NFD_LOG_INIT("BasicTests");
Alexander Afanasyevb84cc922014-02-24 17:52:58 -080054 g_logger.setLogLevel(LOG_ALL);
55
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -060056 const string EXPECTED[] =
57 {
58 "TRACE:", "[BasicTests]", "trace-message-JHGFDSR^1\n",
59 "DEBUG:", "[BasicTests]", "debug-message-IGg2474fdksd-fo-151617\n",
60 "WARNING:", "[BasicTests]", "warning-message-XXXhdhd111x\n",
61 "INFO:", "[BasicTests]", "info-message-Jjxjshj13\n",
62 "ERROR:", "[BasicTests]", "error-message-!#$&^%$#@\n",
63 "FATAL:", "[BasicTests]", "fatal-message-JJSjaamcng\n",
64 };
Alexander Afanasyevb84cc922014-02-24 17:52:58 -080065
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -060066 const size_t N_EXPECTED = sizeof(EXPECTED) / sizeof(string);
67
68 microseconds::rep before =
69 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
70
71 NFD_LOG_TRACE("trace-message-JHGFDSR^1");
72 NFD_LOG_DEBUG("debug-message-IGg2474fdksd-fo-" << 15 << 16 << 17);
73 NFD_LOG_WARN("warning-message-XXXhdhd11" << 1 <<"x");
74 NFD_LOG_INFO("info-message-Jjxjshj13");
75 NFD_LOG_ERROR("error-message-!#$&^%$#@");
76 NFD_LOG_FATAL("fatal-message-JJSjaamcng");
77
78 microseconds::rep after =
79 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
80
81 const string buffer = m_buffer.str();
82
83 std::vector<string> components;
84 boost::split(components, buffer, boost::is_any_of(" ,\n"));
85
86 // std::cout << components.size() << " for " << moduleName << std::endl;
87 // for (size_t i = 0; i < components.size(); ++i)
88 // {
89 // std::cout << "-> " << components[i] << std::endl;
90 // }
91
92 // expected + number of timestamps (one per log statement) + trailing newline of last statement
93 BOOST_REQUIRE_EQUAL(components.size(), N_EXPECTED + 6 + 1);
94
95 std::vector<std::string>::const_iterator componentIter = components.begin();
96 for (size_t i = 0; i < N_EXPECTED; ++i)
97 {
98 // timestamp LOG_LEVEL: [ModuleName] message\n
99
100 const string& timestamp = *componentIter;
101 // std::cout << "timestamp = " << timestamp << std::endl;
102 ++componentIter;
103
104 size_t timeDelimiterPosition = timestamp.find(".");
105
106 BOOST_REQUIRE_NE(string::npos, timeDelimiterPosition);
107
108 string secondsString = timestamp.substr(0, timeDelimiterPosition);
109 string usecondsString = timestamp.substr(timeDelimiterPosition + 1);
110
111 microseconds::rep extractedTime =
112 ONE_SECOND * boost::lexical_cast<microseconds::rep>(secondsString) +
113 boost::lexical_cast<microseconds::rep>(usecondsString);
114
115 // std::cout << "before=" << before << " extracted=" << extractedTime << " after=" << after << std::endl;
116
117 BOOST_REQUIRE(before <= extractedTime);
118 BOOST_REQUIRE(extractedTime <= after);
119
120 // LOG_LEVEL:
121 BOOST_REQUIRE_EQUAL(*componentIter, EXPECTED[i]);
122 ++componentIter;
123 ++i;
124
125 // [ModuleName]
126 BOOST_REQUIRE_EQUAL(*componentIter, EXPECTED[i]);
127 ++componentIter;
128 ++i;
129
130 const string& message = *componentIter;
131
132 // std::cout << "message = " << message << std::endl;
133
134 // add back the newline that we split on
135 BOOST_REQUIRE_EQUAL(message + "\n", EXPECTED[i]);
136 ++componentIter;
137 }
138
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800139}
140
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600141
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600142BOOST_FIXTURE_TEST_CASE(ConfigureFactory, LoggerFixture)
143{
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600144 using namespace ndn::time;
145 using std::string;
146
147 const ndn::time::microseconds::rep ONE_SECOND = 1000000;
148
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600149 NFD_LOG_INIT("ConfigureFactoryTests");
150
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600151 const string LOG_CONFIG =
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600152 "log\n"
153 "{\n"
154 " default_level INFO\n"
155 "}\n";
156
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600157 LoggerFactory::getInstance().setDefaultLevel(LOG_ALL);
158
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600159 ConfigFile config;
160 LoggerFactory::getInstance().setConfigFile(config);
161
162 config.parse(LOG_CONFIG, false, "LOG_CONFIG");
163
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600164 BOOST_REQUIRE_EQUAL(LoggerFactory::getInstance().getDefaultLevel(), LOG_INFO);
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600165
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600166 const std::string EXPECTED[] =
167 {
168 "WARNING:", "[ConfigureFactoryTests]", "warning-message-XXXhdhd111x\n",
169 "INFO:", "[ConfigureFactoryTests]", "info-message-Jjxjshj13\n",
170 "ERROR:", "[ConfigureFactoryTests]", "error-message-!#$&^%$#@\n",
171 "FATAL:", "[ConfigureFactoryTests]", "fatal-message-JJSjaamcng\n",
172 };
173
174 const size_t N_EXPECTED = sizeof(EXPECTED) / sizeof(std::string);
175
176 microseconds::rep before =
177 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
178
179 NFD_LOG_TRACE("trace-message-JHGFDSR^1");
180 NFD_LOG_DEBUG("debug-message-IGg2474fdksd-fo-" << 15 << 16 << 17);
181 NFD_LOG_WARN("warning-message-XXXhdhd11" << 1 <<"x");
182 NFD_LOG_INFO("info-message-Jjxjshj13");
183 NFD_LOG_ERROR("error-message-!#$&^%$#@");
184 NFD_LOG_FATAL("fatal-message-JJSjaamcng");
185
186 microseconds::rep after =
187 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
188
189 const string buffer = m_buffer.str();
190
191 std::vector<string> components;
192 boost::split(components, buffer, boost::is_any_of(" ,\n"));
193
194 // std::cout << components.size() << " for " << moduleName << std::endl;
195 // for (size_t i = 0; i < components.size(); ++i)
196 // {
197 // std::cout << "-> " << components[i] << std::endl;
198 // }
199
200 // expected + number of timestamps (one per log statement) + trailing newline of last statement
201 BOOST_REQUIRE_EQUAL(components.size(), N_EXPECTED + 4 + 1);
202
203 std::vector<std::string>::const_iterator componentIter = components.begin();
204 for (size_t i = 0; i < N_EXPECTED; ++i)
205 {
206 // timestamp LOG_LEVEL: [ModuleName] message\n
207
208 const string& timestamp = *componentIter;
209 // std::cout << "timestamp = " << timestamp << std::endl;
210 ++componentIter;
211
212 size_t timeDelimiterPosition = timestamp.find(".");
213
214 BOOST_REQUIRE_NE(string::npos, timeDelimiterPosition);
215
216 string secondsString = timestamp.substr(0, timeDelimiterPosition);
217 string usecondsString = timestamp.substr(timeDelimiterPosition + 1);
218
219 microseconds::rep extractedTime =
220 ONE_SECOND * boost::lexical_cast<microseconds::rep>(secondsString) +
221 boost::lexical_cast<microseconds::rep>(usecondsString);
222
223 // std::cout << "before=" << before << " extracted=" << extractedTime << " after=" << after << std::endl;
224
225 BOOST_REQUIRE(before <= extractedTime);
226 BOOST_REQUIRE(extractedTime <= after);
227
228 // LOG_LEVEL:
229 BOOST_REQUIRE_EQUAL(*componentIter, EXPECTED[i]);
230 ++componentIter;
231 ++i;
232
233 // [ModuleName]
234 BOOST_REQUIRE_EQUAL(*componentIter, EXPECTED[i]);
235 ++componentIter;
236 ++i;
237
238 const string& message = *componentIter;
239
240 // std::cout << "message = " << message << std::endl;
241
242 // add back the newline that we split on
243 BOOST_REQUIRE_EQUAL(message + "\n", EXPECTED[i]);
244 ++componentIter;
245 }
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600246}
247
248BOOST_FIXTURE_TEST_CASE(TestNumberLevel, LoggerFixture)
249{
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600250 const std::string LOG_CONFIG =
251 "log\n"
252 "{\n"
253 " default_level 2\n" // equivalent of WARN
254 "}\n";
255
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600256 LoggerFactory::getInstance().setDefaultLevel(LOG_ALL);
257
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600258 ConfigFile config;
259 LoggerFactory::getInstance().setConfigFile(config);
260
261 config.parse(LOG_CONFIG, false, "LOG_CONFIG");
262
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600263 BOOST_REQUIRE_EQUAL(LoggerFactory::getInstance().getDefaultLevel(), LOG_WARN);
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600264}
265
266static void
267testModuleBPrint()
268{
269 NFD_LOG_INIT("TestModuleB");
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600270 NFD_LOG_DEBUG("debug-message-IGg2474fdksd-fo-" << 15 << 16 << 17);
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600271}
272
273BOOST_FIXTURE_TEST_CASE(LimitModules, LoggerFixture)
274{
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600275 using namespace ndn::time;
276 using std::string;
277
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600278 NFD_LOG_INIT("TestModuleA");
279
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600280 const ndn::time::microseconds::rep ONE_SECOND = 1000000;
281
282 const std::string EXPECTED[] =
283 {
284 "WARNING:", "[TestModuleA]", "warning-message-XXXhdhd111x\n",
285 };
286
287 const size_t N_EXPECTED = sizeof(EXPECTED) / sizeof(std::string);
288
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600289 const std::string LOG_CONFIG =
290 "log\n"
291 "{\n"
292 " default_level WARN\n"
293 "}\n";
294
295 ConfigFile config;
296 LoggerFactory::getInstance().setConfigFile(config);
297
298 config.parse(LOG_CONFIG, false, "LOG_CONFIG");
299
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600300 microseconds::rep before =
301 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
302
303 // this should print
304 NFD_LOG_WARN("warning-message-XXXhdhd11" << 1 << "x");
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600305
306 // this should not because it's level is < WARN
307 testModuleBPrint();
308
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600309 microseconds::rep after =
310 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
311
312 const string buffer = m_buffer.str();
313
314 std::vector<string> components;
315 boost::split(components, buffer, boost::is_any_of(" ,\n"));
316
317 // expected + number of timestamps (one per log statement) + trailing newline of last statement
318 BOOST_REQUIRE_EQUAL(components.size(), N_EXPECTED + 1 + 1);
319
320 std::vector<std::string>::const_iterator componentIter = components.begin();
321 for (size_t i = 0; i < N_EXPECTED; ++i)
322 {
323 // timestamp LOG_LEVEL: [ModuleName] message\n
324
325 const string& timestamp = *componentIter;
326 // std::cout << "timestamp = " << timestamp << std::endl;
327 ++componentIter;
328
329 size_t timeDelimiterPosition = timestamp.find(".");
330
331 BOOST_REQUIRE_NE(string::npos, timeDelimiterPosition);
332
333 string secondsString = timestamp.substr(0, timeDelimiterPosition);
334 string usecondsString = timestamp.substr(timeDelimiterPosition + 1);
335
336 microseconds::rep extractedTime =
337 ONE_SECOND * boost::lexical_cast<microseconds::rep>(secondsString) +
338 boost::lexical_cast<microseconds::rep>(usecondsString);
339
340 // std::cout << "before=" << before << " extracted=" << extractedTime << " after=" << after << std::endl;
341
342 BOOST_REQUIRE(before <= extractedTime);
343 BOOST_REQUIRE(extractedTime <= after);
344
345 // LOG_LEVEL:
346 BOOST_REQUIRE_EQUAL(*componentIter, EXPECTED[i]);
347 ++componentIter;
348 ++i;
349
350 // [ModuleName]
351 BOOST_REQUIRE_EQUAL(*componentIter, EXPECTED[i]);
352 ++componentIter;
353 ++i;
354
355 const string& message = *componentIter;
356
357 // std::cout << "message = " << message << std::endl;
358
359 // add back the newline that we split on
360 BOOST_REQUIRE_EQUAL(message + "\n", EXPECTED[i]);
361 ++componentIter;
362 }
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600363}
364
365BOOST_FIXTURE_TEST_CASE(ExplicitlySetModule, LoggerFixture)
366{
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600367 using namespace ndn::time;
368 using std::string;
369
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600370 NFD_LOG_INIT("TestModuleA");
371
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600372 const ndn::time::microseconds::rep ONE_SECOND = 1000000;
373
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600374 const std::string LOG_CONFIG =
375 "log\n"
376 "{\n"
377 " default_level WARN\n"
378 " TestModuleB DEBUG\n"
379 "}\n";
380
381 ConfigFile config;
382 LoggerFactory::getInstance().setConfigFile(config);
383
384 config.parse(LOG_CONFIG, false, "LOG_CONFIG");
385
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600386 microseconds::rep before =
387 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
388
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600389 // this should print
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600390 NFD_LOG_WARN("warning-message-XXXhdhd11" << 1 << "x");
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600391
392 // this too because its level is explicitly set to DEBUG
393 testModuleBPrint();
394
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600395 microseconds::rep after =
396 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
397
398 const std::string EXPECTED[] =
399 {
400 "WARNING:", "[TestModuleA]", "warning-message-XXXhdhd111x\n",
401 "DEBUG:", "[TestModuleB]", "debug-message-IGg2474fdksd-fo-151617\n",
402 };
403
404 const size_t N_EXPECTED = sizeof(EXPECTED) / sizeof(std::string);
405
406 const string buffer = m_buffer.str();
407
408 std::vector<string> components;
409 boost::split(components, buffer, boost::is_any_of(" ,\n"));
410
411 // for (size_t i = 0; i < components.size(); ++i)
412 // {
413 // std::cout << "-> " << components[i] << std::endl;
414 // }
415
416 // expected + number of timestamps (one per log statement) + trailing newline of last statement
417 BOOST_REQUIRE_EQUAL(components.size(), N_EXPECTED + 2 + 1);
418
419 std::vector<std::string>::const_iterator componentIter = components.begin();
420 for (size_t i = 0; i < N_EXPECTED; ++i)
421 {
422 // timestamp LOG_LEVEL: [ModuleName] message\n
423
424 const string& timestamp = *componentIter;
425 // std::cout << "timestamp = " << timestamp << std::endl;
426 ++componentIter;
427
428 size_t timeDelimiterPosition = timestamp.find(".");
429
430 BOOST_REQUIRE_NE(string::npos, timeDelimiterPosition);
431
432 string secondsString = timestamp.substr(0, timeDelimiterPosition);
433 string usecondsString = timestamp.substr(timeDelimiterPosition + 1);
434
435 microseconds::rep extractedTime =
436 ONE_SECOND * boost::lexical_cast<microseconds::rep>(secondsString) +
437 boost::lexical_cast<microseconds::rep>(usecondsString);
438
439 // std::cout << "before=" << before << " extracted=" << extractedTime << " after=" << after << std::endl;
440
441 BOOST_REQUIRE(before <= extractedTime);
442 BOOST_REQUIRE(extractedTime <= after);
443
444 // LOG_LEVEL:
445 BOOST_REQUIRE_EQUAL(*componentIter, EXPECTED[i]);
446 ++componentIter;
447 ++i;
448
449 // [ModuleName]
450 BOOST_REQUIRE_EQUAL(*componentIter, EXPECTED[i]);
451 ++componentIter;
452 ++i;
453
454 const string& message = *componentIter;
455
456 // std::cout << "message = " << message << std::endl;
457
458 // add back the newline that we split on
459 BOOST_REQUIRE_EQUAL(message + "\n", EXPECTED[i]);
460 ++componentIter;
461 }
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600462}
463
464static bool
465checkError(const LoggerFactory::Error& error, const std::string& expected)
466{
467 return error.what() == expected;
468}
469
470BOOST_FIXTURE_TEST_CASE(UnknownLevelString, LoggerFixture)
471{
472 const std::string LOG_CONFIG =
473 "log\n"
474 "{\n"
475 " default_level TestMadeUpLevel\n"
476 "}\n";
477
478 ConfigFile config;
479 LoggerFactory::getInstance().setConfigFile(config);
480
481 BOOST_REQUIRE_EXCEPTION(config.parse(LOG_CONFIG, false, "LOG_CONFIG"),
482 LoggerFactory::Error,
483 bind(&checkError,
484 _1,
485 "Unsupported logging level \"TestMadeUpLevel\""));
486}
487
488BOOST_FIXTURE_TEST_CASE(UnknownOption, LoggerFixture)
489{
490 const std::string LOG_CONFIG =
491 "log\n"
492 "{\n"
493 " TestMadeUpOption\n"
494 "}\n";
495
496 ConfigFile config;
497 LoggerFactory::getInstance().setConfigFile(config);
498
499 BOOST_REQUIRE_EXCEPTION(config.parse(LOG_CONFIG, false, "LOG_CONFIG"),
500 LoggerFactory::Error,
501 bind(&checkError,
502 _1,
503 "No logging level found for option \"TestMadeUpOption\""));
504}
505
Alexander Afanasyev66886812014-01-31 14:48:48 -0800506class InClassLogger : public LoggerFixture
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800507{
Alexander Afanasyev66886812014-01-31 14:48:48 -0800508public:
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600509
Alexander Afanasyevb84cc922014-02-24 17:52:58 -0800510 InClassLogger()
511 {
512 g_logger.setLogLevel(LOG_ALL);
513 }
514
Alexander Afanasyev66886812014-01-31 14:48:48 -0800515 void
516 writeLogs()
517 {
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600518 NFD_LOG_TRACE("trace-message-JHGFDSR^1");
519 NFD_LOG_DEBUG("debug-message-IGg2474fdksd-fo-" << 15 << 16 << 17);
520 NFD_LOG_WARN("warning-message-XXXhdhd11" << 1 <<"x");
521 NFD_LOG_INFO("info-message-Jjxjshj13");
522 NFD_LOG_ERROR("error-message-!#$&^%$#@");
523 NFD_LOG_FATAL("fatal-message-JJSjaamcng");
Alexander Afanasyev66886812014-01-31 14:48:48 -0800524 }
Alexander Afanasyevb84cc922014-02-24 17:52:58 -0800525
Alexander Afanasyev66886812014-01-31 14:48:48 -0800526private:
527 NFD_LOG_INCLASS_DECLARE();
528};
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800529
Alexander Afanasyev66886812014-01-31 14:48:48 -0800530NFD_LOG_INCLASS_DEFINE(InClassLogger, "InClassLogger");
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800531
Alexander Afanasyev66886812014-01-31 14:48:48 -0800532BOOST_FIXTURE_TEST_CASE(InClass, InClassLogger)
533{
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600534 using namespace ndn::time;
535 using std::string;
536
537 const ndn::time::microseconds::rep ONE_SECOND = 1000000;
538
539 microseconds::rep before =
540 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
541
Alexander Afanasyev66886812014-01-31 14:48:48 -0800542 writeLogs();
Alexander Afanasyevb84cc922014-02-24 17:52:58 -0800543
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600544 microseconds::rep after =
545 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
546
547 const string EXPECTED[] =
548 {
549 "TRACE:", "[InClassLogger]", "trace-message-JHGFDSR^1\n",
550 "DEBUG:", "[InClassLogger]", "debug-message-IGg2474fdksd-fo-151617\n",
551 "WARNING:", "[InClassLogger]", "warning-message-XXXhdhd111x\n",
552 "INFO:", "[InClassLogger]", "info-message-Jjxjshj13\n",
553 "ERROR:", "[InClassLogger]", "error-message-!#$&^%$#@\n",
554 "FATAL:", "[InClassLogger]", "fatal-message-JJSjaamcng\n",
555 };
556
557 const size_t N_EXPECTED = sizeof(EXPECTED) / sizeof(string);
558
559 const string buffer = m_buffer.str();
560
561 std::vector<string> components;
562 boost::split(components, buffer, boost::is_any_of(" ,\n"));
563
564 // expected + number of timestamps (one per log statement) + trailing newline of last statement
565 BOOST_REQUIRE_EQUAL(components.size(), N_EXPECTED + 6 + 1);
566
567 std::vector<std::string>::const_iterator componentIter = components.begin();
568 for (size_t i = 0; i < N_EXPECTED; ++i)
569 {
570 // timestamp LOG_LEVEL: [ModuleName] message\n
571
572 const string& timestamp = *componentIter;
573 // std::cout << "timestamp = " << timestamp << std::endl;
574 ++componentIter;
575
576 size_t timeDelimiterPosition = timestamp.find(".");
577
578 BOOST_REQUIRE_NE(string::npos, timeDelimiterPosition);
579
580 string secondsString = timestamp.substr(0, timeDelimiterPosition);
581 string usecondsString = timestamp.substr(timeDelimiterPosition + 1);
582
583 microseconds::rep extractedTime =
584 ONE_SECOND * boost::lexical_cast<microseconds::rep>(secondsString) +
585 boost::lexical_cast<microseconds::rep>(usecondsString);
586
587 // std::cout << "before=" << before << " extracted=" << extractedTime << " after=" << after << std::endl;
588
589 BOOST_REQUIRE(before <= extractedTime);
590 BOOST_REQUIRE(extractedTime <= after);
591
592 // LOG_LEVEL:
593 BOOST_REQUIRE_EQUAL(*componentIter, EXPECTED[i]);
594 ++componentIter;
595 ++i;
596
597 // [ModuleName]
598 BOOST_REQUIRE_EQUAL(*componentIter, EXPECTED[i]);
599 ++componentIter;
600 ++i;
601
602 const string& message = *componentIter;
603
604 // std::cout << "message = " << message << std::endl;
605
606 // add back the newline that we split on
607 BOOST_REQUIRE_EQUAL(message + "\n", EXPECTED[i]);
608 ++componentIter;
609 }
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800610}
611
Alexander Afanasyev66886812014-01-31 14:48:48 -0800612
613template<class T>
614class InClassTemplateLogger : public LoggerFixture
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800615{
Alexander Afanasyev66886812014-01-31 14:48:48 -0800616public:
Alexander Afanasyevb84cc922014-02-24 17:52:58 -0800617 InClassTemplateLogger()
618 {
619 g_logger.setLogLevel(LOG_ALL);
620 }
621
Alexander Afanasyev66886812014-01-31 14:48:48 -0800622 void
623 writeLogs()
624 {
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600625 NFD_LOG_TRACE("trace-message-JHGFDSR^1");
626 NFD_LOG_DEBUG("debug-message-IGg2474fdksd-fo-" << 15 << 16 << 17);
627 NFD_LOG_WARN("warning-message-XXXhdhd11" << 1 <<"x");
628 NFD_LOG_INFO("info-message-Jjxjshj13");
629 NFD_LOG_ERROR("error-message-!#$&^%$#@");
630 NFD_LOG_FATAL("fatal-message-JJSjaamcng");
Alexander Afanasyev66886812014-01-31 14:48:48 -0800631 }
Alexander Afanasyevb84cc922014-02-24 17:52:58 -0800632
Alexander Afanasyev66886812014-01-31 14:48:48 -0800633private:
634 NFD_LOG_INCLASS_DECLARE();
635};
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800636
Alexander Afanasyev66886812014-01-31 14:48:48 -0800637NFD_LOG_INCLASS_TEMPLATE_DEFINE(InClassTemplateLogger, "GenericInClassTemplateLogger");
638NFD_LOG_INCLASS_TEMPLATE_SPECIALIZATION_DEFINE(InClassTemplateLogger, int, "IntInClassLogger");
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800639
Alexander Afanasyev66886812014-01-31 14:48:48 -0800640BOOST_FIXTURE_TEST_CASE(GenericInTemplatedClass, InClassTemplateLogger<bool>)
641{
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600642 using namespace ndn::time;
643 using std::string;
644
645 const ndn::time::microseconds::rep ONE_SECOND = 1000000;
646
647 microseconds::rep before =
648 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
649
Alexander Afanasyev66886812014-01-31 14:48:48 -0800650 writeLogs();
Alexander Afanasyevb84cc922014-02-24 17:52:58 -0800651
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600652 microseconds::rep after =
653 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
654
655 const string EXPECTED[] =
656 {
657 "TRACE:", "[GenericInClassTemplateLogger]", "trace-message-JHGFDSR^1\n",
658 "DEBUG:", "[GenericInClassTemplateLogger]", "debug-message-IGg2474fdksd-fo-151617\n",
659 "WARNING:", "[GenericInClassTemplateLogger]", "warning-message-XXXhdhd111x\n",
660 "INFO:", "[GenericInClassTemplateLogger]", "info-message-Jjxjshj13\n",
661 "ERROR:", "[GenericInClassTemplateLogger]", "error-message-!#$&^%$#@\n",
662 "FATAL:", "[GenericInClassTemplateLogger]", "fatal-message-JJSjaamcng\n",
663 };
664
665 const size_t N_EXPECTED = sizeof(EXPECTED) / sizeof(string);
666
667 const string buffer = m_buffer.str();
668
669 std::vector<string> components;
670 boost::split(components, buffer, boost::is_any_of(" ,\n"));
671
672 // expected + number of timestamps (one per log statement) + trailing newline of last statement
673 BOOST_REQUIRE_EQUAL(components.size(), N_EXPECTED + 6 + 1);
674
675 std::vector<std::string>::const_iterator componentIter = components.begin();
676 for (size_t i = 0; i < N_EXPECTED; ++i)
677 {
678 // timestamp LOG_LEVEL: [ModuleName] message\n
679
680 const string& timestamp = *componentIter;
681 // std::cout << "timestamp = " << timestamp << std::endl;
682 ++componentIter;
683
684 size_t timeDelimiterPosition = timestamp.find(".");
685
686 BOOST_REQUIRE_NE(string::npos, timeDelimiterPosition);
687
688 string secondsString = timestamp.substr(0, timeDelimiterPosition);
689 string usecondsString = timestamp.substr(timeDelimiterPosition + 1);
690
691 microseconds::rep extractedTime =
692 ONE_SECOND * boost::lexical_cast<microseconds::rep>(secondsString) +
693 boost::lexical_cast<microseconds::rep>(usecondsString);
694
695 // std::cout << "before=" << before << " extracted=" << extractedTime << " after=" << after << std::endl;
696
697 BOOST_REQUIRE(before <= extractedTime);
698 BOOST_REQUIRE(extractedTime <= after);
699
700 // LOG_LEVEL:
701 BOOST_REQUIRE_EQUAL(*componentIter, EXPECTED[i]);
702 ++componentIter;
703 ++i;
704
705 // [ModuleName]
706 BOOST_REQUIRE_EQUAL(*componentIter, EXPECTED[i]);
707 ++componentIter;
708 ++i;
709
710 const string& message = *componentIter;
711
712 // std::cout << "message = " << message << std::endl;
713
714 // add back the newline that we split on
715 BOOST_REQUIRE_EQUAL(message + "\n", EXPECTED[i]);
716 ++componentIter;
717 }
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800718}
719
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600720
Alexander Afanasyev66886812014-01-31 14:48:48 -0800721BOOST_FIXTURE_TEST_CASE(SpecializedInTemplatedClass, InClassTemplateLogger<int>)
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800722{
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600723 using namespace ndn::time;
724 using std::string;
725
726 const ndn::time::microseconds::rep ONE_SECOND = 1000000;
727
728 microseconds::rep before =
729 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
730
Alexander Afanasyev66886812014-01-31 14:48:48 -0800731 writeLogs();
Alexander Afanasyevb84cc922014-02-24 17:52:58 -0800732
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600733 microseconds::rep after =
734 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
735
736 const string EXPECTED[] =
737 {
738 "TRACE:", "[IntInClassLogger]", "trace-message-JHGFDSR^1\n",
739 "DEBUG:", "[IntInClassLogger]", "debug-message-IGg2474fdksd-fo-151617\n",
740 "WARNING:", "[IntInClassLogger]", "warning-message-XXXhdhd111x\n",
741 "INFO:", "[IntInClassLogger]", "info-message-Jjxjshj13\n",
742 "ERROR:", "[IntInClassLogger]", "error-message-!#$&^%$#@\n",
743 "FATAL:", "[IntInClassLogger]", "fatal-message-JJSjaamcng\n",
744 };
745
746 const size_t N_EXPECTED = sizeof(EXPECTED) / sizeof(string);
747
748 const string buffer = m_buffer.str();
749
750 std::vector<string> components;
751 boost::split(components, buffer, boost::is_any_of(" ,\n"));
752
753 // expected + number of timestamps (one per log statement) + trailing newline of last statement
754 BOOST_REQUIRE_EQUAL(components.size(), N_EXPECTED + 6 + 1);
755
756 std::vector<std::string>::const_iterator componentIter = components.begin();
757 for (size_t i = 0; i < N_EXPECTED; ++i)
758 {
759 // timestamp LOG_LEVEL: [ModuleName] message\n
760
761 const string& timestamp = *componentIter;
762 // std::cout << "timestamp = " << timestamp << std::endl;
763 ++componentIter;
764
765 size_t timeDelimiterPosition = timestamp.find(".");
766
767 BOOST_REQUIRE_NE(string::npos, timeDelimiterPosition);
768
769 string secondsString = timestamp.substr(0, timeDelimiterPosition);
770 string usecondsString = timestamp.substr(timeDelimiterPosition + 1);
771
772 microseconds::rep extractedTime =
773 ONE_SECOND * boost::lexical_cast<microseconds::rep>(secondsString) +
774 boost::lexical_cast<microseconds::rep>(usecondsString);
775
776 // std::cout << "before=" << before << " extracted=" << extractedTime << " after=" << after << std::endl;
777
778 BOOST_REQUIRE(before <= extractedTime);
779 BOOST_REQUIRE(extractedTime <= after);
780
781 // LOG_LEVEL:
782 BOOST_REQUIRE_EQUAL(*componentIter, EXPECTED[i]);
783 ++componentIter;
784 ++i;
785
786 // [ModuleName]
787 BOOST_REQUIRE_EQUAL(*componentIter, EXPECTED[i]);
788 ++componentIter;
789 ++i;
790
791 const string& message = *componentIter;
792
793 // std::cout << "message = " << message << std::endl;
794
795 // add back the newline that we split on
796 BOOST_REQUIRE_EQUAL(message + "\n", EXPECTED[i]);
797 ++componentIter;
798 }
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800799}
800
801BOOST_AUTO_TEST_SUITE_END()
802
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700803} // namespace tests
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800804} // namespace nfd