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