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