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" |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 496 | " default_level DEBUG\n" |
Steve DiBenedetto | 6ad48ca | 2014-04-22 09:39:31 -0600 | [diff] [blame] | 497 | " TestMadeUpModule INFO\n" |
| 498 | "}\n"; |
| 499 | |
| 500 | ConfigFile config; |
| 501 | LoggerFactory::getInstance().setDefaultLevel(LOG_ALL); |
| 502 | LoggerFactory::getInstance().setConfigFile(config); |
| 503 | |
| 504 | const std::string EXPECTED = "DEBUG: [LoggerFactory] " |
| 505 | "Failed to configure logging level for module \"TestMadeUpModule\" (module not found)\n"; |
| 506 | |
| 507 | microseconds::rep before = |
| 508 | duration_cast<microseconds>(system_clock::now().time_since_epoch()).count(); |
| 509 | |
| 510 | config.parse(LOG_CONFIG, false, "LOG_CONFIG"); |
| 511 | |
| 512 | microseconds::rep after = |
| 513 | duration_cast<microseconds>(system_clock::now().time_since_epoch()).count(); |
| 514 | |
| 515 | const string buffer = m_buffer.str(); |
| 516 | |
| 517 | const size_t firstSpace = buffer.find(" "); |
| 518 | BOOST_REQUIRE(firstSpace != string::npos); |
| 519 | |
| 520 | const string timestamp = buffer.substr(0, firstSpace); |
| 521 | const string message = buffer.substr(firstSpace + 1); |
| 522 | |
| 523 | size_t timeDelimiterPosition = timestamp.find("."); |
| 524 | |
| 525 | BOOST_REQUIRE_NE(string::npos, timeDelimiterPosition); |
| 526 | |
| 527 | string secondsString = timestamp.substr(0, timeDelimiterPosition); |
| 528 | string usecondsString = timestamp.substr(timeDelimiterPosition + 1); |
| 529 | |
| 530 | microseconds::rep extractedTime = |
| 531 | ONE_SECOND * boost::lexical_cast<microseconds::rep>(secondsString) + |
| 532 | boost::lexical_cast<microseconds::rep>(usecondsString); |
| 533 | |
| 534 | // std::cout << "before=" << before |
| 535 | // << " extracted=" << extractedTime |
| 536 | // << " after=" << after << std::endl; |
| 537 | |
| 538 | BOOST_CHECK_LE(before, extractedTime); |
| 539 | BOOST_CHECK_LE(extractedTime, after); |
| 540 | |
| 541 | BOOST_CHECK_EQUAL(message, EXPECTED); |
| 542 | } |
| 543 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 544 | static bool |
| 545 | checkError(const LoggerFactory::Error& error, const std::string& expected) |
| 546 | { |
| 547 | return error.what() == expected; |
| 548 | } |
| 549 | |
| 550 | BOOST_FIXTURE_TEST_CASE(UnknownLevelString, LoggerFixture) |
| 551 | { |
| 552 | const std::string LOG_CONFIG = |
| 553 | "log\n" |
| 554 | "{\n" |
| 555 | " default_level TestMadeUpLevel\n" |
| 556 | "}\n"; |
| 557 | |
| 558 | ConfigFile config; |
| 559 | LoggerFactory::getInstance().setConfigFile(config); |
| 560 | |
| 561 | BOOST_REQUIRE_EXCEPTION(config.parse(LOG_CONFIG, false, "LOG_CONFIG"), |
| 562 | LoggerFactory::Error, |
| 563 | bind(&checkError, |
| 564 | _1, |
| 565 | "Unsupported logging level \"TestMadeUpLevel\"")); |
| 566 | } |
| 567 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 568 | class InClassLogger : public LoggerFixture |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 569 | { |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 570 | public: |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 571 | |
Alexander Afanasyev | b84cc92 | 2014-02-24 17:52:58 -0800 | [diff] [blame] | 572 | InClassLogger() |
| 573 | { |
| 574 | g_logger.setLogLevel(LOG_ALL); |
| 575 | } |
| 576 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 577 | void |
| 578 | writeLogs() |
| 579 | { |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 580 | NFD_LOG_TRACE("trace-message-JHGFDSR^1"); |
| 581 | NFD_LOG_DEBUG("debug-message-IGg2474fdksd-fo-" << 15 << 16 << 17); |
| 582 | NFD_LOG_WARN("warning-message-XXXhdhd11" << 1 <<"x"); |
| 583 | NFD_LOG_INFO("info-message-Jjxjshj13"); |
| 584 | NFD_LOG_ERROR("error-message-!#$&^%$#@"); |
| 585 | NFD_LOG_FATAL("fatal-message-JJSjaamcng"); |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 586 | } |
Alexander Afanasyev | b84cc92 | 2014-02-24 17:52:58 -0800 | [diff] [blame] | 587 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 588 | private: |
| 589 | NFD_LOG_INCLASS_DECLARE(); |
| 590 | }; |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 591 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 592 | NFD_LOG_INCLASS_DEFINE(InClassLogger, "InClassLogger"); |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 593 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 594 | BOOST_FIXTURE_TEST_CASE(InClass, InClassLogger) |
| 595 | { |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 596 | using namespace ndn::time; |
| 597 | using std::string; |
| 598 | |
| 599 | const ndn::time::microseconds::rep ONE_SECOND = 1000000; |
| 600 | |
| 601 | microseconds::rep before = |
| 602 | duration_cast<microseconds>(system_clock::now().time_since_epoch()).count(); |
| 603 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 604 | writeLogs(); |
Alexander Afanasyev | b84cc92 | 2014-02-24 17:52:58 -0800 | [diff] [blame] | 605 | |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 606 | microseconds::rep after = |
| 607 | duration_cast<microseconds>(system_clock::now().time_since_epoch()).count(); |
| 608 | |
| 609 | const string EXPECTED[] = |
| 610 | { |
| 611 | "TRACE:", "[InClassLogger]", "trace-message-JHGFDSR^1\n", |
| 612 | "DEBUG:", "[InClassLogger]", "debug-message-IGg2474fdksd-fo-151617\n", |
| 613 | "WARNING:", "[InClassLogger]", "warning-message-XXXhdhd111x\n", |
| 614 | "INFO:", "[InClassLogger]", "info-message-Jjxjshj13\n", |
| 615 | "ERROR:", "[InClassLogger]", "error-message-!#$&^%$#@\n", |
| 616 | "FATAL:", "[InClassLogger]", "fatal-message-JJSjaamcng\n", |
| 617 | }; |
| 618 | |
| 619 | const size_t N_EXPECTED = sizeof(EXPECTED) / sizeof(string); |
| 620 | |
| 621 | const string buffer = m_buffer.str(); |
| 622 | |
| 623 | std::vector<string> components; |
| 624 | boost::split(components, buffer, boost::is_any_of(" ,\n")); |
| 625 | |
| 626 | // expected + number of timestamps (one per log statement) + trailing newline of last statement |
| 627 | BOOST_REQUIRE_EQUAL(components.size(), N_EXPECTED + 6 + 1); |
| 628 | |
| 629 | std::vector<std::string>::const_iterator componentIter = components.begin(); |
| 630 | for (size_t i = 0; i < N_EXPECTED; ++i) |
| 631 | { |
| 632 | // timestamp LOG_LEVEL: [ModuleName] message\n |
| 633 | |
| 634 | const string& timestamp = *componentIter; |
| 635 | // std::cout << "timestamp = " << timestamp << std::endl; |
| 636 | ++componentIter; |
| 637 | |
| 638 | size_t timeDelimiterPosition = timestamp.find("."); |
| 639 | |
| 640 | BOOST_REQUIRE_NE(string::npos, timeDelimiterPosition); |
| 641 | |
| 642 | string secondsString = timestamp.substr(0, timeDelimiterPosition); |
| 643 | string usecondsString = timestamp.substr(timeDelimiterPosition + 1); |
| 644 | |
| 645 | microseconds::rep extractedTime = |
| 646 | ONE_SECOND * boost::lexical_cast<microseconds::rep>(secondsString) + |
| 647 | boost::lexical_cast<microseconds::rep>(usecondsString); |
| 648 | |
Steve DiBenedetto | 6ad48ca | 2014-04-22 09:39:31 -0600 | [diff] [blame] | 649 | // std::cout << "before=" << before |
| 650 | // << " extracted=" << extractedTime |
| 651 | // << " after=" << after << std::endl; |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 652 | |
Steve DiBenedetto | 6ad48ca | 2014-04-22 09:39:31 -0600 | [diff] [blame] | 653 | BOOST_CHECK_LE(before, extractedTime); |
| 654 | BOOST_CHECK_LE(extractedTime, after); |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 655 | |
| 656 | // LOG_LEVEL: |
Steve DiBenedetto | 6ad48ca | 2014-04-22 09:39:31 -0600 | [diff] [blame] | 657 | BOOST_CHECK_EQUAL(*componentIter, EXPECTED[i]); |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 658 | ++componentIter; |
| 659 | ++i; |
| 660 | |
| 661 | // [ModuleName] |
Steve DiBenedetto | 6ad48ca | 2014-04-22 09:39:31 -0600 | [diff] [blame] | 662 | BOOST_CHECK_EQUAL(*componentIter, EXPECTED[i]); |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 663 | ++componentIter; |
| 664 | ++i; |
| 665 | |
| 666 | const string& message = *componentIter; |
| 667 | |
| 668 | // std::cout << "message = " << message << std::endl; |
| 669 | |
| 670 | // add back the newline that we split on |
Steve DiBenedetto | 6ad48ca | 2014-04-22 09:39:31 -0600 | [diff] [blame] | 671 | BOOST_CHECK_EQUAL(message + "\n", EXPECTED[i]); |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 672 | ++componentIter; |
| 673 | } |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 674 | } |
| 675 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 676 | |
| 677 | template<class T> |
| 678 | class InClassTemplateLogger : public LoggerFixture |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 679 | { |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 680 | public: |
Alexander Afanasyev | b84cc92 | 2014-02-24 17:52:58 -0800 | [diff] [blame] | 681 | InClassTemplateLogger() |
| 682 | { |
| 683 | g_logger.setLogLevel(LOG_ALL); |
| 684 | } |
| 685 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 686 | void |
| 687 | writeLogs() |
| 688 | { |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 689 | NFD_LOG_TRACE("trace-message-JHGFDSR^1"); |
| 690 | NFD_LOG_DEBUG("debug-message-IGg2474fdksd-fo-" << 15 << 16 << 17); |
| 691 | NFD_LOG_WARN("warning-message-XXXhdhd11" << 1 <<"x"); |
| 692 | NFD_LOG_INFO("info-message-Jjxjshj13"); |
| 693 | NFD_LOG_ERROR("error-message-!#$&^%$#@"); |
| 694 | NFD_LOG_FATAL("fatal-message-JJSjaamcng"); |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 695 | } |
Alexander Afanasyev | b84cc92 | 2014-02-24 17:52:58 -0800 | [diff] [blame] | 696 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 697 | private: |
| 698 | NFD_LOG_INCLASS_DECLARE(); |
| 699 | }; |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 700 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 701 | NFD_LOG_INCLASS_TEMPLATE_DEFINE(InClassTemplateLogger, "GenericInClassTemplateLogger"); |
| 702 | NFD_LOG_INCLASS_TEMPLATE_SPECIALIZATION_DEFINE(InClassTemplateLogger, int, "IntInClassLogger"); |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 703 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 704 | BOOST_FIXTURE_TEST_CASE(GenericInTemplatedClass, InClassTemplateLogger<bool>) |
| 705 | { |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 706 | using namespace ndn::time; |
| 707 | using std::string; |
| 708 | |
| 709 | const ndn::time::microseconds::rep ONE_SECOND = 1000000; |
| 710 | |
| 711 | microseconds::rep before = |
| 712 | duration_cast<microseconds>(system_clock::now().time_since_epoch()).count(); |
| 713 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 714 | writeLogs(); |
Alexander Afanasyev | b84cc92 | 2014-02-24 17:52:58 -0800 | [diff] [blame] | 715 | |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 716 | microseconds::rep after = |
| 717 | duration_cast<microseconds>(system_clock::now().time_since_epoch()).count(); |
| 718 | |
| 719 | const string EXPECTED[] = |
| 720 | { |
| 721 | "TRACE:", "[GenericInClassTemplateLogger]", "trace-message-JHGFDSR^1\n", |
| 722 | "DEBUG:", "[GenericInClassTemplateLogger]", "debug-message-IGg2474fdksd-fo-151617\n", |
| 723 | "WARNING:", "[GenericInClassTemplateLogger]", "warning-message-XXXhdhd111x\n", |
| 724 | "INFO:", "[GenericInClassTemplateLogger]", "info-message-Jjxjshj13\n", |
| 725 | "ERROR:", "[GenericInClassTemplateLogger]", "error-message-!#$&^%$#@\n", |
| 726 | "FATAL:", "[GenericInClassTemplateLogger]", "fatal-message-JJSjaamcng\n", |
| 727 | }; |
| 728 | |
| 729 | const size_t N_EXPECTED = sizeof(EXPECTED) / sizeof(string); |
| 730 | |
| 731 | const string buffer = m_buffer.str(); |
| 732 | |
| 733 | std::vector<string> components; |
| 734 | boost::split(components, buffer, boost::is_any_of(" ,\n")); |
| 735 | |
| 736 | // expected + number of timestamps (one per log statement) + trailing newline of last statement |
| 737 | BOOST_REQUIRE_EQUAL(components.size(), N_EXPECTED + 6 + 1); |
| 738 | |
| 739 | std::vector<std::string>::const_iterator componentIter = components.begin(); |
| 740 | for (size_t i = 0; i < N_EXPECTED; ++i) |
| 741 | { |
| 742 | // timestamp LOG_LEVEL: [ModuleName] message\n |
| 743 | |
| 744 | const string& timestamp = *componentIter; |
| 745 | // std::cout << "timestamp = " << timestamp << std::endl; |
| 746 | ++componentIter; |
| 747 | |
| 748 | size_t timeDelimiterPosition = timestamp.find("."); |
| 749 | |
| 750 | BOOST_REQUIRE_NE(string::npos, timeDelimiterPosition); |
| 751 | |
| 752 | string secondsString = timestamp.substr(0, timeDelimiterPosition); |
| 753 | string usecondsString = timestamp.substr(timeDelimiterPosition + 1); |
| 754 | |
| 755 | microseconds::rep extractedTime = |
| 756 | ONE_SECOND * boost::lexical_cast<microseconds::rep>(secondsString) + |
| 757 | boost::lexical_cast<microseconds::rep>(usecondsString); |
| 758 | |
Steve DiBenedetto | 6ad48ca | 2014-04-22 09:39:31 -0600 | [diff] [blame] | 759 | // std::cout << "before=" << before |
| 760 | // << " extracted=" << extractedTime |
| 761 | // << " after=" << after << std::endl; |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 762 | |
Steve DiBenedetto | 6ad48ca | 2014-04-22 09:39:31 -0600 | [diff] [blame] | 763 | BOOST_CHECK_LE(before, extractedTime); |
| 764 | BOOST_CHECK_LE(extractedTime, after); |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 765 | |
| 766 | // LOG_LEVEL: |
Steve DiBenedetto | 6ad48ca | 2014-04-22 09:39:31 -0600 | [diff] [blame] | 767 | BOOST_CHECK_EQUAL(*componentIter, EXPECTED[i]); |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 768 | ++componentIter; |
| 769 | ++i; |
| 770 | |
| 771 | // [ModuleName] |
Steve DiBenedetto | 6ad48ca | 2014-04-22 09:39:31 -0600 | [diff] [blame] | 772 | BOOST_CHECK_EQUAL(*componentIter, EXPECTED[i]); |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 773 | ++componentIter; |
| 774 | ++i; |
| 775 | |
| 776 | const string& message = *componentIter; |
| 777 | |
| 778 | // std::cout << "message = " << message << std::endl; |
| 779 | |
| 780 | // add back the newline that we split on |
Steve DiBenedetto | 6ad48ca | 2014-04-22 09:39:31 -0600 | [diff] [blame] | 781 | BOOST_CHECK_EQUAL(message + "\n", EXPECTED[i]); |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 782 | ++componentIter; |
| 783 | } |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 784 | } |
| 785 | |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 786 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 787 | BOOST_FIXTURE_TEST_CASE(SpecializedInTemplatedClass, InClassTemplateLogger<int>) |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 788 | { |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 789 | using namespace ndn::time; |
| 790 | using std::string; |
| 791 | |
| 792 | const ndn::time::microseconds::rep ONE_SECOND = 1000000; |
| 793 | |
| 794 | microseconds::rep before = |
| 795 | duration_cast<microseconds>(system_clock::now().time_since_epoch()).count(); |
| 796 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 797 | writeLogs(); |
Alexander Afanasyev | b84cc92 | 2014-02-24 17:52:58 -0800 | [diff] [blame] | 798 | |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 799 | microseconds::rep after = |
| 800 | duration_cast<microseconds>(system_clock::now().time_since_epoch()).count(); |
| 801 | |
| 802 | const string EXPECTED[] = |
| 803 | { |
| 804 | "TRACE:", "[IntInClassLogger]", "trace-message-JHGFDSR^1\n", |
| 805 | "DEBUG:", "[IntInClassLogger]", "debug-message-IGg2474fdksd-fo-151617\n", |
| 806 | "WARNING:", "[IntInClassLogger]", "warning-message-XXXhdhd111x\n", |
| 807 | "INFO:", "[IntInClassLogger]", "info-message-Jjxjshj13\n", |
| 808 | "ERROR:", "[IntInClassLogger]", "error-message-!#$&^%$#@\n", |
| 809 | "FATAL:", "[IntInClassLogger]", "fatal-message-JJSjaamcng\n", |
| 810 | }; |
| 811 | |
| 812 | const size_t N_EXPECTED = sizeof(EXPECTED) / sizeof(string); |
| 813 | |
| 814 | const string buffer = m_buffer.str(); |
| 815 | |
| 816 | std::vector<string> components; |
| 817 | boost::split(components, buffer, boost::is_any_of(" ,\n")); |
| 818 | |
| 819 | // expected + number of timestamps (one per log statement) + trailing newline of last statement |
| 820 | BOOST_REQUIRE_EQUAL(components.size(), N_EXPECTED + 6 + 1); |
| 821 | |
| 822 | std::vector<std::string>::const_iterator componentIter = components.begin(); |
| 823 | for (size_t i = 0; i < N_EXPECTED; ++i) |
| 824 | { |
| 825 | // timestamp LOG_LEVEL: [ModuleName] message\n |
| 826 | |
| 827 | const string& timestamp = *componentIter; |
| 828 | // std::cout << "timestamp = " << timestamp << std::endl; |
| 829 | ++componentIter; |
| 830 | |
| 831 | size_t timeDelimiterPosition = timestamp.find("."); |
| 832 | |
| 833 | BOOST_REQUIRE_NE(string::npos, timeDelimiterPosition); |
| 834 | |
| 835 | string secondsString = timestamp.substr(0, timeDelimiterPosition); |
| 836 | string usecondsString = timestamp.substr(timeDelimiterPosition + 1); |
| 837 | |
| 838 | microseconds::rep extractedTime = |
| 839 | ONE_SECOND * boost::lexical_cast<microseconds::rep>(secondsString) + |
| 840 | boost::lexical_cast<microseconds::rep>(usecondsString); |
| 841 | |
| 842 | // std::cout << "before=" << before << " extracted=" << extractedTime << " after=" << after << std::endl; |
| 843 | |
Steve DiBenedetto | 6ad48ca | 2014-04-22 09:39:31 -0600 | [diff] [blame] | 844 | BOOST_CHECK_LE(before, extractedTime); |
| 845 | BOOST_CHECK_LE(extractedTime, after); |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 846 | |
| 847 | // LOG_LEVEL: |
Steve DiBenedetto | 6ad48ca | 2014-04-22 09:39:31 -0600 | [diff] [blame] | 848 | BOOST_CHECK_EQUAL(*componentIter, EXPECTED[i]); |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 849 | ++componentIter; |
| 850 | ++i; |
| 851 | |
| 852 | // [ModuleName] |
Steve DiBenedetto | 6ad48ca | 2014-04-22 09:39:31 -0600 | [diff] [blame] | 853 | BOOST_CHECK_EQUAL(*componentIter, EXPECTED[i]); |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 854 | ++componentIter; |
| 855 | ++i; |
| 856 | |
| 857 | const string& message = *componentIter; |
| 858 | |
| 859 | // std::cout << "message = " << message << std::endl; |
| 860 | |
| 861 | // add back the newline that we split on |
Steve DiBenedetto | 6ad48ca | 2014-04-22 09:39:31 -0600 | [diff] [blame] | 862 | BOOST_CHECK_EQUAL(message + "\n", EXPECTED[i]); |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 863 | ++componentIter; |
| 864 | } |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 865 | } |
| 866 | |
Alexander Afanasyev | 6b34ab9 | 2015-02-11 21:22:46 -0800 | [diff] [blame^] | 867 | BOOST_FIXTURE_TEST_CASE(LoggerFactoryListModules, LoggerFixture) |
| 868 | { |
| 869 | std::set<std::string> testCaseLoggers{"LoggerFactoryListModules1", "LoggerFactoryListModules2"}; |
| 870 | |
| 871 | for (const auto& name : testCaseLoggers) { |
| 872 | LoggerFactory::create(name); |
| 873 | } |
| 874 | |
| 875 | auto&& modules = LoggerFactory::getInstance().getModules(); |
| 876 | BOOST_CHECK_GE(modules.size(), 2); |
| 877 | |
| 878 | for (const auto& name : modules) { |
| 879 | testCaseLoggers.erase(name); |
| 880 | } |
| 881 | |
| 882 | BOOST_CHECK_EQUAL(testCaseLoggers.size(), 0); |
| 883 | } |
| 884 | |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 885 | BOOST_AUTO_TEST_SUITE_END() |
| 886 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 887 | } // namespace tests |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 888 | } // namespace nfd |