blob: febadfc5479f5a576373b8d9849aad023a6c8ac9 [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
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600128 // std::cout << "before=" << before
129 // << " extracted=" << extractedTime
130 // << " after=" << after << std::endl;
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600131
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600132
133 BOOST_CHECK_LE(before, extractedTime);
134 BOOST_CHECK_LE(extractedTime, after);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600135
136 // LOG_LEVEL:
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600137 BOOST_CHECK_EQUAL(*componentIter, EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600138 ++componentIter;
139 ++i;
140
141 // [ModuleName]
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600142 BOOST_CHECK_EQUAL(*componentIter, EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600143 ++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
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600151 BOOST_CHECK_EQUAL(message + "\n", EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600152 ++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
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600239 // std::cout << "before=" << before
240 // << " extracted=" << extractedTime
241 // << " after=" << after << std::endl;
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600242
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600243 BOOST_CHECK_LE(before, extractedTime);
244 BOOST_CHECK_LE(extractedTime, after);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600245
246 // LOG_LEVEL:
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600247 BOOST_CHECK_EQUAL(*componentIter, EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600248 ++componentIter;
249 ++i;
250
251 // [ModuleName]
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600252 BOOST_CHECK_EQUAL(*componentIter, EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600253 ++componentIter;
254 ++i;
255
256 const string& message = *componentIter;
257
258 // std::cout << "message = " << message << std::endl;
259
260 // add back the newline that we split on
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600261 BOOST_CHECK_EQUAL(message + "\n", EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600262 ++componentIter;
263 }
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600264}
265
266BOOST_FIXTURE_TEST_CASE(TestNumberLevel, LoggerFixture)
267{
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600268 const std::string LOG_CONFIG =
269 "log\n"
270 "{\n"
271 " default_level 2\n" // equivalent of WARN
272 "}\n";
273
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600274 LoggerFactory::getInstance().setDefaultLevel(LOG_ALL);
275
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600276 ConfigFile config;
277 LoggerFactory::getInstance().setConfigFile(config);
278
279 config.parse(LOG_CONFIG, false, "LOG_CONFIG");
280
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600281 BOOST_REQUIRE_EQUAL(LoggerFactory::getInstance().getDefaultLevel(), LOG_WARN);
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600282}
283
284static void
285testModuleBPrint()
286{
287 NFD_LOG_INIT("TestModuleB");
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600288 NFD_LOG_DEBUG("debug-message-IGg2474fdksd-fo-" << 15 << 16 << 17);
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600289}
290
291BOOST_FIXTURE_TEST_CASE(LimitModules, LoggerFixture)
292{
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600293 using namespace ndn::time;
294 using std::string;
295
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600296 NFD_LOG_INIT("TestModuleA");
297
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600298 const ndn::time::microseconds::rep ONE_SECOND = 1000000;
299
300 const std::string EXPECTED[] =
301 {
302 "WARNING:", "[TestModuleA]", "warning-message-XXXhdhd111x\n",
303 };
304
305 const size_t N_EXPECTED = sizeof(EXPECTED) / sizeof(std::string);
306
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600307 const std::string LOG_CONFIG =
308 "log\n"
309 "{\n"
310 " default_level WARN\n"
311 "}\n";
312
313 ConfigFile config;
314 LoggerFactory::getInstance().setConfigFile(config);
315
316 config.parse(LOG_CONFIG, false, "LOG_CONFIG");
317
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600318 microseconds::rep before =
319 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
320
321 // this should print
322 NFD_LOG_WARN("warning-message-XXXhdhd11" << 1 << "x");
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600323
324 // this should not because it's level is < WARN
325 testModuleBPrint();
326
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600327 microseconds::rep after =
328 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
329
330 const string buffer = m_buffer.str();
331
332 std::vector<string> components;
333 boost::split(components, buffer, boost::is_any_of(" ,\n"));
334
335 // expected + number of timestamps (one per log statement) + trailing newline of last statement
336 BOOST_REQUIRE_EQUAL(components.size(), N_EXPECTED + 1 + 1);
337
338 std::vector<std::string>::const_iterator componentIter = components.begin();
339 for (size_t i = 0; i < N_EXPECTED; ++i)
340 {
341 // timestamp LOG_LEVEL: [ModuleName] message\n
342
343 const string& timestamp = *componentIter;
344 // std::cout << "timestamp = " << timestamp << std::endl;
345 ++componentIter;
346
347 size_t timeDelimiterPosition = timestamp.find(".");
348
349 BOOST_REQUIRE_NE(string::npos, timeDelimiterPosition);
350
351 string secondsString = timestamp.substr(0, timeDelimiterPosition);
352 string usecondsString = timestamp.substr(timeDelimiterPosition + 1);
353
354 microseconds::rep extractedTime =
355 ONE_SECOND * boost::lexical_cast<microseconds::rep>(secondsString) +
356 boost::lexical_cast<microseconds::rep>(usecondsString);
357
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600358 // std::cout << "before=" << before
359 // << " extracted=" << extractedTime
360 // << " after=" << after << std::endl;
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600361
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600362 BOOST_CHECK_LE(before, extractedTime);
363 BOOST_CHECK_LE(extractedTime, after);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600364
365 // LOG_LEVEL:
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600366 BOOST_CHECK_EQUAL(*componentIter, EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600367 ++componentIter;
368 ++i;
369
370 // [ModuleName]
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600371 BOOST_CHECK_EQUAL(*componentIter, EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600372 ++componentIter;
373 ++i;
374
375 const string& message = *componentIter;
376
377 // std::cout << "message = " << message << std::endl;
378
379 // add back the newline that we split on
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600380 BOOST_CHECK_EQUAL(message + "\n", EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600381 ++componentIter;
382 }
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600383}
384
385BOOST_FIXTURE_TEST_CASE(ExplicitlySetModule, LoggerFixture)
386{
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600387 using namespace ndn::time;
388 using std::string;
389
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600390 NFD_LOG_INIT("TestModuleA");
391
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600392 const ndn::time::microseconds::rep ONE_SECOND = 1000000;
393
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600394 const std::string LOG_CONFIG =
395 "log\n"
396 "{\n"
397 " default_level WARN\n"
398 " TestModuleB DEBUG\n"
399 "}\n";
400
401 ConfigFile config;
402 LoggerFactory::getInstance().setConfigFile(config);
403
404 config.parse(LOG_CONFIG, false, "LOG_CONFIG");
405
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600406 microseconds::rep before =
407 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
408
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600409 // this should print
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600410 NFD_LOG_WARN("warning-message-XXXhdhd11" << 1 << "x");
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600411
412 // this too because its level is explicitly set to DEBUG
413 testModuleBPrint();
414
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600415 microseconds::rep after =
416 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
417
418 const std::string EXPECTED[] =
419 {
420 "WARNING:", "[TestModuleA]", "warning-message-XXXhdhd111x\n",
421 "DEBUG:", "[TestModuleB]", "debug-message-IGg2474fdksd-fo-151617\n",
422 };
423
424 const size_t N_EXPECTED = sizeof(EXPECTED) / sizeof(std::string);
425
426 const string buffer = m_buffer.str();
427
428 std::vector<string> components;
429 boost::split(components, buffer, boost::is_any_of(" ,\n"));
430
431 // for (size_t i = 0; i < components.size(); ++i)
432 // {
433 // std::cout << "-> " << components[i] << std::endl;
434 // }
435
436 // expected + number of timestamps (one per log statement) + trailing newline of last statement
437 BOOST_REQUIRE_EQUAL(components.size(), N_EXPECTED + 2 + 1);
438
439 std::vector<std::string>::const_iterator componentIter = components.begin();
440 for (size_t i = 0; i < N_EXPECTED; ++i)
441 {
442 // timestamp LOG_LEVEL: [ModuleName] message\n
443
444 const string& timestamp = *componentIter;
445 // std::cout << "timestamp = " << timestamp << std::endl;
446 ++componentIter;
447
448 size_t timeDelimiterPosition = timestamp.find(".");
449
450 BOOST_REQUIRE_NE(string::npos, timeDelimiterPosition);
451
452 string secondsString = timestamp.substr(0, timeDelimiterPosition);
453 string usecondsString = timestamp.substr(timeDelimiterPosition + 1);
454
455 microseconds::rep extractedTime =
456 ONE_SECOND * boost::lexical_cast<microseconds::rep>(secondsString) +
457 boost::lexical_cast<microseconds::rep>(usecondsString);
458
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600459 // std::cout << "before=" << before
460 // << " extracted=" << extractedTime
461 // << " after=" << after << std::endl;
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600462
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600463 BOOST_CHECK_LE(before, extractedTime);
464 BOOST_CHECK_LE(extractedTime, after);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600465
466 // LOG_LEVEL:
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600467 BOOST_CHECK_EQUAL(*componentIter, EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600468 ++componentIter;
469 ++i;
470
471 // [ModuleName]
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600472 BOOST_CHECK_EQUAL(*componentIter, EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600473 ++componentIter;
474 ++i;
475
476 const string& message = *componentIter;
477
478 // std::cout << "message = " << message << std::endl;
479
480 // add back the newline that we split on
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600481 BOOST_CHECK_EQUAL(message + "\n", EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600482 ++componentIter;
483 }
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600484}
485
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600486BOOST_FIXTURE_TEST_CASE(UnknownModule, LoggerFixture)
487{
488 using namespace ndn::time;
489 using std::string;
490
491 const ndn::time::microseconds::rep ONE_SECOND = 1000000;
492
493 const std::string LOG_CONFIG =
494 "log\n"
495 "{\n"
496 " TestMadeUpModule INFO\n"
497 "}\n";
498
499 ConfigFile config;
500 LoggerFactory::getInstance().setDefaultLevel(LOG_ALL);
501 LoggerFactory::getInstance().setConfigFile(config);
502
503 const std::string EXPECTED = "DEBUG: [LoggerFactory] "
504 "Failed to configure logging level for module \"TestMadeUpModule\" (module not found)\n";
505
506 microseconds::rep before =
507 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
508
509 config.parse(LOG_CONFIG, false, "LOG_CONFIG");
510
511 microseconds::rep after =
512 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
513
514 const string buffer = m_buffer.str();
515
516 const size_t firstSpace = buffer.find(" ");
517 BOOST_REQUIRE(firstSpace != string::npos);
518
519 const string timestamp = buffer.substr(0, firstSpace);
520 const string message = buffer.substr(firstSpace + 1);
521
522 size_t timeDelimiterPosition = timestamp.find(".");
523
524 BOOST_REQUIRE_NE(string::npos, timeDelimiterPosition);
525
526 string secondsString = timestamp.substr(0, timeDelimiterPosition);
527 string usecondsString = timestamp.substr(timeDelimiterPosition + 1);
528
529 microseconds::rep extractedTime =
530 ONE_SECOND * boost::lexical_cast<microseconds::rep>(secondsString) +
531 boost::lexical_cast<microseconds::rep>(usecondsString);
532
533 // std::cout << "before=" << before
534 // << " extracted=" << extractedTime
535 // << " after=" << after << std::endl;
536
537 BOOST_CHECK_LE(before, extractedTime);
538 BOOST_CHECK_LE(extractedTime, after);
539
540 BOOST_CHECK_EQUAL(message, EXPECTED);
541}
542
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600543static bool
544checkError(const LoggerFactory::Error& error, const std::string& expected)
545{
546 return error.what() == expected;
547}
548
549BOOST_FIXTURE_TEST_CASE(UnknownLevelString, LoggerFixture)
550{
551 const std::string LOG_CONFIG =
552 "log\n"
553 "{\n"
554 " default_level TestMadeUpLevel\n"
555 "}\n";
556
557 ConfigFile config;
558 LoggerFactory::getInstance().setConfigFile(config);
559
560 BOOST_REQUIRE_EXCEPTION(config.parse(LOG_CONFIG, false, "LOG_CONFIG"),
561 LoggerFactory::Error,
562 bind(&checkError,
563 _1,
564 "Unsupported logging level \"TestMadeUpLevel\""));
565}
566
Alexander Afanasyev66886812014-01-31 14:48:48 -0800567class InClassLogger : public LoggerFixture
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800568{
Alexander Afanasyev66886812014-01-31 14:48:48 -0800569public:
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600570
Alexander Afanasyevb84cc922014-02-24 17:52:58 -0800571 InClassLogger()
572 {
573 g_logger.setLogLevel(LOG_ALL);
574 }
575
Alexander Afanasyev66886812014-01-31 14:48:48 -0800576 void
577 writeLogs()
578 {
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600579 NFD_LOG_TRACE("trace-message-JHGFDSR^1");
580 NFD_LOG_DEBUG("debug-message-IGg2474fdksd-fo-" << 15 << 16 << 17);
581 NFD_LOG_WARN("warning-message-XXXhdhd11" << 1 <<"x");
582 NFD_LOG_INFO("info-message-Jjxjshj13");
583 NFD_LOG_ERROR("error-message-!#$&^%$#@");
584 NFD_LOG_FATAL("fatal-message-JJSjaamcng");
Alexander Afanasyev66886812014-01-31 14:48:48 -0800585 }
Alexander Afanasyevb84cc922014-02-24 17:52:58 -0800586
Alexander Afanasyev66886812014-01-31 14:48:48 -0800587private:
588 NFD_LOG_INCLASS_DECLARE();
589};
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800590
Alexander Afanasyev66886812014-01-31 14:48:48 -0800591NFD_LOG_INCLASS_DEFINE(InClassLogger, "InClassLogger");
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800592
Alexander Afanasyev66886812014-01-31 14:48:48 -0800593BOOST_FIXTURE_TEST_CASE(InClass, InClassLogger)
594{
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600595 using namespace ndn::time;
596 using std::string;
597
598 const ndn::time::microseconds::rep ONE_SECOND = 1000000;
599
600 microseconds::rep before =
601 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
602
Alexander Afanasyev66886812014-01-31 14:48:48 -0800603 writeLogs();
Alexander Afanasyevb84cc922014-02-24 17:52:58 -0800604
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600605 microseconds::rep after =
606 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
607
608 const string EXPECTED[] =
609 {
610 "TRACE:", "[InClassLogger]", "trace-message-JHGFDSR^1\n",
611 "DEBUG:", "[InClassLogger]", "debug-message-IGg2474fdksd-fo-151617\n",
612 "WARNING:", "[InClassLogger]", "warning-message-XXXhdhd111x\n",
613 "INFO:", "[InClassLogger]", "info-message-Jjxjshj13\n",
614 "ERROR:", "[InClassLogger]", "error-message-!#$&^%$#@\n",
615 "FATAL:", "[InClassLogger]", "fatal-message-JJSjaamcng\n",
616 };
617
618 const size_t N_EXPECTED = sizeof(EXPECTED) / sizeof(string);
619
620 const string buffer = m_buffer.str();
621
622 std::vector<string> components;
623 boost::split(components, buffer, boost::is_any_of(" ,\n"));
624
625 // expected + number of timestamps (one per log statement) + trailing newline of last statement
626 BOOST_REQUIRE_EQUAL(components.size(), N_EXPECTED + 6 + 1);
627
628 std::vector<std::string>::const_iterator componentIter = components.begin();
629 for (size_t i = 0; i < N_EXPECTED; ++i)
630 {
631 // timestamp LOG_LEVEL: [ModuleName] message\n
632
633 const string& timestamp = *componentIter;
634 // std::cout << "timestamp = " << timestamp << std::endl;
635 ++componentIter;
636
637 size_t timeDelimiterPosition = timestamp.find(".");
638
639 BOOST_REQUIRE_NE(string::npos, timeDelimiterPosition);
640
641 string secondsString = timestamp.substr(0, timeDelimiterPosition);
642 string usecondsString = timestamp.substr(timeDelimiterPosition + 1);
643
644 microseconds::rep extractedTime =
645 ONE_SECOND * boost::lexical_cast<microseconds::rep>(secondsString) +
646 boost::lexical_cast<microseconds::rep>(usecondsString);
647
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600648 // std::cout << "before=" << before
649 // << " extracted=" << extractedTime
650 // << " after=" << after << std::endl;
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600651
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600652 BOOST_CHECK_LE(before, extractedTime);
653 BOOST_CHECK_LE(extractedTime, after);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600654
655 // LOG_LEVEL:
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600656 BOOST_CHECK_EQUAL(*componentIter, EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600657 ++componentIter;
658 ++i;
659
660 // [ModuleName]
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600661 BOOST_CHECK_EQUAL(*componentIter, EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600662 ++componentIter;
663 ++i;
664
665 const string& message = *componentIter;
666
667 // std::cout << "message = " << message << std::endl;
668
669 // add back the newline that we split on
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600670 BOOST_CHECK_EQUAL(message + "\n", EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600671 ++componentIter;
672 }
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800673}
674
Alexander Afanasyev66886812014-01-31 14:48:48 -0800675
676template<class T>
677class InClassTemplateLogger : public LoggerFixture
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800678{
Alexander Afanasyev66886812014-01-31 14:48:48 -0800679public:
Alexander Afanasyevb84cc922014-02-24 17:52:58 -0800680 InClassTemplateLogger()
681 {
682 g_logger.setLogLevel(LOG_ALL);
683 }
684
Alexander Afanasyev66886812014-01-31 14:48:48 -0800685 void
686 writeLogs()
687 {
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600688 NFD_LOG_TRACE("trace-message-JHGFDSR^1");
689 NFD_LOG_DEBUG("debug-message-IGg2474fdksd-fo-" << 15 << 16 << 17);
690 NFD_LOG_WARN("warning-message-XXXhdhd11" << 1 <<"x");
691 NFD_LOG_INFO("info-message-Jjxjshj13");
692 NFD_LOG_ERROR("error-message-!#$&^%$#@");
693 NFD_LOG_FATAL("fatal-message-JJSjaamcng");
Alexander Afanasyev66886812014-01-31 14:48:48 -0800694 }
Alexander Afanasyevb84cc922014-02-24 17:52:58 -0800695
Alexander Afanasyev66886812014-01-31 14:48:48 -0800696private:
697 NFD_LOG_INCLASS_DECLARE();
698};
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800699
Alexander Afanasyev66886812014-01-31 14:48:48 -0800700NFD_LOG_INCLASS_TEMPLATE_DEFINE(InClassTemplateLogger, "GenericInClassTemplateLogger");
701NFD_LOG_INCLASS_TEMPLATE_SPECIALIZATION_DEFINE(InClassTemplateLogger, int, "IntInClassLogger");
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800702
Alexander Afanasyev66886812014-01-31 14:48:48 -0800703BOOST_FIXTURE_TEST_CASE(GenericInTemplatedClass, InClassTemplateLogger<bool>)
704{
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600705 using namespace ndn::time;
706 using std::string;
707
708 const ndn::time::microseconds::rep ONE_SECOND = 1000000;
709
710 microseconds::rep before =
711 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
712
Alexander Afanasyev66886812014-01-31 14:48:48 -0800713 writeLogs();
Alexander Afanasyevb84cc922014-02-24 17:52:58 -0800714
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600715 microseconds::rep after =
716 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
717
718 const string EXPECTED[] =
719 {
720 "TRACE:", "[GenericInClassTemplateLogger]", "trace-message-JHGFDSR^1\n",
721 "DEBUG:", "[GenericInClassTemplateLogger]", "debug-message-IGg2474fdksd-fo-151617\n",
722 "WARNING:", "[GenericInClassTemplateLogger]", "warning-message-XXXhdhd111x\n",
723 "INFO:", "[GenericInClassTemplateLogger]", "info-message-Jjxjshj13\n",
724 "ERROR:", "[GenericInClassTemplateLogger]", "error-message-!#$&^%$#@\n",
725 "FATAL:", "[GenericInClassTemplateLogger]", "fatal-message-JJSjaamcng\n",
726 };
727
728 const size_t N_EXPECTED = sizeof(EXPECTED) / sizeof(string);
729
730 const string buffer = m_buffer.str();
731
732 std::vector<string> components;
733 boost::split(components, buffer, boost::is_any_of(" ,\n"));
734
735 // expected + number of timestamps (one per log statement) + trailing newline of last statement
736 BOOST_REQUIRE_EQUAL(components.size(), N_EXPECTED + 6 + 1);
737
738 std::vector<std::string>::const_iterator componentIter = components.begin();
739 for (size_t i = 0; i < N_EXPECTED; ++i)
740 {
741 // timestamp LOG_LEVEL: [ModuleName] message\n
742
743 const string& timestamp = *componentIter;
744 // std::cout << "timestamp = " << timestamp << std::endl;
745 ++componentIter;
746
747 size_t timeDelimiterPosition = timestamp.find(".");
748
749 BOOST_REQUIRE_NE(string::npos, timeDelimiterPosition);
750
751 string secondsString = timestamp.substr(0, timeDelimiterPosition);
752 string usecondsString = timestamp.substr(timeDelimiterPosition + 1);
753
754 microseconds::rep extractedTime =
755 ONE_SECOND * boost::lexical_cast<microseconds::rep>(secondsString) +
756 boost::lexical_cast<microseconds::rep>(usecondsString);
757
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600758 // std::cout << "before=" << before
759 // << " extracted=" << extractedTime
760 // << " after=" << after << std::endl;
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600761
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600762 BOOST_CHECK_LE(before, extractedTime);
763 BOOST_CHECK_LE(extractedTime, after);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600764
765 // LOG_LEVEL:
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600766 BOOST_CHECK_EQUAL(*componentIter, EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600767 ++componentIter;
768 ++i;
769
770 // [ModuleName]
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600771 BOOST_CHECK_EQUAL(*componentIter, EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600772 ++componentIter;
773 ++i;
774
775 const string& message = *componentIter;
776
777 // std::cout << "message = " << message << std::endl;
778
779 // add back the newline that we split on
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600780 BOOST_CHECK_EQUAL(message + "\n", EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600781 ++componentIter;
782 }
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800783}
784
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600785
Alexander Afanasyev66886812014-01-31 14:48:48 -0800786BOOST_FIXTURE_TEST_CASE(SpecializedInTemplatedClass, InClassTemplateLogger<int>)
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800787{
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600788 using namespace ndn::time;
789 using std::string;
790
791 const ndn::time::microseconds::rep ONE_SECOND = 1000000;
792
793 microseconds::rep before =
794 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
795
Alexander Afanasyev66886812014-01-31 14:48:48 -0800796 writeLogs();
Alexander Afanasyevb84cc922014-02-24 17:52:58 -0800797
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600798 microseconds::rep after =
799 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
800
801 const string EXPECTED[] =
802 {
803 "TRACE:", "[IntInClassLogger]", "trace-message-JHGFDSR^1\n",
804 "DEBUG:", "[IntInClassLogger]", "debug-message-IGg2474fdksd-fo-151617\n",
805 "WARNING:", "[IntInClassLogger]", "warning-message-XXXhdhd111x\n",
806 "INFO:", "[IntInClassLogger]", "info-message-Jjxjshj13\n",
807 "ERROR:", "[IntInClassLogger]", "error-message-!#$&^%$#@\n",
808 "FATAL:", "[IntInClassLogger]", "fatal-message-JJSjaamcng\n",
809 };
810
811 const size_t N_EXPECTED = sizeof(EXPECTED) / sizeof(string);
812
813 const string buffer = m_buffer.str();
814
815 std::vector<string> components;
816 boost::split(components, buffer, boost::is_any_of(" ,\n"));
817
818 // expected + number of timestamps (one per log statement) + trailing newline of last statement
819 BOOST_REQUIRE_EQUAL(components.size(), N_EXPECTED + 6 + 1);
820
821 std::vector<std::string>::const_iterator componentIter = components.begin();
822 for (size_t i = 0; i < N_EXPECTED; ++i)
823 {
824 // timestamp LOG_LEVEL: [ModuleName] message\n
825
826 const string& timestamp = *componentIter;
827 // std::cout << "timestamp = " << timestamp << std::endl;
828 ++componentIter;
829
830 size_t timeDelimiterPosition = timestamp.find(".");
831
832 BOOST_REQUIRE_NE(string::npos, timeDelimiterPosition);
833
834 string secondsString = timestamp.substr(0, timeDelimiterPosition);
835 string usecondsString = timestamp.substr(timeDelimiterPosition + 1);
836
837 microseconds::rep extractedTime =
838 ONE_SECOND * boost::lexical_cast<microseconds::rep>(secondsString) +
839 boost::lexical_cast<microseconds::rep>(usecondsString);
840
841 // std::cout << "before=" << before << " extracted=" << extractedTime << " after=" << after << std::endl;
842
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600843 BOOST_CHECK_LE(before, extractedTime);
844 BOOST_CHECK_LE(extractedTime, after);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600845
846 // LOG_LEVEL:
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600847 BOOST_CHECK_EQUAL(*componentIter, EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600848 ++componentIter;
849 ++i;
850
851 // [ModuleName]
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600852 BOOST_CHECK_EQUAL(*componentIter, EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600853 ++componentIter;
854 ++i;
855
856 const string& message = *componentIter;
857
858 // std::cout << "message = " << message << std::endl;
859
860 // add back the newline that we split on
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600861 BOOST_CHECK_EQUAL(message + "\n", EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600862 ++componentIter;
863 }
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800864}
865
866BOOST_AUTO_TEST_SUITE_END()
867
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700868} // namespace tests
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800869} // namespace nfd