Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 3 | * 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 Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 9 | * |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 10 | * 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 Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 24 | |
| 25 | #include "core/logger.hpp" |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 26 | |
Davide Pesavento | 52a18f9 | 2014-04-10 00:55:01 +0200 | [diff] [blame] | 27 | #include "tests/test-common.hpp" |
| 28 | |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 29 | #include <boost/algorithm/string.hpp> |
| 30 | #include <boost/algorithm/string/classification.hpp> |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 31 | |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 32 | namespace nfd { |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 33 | namespace tests { |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 34 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 35 | BOOST_FIXTURE_TEST_SUITE(CoreLogger, BaseFixture) |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 36 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 37 | class LoggerFixture : protected BaseFixture |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 38 | { |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 39 | public: |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 40 | LoggerFixture() |
Steve DiBenedetto | 4d43a45 | 2014-04-06 18:55:29 -0600 | [diff] [blame] | 41 | : m_savedBuf(std::clog.rdbuf()) |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 42 | , m_savedLevel(LoggerFactory::getInstance().getDefaultLevel()) |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 43 | { |
Steve DiBenedetto | 4d43a45 | 2014-04-06 18:55:29 -0600 | [diff] [blame] | 44 | std::clog.rdbuf(m_buffer.rdbuf()); |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 45 | } |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 46 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 47 | ~LoggerFixture() |
| 48 | { |
Steve DiBenedetto | 4d43a45 | 2014-04-06 18:55:29 -0600 | [diff] [blame] | 49 | std::clog.rdbuf(m_savedBuf); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 50 | LoggerFactory::getInstance().setDefaultLevel(m_savedLevel); |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 51 | } |
Alexander Afanasyev | b84cc92 | 2014-02-24 17:52:58 -0800 | [diff] [blame] | 52 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 53 | std::stringstream m_buffer; |
| 54 | std::streambuf* m_savedBuf; |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 55 | LogLevel m_savedLevel; |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 56 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 57 | }; |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 58 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 59 | BOOST_FIXTURE_TEST_CASE(Basic, LoggerFixture) |
| 60 | { |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 61 | using namespace ndn::time; |
| 62 | using std::string; |
| 63 | |
| 64 | const ndn::time::microseconds::rep ONE_SECOND = 1000000; |
| 65 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 66 | NFD_LOG_INIT("BasicTests"); |
Alexander Afanasyev | b84cc92 | 2014-02-24 17:52:58 -0800 | [diff] [blame] | 67 | g_logger.setLogLevel(LOG_ALL); |
| 68 | |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 69 | 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 Afanasyev | b84cc92 | 2014-02-24 17:52:58 -0800 | [diff] [blame] | 78 | |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 79 | 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 | |
| 128 | // std::cout << "before=" << before << " extracted=" << extractedTime << " after=" << after << std::endl; |
| 129 | |
| 130 | BOOST_REQUIRE(before <= extractedTime); |
| 131 | BOOST_REQUIRE(extractedTime <= after); |
| 132 | |
| 133 | // LOG_LEVEL: |
| 134 | BOOST_REQUIRE_EQUAL(*componentIter, EXPECTED[i]); |
| 135 | ++componentIter; |
| 136 | ++i; |
| 137 | |
| 138 | // [ModuleName] |
| 139 | BOOST_REQUIRE_EQUAL(*componentIter, EXPECTED[i]); |
| 140 | ++componentIter; |
| 141 | ++i; |
| 142 | |
| 143 | const string& message = *componentIter; |
| 144 | |
| 145 | // std::cout << "message = " << message << std::endl; |
| 146 | |
| 147 | // add back the newline that we split on |
| 148 | BOOST_REQUIRE_EQUAL(message + "\n", EXPECTED[i]); |
| 149 | ++componentIter; |
| 150 | } |
| 151 | |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 152 | } |
| 153 | |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 154 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 155 | BOOST_FIXTURE_TEST_CASE(ConfigureFactory, LoggerFixture) |
| 156 | { |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 157 | using namespace ndn::time; |
| 158 | using std::string; |
| 159 | |
| 160 | const ndn::time::microseconds::rep ONE_SECOND = 1000000; |
| 161 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 162 | NFD_LOG_INIT("ConfigureFactoryTests"); |
| 163 | |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 164 | const string LOG_CONFIG = |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 165 | "log\n" |
| 166 | "{\n" |
| 167 | " default_level INFO\n" |
| 168 | "}\n"; |
| 169 | |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 170 | LoggerFactory::getInstance().setDefaultLevel(LOG_ALL); |
| 171 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 172 | ConfigFile config; |
| 173 | LoggerFactory::getInstance().setConfigFile(config); |
| 174 | |
| 175 | config.parse(LOG_CONFIG, false, "LOG_CONFIG"); |
| 176 | |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 177 | BOOST_REQUIRE_EQUAL(LoggerFactory::getInstance().getDefaultLevel(), LOG_INFO); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 178 | |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 179 | const std::string EXPECTED[] = |
| 180 | { |
| 181 | "WARNING:", "[ConfigureFactoryTests]", "warning-message-XXXhdhd111x\n", |
| 182 | "INFO:", "[ConfigureFactoryTests]", "info-message-Jjxjshj13\n", |
| 183 | "ERROR:", "[ConfigureFactoryTests]", "error-message-!#$&^%$#@\n", |
| 184 | "FATAL:", "[ConfigureFactoryTests]", "fatal-message-JJSjaamcng\n", |
| 185 | }; |
| 186 | |
| 187 | const size_t N_EXPECTED = sizeof(EXPECTED) / sizeof(std::string); |
| 188 | |
| 189 | microseconds::rep before = |
| 190 | duration_cast<microseconds>(system_clock::now().time_since_epoch()).count(); |
| 191 | |
| 192 | NFD_LOG_TRACE("trace-message-JHGFDSR^1"); |
| 193 | NFD_LOG_DEBUG("debug-message-IGg2474fdksd-fo-" << 15 << 16 << 17); |
| 194 | NFD_LOG_WARN("warning-message-XXXhdhd11" << 1 <<"x"); |
| 195 | NFD_LOG_INFO("info-message-Jjxjshj13"); |
| 196 | NFD_LOG_ERROR("error-message-!#$&^%$#@"); |
| 197 | NFD_LOG_FATAL("fatal-message-JJSjaamcng"); |
| 198 | |
| 199 | microseconds::rep after = |
| 200 | duration_cast<microseconds>(system_clock::now().time_since_epoch()).count(); |
| 201 | |
| 202 | const string buffer = m_buffer.str(); |
| 203 | |
| 204 | std::vector<string> components; |
| 205 | boost::split(components, buffer, boost::is_any_of(" ,\n")); |
| 206 | |
| 207 | // std::cout << components.size() << " for " << moduleName << std::endl; |
| 208 | // for (size_t i = 0; i < components.size(); ++i) |
| 209 | // { |
| 210 | // std::cout << "-> " << components[i] << std::endl; |
| 211 | // } |
| 212 | |
| 213 | // expected + number of timestamps (one per log statement) + trailing newline of last statement |
| 214 | BOOST_REQUIRE_EQUAL(components.size(), N_EXPECTED + 4 + 1); |
| 215 | |
| 216 | std::vector<std::string>::const_iterator componentIter = components.begin(); |
| 217 | for (size_t i = 0; i < N_EXPECTED; ++i) |
| 218 | { |
| 219 | // timestamp LOG_LEVEL: [ModuleName] message\n |
| 220 | |
| 221 | const string& timestamp = *componentIter; |
| 222 | // std::cout << "timestamp = " << timestamp << std::endl; |
| 223 | ++componentIter; |
| 224 | |
| 225 | size_t timeDelimiterPosition = timestamp.find("."); |
| 226 | |
| 227 | BOOST_REQUIRE_NE(string::npos, timeDelimiterPosition); |
| 228 | |
| 229 | string secondsString = timestamp.substr(0, timeDelimiterPosition); |
| 230 | string usecondsString = timestamp.substr(timeDelimiterPosition + 1); |
| 231 | |
| 232 | microseconds::rep extractedTime = |
| 233 | ONE_SECOND * boost::lexical_cast<microseconds::rep>(secondsString) + |
| 234 | boost::lexical_cast<microseconds::rep>(usecondsString); |
| 235 | |
| 236 | // std::cout << "before=" << before << " extracted=" << extractedTime << " after=" << after << std::endl; |
| 237 | |
| 238 | BOOST_REQUIRE(before <= extractedTime); |
| 239 | BOOST_REQUIRE(extractedTime <= after); |
| 240 | |
| 241 | // LOG_LEVEL: |
| 242 | BOOST_REQUIRE_EQUAL(*componentIter, EXPECTED[i]); |
| 243 | ++componentIter; |
| 244 | ++i; |
| 245 | |
| 246 | // [ModuleName] |
| 247 | BOOST_REQUIRE_EQUAL(*componentIter, EXPECTED[i]); |
| 248 | ++componentIter; |
| 249 | ++i; |
| 250 | |
| 251 | const string& message = *componentIter; |
| 252 | |
| 253 | // std::cout << "message = " << message << std::endl; |
| 254 | |
| 255 | // add back the newline that we split on |
| 256 | BOOST_REQUIRE_EQUAL(message + "\n", EXPECTED[i]); |
| 257 | ++componentIter; |
| 258 | } |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | BOOST_FIXTURE_TEST_CASE(TestNumberLevel, LoggerFixture) |
| 262 | { |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 263 | const std::string LOG_CONFIG = |
| 264 | "log\n" |
| 265 | "{\n" |
| 266 | " default_level 2\n" // equivalent of WARN |
| 267 | "}\n"; |
| 268 | |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 269 | LoggerFactory::getInstance().setDefaultLevel(LOG_ALL); |
| 270 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 271 | ConfigFile config; |
| 272 | LoggerFactory::getInstance().setConfigFile(config); |
| 273 | |
| 274 | config.parse(LOG_CONFIG, false, "LOG_CONFIG"); |
| 275 | |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 276 | BOOST_REQUIRE_EQUAL(LoggerFactory::getInstance().getDefaultLevel(), LOG_WARN); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | static void |
| 280 | testModuleBPrint() |
| 281 | { |
| 282 | NFD_LOG_INIT("TestModuleB"); |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 283 | NFD_LOG_DEBUG("debug-message-IGg2474fdksd-fo-" << 15 << 16 << 17); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | BOOST_FIXTURE_TEST_CASE(LimitModules, LoggerFixture) |
| 287 | { |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 288 | using namespace ndn::time; |
| 289 | using std::string; |
| 290 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 291 | NFD_LOG_INIT("TestModuleA"); |
| 292 | |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 293 | const ndn::time::microseconds::rep ONE_SECOND = 1000000; |
| 294 | |
| 295 | const std::string EXPECTED[] = |
| 296 | { |
| 297 | "WARNING:", "[TestModuleA]", "warning-message-XXXhdhd111x\n", |
| 298 | }; |
| 299 | |
| 300 | const size_t N_EXPECTED = sizeof(EXPECTED) / sizeof(std::string); |
| 301 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 302 | const std::string LOG_CONFIG = |
| 303 | "log\n" |
| 304 | "{\n" |
| 305 | " default_level WARN\n" |
| 306 | "}\n"; |
| 307 | |
| 308 | ConfigFile config; |
| 309 | LoggerFactory::getInstance().setConfigFile(config); |
| 310 | |
| 311 | config.parse(LOG_CONFIG, false, "LOG_CONFIG"); |
| 312 | |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 313 | microseconds::rep before = |
| 314 | duration_cast<microseconds>(system_clock::now().time_since_epoch()).count(); |
| 315 | |
| 316 | // this should print |
| 317 | NFD_LOG_WARN("warning-message-XXXhdhd11" << 1 << "x"); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 318 | |
| 319 | // this should not because it's level is < WARN |
| 320 | testModuleBPrint(); |
| 321 | |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 322 | microseconds::rep after = |
| 323 | duration_cast<microseconds>(system_clock::now().time_since_epoch()).count(); |
| 324 | |
| 325 | const string buffer = m_buffer.str(); |
| 326 | |
| 327 | std::vector<string> components; |
| 328 | boost::split(components, buffer, boost::is_any_of(" ,\n")); |
| 329 | |
| 330 | // expected + number of timestamps (one per log statement) + trailing newline of last statement |
| 331 | BOOST_REQUIRE_EQUAL(components.size(), N_EXPECTED + 1 + 1); |
| 332 | |
| 333 | std::vector<std::string>::const_iterator componentIter = components.begin(); |
| 334 | for (size_t i = 0; i < N_EXPECTED; ++i) |
| 335 | { |
| 336 | // timestamp LOG_LEVEL: [ModuleName] message\n |
| 337 | |
| 338 | const string& timestamp = *componentIter; |
| 339 | // std::cout << "timestamp = " << timestamp << std::endl; |
| 340 | ++componentIter; |
| 341 | |
| 342 | size_t timeDelimiterPosition = timestamp.find("."); |
| 343 | |
| 344 | BOOST_REQUIRE_NE(string::npos, timeDelimiterPosition); |
| 345 | |
| 346 | string secondsString = timestamp.substr(0, timeDelimiterPosition); |
| 347 | string usecondsString = timestamp.substr(timeDelimiterPosition + 1); |
| 348 | |
| 349 | microseconds::rep extractedTime = |
| 350 | ONE_SECOND * boost::lexical_cast<microseconds::rep>(secondsString) + |
| 351 | boost::lexical_cast<microseconds::rep>(usecondsString); |
| 352 | |
| 353 | // std::cout << "before=" << before << " extracted=" << extractedTime << " after=" << after << std::endl; |
| 354 | |
| 355 | BOOST_REQUIRE(before <= extractedTime); |
| 356 | BOOST_REQUIRE(extractedTime <= after); |
| 357 | |
| 358 | // LOG_LEVEL: |
| 359 | BOOST_REQUIRE_EQUAL(*componentIter, EXPECTED[i]); |
| 360 | ++componentIter; |
| 361 | ++i; |
| 362 | |
| 363 | // [ModuleName] |
| 364 | BOOST_REQUIRE_EQUAL(*componentIter, EXPECTED[i]); |
| 365 | ++componentIter; |
| 366 | ++i; |
| 367 | |
| 368 | const string& message = *componentIter; |
| 369 | |
| 370 | // std::cout << "message = " << message << std::endl; |
| 371 | |
| 372 | // add back the newline that we split on |
| 373 | BOOST_REQUIRE_EQUAL(message + "\n", EXPECTED[i]); |
| 374 | ++componentIter; |
| 375 | } |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | BOOST_FIXTURE_TEST_CASE(ExplicitlySetModule, LoggerFixture) |
| 379 | { |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 380 | using namespace ndn::time; |
| 381 | using std::string; |
| 382 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 383 | NFD_LOG_INIT("TestModuleA"); |
| 384 | |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 385 | const ndn::time::microseconds::rep ONE_SECOND = 1000000; |
| 386 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 387 | const std::string LOG_CONFIG = |
| 388 | "log\n" |
| 389 | "{\n" |
| 390 | " default_level WARN\n" |
| 391 | " TestModuleB DEBUG\n" |
| 392 | "}\n"; |
| 393 | |
| 394 | ConfigFile config; |
| 395 | LoggerFactory::getInstance().setConfigFile(config); |
| 396 | |
| 397 | config.parse(LOG_CONFIG, false, "LOG_CONFIG"); |
| 398 | |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 399 | microseconds::rep before = |
| 400 | duration_cast<microseconds>(system_clock::now().time_since_epoch()).count(); |
| 401 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 402 | // this should print |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 403 | NFD_LOG_WARN("warning-message-XXXhdhd11" << 1 << "x"); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 404 | |
| 405 | // this too because its level is explicitly set to DEBUG |
| 406 | testModuleBPrint(); |
| 407 | |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 408 | microseconds::rep after = |
| 409 | duration_cast<microseconds>(system_clock::now().time_since_epoch()).count(); |
| 410 | |
| 411 | const std::string EXPECTED[] = |
| 412 | { |
| 413 | "WARNING:", "[TestModuleA]", "warning-message-XXXhdhd111x\n", |
| 414 | "DEBUG:", "[TestModuleB]", "debug-message-IGg2474fdksd-fo-151617\n", |
| 415 | }; |
| 416 | |
| 417 | const size_t N_EXPECTED = sizeof(EXPECTED) / sizeof(std::string); |
| 418 | |
| 419 | const string buffer = m_buffer.str(); |
| 420 | |
| 421 | std::vector<string> components; |
| 422 | boost::split(components, buffer, boost::is_any_of(" ,\n")); |
| 423 | |
| 424 | // for (size_t i = 0; i < components.size(); ++i) |
| 425 | // { |
| 426 | // std::cout << "-> " << components[i] << std::endl; |
| 427 | // } |
| 428 | |
| 429 | // expected + number of timestamps (one per log statement) + trailing newline of last statement |
| 430 | BOOST_REQUIRE_EQUAL(components.size(), N_EXPECTED + 2 + 1); |
| 431 | |
| 432 | std::vector<std::string>::const_iterator componentIter = components.begin(); |
| 433 | for (size_t i = 0; i < N_EXPECTED; ++i) |
| 434 | { |
| 435 | // timestamp LOG_LEVEL: [ModuleName] message\n |
| 436 | |
| 437 | const string& timestamp = *componentIter; |
| 438 | // std::cout << "timestamp = " << timestamp << std::endl; |
| 439 | ++componentIter; |
| 440 | |
| 441 | size_t timeDelimiterPosition = timestamp.find("."); |
| 442 | |
| 443 | BOOST_REQUIRE_NE(string::npos, timeDelimiterPosition); |
| 444 | |
| 445 | string secondsString = timestamp.substr(0, timeDelimiterPosition); |
| 446 | string usecondsString = timestamp.substr(timeDelimiterPosition + 1); |
| 447 | |
| 448 | microseconds::rep extractedTime = |
| 449 | ONE_SECOND * boost::lexical_cast<microseconds::rep>(secondsString) + |
| 450 | boost::lexical_cast<microseconds::rep>(usecondsString); |
| 451 | |
| 452 | // std::cout << "before=" << before << " extracted=" << extractedTime << " after=" << after << std::endl; |
| 453 | |
| 454 | BOOST_REQUIRE(before <= extractedTime); |
| 455 | BOOST_REQUIRE(extractedTime <= after); |
| 456 | |
| 457 | // LOG_LEVEL: |
| 458 | BOOST_REQUIRE_EQUAL(*componentIter, EXPECTED[i]); |
| 459 | ++componentIter; |
| 460 | ++i; |
| 461 | |
| 462 | // [ModuleName] |
| 463 | BOOST_REQUIRE_EQUAL(*componentIter, EXPECTED[i]); |
| 464 | ++componentIter; |
| 465 | ++i; |
| 466 | |
| 467 | const string& message = *componentIter; |
| 468 | |
| 469 | // std::cout << "message = " << message << std::endl; |
| 470 | |
| 471 | // add back the newline that we split on |
| 472 | BOOST_REQUIRE_EQUAL(message + "\n", EXPECTED[i]); |
| 473 | ++componentIter; |
| 474 | } |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | static bool |
| 478 | checkError(const LoggerFactory::Error& error, const std::string& expected) |
| 479 | { |
| 480 | return error.what() == expected; |
| 481 | } |
| 482 | |
| 483 | BOOST_FIXTURE_TEST_CASE(UnknownLevelString, LoggerFixture) |
| 484 | { |
| 485 | const std::string LOG_CONFIG = |
| 486 | "log\n" |
| 487 | "{\n" |
| 488 | " default_level TestMadeUpLevel\n" |
| 489 | "}\n"; |
| 490 | |
| 491 | ConfigFile config; |
| 492 | LoggerFactory::getInstance().setConfigFile(config); |
| 493 | |
| 494 | BOOST_REQUIRE_EXCEPTION(config.parse(LOG_CONFIG, false, "LOG_CONFIG"), |
| 495 | LoggerFactory::Error, |
| 496 | bind(&checkError, |
| 497 | _1, |
| 498 | "Unsupported logging level \"TestMadeUpLevel\"")); |
| 499 | } |
| 500 | |
| 501 | BOOST_FIXTURE_TEST_CASE(UnknownOption, LoggerFixture) |
| 502 | { |
| 503 | const std::string LOG_CONFIG = |
| 504 | "log\n" |
| 505 | "{\n" |
| 506 | " TestMadeUpOption\n" |
| 507 | "}\n"; |
| 508 | |
| 509 | ConfigFile config; |
| 510 | LoggerFactory::getInstance().setConfigFile(config); |
| 511 | |
| 512 | BOOST_REQUIRE_EXCEPTION(config.parse(LOG_CONFIG, false, "LOG_CONFIG"), |
| 513 | LoggerFactory::Error, |
| 514 | bind(&checkError, |
| 515 | _1, |
| 516 | "No logging level found for option \"TestMadeUpOption\"")); |
| 517 | } |
| 518 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 519 | class InClassLogger : public LoggerFixture |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 520 | { |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 521 | public: |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 522 | |
Alexander Afanasyev | b84cc92 | 2014-02-24 17:52:58 -0800 | [diff] [blame] | 523 | InClassLogger() |
| 524 | { |
| 525 | g_logger.setLogLevel(LOG_ALL); |
| 526 | } |
| 527 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 528 | void |
| 529 | writeLogs() |
| 530 | { |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 531 | NFD_LOG_TRACE("trace-message-JHGFDSR^1"); |
| 532 | NFD_LOG_DEBUG("debug-message-IGg2474fdksd-fo-" << 15 << 16 << 17); |
| 533 | NFD_LOG_WARN("warning-message-XXXhdhd11" << 1 <<"x"); |
| 534 | NFD_LOG_INFO("info-message-Jjxjshj13"); |
| 535 | NFD_LOG_ERROR("error-message-!#$&^%$#@"); |
| 536 | NFD_LOG_FATAL("fatal-message-JJSjaamcng"); |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 537 | } |
Alexander Afanasyev | b84cc92 | 2014-02-24 17:52:58 -0800 | [diff] [blame] | 538 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 539 | private: |
| 540 | NFD_LOG_INCLASS_DECLARE(); |
| 541 | }; |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 542 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 543 | NFD_LOG_INCLASS_DEFINE(InClassLogger, "InClassLogger"); |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 544 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 545 | BOOST_FIXTURE_TEST_CASE(InClass, InClassLogger) |
| 546 | { |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 547 | using namespace ndn::time; |
| 548 | using std::string; |
| 549 | |
| 550 | const ndn::time::microseconds::rep ONE_SECOND = 1000000; |
| 551 | |
| 552 | microseconds::rep before = |
| 553 | duration_cast<microseconds>(system_clock::now().time_since_epoch()).count(); |
| 554 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 555 | writeLogs(); |
Alexander Afanasyev | b84cc92 | 2014-02-24 17:52:58 -0800 | [diff] [blame] | 556 | |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 557 | microseconds::rep after = |
| 558 | duration_cast<microseconds>(system_clock::now().time_since_epoch()).count(); |
| 559 | |
| 560 | const string EXPECTED[] = |
| 561 | { |
| 562 | "TRACE:", "[InClassLogger]", "trace-message-JHGFDSR^1\n", |
| 563 | "DEBUG:", "[InClassLogger]", "debug-message-IGg2474fdksd-fo-151617\n", |
| 564 | "WARNING:", "[InClassLogger]", "warning-message-XXXhdhd111x\n", |
| 565 | "INFO:", "[InClassLogger]", "info-message-Jjxjshj13\n", |
| 566 | "ERROR:", "[InClassLogger]", "error-message-!#$&^%$#@\n", |
| 567 | "FATAL:", "[InClassLogger]", "fatal-message-JJSjaamcng\n", |
| 568 | }; |
| 569 | |
| 570 | const size_t N_EXPECTED = sizeof(EXPECTED) / sizeof(string); |
| 571 | |
| 572 | const string buffer = m_buffer.str(); |
| 573 | |
| 574 | std::vector<string> components; |
| 575 | boost::split(components, buffer, boost::is_any_of(" ,\n")); |
| 576 | |
| 577 | // expected + number of timestamps (one per log statement) + trailing newline of last statement |
| 578 | BOOST_REQUIRE_EQUAL(components.size(), N_EXPECTED + 6 + 1); |
| 579 | |
| 580 | std::vector<std::string>::const_iterator componentIter = components.begin(); |
| 581 | for (size_t i = 0; i < N_EXPECTED; ++i) |
| 582 | { |
| 583 | // timestamp LOG_LEVEL: [ModuleName] message\n |
| 584 | |
| 585 | const string& timestamp = *componentIter; |
| 586 | // std::cout << "timestamp = " << timestamp << std::endl; |
| 587 | ++componentIter; |
| 588 | |
| 589 | size_t timeDelimiterPosition = timestamp.find("."); |
| 590 | |
| 591 | BOOST_REQUIRE_NE(string::npos, timeDelimiterPosition); |
| 592 | |
| 593 | string secondsString = timestamp.substr(0, timeDelimiterPosition); |
| 594 | string usecondsString = timestamp.substr(timeDelimiterPosition + 1); |
| 595 | |
| 596 | microseconds::rep extractedTime = |
| 597 | ONE_SECOND * boost::lexical_cast<microseconds::rep>(secondsString) + |
| 598 | boost::lexical_cast<microseconds::rep>(usecondsString); |
| 599 | |
| 600 | // std::cout << "before=" << before << " extracted=" << extractedTime << " after=" << after << std::endl; |
| 601 | |
| 602 | BOOST_REQUIRE(before <= extractedTime); |
| 603 | BOOST_REQUIRE(extractedTime <= after); |
| 604 | |
| 605 | // LOG_LEVEL: |
| 606 | BOOST_REQUIRE_EQUAL(*componentIter, EXPECTED[i]); |
| 607 | ++componentIter; |
| 608 | ++i; |
| 609 | |
| 610 | // [ModuleName] |
| 611 | BOOST_REQUIRE_EQUAL(*componentIter, EXPECTED[i]); |
| 612 | ++componentIter; |
| 613 | ++i; |
| 614 | |
| 615 | const string& message = *componentIter; |
| 616 | |
| 617 | // std::cout << "message = " << message << std::endl; |
| 618 | |
| 619 | // add back the newline that we split on |
| 620 | BOOST_REQUIRE_EQUAL(message + "\n", EXPECTED[i]); |
| 621 | ++componentIter; |
| 622 | } |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 623 | } |
| 624 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 625 | |
| 626 | template<class T> |
| 627 | class InClassTemplateLogger : public LoggerFixture |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 628 | { |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 629 | public: |
Alexander Afanasyev | b84cc92 | 2014-02-24 17:52:58 -0800 | [diff] [blame] | 630 | InClassTemplateLogger() |
| 631 | { |
| 632 | g_logger.setLogLevel(LOG_ALL); |
| 633 | } |
| 634 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 635 | void |
| 636 | writeLogs() |
| 637 | { |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 638 | NFD_LOG_TRACE("trace-message-JHGFDSR^1"); |
| 639 | NFD_LOG_DEBUG("debug-message-IGg2474fdksd-fo-" << 15 << 16 << 17); |
| 640 | NFD_LOG_WARN("warning-message-XXXhdhd11" << 1 <<"x"); |
| 641 | NFD_LOG_INFO("info-message-Jjxjshj13"); |
| 642 | NFD_LOG_ERROR("error-message-!#$&^%$#@"); |
| 643 | NFD_LOG_FATAL("fatal-message-JJSjaamcng"); |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 644 | } |
Alexander Afanasyev | b84cc92 | 2014-02-24 17:52:58 -0800 | [diff] [blame] | 645 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 646 | private: |
| 647 | NFD_LOG_INCLASS_DECLARE(); |
| 648 | }; |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 649 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 650 | NFD_LOG_INCLASS_TEMPLATE_DEFINE(InClassTemplateLogger, "GenericInClassTemplateLogger"); |
| 651 | NFD_LOG_INCLASS_TEMPLATE_SPECIALIZATION_DEFINE(InClassTemplateLogger, int, "IntInClassLogger"); |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 652 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 653 | BOOST_FIXTURE_TEST_CASE(GenericInTemplatedClass, InClassTemplateLogger<bool>) |
| 654 | { |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 655 | using namespace ndn::time; |
| 656 | using std::string; |
| 657 | |
| 658 | const ndn::time::microseconds::rep ONE_SECOND = 1000000; |
| 659 | |
| 660 | microseconds::rep before = |
| 661 | duration_cast<microseconds>(system_clock::now().time_since_epoch()).count(); |
| 662 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 663 | writeLogs(); |
Alexander Afanasyev | b84cc92 | 2014-02-24 17:52:58 -0800 | [diff] [blame] | 664 | |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 665 | microseconds::rep after = |
| 666 | duration_cast<microseconds>(system_clock::now().time_since_epoch()).count(); |
| 667 | |
| 668 | const string EXPECTED[] = |
| 669 | { |
| 670 | "TRACE:", "[GenericInClassTemplateLogger]", "trace-message-JHGFDSR^1\n", |
| 671 | "DEBUG:", "[GenericInClassTemplateLogger]", "debug-message-IGg2474fdksd-fo-151617\n", |
| 672 | "WARNING:", "[GenericInClassTemplateLogger]", "warning-message-XXXhdhd111x\n", |
| 673 | "INFO:", "[GenericInClassTemplateLogger]", "info-message-Jjxjshj13\n", |
| 674 | "ERROR:", "[GenericInClassTemplateLogger]", "error-message-!#$&^%$#@\n", |
| 675 | "FATAL:", "[GenericInClassTemplateLogger]", "fatal-message-JJSjaamcng\n", |
| 676 | }; |
| 677 | |
| 678 | const size_t N_EXPECTED = sizeof(EXPECTED) / sizeof(string); |
| 679 | |
| 680 | const string buffer = m_buffer.str(); |
| 681 | |
| 682 | std::vector<string> components; |
| 683 | boost::split(components, buffer, boost::is_any_of(" ,\n")); |
| 684 | |
| 685 | // expected + number of timestamps (one per log statement) + trailing newline of last statement |
| 686 | BOOST_REQUIRE_EQUAL(components.size(), N_EXPECTED + 6 + 1); |
| 687 | |
| 688 | std::vector<std::string>::const_iterator componentIter = components.begin(); |
| 689 | for (size_t i = 0; i < N_EXPECTED; ++i) |
| 690 | { |
| 691 | // timestamp LOG_LEVEL: [ModuleName] message\n |
| 692 | |
| 693 | const string& timestamp = *componentIter; |
| 694 | // std::cout << "timestamp = " << timestamp << std::endl; |
| 695 | ++componentIter; |
| 696 | |
| 697 | size_t timeDelimiterPosition = timestamp.find("."); |
| 698 | |
| 699 | BOOST_REQUIRE_NE(string::npos, timeDelimiterPosition); |
| 700 | |
| 701 | string secondsString = timestamp.substr(0, timeDelimiterPosition); |
| 702 | string usecondsString = timestamp.substr(timeDelimiterPosition + 1); |
| 703 | |
| 704 | microseconds::rep extractedTime = |
| 705 | ONE_SECOND * boost::lexical_cast<microseconds::rep>(secondsString) + |
| 706 | boost::lexical_cast<microseconds::rep>(usecondsString); |
| 707 | |
| 708 | // std::cout << "before=" << before << " extracted=" << extractedTime << " after=" << after << std::endl; |
| 709 | |
| 710 | BOOST_REQUIRE(before <= extractedTime); |
| 711 | BOOST_REQUIRE(extractedTime <= after); |
| 712 | |
| 713 | // LOG_LEVEL: |
| 714 | BOOST_REQUIRE_EQUAL(*componentIter, EXPECTED[i]); |
| 715 | ++componentIter; |
| 716 | ++i; |
| 717 | |
| 718 | // [ModuleName] |
| 719 | BOOST_REQUIRE_EQUAL(*componentIter, EXPECTED[i]); |
| 720 | ++componentIter; |
| 721 | ++i; |
| 722 | |
| 723 | const string& message = *componentIter; |
| 724 | |
| 725 | // std::cout << "message = " << message << std::endl; |
| 726 | |
| 727 | // add back the newline that we split on |
| 728 | BOOST_REQUIRE_EQUAL(message + "\n", EXPECTED[i]); |
| 729 | ++componentIter; |
| 730 | } |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 731 | } |
| 732 | |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 733 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 734 | BOOST_FIXTURE_TEST_CASE(SpecializedInTemplatedClass, InClassTemplateLogger<int>) |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 735 | { |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 736 | using namespace ndn::time; |
| 737 | using std::string; |
| 738 | |
| 739 | const ndn::time::microseconds::rep ONE_SECOND = 1000000; |
| 740 | |
| 741 | microseconds::rep before = |
| 742 | duration_cast<microseconds>(system_clock::now().time_since_epoch()).count(); |
| 743 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 744 | writeLogs(); |
Alexander Afanasyev | b84cc92 | 2014-02-24 17:52:58 -0800 | [diff] [blame] | 745 | |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 746 | microseconds::rep after = |
| 747 | duration_cast<microseconds>(system_clock::now().time_since_epoch()).count(); |
| 748 | |
| 749 | const string EXPECTED[] = |
| 750 | { |
| 751 | "TRACE:", "[IntInClassLogger]", "trace-message-JHGFDSR^1\n", |
| 752 | "DEBUG:", "[IntInClassLogger]", "debug-message-IGg2474fdksd-fo-151617\n", |
| 753 | "WARNING:", "[IntInClassLogger]", "warning-message-XXXhdhd111x\n", |
| 754 | "INFO:", "[IntInClassLogger]", "info-message-Jjxjshj13\n", |
| 755 | "ERROR:", "[IntInClassLogger]", "error-message-!#$&^%$#@\n", |
| 756 | "FATAL:", "[IntInClassLogger]", "fatal-message-JJSjaamcng\n", |
| 757 | }; |
| 758 | |
| 759 | const size_t N_EXPECTED = sizeof(EXPECTED) / sizeof(string); |
| 760 | |
| 761 | const string buffer = m_buffer.str(); |
| 762 | |
| 763 | std::vector<string> components; |
| 764 | boost::split(components, buffer, boost::is_any_of(" ,\n")); |
| 765 | |
| 766 | // expected + number of timestamps (one per log statement) + trailing newline of last statement |
| 767 | BOOST_REQUIRE_EQUAL(components.size(), N_EXPECTED + 6 + 1); |
| 768 | |
| 769 | std::vector<std::string>::const_iterator componentIter = components.begin(); |
| 770 | for (size_t i = 0; i < N_EXPECTED; ++i) |
| 771 | { |
| 772 | // timestamp LOG_LEVEL: [ModuleName] message\n |
| 773 | |
| 774 | const string& timestamp = *componentIter; |
| 775 | // std::cout << "timestamp = " << timestamp << std::endl; |
| 776 | ++componentIter; |
| 777 | |
| 778 | size_t timeDelimiterPosition = timestamp.find("."); |
| 779 | |
| 780 | BOOST_REQUIRE_NE(string::npos, timeDelimiterPosition); |
| 781 | |
| 782 | string secondsString = timestamp.substr(0, timeDelimiterPosition); |
| 783 | string usecondsString = timestamp.substr(timeDelimiterPosition + 1); |
| 784 | |
| 785 | microseconds::rep extractedTime = |
| 786 | ONE_SECOND * boost::lexical_cast<microseconds::rep>(secondsString) + |
| 787 | boost::lexical_cast<microseconds::rep>(usecondsString); |
| 788 | |
| 789 | // std::cout << "before=" << before << " extracted=" << extractedTime << " after=" << after << std::endl; |
| 790 | |
| 791 | BOOST_REQUIRE(before <= extractedTime); |
| 792 | BOOST_REQUIRE(extractedTime <= after); |
| 793 | |
| 794 | // LOG_LEVEL: |
| 795 | BOOST_REQUIRE_EQUAL(*componentIter, EXPECTED[i]); |
| 796 | ++componentIter; |
| 797 | ++i; |
| 798 | |
| 799 | // [ModuleName] |
| 800 | BOOST_REQUIRE_EQUAL(*componentIter, EXPECTED[i]); |
| 801 | ++componentIter; |
| 802 | ++i; |
| 803 | |
| 804 | const string& message = *componentIter; |
| 805 | |
| 806 | // std::cout << "message = " << message << std::endl; |
| 807 | |
| 808 | // add back the newline that we split on |
| 809 | BOOST_REQUIRE_EQUAL(message + "\n", EXPECTED[i]); |
| 810 | ++componentIter; |
| 811 | } |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 812 | } |
| 813 | |
| 814 | BOOST_AUTO_TEST_SUITE_END() |
| 815 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 816 | } // namespace tests |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 817 | } // namespace nfd |