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