blob: 50df2795b9ccbda212634398f5ef100eb1e6b73d [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"
Alexander Afanasyev5959b012014-06-02 19:18:12 +0300496 " default_level DEBUG\n"
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600497 " TestMadeUpModule INFO\n"
498 "}\n";
499
500 ConfigFile config;
501 LoggerFactory::getInstance().setDefaultLevel(LOG_ALL);
502 LoggerFactory::getInstance().setConfigFile(config);
503
504 const std::string EXPECTED = "DEBUG: [LoggerFactory] "
505 "Failed to configure logging level for module \"TestMadeUpModule\" (module not found)\n";
506
507 microseconds::rep before =
508 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
509
510 config.parse(LOG_CONFIG, false, "LOG_CONFIG");
511
512 microseconds::rep after =
513 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
514
515 const string buffer = m_buffer.str();
516
517 const size_t firstSpace = buffer.find(" ");
518 BOOST_REQUIRE(firstSpace != string::npos);
519
520 const string timestamp = buffer.substr(0, firstSpace);
521 const string message = buffer.substr(firstSpace + 1);
522
523 size_t timeDelimiterPosition = timestamp.find(".");
524
525 BOOST_REQUIRE_NE(string::npos, timeDelimiterPosition);
526
527 string secondsString = timestamp.substr(0, timeDelimiterPosition);
528 string usecondsString = timestamp.substr(timeDelimiterPosition + 1);
529
530 microseconds::rep extractedTime =
531 ONE_SECOND * boost::lexical_cast<microseconds::rep>(secondsString) +
532 boost::lexical_cast<microseconds::rep>(usecondsString);
533
534 // std::cout << "before=" << before
535 // << " extracted=" << extractedTime
536 // << " after=" << after << std::endl;
537
538 BOOST_CHECK_LE(before, extractedTime);
539 BOOST_CHECK_LE(extractedTime, after);
540
541 BOOST_CHECK_EQUAL(message, EXPECTED);
542}
543
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600544static bool
545checkError(const LoggerFactory::Error& error, const std::string& expected)
546{
547 return error.what() == expected;
548}
549
550BOOST_FIXTURE_TEST_CASE(UnknownLevelString, LoggerFixture)
551{
552 const std::string LOG_CONFIG =
553 "log\n"
554 "{\n"
555 " default_level TestMadeUpLevel\n"
556 "}\n";
557
558 ConfigFile config;
559 LoggerFactory::getInstance().setConfigFile(config);
560
561 BOOST_REQUIRE_EXCEPTION(config.parse(LOG_CONFIG, false, "LOG_CONFIG"),
562 LoggerFactory::Error,
563 bind(&checkError,
564 _1,
565 "Unsupported logging level \"TestMadeUpLevel\""));
566}
567
Alexander Afanasyev66886812014-01-31 14:48:48 -0800568class InClassLogger : public LoggerFixture
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800569{
Alexander Afanasyev66886812014-01-31 14:48:48 -0800570public:
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600571
Alexander Afanasyevb84cc922014-02-24 17:52:58 -0800572 InClassLogger()
573 {
574 g_logger.setLogLevel(LOG_ALL);
575 }
576
Alexander Afanasyev66886812014-01-31 14:48:48 -0800577 void
578 writeLogs()
579 {
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600580 NFD_LOG_TRACE("trace-message-JHGFDSR^1");
581 NFD_LOG_DEBUG("debug-message-IGg2474fdksd-fo-" << 15 << 16 << 17);
582 NFD_LOG_WARN("warning-message-XXXhdhd11" << 1 <<"x");
583 NFD_LOG_INFO("info-message-Jjxjshj13");
584 NFD_LOG_ERROR("error-message-!#$&^%$#@");
585 NFD_LOG_FATAL("fatal-message-JJSjaamcng");
Alexander Afanasyev66886812014-01-31 14:48:48 -0800586 }
Alexander Afanasyevb84cc922014-02-24 17:52:58 -0800587
Alexander Afanasyev66886812014-01-31 14:48:48 -0800588private:
589 NFD_LOG_INCLASS_DECLARE();
590};
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800591
Alexander Afanasyev66886812014-01-31 14:48:48 -0800592NFD_LOG_INCLASS_DEFINE(InClassLogger, "InClassLogger");
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800593
Alexander Afanasyev66886812014-01-31 14:48:48 -0800594BOOST_FIXTURE_TEST_CASE(InClass, InClassLogger)
595{
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600596 using namespace ndn::time;
597 using std::string;
598
599 const ndn::time::microseconds::rep ONE_SECOND = 1000000;
600
601 microseconds::rep before =
602 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
603
Alexander Afanasyev66886812014-01-31 14:48:48 -0800604 writeLogs();
Alexander Afanasyevb84cc922014-02-24 17:52:58 -0800605
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600606 microseconds::rep after =
607 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
608
609 const string EXPECTED[] =
610 {
611 "TRACE:", "[InClassLogger]", "trace-message-JHGFDSR^1\n",
612 "DEBUG:", "[InClassLogger]", "debug-message-IGg2474fdksd-fo-151617\n",
613 "WARNING:", "[InClassLogger]", "warning-message-XXXhdhd111x\n",
614 "INFO:", "[InClassLogger]", "info-message-Jjxjshj13\n",
615 "ERROR:", "[InClassLogger]", "error-message-!#$&^%$#@\n",
616 "FATAL:", "[InClassLogger]", "fatal-message-JJSjaamcng\n",
617 };
618
619 const size_t N_EXPECTED = sizeof(EXPECTED) / sizeof(string);
620
621 const string buffer = m_buffer.str();
622
623 std::vector<string> components;
624 boost::split(components, buffer, boost::is_any_of(" ,\n"));
625
626 // expected + number of timestamps (one per log statement) + trailing newline of last statement
627 BOOST_REQUIRE_EQUAL(components.size(), N_EXPECTED + 6 + 1);
628
629 std::vector<std::string>::const_iterator componentIter = components.begin();
630 for (size_t i = 0; i < N_EXPECTED; ++i)
631 {
632 // timestamp LOG_LEVEL: [ModuleName] message\n
633
634 const string& timestamp = *componentIter;
635 // std::cout << "timestamp = " << timestamp << std::endl;
636 ++componentIter;
637
638 size_t timeDelimiterPosition = timestamp.find(".");
639
640 BOOST_REQUIRE_NE(string::npos, timeDelimiterPosition);
641
642 string secondsString = timestamp.substr(0, timeDelimiterPosition);
643 string usecondsString = timestamp.substr(timeDelimiterPosition + 1);
644
645 microseconds::rep extractedTime =
646 ONE_SECOND * boost::lexical_cast<microseconds::rep>(secondsString) +
647 boost::lexical_cast<microseconds::rep>(usecondsString);
648
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600649 // std::cout << "before=" << before
650 // << " extracted=" << extractedTime
651 // << " after=" << after << std::endl;
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600652
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600653 BOOST_CHECK_LE(before, extractedTime);
654 BOOST_CHECK_LE(extractedTime, after);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600655
656 // LOG_LEVEL:
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600657 BOOST_CHECK_EQUAL(*componentIter, EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600658 ++componentIter;
659 ++i;
660
661 // [ModuleName]
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600662 BOOST_CHECK_EQUAL(*componentIter, EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600663 ++componentIter;
664 ++i;
665
666 const string& message = *componentIter;
667
668 // std::cout << "message = " << message << std::endl;
669
670 // add back the newline that we split on
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600671 BOOST_CHECK_EQUAL(message + "\n", EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600672 ++componentIter;
673 }
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800674}
675
Alexander Afanasyev66886812014-01-31 14:48:48 -0800676
677template<class T>
678class InClassTemplateLogger : public LoggerFixture
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800679{
Alexander Afanasyev66886812014-01-31 14:48:48 -0800680public:
Alexander Afanasyevb84cc922014-02-24 17:52:58 -0800681 InClassTemplateLogger()
682 {
683 g_logger.setLogLevel(LOG_ALL);
684 }
685
Alexander Afanasyev66886812014-01-31 14:48:48 -0800686 void
687 writeLogs()
688 {
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600689 NFD_LOG_TRACE("trace-message-JHGFDSR^1");
690 NFD_LOG_DEBUG("debug-message-IGg2474fdksd-fo-" << 15 << 16 << 17);
691 NFD_LOG_WARN("warning-message-XXXhdhd11" << 1 <<"x");
692 NFD_LOG_INFO("info-message-Jjxjshj13");
693 NFD_LOG_ERROR("error-message-!#$&^%$#@");
694 NFD_LOG_FATAL("fatal-message-JJSjaamcng");
Alexander Afanasyev66886812014-01-31 14:48:48 -0800695 }
Alexander Afanasyevb84cc922014-02-24 17:52:58 -0800696
Alexander Afanasyev66886812014-01-31 14:48:48 -0800697private:
698 NFD_LOG_INCLASS_DECLARE();
699};
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800700
Alexander Afanasyev66886812014-01-31 14:48:48 -0800701NFD_LOG_INCLASS_TEMPLATE_DEFINE(InClassTemplateLogger, "GenericInClassTemplateLogger");
702NFD_LOG_INCLASS_TEMPLATE_SPECIALIZATION_DEFINE(InClassTemplateLogger, int, "IntInClassLogger");
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800703
Alexander Afanasyev66886812014-01-31 14:48:48 -0800704BOOST_FIXTURE_TEST_CASE(GenericInTemplatedClass, InClassTemplateLogger<bool>)
705{
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600706 using namespace ndn::time;
707 using std::string;
708
709 const ndn::time::microseconds::rep ONE_SECOND = 1000000;
710
711 microseconds::rep before =
712 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
713
Alexander Afanasyev66886812014-01-31 14:48:48 -0800714 writeLogs();
Alexander Afanasyevb84cc922014-02-24 17:52:58 -0800715
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600716 microseconds::rep after =
717 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
718
719 const string EXPECTED[] =
720 {
721 "TRACE:", "[GenericInClassTemplateLogger]", "trace-message-JHGFDSR^1\n",
722 "DEBUG:", "[GenericInClassTemplateLogger]", "debug-message-IGg2474fdksd-fo-151617\n",
723 "WARNING:", "[GenericInClassTemplateLogger]", "warning-message-XXXhdhd111x\n",
724 "INFO:", "[GenericInClassTemplateLogger]", "info-message-Jjxjshj13\n",
725 "ERROR:", "[GenericInClassTemplateLogger]", "error-message-!#$&^%$#@\n",
726 "FATAL:", "[GenericInClassTemplateLogger]", "fatal-message-JJSjaamcng\n",
727 };
728
729 const size_t N_EXPECTED = sizeof(EXPECTED) / sizeof(string);
730
731 const string buffer = m_buffer.str();
732
733 std::vector<string> components;
734 boost::split(components, buffer, boost::is_any_of(" ,\n"));
735
736 // expected + number of timestamps (one per log statement) + trailing newline of last statement
737 BOOST_REQUIRE_EQUAL(components.size(), N_EXPECTED + 6 + 1);
738
739 std::vector<std::string>::const_iterator componentIter = components.begin();
740 for (size_t i = 0; i < N_EXPECTED; ++i)
741 {
742 // timestamp LOG_LEVEL: [ModuleName] message\n
743
744 const string& timestamp = *componentIter;
745 // std::cout << "timestamp = " << timestamp << std::endl;
746 ++componentIter;
747
748 size_t timeDelimiterPosition = timestamp.find(".");
749
750 BOOST_REQUIRE_NE(string::npos, timeDelimiterPosition);
751
752 string secondsString = timestamp.substr(0, timeDelimiterPosition);
753 string usecondsString = timestamp.substr(timeDelimiterPosition + 1);
754
755 microseconds::rep extractedTime =
756 ONE_SECOND * boost::lexical_cast<microseconds::rep>(secondsString) +
757 boost::lexical_cast<microseconds::rep>(usecondsString);
758
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600759 // std::cout << "before=" << before
760 // << " extracted=" << extractedTime
761 // << " after=" << after << std::endl;
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600762
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600763 BOOST_CHECK_LE(before, extractedTime);
764 BOOST_CHECK_LE(extractedTime, after);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600765
766 // LOG_LEVEL:
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600767 BOOST_CHECK_EQUAL(*componentIter, EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600768 ++componentIter;
769 ++i;
770
771 // [ModuleName]
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600772 BOOST_CHECK_EQUAL(*componentIter, EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600773 ++componentIter;
774 ++i;
775
776 const string& message = *componentIter;
777
778 // std::cout << "message = " << message << std::endl;
779
780 // add back the newline that we split on
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600781 BOOST_CHECK_EQUAL(message + "\n", EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600782 ++componentIter;
783 }
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800784}
785
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600786
Alexander Afanasyev66886812014-01-31 14:48:48 -0800787BOOST_FIXTURE_TEST_CASE(SpecializedInTemplatedClass, InClassTemplateLogger<int>)
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800788{
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600789 using namespace ndn::time;
790 using std::string;
791
792 const ndn::time::microseconds::rep ONE_SECOND = 1000000;
793
794 microseconds::rep before =
795 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
796
Alexander Afanasyev66886812014-01-31 14:48:48 -0800797 writeLogs();
Alexander Afanasyevb84cc922014-02-24 17:52:58 -0800798
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600799 microseconds::rep after =
800 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
801
802 const string EXPECTED[] =
803 {
804 "TRACE:", "[IntInClassLogger]", "trace-message-JHGFDSR^1\n",
805 "DEBUG:", "[IntInClassLogger]", "debug-message-IGg2474fdksd-fo-151617\n",
806 "WARNING:", "[IntInClassLogger]", "warning-message-XXXhdhd111x\n",
807 "INFO:", "[IntInClassLogger]", "info-message-Jjxjshj13\n",
808 "ERROR:", "[IntInClassLogger]", "error-message-!#$&^%$#@\n",
809 "FATAL:", "[IntInClassLogger]", "fatal-message-JJSjaamcng\n",
810 };
811
812 const size_t N_EXPECTED = sizeof(EXPECTED) / sizeof(string);
813
814 const string buffer = m_buffer.str();
815
816 std::vector<string> components;
817 boost::split(components, buffer, boost::is_any_of(" ,\n"));
818
819 // expected + number of timestamps (one per log statement) + trailing newline of last statement
820 BOOST_REQUIRE_EQUAL(components.size(), N_EXPECTED + 6 + 1);
821
822 std::vector<std::string>::const_iterator componentIter = components.begin();
823 for (size_t i = 0; i < N_EXPECTED; ++i)
824 {
825 // timestamp LOG_LEVEL: [ModuleName] message\n
826
827 const string& timestamp = *componentIter;
828 // std::cout << "timestamp = " << timestamp << std::endl;
829 ++componentIter;
830
831 size_t timeDelimiterPosition = timestamp.find(".");
832
833 BOOST_REQUIRE_NE(string::npos, timeDelimiterPosition);
834
835 string secondsString = timestamp.substr(0, timeDelimiterPosition);
836 string usecondsString = timestamp.substr(timeDelimiterPosition + 1);
837
838 microseconds::rep extractedTime =
839 ONE_SECOND * boost::lexical_cast<microseconds::rep>(secondsString) +
840 boost::lexical_cast<microseconds::rep>(usecondsString);
841
842 // std::cout << "before=" << before << " extracted=" << extractedTime << " after=" << after << std::endl;
843
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600844 BOOST_CHECK_LE(before, extractedTime);
845 BOOST_CHECK_LE(extractedTime, after);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600846
847 // LOG_LEVEL:
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600848 BOOST_CHECK_EQUAL(*componentIter, EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600849 ++componentIter;
850 ++i;
851
852 // [ModuleName]
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600853 BOOST_CHECK_EQUAL(*componentIter, EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600854 ++componentIter;
855 ++i;
856
857 const string& message = *componentIter;
858
859 // std::cout << "message = " << message << std::endl;
860
861 // add back the newline that we split on
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600862 BOOST_CHECK_EQUAL(message + "\n", EXPECTED[i]);
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600863 ++componentIter;
864 }
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800865}
866
Alexander Afanasyev6b34ab92015-02-11 21:22:46 -0800867BOOST_FIXTURE_TEST_CASE(LoggerFactoryListModules, LoggerFixture)
868{
869 std::set<std::string> testCaseLoggers{"LoggerFactoryListModules1", "LoggerFactoryListModules2"};
870
871 for (const auto& name : testCaseLoggers) {
872 LoggerFactory::create(name);
873 }
874
875 auto&& modules = LoggerFactory::getInstance().getModules();
876 BOOST_CHECK_GE(modules.size(), 2);
877
878 for (const auto& name : modules) {
879 testCaseLoggers.erase(name);
880 }
881
882 BOOST_CHECK_EQUAL(testCaseLoggers.size(), 0);
883}
884
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800885BOOST_AUTO_TEST_SUITE_END()
886
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700887} // namespace tests
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800888} // namespace nfd