Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 1 | /* -*- 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 DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 10 | |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 11 | #include <boost/test/unit_test.hpp> |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 12 | #include <boost/algorithm/string.hpp> |
| 13 | #include <boost/algorithm/string/classification.hpp> |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 14 | |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 15 | #include <iostream> |
| 16 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 17 | #include "tests/test-common.hpp" |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 18 | |
| 19 | namespace nfd { |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 20 | namespace tests { |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 21 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 22 | BOOST_FIXTURE_TEST_SUITE(CoreLogger, BaseFixture) |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 23 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 24 | class LoggerFixture : protected BaseFixture |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 25 | { |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 26 | public: |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 27 | LoggerFixture() |
Steve DiBenedetto | 4d43a45 | 2014-04-06 18:55:29 -0600 | [diff] [blame^] | 28 | : m_savedBuf(std::clog.rdbuf()) |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 29 | , m_savedLevel(LoggerFactory::getInstance().getDefaultLevel()) |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 30 | { |
Steve DiBenedetto | 4d43a45 | 2014-04-06 18:55:29 -0600 | [diff] [blame^] | 31 | std::clog.rdbuf(m_buffer.rdbuf()); |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 32 | } |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 33 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 34 | ~LoggerFixture() |
| 35 | { |
Steve DiBenedetto | 4d43a45 | 2014-04-06 18:55:29 -0600 | [diff] [blame^] | 36 | std::clog.rdbuf(m_savedBuf); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 37 | LoggerFactory::getInstance().setDefaultLevel(m_savedLevel); |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 38 | } |
Alexander Afanasyev | b84cc92 | 2014-02-24 17:52:58 -0800 | [diff] [blame] | 39 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 40 | std::stringstream m_buffer; |
| 41 | std::streambuf* m_savedBuf; |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 42 | LogLevel m_savedLevel; |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 43 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 44 | }; |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 45 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 46 | BOOST_FIXTURE_TEST_CASE(Basic, LoggerFixture) |
| 47 | { |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 48 | using namespace ndn::time; |
| 49 | using std::string; |
| 50 | |
| 51 | const ndn::time::microseconds::rep ONE_SECOND = 1000000; |
| 52 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 53 | NFD_LOG_INIT("BasicTests"); |
Alexander Afanasyev | b84cc92 | 2014-02-24 17:52:58 -0800 | [diff] [blame] | 54 | g_logger.setLogLevel(LOG_ALL); |
| 55 | |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 56 | 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 Afanasyev | b84cc92 | 2014-02-24 17:52:58 -0800 | [diff] [blame] | 65 | |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 66 | 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 Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 139 | } |
| 140 | |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 141 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 142 | BOOST_FIXTURE_TEST_CASE(ConfigureFactory, LoggerFixture) |
| 143 | { |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 144 | using namespace ndn::time; |
| 145 | using std::string; |
| 146 | |
| 147 | const ndn::time::microseconds::rep ONE_SECOND = 1000000; |
| 148 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 149 | NFD_LOG_INIT("ConfigureFactoryTests"); |
| 150 | |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 151 | const string LOG_CONFIG = |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 152 | "log\n" |
| 153 | "{\n" |
| 154 | " default_level INFO\n" |
| 155 | "}\n"; |
| 156 | |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 157 | LoggerFactory::getInstance().setDefaultLevel(LOG_ALL); |
| 158 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 159 | ConfigFile config; |
| 160 | LoggerFactory::getInstance().setConfigFile(config); |
| 161 | |
| 162 | config.parse(LOG_CONFIG, false, "LOG_CONFIG"); |
| 163 | |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 164 | BOOST_REQUIRE_EQUAL(LoggerFactory::getInstance().getDefaultLevel(), LOG_INFO); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 165 | |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 166 | 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 DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | BOOST_FIXTURE_TEST_CASE(TestNumberLevel, LoggerFixture) |
| 249 | { |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 250 | const std::string LOG_CONFIG = |
| 251 | "log\n" |
| 252 | "{\n" |
| 253 | " default_level 2\n" // equivalent of WARN |
| 254 | "}\n"; |
| 255 | |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 256 | LoggerFactory::getInstance().setDefaultLevel(LOG_ALL); |
| 257 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 258 | ConfigFile config; |
| 259 | LoggerFactory::getInstance().setConfigFile(config); |
| 260 | |
| 261 | config.parse(LOG_CONFIG, false, "LOG_CONFIG"); |
| 262 | |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 263 | BOOST_REQUIRE_EQUAL(LoggerFactory::getInstance().getDefaultLevel(), LOG_WARN); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | static void |
| 267 | testModuleBPrint() |
| 268 | { |
| 269 | NFD_LOG_INIT("TestModuleB"); |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 270 | NFD_LOG_DEBUG("debug-message-IGg2474fdksd-fo-" << 15 << 16 << 17); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | BOOST_FIXTURE_TEST_CASE(LimitModules, LoggerFixture) |
| 274 | { |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 275 | using namespace ndn::time; |
| 276 | using std::string; |
| 277 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 278 | NFD_LOG_INIT("TestModuleA"); |
| 279 | |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 280 | 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 DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 289 | 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 DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 300 | 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 DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 305 | |
| 306 | // this should not because it's level is < WARN |
| 307 | testModuleBPrint(); |
| 308 | |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 309 | 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 DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | BOOST_FIXTURE_TEST_CASE(ExplicitlySetModule, LoggerFixture) |
| 366 | { |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 367 | using namespace ndn::time; |
| 368 | using std::string; |
| 369 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 370 | NFD_LOG_INIT("TestModuleA"); |
| 371 | |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 372 | const ndn::time::microseconds::rep ONE_SECOND = 1000000; |
| 373 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 374 | 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 DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 386 | microseconds::rep before = |
| 387 | duration_cast<microseconds>(system_clock::now().time_since_epoch()).count(); |
| 388 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 389 | // this should print |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 390 | NFD_LOG_WARN("warning-message-XXXhdhd11" << 1 << "x"); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 391 | |
| 392 | // this too because its level is explicitly set to DEBUG |
| 393 | testModuleBPrint(); |
| 394 | |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 395 | 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 DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | static bool |
| 465 | checkError(const LoggerFactory::Error& error, const std::string& expected) |
| 466 | { |
| 467 | return error.what() == expected; |
| 468 | } |
| 469 | |
| 470 | BOOST_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 | |
| 488 | BOOST_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 Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 506 | class InClassLogger : public LoggerFixture |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 507 | { |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 508 | public: |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 509 | |
Alexander Afanasyev | b84cc92 | 2014-02-24 17:52:58 -0800 | [diff] [blame] | 510 | InClassLogger() |
| 511 | { |
| 512 | g_logger.setLogLevel(LOG_ALL); |
| 513 | } |
| 514 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 515 | void |
| 516 | writeLogs() |
| 517 | { |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 518 | 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 Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 524 | } |
Alexander Afanasyev | b84cc92 | 2014-02-24 17:52:58 -0800 | [diff] [blame] | 525 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 526 | private: |
| 527 | NFD_LOG_INCLASS_DECLARE(); |
| 528 | }; |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 529 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 530 | NFD_LOG_INCLASS_DEFINE(InClassLogger, "InClassLogger"); |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 531 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 532 | BOOST_FIXTURE_TEST_CASE(InClass, InClassLogger) |
| 533 | { |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 534 | 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 Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 542 | writeLogs(); |
Alexander Afanasyev | b84cc92 | 2014-02-24 17:52:58 -0800 | [diff] [blame] | 543 | |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 544 | 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 Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 610 | } |
| 611 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 612 | |
| 613 | template<class T> |
| 614 | class InClassTemplateLogger : public LoggerFixture |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 615 | { |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 616 | public: |
Alexander Afanasyev | b84cc92 | 2014-02-24 17:52:58 -0800 | [diff] [blame] | 617 | InClassTemplateLogger() |
| 618 | { |
| 619 | g_logger.setLogLevel(LOG_ALL); |
| 620 | } |
| 621 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 622 | void |
| 623 | writeLogs() |
| 624 | { |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 625 | 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 Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 631 | } |
Alexander Afanasyev | b84cc92 | 2014-02-24 17:52:58 -0800 | [diff] [blame] | 632 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 633 | private: |
| 634 | NFD_LOG_INCLASS_DECLARE(); |
| 635 | }; |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 636 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 637 | NFD_LOG_INCLASS_TEMPLATE_DEFINE(InClassTemplateLogger, "GenericInClassTemplateLogger"); |
| 638 | NFD_LOG_INCLASS_TEMPLATE_SPECIALIZATION_DEFINE(InClassTemplateLogger, int, "IntInClassLogger"); |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 639 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 640 | BOOST_FIXTURE_TEST_CASE(GenericInTemplatedClass, InClassTemplateLogger<bool>) |
| 641 | { |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 642 | 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 Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 650 | writeLogs(); |
Alexander Afanasyev | b84cc92 | 2014-02-24 17:52:58 -0800 | [diff] [blame] | 651 | |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 652 | 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 Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 718 | } |
| 719 | |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 720 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 721 | BOOST_FIXTURE_TEST_CASE(SpecializedInTemplatedClass, InClassTemplateLogger<int>) |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 722 | { |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 723 | 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 Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 731 | writeLogs(); |
Alexander Afanasyev | b84cc92 | 2014-02-24 17:52:58 -0800 | [diff] [blame] | 732 | |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 733 | 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 Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 799 | } |
| 800 | |
| 801 | BOOST_AUTO_TEST_SUITE_END() |
| 802 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 803 | } // namespace tests |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 804 | } // namespace nfd |