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 | |
Steve DiBenedetto | 6ad48ca | 2014-04-22 09:39:31 -0600 | [diff] [blame] | 128 | // std::cout << "before=" << before |
| 129 | // << " extracted=" << extractedTime |
| 130 | // << " after=" << after << std::endl; |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 131 | |
Steve DiBenedetto | 6ad48ca | 2014-04-22 09:39:31 -0600 | [diff] [blame] | 132 | |
| 133 | BOOST_CHECK_LE(before, extractedTime); |
| 134 | BOOST_CHECK_LE(extractedTime, after); |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 135 | |
| 136 | // LOG_LEVEL: |
Steve DiBenedetto | 6ad48ca | 2014-04-22 09:39:31 -0600 | [diff] [blame] | 137 | BOOST_CHECK_EQUAL(*componentIter, EXPECTED[i]); |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 138 | ++componentIter; |
| 139 | ++i; |
| 140 | |
| 141 | // [ModuleName] |
Steve DiBenedetto | 6ad48ca | 2014-04-22 09:39:31 -0600 | [diff] [blame] | 142 | BOOST_CHECK_EQUAL(*componentIter, EXPECTED[i]); |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 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 |
Steve DiBenedetto | 6ad48ca | 2014-04-22 09:39:31 -0600 | [diff] [blame] | 151 | BOOST_CHECK_EQUAL(message + "\n", EXPECTED[i]); |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 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 | |
Steve DiBenedetto | 6ad48ca | 2014-04-22 09:39:31 -0600 | [diff] [blame] | 239 | // std::cout << "before=" << before |
| 240 | // << " extracted=" << extractedTime |
| 241 | // << " after=" << after << std::endl; |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 242 | |
Steve DiBenedetto | 6ad48ca | 2014-04-22 09:39:31 -0600 | [diff] [blame] | 243 | BOOST_CHECK_LE(before, extractedTime); |
| 244 | BOOST_CHECK_LE(extractedTime, after); |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 245 | |
| 246 | // LOG_LEVEL: |
Steve DiBenedetto | 6ad48ca | 2014-04-22 09:39:31 -0600 | [diff] [blame] | 247 | BOOST_CHECK_EQUAL(*componentIter, EXPECTED[i]); |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 248 | ++componentIter; |
| 249 | ++i; |
| 250 | |
| 251 | // [ModuleName] |
Steve DiBenedetto | 6ad48ca | 2014-04-22 09:39:31 -0600 | [diff] [blame] | 252 | BOOST_CHECK_EQUAL(*componentIter, EXPECTED[i]); |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 253 | ++componentIter; |
| 254 | ++i; |
| 255 | |
| 256 | const string& message = *componentIter; |
| 257 | |
| 258 | // std::cout << "message = " << message << std::endl; |
| 259 | |
| 260 | // add back the newline that we split on |
Steve DiBenedetto | 6ad48ca | 2014-04-22 09:39:31 -0600 | [diff] [blame] | 261 | BOOST_CHECK_EQUAL(message + "\n", EXPECTED[i]); |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 262 | ++componentIter; |
| 263 | } |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | BOOST_FIXTURE_TEST_CASE(TestNumberLevel, LoggerFixture) |
| 267 | { |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 268 | const std::string LOG_CONFIG = |
| 269 | "log\n" |
| 270 | "{\n" |
| 271 | " default_level 2\n" // equivalent of WARN |
| 272 | "}\n"; |
| 273 | |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 274 | LoggerFactory::getInstance().setDefaultLevel(LOG_ALL); |
| 275 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 276 | ConfigFile config; |
| 277 | LoggerFactory::getInstance().setConfigFile(config); |
| 278 | |
| 279 | config.parse(LOG_CONFIG, false, "LOG_CONFIG"); |
| 280 | |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 281 | BOOST_REQUIRE_EQUAL(LoggerFactory::getInstance().getDefaultLevel(), LOG_WARN); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | static void |
| 285 | testModuleBPrint() |
| 286 | { |
| 287 | NFD_LOG_INIT("TestModuleB"); |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 288 | NFD_LOG_DEBUG("debug-message-IGg2474fdksd-fo-" << 15 << 16 << 17); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | BOOST_FIXTURE_TEST_CASE(LimitModules, LoggerFixture) |
| 292 | { |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 293 | using namespace ndn::time; |
| 294 | using std::string; |
| 295 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 296 | NFD_LOG_INIT("TestModuleA"); |
| 297 | |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 298 | const ndn::time::microseconds::rep ONE_SECOND = 1000000; |
| 299 | |
| 300 | const std::string EXPECTED[] = |
| 301 | { |
| 302 | "WARNING:", "[TestModuleA]", "warning-message-XXXhdhd111x\n", |
| 303 | }; |
| 304 | |
| 305 | const size_t N_EXPECTED = sizeof(EXPECTED) / sizeof(std::string); |
| 306 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 307 | const std::string LOG_CONFIG = |
| 308 | "log\n" |
| 309 | "{\n" |
| 310 | " default_level WARN\n" |
| 311 | "}\n"; |
| 312 | |
| 313 | ConfigFile config; |
| 314 | LoggerFactory::getInstance().setConfigFile(config); |
| 315 | |
| 316 | config.parse(LOG_CONFIG, false, "LOG_CONFIG"); |
| 317 | |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 318 | microseconds::rep before = |
| 319 | duration_cast<microseconds>(system_clock::now().time_since_epoch()).count(); |
| 320 | |
| 321 | // this should print |
| 322 | NFD_LOG_WARN("warning-message-XXXhdhd11" << 1 << "x"); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 323 | |
| 324 | // this should not because it's level is < WARN |
| 325 | testModuleBPrint(); |
| 326 | |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 327 | microseconds::rep after = |
| 328 | duration_cast<microseconds>(system_clock::now().time_since_epoch()).count(); |
| 329 | |
| 330 | const string buffer = m_buffer.str(); |
| 331 | |
| 332 | std::vector<string> components; |
| 333 | boost::split(components, buffer, boost::is_any_of(" ,\n")); |
| 334 | |
| 335 | // expected + number of timestamps (one per log statement) + trailing newline of last statement |
| 336 | BOOST_REQUIRE_EQUAL(components.size(), N_EXPECTED + 1 + 1); |
| 337 | |
| 338 | std::vector<std::string>::const_iterator componentIter = components.begin(); |
| 339 | for (size_t i = 0; i < N_EXPECTED; ++i) |
| 340 | { |
| 341 | // timestamp LOG_LEVEL: [ModuleName] message\n |
| 342 | |
| 343 | const string& timestamp = *componentIter; |
| 344 | // std::cout << "timestamp = " << timestamp << std::endl; |
| 345 | ++componentIter; |
| 346 | |
| 347 | size_t timeDelimiterPosition = timestamp.find("."); |
| 348 | |
| 349 | BOOST_REQUIRE_NE(string::npos, timeDelimiterPosition); |
| 350 | |
| 351 | string secondsString = timestamp.substr(0, timeDelimiterPosition); |
| 352 | string usecondsString = timestamp.substr(timeDelimiterPosition + 1); |
| 353 | |
| 354 | microseconds::rep extractedTime = |
| 355 | ONE_SECOND * boost::lexical_cast<microseconds::rep>(secondsString) + |
| 356 | boost::lexical_cast<microseconds::rep>(usecondsString); |
| 357 | |
Steve DiBenedetto | 6ad48ca | 2014-04-22 09:39:31 -0600 | [diff] [blame] | 358 | // std::cout << "before=" << before |
| 359 | // << " extracted=" << extractedTime |
| 360 | // << " after=" << after << std::endl; |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 361 | |
Steve DiBenedetto | 6ad48ca | 2014-04-22 09:39:31 -0600 | [diff] [blame] | 362 | BOOST_CHECK_LE(before, extractedTime); |
| 363 | BOOST_CHECK_LE(extractedTime, after); |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 364 | |
| 365 | // LOG_LEVEL: |
Steve DiBenedetto | 6ad48ca | 2014-04-22 09:39:31 -0600 | [diff] [blame] | 366 | BOOST_CHECK_EQUAL(*componentIter, EXPECTED[i]); |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 367 | ++componentIter; |
| 368 | ++i; |
| 369 | |
| 370 | // [ModuleName] |
Steve DiBenedetto | 6ad48ca | 2014-04-22 09:39:31 -0600 | [diff] [blame] | 371 | BOOST_CHECK_EQUAL(*componentIter, EXPECTED[i]); |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 372 | ++componentIter; |
| 373 | ++i; |
| 374 | |
| 375 | const string& message = *componentIter; |
| 376 | |
| 377 | // std::cout << "message = " << message << std::endl; |
| 378 | |
| 379 | // add back the newline that we split on |
Steve DiBenedetto | 6ad48ca | 2014-04-22 09:39:31 -0600 | [diff] [blame] | 380 | BOOST_CHECK_EQUAL(message + "\n", EXPECTED[i]); |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 381 | ++componentIter; |
| 382 | } |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | BOOST_FIXTURE_TEST_CASE(ExplicitlySetModule, LoggerFixture) |
| 386 | { |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 387 | using namespace ndn::time; |
| 388 | using std::string; |
| 389 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 390 | NFD_LOG_INIT("TestModuleA"); |
| 391 | |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 392 | const ndn::time::microseconds::rep ONE_SECOND = 1000000; |
| 393 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 394 | const std::string LOG_CONFIG = |
| 395 | "log\n" |
| 396 | "{\n" |
| 397 | " default_level WARN\n" |
| 398 | " TestModuleB DEBUG\n" |
| 399 | "}\n"; |
| 400 | |
| 401 | ConfigFile config; |
| 402 | LoggerFactory::getInstance().setConfigFile(config); |
| 403 | |
| 404 | config.parse(LOG_CONFIG, false, "LOG_CONFIG"); |
| 405 | |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 406 | microseconds::rep before = |
| 407 | duration_cast<microseconds>(system_clock::now().time_since_epoch()).count(); |
| 408 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 409 | // this should print |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 410 | NFD_LOG_WARN("warning-message-XXXhdhd11" << 1 << "x"); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 411 | |
| 412 | // this too because its level is explicitly set to DEBUG |
| 413 | testModuleBPrint(); |
| 414 | |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 415 | microseconds::rep after = |
| 416 | duration_cast<microseconds>(system_clock::now().time_since_epoch()).count(); |
| 417 | |
| 418 | const std::string EXPECTED[] = |
| 419 | { |
| 420 | "WARNING:", "[TestModuleA]", "warning-message-XXXhdhd111x\n", |
| 421 | "DEBUG:", "[TestModuleB]", "debug-message-IGg2474fdksd-fo-151617\n", |
| 422 | }; |
| 423 | |
| 424 | const size_t N_EXPECTED = sizeof(EXPECTED) / sizeof(std::string); |
| 425 | |
| 426 | const string buffer = m_buffer.str(); |
| 427 | |
| 428 | std::vector<string> components; |
| 429 | boost::split(components, buffer, boost::is_any_of(" ,\n")); |
| 430 | |
| 431 | // for (size_t i = 0; i < components.size(); ++i) |
| 432 | // { |
| 433 | // std::cout << "-> " << components[i] << std::endl; |
| 434 | // } |
| 435 | |
| 436 | // expected + number of timestamps (one per log statement) + trailing newline of last statement |
| 437 | BOOST_REQUIRE_EQUAL(components.size(), N_EXPECTED + 2 + 1); |
| 438 | |
| 439 | std::vector<std::string>::const_iterator componentIter = components.begin(); |
| 440 | for (size_t i = 0; i < N_EXPECTED; ++i) |
| 441 | { |
| 442 | // timestamp LOG_LEVEL: [ModuleName] message\n |
| 443 | |
| 444 | const string& timestamp = *componentIter; |
| 445 | // std::cout << "timestamp = " << timestamp << std::endl; |
| 446 | ++componentIter; |
| 447 | |
| 448 | size_t timeDelimiterPosition = timestamp.find("."); |
| 449 | |
| 450 | BOOST_REQUIRE_NE(string::npos, timeDelimiterPosition); |
| 451 | |
| 452 | string secondsString = timestamp.substr(0, timeDelimiterPosition); |
| 453 | string usecondsString = timestamp.substr(timeDelimiterPosition + 1); |
| 454 | |
| 455 | microseconds::rep extractedTime = |
| 456 | ONE_SECOND * boost::lexical_cast<microseconds::rep>(secondsString) + |
| 457 | boost::lexical_cast<microseconds::rep>(usecondsString); |
| 458 | |
Steve DiBenedetto | 6ad48ca | 2014-04-22 09:39:31 -0600 | [diff] [blame] | 459 | // std::cout << "before=" << before |
| 460 | // << " extracted=" << extractedTime |
| 461 | // << " after=" << after << std::endl; |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 462 | |
Steve DiBenedetto | 6ad48ca | 2014-04-22 09:39:31 -0600 | [diff] [blame] | 463 | BOOST_CHECK_LE(before, extractedTime); |
| 464 | BOOST_CHECK_LE(extractedTime, after); |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 465 | |
| 466 | // LOG_LEVEL: |
Steve DiBenedetto | 6ad48ca | 2014-04-22 09:39:31 -0600 | [diff] [blame] | 467 | BOOST_CHECK_EQUAL(*componentIter, EXPECTED[i]); |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 468 | ++componentIter; |
| 469 | ++i; |
| 470 | |
| 471 | // [ModuleName] |
Steve DiBenedetto | 6ad48ca | 2014-04-22 09:39:31 -0600 | [diff] [blame] | 472 | BOOST_CHECK_EQUAL(*componentIter, EXPECTED[i]); |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 473 | ++componentIter; |
| 474 | ++i; |
| 475 | |
| 476 | const string& message = *componentIter; |
| 477 | |
| 478 | // std::cout << "message = " << message << std::endl; |
| 479 | |
| 480 | // add back the newline that we split on |
Steve DiBenedetto | 6ad48ca | 2014-04-22 09:39:31 -0600 | [diff] [blame] | 481 | BOOST_CHECK_EQUAL(message + "\n", EXPECTED[i]); |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 482 | ++componentIter; |
| 483 | } |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 484 | } |
| 485 | |
Steve DiBenedetto | 6ad48ca | 2014-04-22 09:39:31 -0600 | [diff] [blame] | 486 | BOOST_FIXTURE_TEST_CASE(UnknownModule, LoggerFixture) |
| 487 | { |
| 488 | using namespace ndn::time; |
| 489 | using std::string; |
| 490 | |
| 491 | const ndn::time::microseconds::rep ONE_SECOND = 1000000; |
| 492 | |
| 493 | const std::string LOG_CONFIG = |
| 494 | "log\n" |
| 495 | "{\n" |
| 496 | " TestMadeUpModule INFO\n" |
| 497 | "}\n"; |
| 498 | |
| 499 | ConfigFile config; |
| 500 | LoggerFactory::getInstance().setDefaultLevel(LOG_ALL); |
| 501 | LoggerFactory::getInstance().setConfigFile(config); |
| 502 | |
| 503 | const std::string EXPECTED = "DEBUG: [LoggerFactory] " |
| 504 | "Failed to configure logging level for module \"TestMadeUpModule\" (module not found)\n"; |
| 505 | |
| 506 | microseconds::rep before = |
| 507 | duration_cast<microseconds>(system_clock::now().time_since_epoch()).count(); |
| 508 | |
| 509 | config.parse(LOG_CONFIG, false, "LOG_CONFIG"); |
| 510 | |
| 511 | microseconds::rep after = |
| 512 | duration_cast<microseconds>(system_clock::now().time_since_epoch()).count(); |
| 513 | |
| 514 | const string buffer = m_buffer.str(); |
| 515 | |
| 516 | const size_t firstSpace = buffer.find(" "); |
| 517 | BOOST_REQUIRE(firstSpace != string::npos); |
| 518 | |
| 519 | const string timestamp = buffer.substr(0, firstSpace); |
| 520 | const string message = buffer.substr(firstSpace + 1); |
| 521 | |
| 522 | size_t timeDelimiterPosition = timestamp.find("."); |
| 523 | |
| 524 | BOOST_REQUIRE_NE(string::npos, timeDelimiterPosition); |
| 525 | |
| 526 | string secondsString = timestamp.substr(0, timeDelimiterPosition); |
| 527 | string usecondsString = timestamp.substr(timeDelimiterPosition + 1); |
| 528 | |
| 529 | microseconds::rep extractedTime = |
| 530 | ONE_SECOND * boost::lexical_cast<microseconds::rep>(secondsString) + |
| 531 | boost::lexical_cast<microseconds::rep>(usecondsString); |
| 532 | |
| 533 | // std::cout << "before=" << before |
| 534 | // << " extracted=" << extractedTime |
| 535 | // << " after=" << after << std::endl; |
| 536 | |
| 537 | BOOST_CHECK_LE(before, extractedTime); |
| 538 | BOOST_CHECK_LE(extractedTime, after); |
| 539 | |
| 540 | BOOST_CHECK_EQUAL(message, EXPECTED); |
| 541 | } |
| 542 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 543 | static bool |
| 544 | checkError(const LoggerFactory::Error& error, const std::string& expected) |
| 545 | { |
| 546 | return error.what() == expected; |
| 547 | } |
| 548 | |
| 549 | BOOST_FIXTURE_TEST_CASE(UnknownLevelString, LoggerFixture) |
| 550 | { |
| 551 | const std::string LOG_CONFIG = |
| 552 | "log\n" |
| 553 | "{\n" |
| 554 | " default_level TestMadeUpLevel\n" |
| 555 | "}\n"; |
| 556 | |
| 557 | ConfigFile config; |
| 558 | LoggerFactory::getInstance().setConfigFile(config); |
| 559 | |
| 560 | BOOST_REQUIRE_EXCEPTION(config.parse(LOG_CONFIG, false, "LOG_CONFIG"), |
| 561 | LoggerFactory::Error, |
| 562 | bind(&checkError, |
| 563 | _1, |
| 564 | "Unsupported logging level \"TestMadeUpLevel\"")); |
| 565 | } |
| 566 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 567 | class InClassLogger : public LoggerFixture |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 568 | { |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 569 | public: |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 570 | |
Alexander Afanasyev | b84cc92 | 2014-02-24 17:52:58 -0800 | [diff] [blame] | 571 | InClassLogger() |
| 572 | { |
| 573 | g_logger.setLogLevel(LOG_ALL); |
| 574 | } |
| 575 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 576 | void |
| 577 | writeLogs() |
| 578 | { |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 579 | NFD_LOG_TRACE("trace-message-JHGFDSR^1"); |
| 580 | NFD_LOG_DEBUG("debug-message-IGg2474fdksd-fo-" << 15 << 16 << 17); |
| 581 | NFD_LOG_WARN("warning-message-XXXhdhd11" << 1 <<"x"); |
| 582 | NFD_LOG_INFO("info-message-Jjxjshj13"); |
| 583 | NFD_LOG_ERROR("error-message-!#$&^%$#@"); |
| 584 | NFD_LOG_FATAL("fatal-message-JJSjaamcng"); |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 585 | } |
Alexander Afanasyev | b84cc92 | 2014-02-24 17:52:58 -0800 | [diff] [blame] | 586 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 587 | private: |
| 588 | NFD_LOG_INCLASS_DECLARE(); |
| 589 | }; |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 590 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 591 | NFD_LOG_INCLASS_DEFINE(InClassLogger, "InClassLogger"); |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 592 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 593 | BOOST_FIXTURE_TEST_CASE(InClass, InClassLogger) |
| 594 | { |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 595 | using namespace ndn::time; |
| 596 | using std::string; |
| 597 | |
| 598 | const ndn::time::microseconds::rep ONE_SECOND = 1000000; |
| 599 | |
| 600 | microseconds::rep before = |
| 601 | duration_cast<microseconds>(system_clock::now().time_since_epoch()).count(); |
| 602 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 603 | writeLogs(); |
Alexander Afanasyev | b84cc92 | 2014-02-24 17:52:58 -0800 | [diff] [blame] | 604 | |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 605 | microseconds::rep after = |
| 606 | duration_cast<microseconds>(system_clock::now().time_since_epoch()).count(); |
| 607 | |
| 608 | const string EXPECTED[] = |
| 609 | { |
| 610 | "TRACE:", "[InClassLogger]", "trace-message-JHGFDSR^1\n", |
| 611 | "DEBUG:", "[InClassLogger]", "debug-message-IGg2474fdksd-fo-151617\n", |
| 612 | "WARNING:", "[InClassLogger]", "warning-message-XXXhdhd111x\n", |
| 613 | "INFO:", "[InClassLogger]", "info-message-Jjxjshj13\n", |
| 614 | "ERROR:", "[InClassLogger]", "error-message-!#$&^%$#@\n", |
| 615 | "FATAL:", "[InClassLogger]", "fatal-message-JJSjaamcng\n", |
| 616 | }; |
| 617 | |
| 618 | const size_t N_EXPECTED = sizeof(EXPECTED) / sizeof(string); |
| 619 | |
| 620 | const string buffer = m_buffer.str(); |
| 621 | |
| 622 | std::vector<string> components; |
| 623 | boost::split(components, buffer, boost::is_any_of(" ,\n")); |
| 624 | |
| 625 | // expected + number of timestamps (one per log statement) + trailing newline of last statement |
| 626 | BOOST_REQUIRE_EQUAL(components.size(), N_EXPECTED + 6 + 1); |
| 627 | |
| 628 | std::vector<std::string>::const_iterator componentIter = components.begin(); |
| 629 | for (size_t i = 0; i < N_EXPECTED; ++i) |
| 630 | { |
| 631 | // timestamp LOG_LEVEL: [ModuleName] message\n |
| 632 | |
| 633 | const string& timestamp = *componentIter; |
| 634 | // std::cout << "timestamp = " << timestamp << std::endl; |
| 635 | ++componentIter; |
| 636 | |
| 637 | size_t timeDelimiterPosition = timestamp.find("."); |
| 638 | |
| 639 | BOOST_REQUIRE_NE(string::npos, timeDelimiterPosition); |
| 640 | |
| 641 | string secondsString = timestamp.substr(0, timeDelimiterPosition); |
| 642 | string usecondsString = timestamp.substr(timeDelimiterPosition + 1); |
| 643 | |
| 644 | microseconds::rep extractedTime = |
| 645 | ONE_SECOND * boost::lexical_cast<microseconds::rep>(secondsString) + |
| 646 | boost::lexical_cast<microseconds::rep>(usecondsString); |
| 647 | |
Steve DiBenedetto | 6ad48ca | 2014-04-22 09:39:31 -0600 | [diff] [blame] | 648 | // std::cout << "before=" << before |
| 649 | // << " extracted=" << extractedTime |
| 650 | // << " after=" << after << std::endl; |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 651 | |
Steve DiBenedetto | 6ad48ca | 2014-04-22 09:39:31 -0600 | [diff] [blame] | 652 | BOOST_CHECK_LE(before, extractedTime); |
| 653 | BOOST_CHECK_LE(extractedTime, after); |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 654 | |
| 655 | // LOG_LEVEL: |
Steve DiBenedetto | 6ad48ca | 2014-04-22 09:39:31 -0600 | [diff] [blame] | 656 | BOOST_CHECK_EQUAL(*componentIter, EXPECTED[i]); |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 657 | ++componentIter; |
| 658 | ++i; |
| 659 | |
| 660 | // [ModuleName] |
Steve DiBenedetto | 6ad48ca | 2014-04-22 09:39:31 -0600 | [diff] [blame] | 661 | BOOST_CHECK_EQUAL(*componentIter, EXPECTED[i]); |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 662 | ++componentIter; |
| 663 | ++i; |
| 664 | |
| 665 | const string& message = *componentIter; |
| 666 | |
| 667 | // std::cout << "message = " << message << std::endl; |
| 668 | |
| 669 | // add back the newline that we split on |
Steve DiBenedetto | 6ad48ca | 2014-04-22 09:39:31 -0600 | [diff] [blame] | 670 | BOOST_CHECK_EQUAL(message + "\n", EXPECTED[i]); |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 671 | ++componentIter; |
| 672 | } |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 673 | } |
| 674 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 675 | |
| 676 | template<class T> |
| 677 | class InClassTemplateLogger : public LoggerFixture |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 678 | { |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 679 | public: |
Alexander Afanasyev | b84cc92 | 2014-02-24 17:52:58 -0800 | [diff] [blame] | 680 | InClassTemplateLogger() |
| 681 | { |
| 682 | g_logger.setLogLevel(LOG_ALL); |
| 683 | } |
| 684 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 685 | void |
| 686 | writeLogs() |
| 687 | { |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 688 | NFD_LOG_TRACE("trace-message-JHGFDSR^1"); |
| 689 | NFD_LOG_DEBUG("debug-message-IGg2474fdksd-fo-" << 15 << 16 << 17); |
| 690 | NFD_LOG_WARN("warning-message-XXXhdhd11" << 1 <<"x"); |
| 691 | NFD_LOG_INFO("info-message-Jjxjshj13"); |
| 692 | NFD_LOG_ERROR("error-message-!#$&^%$#@"); |
| 693 | NFD_LOG_FATAL("fatal-message-JJSjaamcng"); |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 694 | } |
Alexander Afanasyev | b84cc92 | 2014-02-24 17:52:58 -0800 | [diff] [blame] | 695 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 696 | private: |
| 697 | NFD_LOG_INCLASS_DECLARE(); |
| 698 | }; |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 699 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 700 | NFD_LOG_INCLASS_TEMPLATE_DEFINE(InClassTemplateLogger, "GenericInClassTemplateLogger"); |
| 701 | NFD_LOG_INCLASS_TEMPLATE_SPECIALIZATION_DEFINE(InClassTemplateLogger, int, "IntInClassLogger"); |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 702 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 703 | BOOST_FIXTURE_TEST_CASE(GenericInTemplatedClass, InClassTemplateLogger<bool>) |
| 704 | { |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 705 | using namespace ndn::time; |
| 706 | using std::string; |
| 707 | |
| 708 | const ndn::time::microseconds::rep ONE_SECOND = 1000000; |
| 709 | |
| 710 | microseconds::rep before = |
| 711 | duration_cast<microseconds>(system_clock::now().time_since_epoch()).count(); |
| 712 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 713 | writeLogs(); |
Alexander Afanasyev | b84cc92 | 2014-02-24 17:52:58 -0800 | [diff] [blame] | 714 | |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 715 | microseconds::rep after = |
| 716 | duration_cast<microseconds>(system_clock::now().time_since_epoch()).count(); |
| 717 | |
| 718 | const string EXPECTED[] = |
| 719 | { |
| 720 | "TRACE:", "[GenericInClassTemplateLogger]", "trace-message-JHGFDSR^1\n", |
| 721 | "DEBUG:", "[GenericInClassTemplateLogger]", "debug-message-IGg2474fdksd-fo-151617\n", |
| 722 | "WARNING:", "[GenericInClassTemplateLogger]", "warning-message-XXXhdhd111x\n", |
| 723 | "INFO:", "[GenericInClassTemplateLogger]", "info-message-Jjxjshj13\n", |
| 724 | "ERROR:", "[GenericInClassTemplateLogger]", "error-message-!#$&^%$#@\n", |
| 725 | "FATAL:", "[GenericInClassTemplateLogger]", "fatal-message-JJSjaamcng\n", |
| 726 | }; |
| 727 | |
| 728 | const size_t N_EXPECTED = sizeof(EXPECTED) / sizeof(string); |
| 729 | |
| 730 | const string buffer = m_buffer.str(); |
| 731 | |
| 732 | std::vector<string> components; |
| 733 | boost::split(components, buffer, boost::is_any_of(" ,\n")); |
| 734 | |
| 735 | // expected + number of timestamps (one per log statement) + trailing newline of last statement |
| 736 | BOOST_REQUIRE_EQUAL(components.size(), N_EXPECTED + 6 + 1); |
| 737 | |
| 738 | std::vector<std::string>::const_iterator componentIter = components.begin(); |
| 739 | for (size_t i = 0; i < N_EXPECTED; ++i) |
| 740 | { |
| 741 | // timestamp LOG_LEVEL: [ModuleName] message\n |
| 742 | |
| 743 | const string& timestamp = *componentIter; |
| 744 | // std::cout << "timestamp = " << timestamp << std::endl; |
| 745 | ++componentIter; |
| 746 | |
| 747 | size_t timeDelimiterPosition = timestamp.find("."); |
| 748 | |
| 749 | BOOST_REQUIRE_NE(string::npos, timeDelimiterPosition); |
| 750 | |
| 751 | string secondsString = timestamp.substr(0, timeDelimiterPosition); |
| 752 | string usecondsString = timestamp.substr(timeDelimiterPosition + 1); |
| 753 | |
| 754 | microseconds::rep extractedTime = |
| 755 | ONE_SECOND * boost::lexical_cast<microseconds::rep>(secondsString) + |
| 756 | boost::lexical_cast<microseconds::rep>(usecondsString); |
| 757 | |
Steve DiBenedetto | 6ad48ca | 2014-04-22 09:39:31 -0600 | [diff] [blame] | 758 | // std::cout << "before=" << before |
| 759 | // << " extracted=" << extractedTime |
| 760 | // << " after=" << after << std::endl; |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 761 | |
Steve DiBenedetto | 6ad48ca | 2014-04-22 09:39:31 -0600 | [diff] [blame] | 762 | BOOST_CHECK_LE(before, extractedTime); |
| 763 | BOOST_CHECK_LE(extractedTime, after); |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 764 | |
| 765 | // LOG_LEVEL: |
Steve DiBenedetto | 6ad48ca | 2014-04-22 09:39:31 -0600 | [diff] [blame] | 766 | BOOST_CHECK_EQUAL(*componentIter, EXPECTED[i]); |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 767 | ++componentIter; |
| 768 | ++i; |
| 769 | |
| 770 | // [ModuleName] |
Steve DiBenedetto | 6ad48ca | 2014-04-22 09:39:31 -0600 | [diff] [blame] | 771 | BOOST_CHECK_EQUAL(*componentIter, EXPECTED[i]); |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 772 | ++componentIter; |
| 773 | ++i; |
| 774 | |
| 775 | const string& message = *componentIter; |
| 776 | |
| 777 | // std::cout << "message = " << message << std::endl; |
| 778 | |
| 779 | // add back the newline that we split on |
Steve DiBenedetto | 6ad48ca | 2014-04-22 09:39:31 -0600 | [diff] [blame] | 780 | BOOST_CHECK_EQUAL(message + "\n", EXPECTED[i]); |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 781 | ++componentIter; |
| 782 | } |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 783 | } |
| 784 | |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 785 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 786 | BOOST_FIXTURE_TEST_CASE(SpecializedInTemplatedClass, InClassTemplateLogger<int>) |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 787 | { |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 788 | using namespace ndn::time; |
| 789 | using std::string; |
| 790 | |
| 791 | const ndn::time::microseconds::rep ONE_SECOND = 1000000; |
| 792 | |
| 793 | microseconds::rep before = |
| 794 | duration_cast<microseconds>(system_clock::now().time_since_epoch()).count(); |
| 795 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 796 | writeLogs(); |
Alexander Afanasyev | b84cc92 | 2014-02-24 17:52:58 -0800 | [diff] [blame] | 797 | |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 798 | microseconds::rep after = |
| 799 | duration_cast<microseconds>(system_clock::now().time_since_epoch()).count(); |
| 800 | |
| 801 | const string EXPECTED[] = |
| 802 | { |
| 803 | "TRACE:", "[IntInClassLogger]", "trace-message-JHGFDSR^1\n", |
| 804 | "DEBUG:", "[IntInClassLogger]", "debug-message-IGg2474fdksd-fo-151617\n", |
| 805 | "WARNING:", "[IntInClassLogger]", "warning-message-XXXhdhd111x\n", |
| 806 | "INFO:", "[IntInClassLogger]", "info-message-Jjxjshj13\n", |
| 807 | "ERROR:", "[IntInClassLogger]", "error-message-!#$&^%$#@\n", |
| 808 | "FATAL:", "[IntInClassLogger]", "fatal-message-JJSjaamcng\n", |
| 809 | }; |
| 810 | |
| 811 | const size_t N_EXPECTED = sizeof(EXPECTED) / sizeof(string); |
| 812 | |
| 813 | const string buffer = m_buffer.str(); |
| 814 | |
| 815 | std::vector<string> components; |
| 816 | boost::split(components, buffer, boost::is_any_of(" ,\n")); |
| 817 | |
| 818 | // expected + number of timestamps (one per log statement) + trailing newline of last statement |
| 819 | BOOST_REQUIRE_EQUAL(components.size(), N_EXPECTED + 6 + 1); |
| 820 | |
| 821 | std::vector<std::string>::const_iterator componentIter = components.begin(); |
| 822 | for (size_t i = 0; i < N_EXPECTED; ++i) |
| 823 | { |
| 824 | // timestamp LOG_LEVEL: [ModuleName] message\n |
| 825 | |
| 826 | const string& timestamp = *componentIter; |
| 827 | // std::cout << "timestamp = " << timestamp << std::endl; |
| 828 | ++componentIter; |
| 829 | |
| 830 | size_t timeDelimiterPosition = timestamp.find("."); |
| 831 | |
| 832 | BOOST_REQUIRE_NE(string::npos, timeDelimiterPosition); |
| 833 | |
| 834 | string secondsString = timestamp.substr(0, timeDelimiterPosition); |
| 835 | string usecondsString = timestamp.substr(timeDelimiterPosition + 1); |
| 836 | |
| 837 | microseconds::rep extractedTime = |
| 838 | ONE_SECOND * boost::lexical_cast<microseconds::rep>(secondsString) + |
| 839 | boost::lexical_cast<microseconds::rep>(usecondsString); |
| 840 | |
| 841 | // std::cout << "before=" << before << " extracted=" << extractedTime << " after=" << after << std::endl; |
| 842 | |
Steve DiBenedetto | 6ad48ca | 2014-04-22 09:39:31 -0600 | [diff] [blame] | 843 | BOOST_CHECK_LE(before, extractedTime); |
| 844 | BOOST_CHECK_LE(extractedTime, after); |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 845 | |
| 846 | // LOG_LEVEL: |
Steve DiBenedetto | 6ad48ca | 2014-04-22 09:39:31 -0600 | [diff] [blame] | 847 | BOOST_CHECK_EQUAL(*componentIter, EXPECTED[i]); |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 848 | ++componentIter; |
| 849 | ++i; |
| 850 | |
| 851 | // [ModuleName] |
Steve DiBenedetto | 6ad48ca | 2014-04-22 09:39:31 -0600 | [diff] [blame] | 852 | BOOST_CHECK_EQUAL(*componentIter, EXPECTED[i]); |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 853 | ++componentIter; |
| 854 | ++i; |
| 855 | |
| 856 | const string& message = *componentIter; |
| 857 | |
| 858 | // std::cout << "message = " << message << std::endl; |
| 859 | |
| 860 | // add back the newline that we split on |
Steve DiBenedetto | 6ad48ca | 2014-04-22 09:39:31 -0600 | [diff] [blame] | 861 | BOOST_CHECK_EQUAL(message + "\n", EXPECTED[i]); |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 862 | ++componentIter; |
| 863 | } |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 864 | } |
| 865 | |
| 866 | BOOST_AUTO_TEST_SUITE_END() |
| 867 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 868 | } // namespace tests |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 869 | } // namespace nfd |