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