Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
dmcoomes | e062a18 | 2017-06-12 11:10:31 -0500 | [diff] [blame] | 2 | /* |
Alexander Afanasyev | 354f382 | 2017-03-27 15:26:41 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 6 | * |
| 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 20 | */ |
Junxiao Shi | 1fe7ce5 | 2016-08-08 05:48:02 +0000 | [diff] [blame] | 21 | |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 22 | #include "util/logging.hpp" |
| 23 | #include "util/logger.hpp" |
dmcoomes | e062a18 | 2017-06-12 11:10:31 -0500 | [diff] [blame] | 24 | #include "../unit-test-time-fixture.hpp" |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 25 | |
| 26 | #include "boost-test.hpp" |
| 27 | #include <boost/test/output_test_stream.hpp> |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 28 | |
| 29 | namespace ndn { |
| 30 | namespace util { |
| 31 | namespace tests { |
| 32 | |
Alexander Afanasyev | 354f382 | 2017-03-27 15:26:41 -0500 | [diff] [blame] | 33 | NDN_LOG_INIT(ndn.util.tests.Logging); |
| 34 | |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 35 | using namespace ndn::tests; |
| 36 | using boost::test_tools::output_test_stream; |
| 37 | |
| 38 | void |
| 39 | logFromModule1(); |
| 40 | |
| 41 | void |
| 42 | logFromModule2(); |
| 43 | |
| 44 | void |
dmcoomes | e062a18 | 2017-06-12 11:10:31 -0500 | [diff] [blame] | 45 | logFromFilterModule(); |
| 46 | |
| 47 | static void |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 48 | logFromNewLogger(const std::string& moduleName) |
| 49 | { |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 50 | auto loggerPtr = make_unique<Logger>(moduleName); |
| 51 | Logger& logger = *loggerPtr; |
| 52 | |
| 53 | auto getNdnCxxLogger = [&logger] () -> Logger& { return logger; }; |
Junxiao Shi | 1fe7ce5 | 2016-08-08 05:48:02 +0000 | [diff] [blame] | 54 | NDN_LOG_TRACE("trace" << moduleName); |
| 55 | NDN_LOG_DEBUG("debug" << moduleName); |
| 56 | NDN_LOG_INFO("info" << moduleName); |
| 57 | NDN_LOG_WARN("warn" << moduleName); |
| 58 | NDN_LOG_ERROR("error" << moduleName); |
| 59 | NDN_LOG_FATAL("fatal" << moduleName); |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 60 | |
| 61 | BOOST_CHECK(Logging::get().removeLogger(logger)); |
| 62 | } |
| 63 | |
| 64 | const time::system_clock::Duration LOG_SYSTIME = time::microseconds(1468108800311239LL); |
| 65 | const std::string LOG_SYSTIME_STR = "1468108800.311239"; |
| 66 | |
| 67 | class LoggingFixture : public UnitTestTimeFixture |
| 68 | { |
| 69 | protected: |
| 70 | explicit |
| 71 | LoggingFixture() |
dmcoomes | e062a18 | 2017-06-12 11:10:31 -0500 | [diff] [blame] | 72 | : m_oldEnabledLevel(Logging::get().getLevels()) |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 73 | , m_oldDestination(Logging::get().getDestination()) |
| 74 | { |
| 75 | this->systemClock->setNow(LOG_SYSTIME); |
| 76 | Logging::get().resetLevels(); |
| 77 | Logging::setDestination(os); |
| 78 | } |
| 79 | |
| 80 | ~LoggingFixture() |
| 81 | { |
dmcoomes | e062a18 | 2017-06-12 11:10:31 -0500 | [diff] [blame] | 82 | Logging::get().setLevelImpl(m_oldEnabledLevel); |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 83 | Logging::setDestination(m_oldDestination); |
| 84 | } |
| 85 | |
| 86 | protected: |
| 87 | output_test_stream os; |
| 88 | |
| 89 | private: |
dmcoomes | e062a18 | 2017-06-12 11:10:31 -0500 | [diff] [blame] | 90 | std::unordered_map<std::string, LogLevel> m_oldEnabledLevel; |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 91 | shared_ptr<std::ostream> m_oldDestination; |
| 92 | }; |
| 93 | |
| 94 | BOOST_AUTO_TEST_SUITE(Util) |
| 95 | BOOST_FIXTURE_TEST_SUITE(TestLogging, LoggingFixture) |
| 96 | |
Alexander Afanasyev | 354f382 | 2017-03-27 15:26:41 -0500 | [diff] [blame] | 97 | BOOST_AUTO_TEST_CASE(GetLoggerNames) |
| 98 | { |
| 99 | NDN_LOG_TRACE("GetLoggerNames"); // to avoid unused function warning |
| 100 | std::set<std::string> names = Logging::getLoggerNames(); |
| 101 | BOOST_CHECK_GT(names.size(), 0); |
| 102 | BOOST_CHECK_EQUAL(names.count("ndn.util.tests.Logging"), 1); |
| 103 | } |
| 104 | |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 105 | BOOST_AUTO_TEST_SUITE(Severity) |
| 106 | |
| 107 | BOOST_AUTO_TEST_CASE(None) |
| 108 | { |
| 109 | Logging::setLevel("Module1", LogLevel::NONE); |
| 110 | logFromModule1(); |
| 111 | |
| 112 | Logging::flush(); |
| 113 | BOOST_CHECK(os.is_equal( |
| 114 | LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" |
| 115 | )); |
| 116 | } |
| 117 | |
| 118 | BOOST_AUTO_TEST_CASE(Error) |
| 119 | { |
| 120 | Logging::setLevel("Module1", LogLevel::ERROR); |
| 121 | logFromModule1(); |
| 122 | |
| 123 | Logging::flush(); |
| 124 | BOOST_CHECK(os.is_equal( |
| 125 | LOG_SYSTIME_STR + " ERROR: [Module1] error1\n" + |
| 126 | LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" |
| 127 | )); |
| 128 | } |
| 129 | |
| 130 | BOOST_AUTO_TEST_CASE(Warn) |
| 131 | { |
| 132 | Logging::setLevel("Module1", LogLevel::WARN); |
| 133 | logFromModule1(); |
| 134 | |
| 135 | Logging::flush(); |
| 136 | BOOST_CHECK(os.is_equal( |
| 137 | LOG_SYSTIME_STR + " WARNING: [Module1] warn1\n" + |
| 138 | LOG_SYSTIME_STR + " ERROR: [Module1] error1\n" + |
| 139 | LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" |
| 140 | )); |
| 141 | } |
| 142 | |
| 143 | BOOST_AUTO_TEST_CASE(Info) |
| 144 | { |
| 145 | Logging::setLevel("Module1", LogLevel::INFO); |
| 146 | logFromModule1(); |
| 147 | |
| 148 | Logging::flush(); |
| 149 | BOOST_CHECK(os.is_equal( |
| 150 | LOG_SYSTIME_STR + " INFO: [Module1] info1\n" + |
| 151 | LOG_SYSTIME_STR + " WARNING: [Module1] warn1\n" + |
| 152 | LOG_SYSTIME_STR + " ERROR: [Module1] error1\n" + |
| 153 | LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" |
| 154 | )); |
| 155 | } |
| 156 | |
| 157 | BOOST_AUTO_TEST_CASE(Debug) |
| 158 | { |
| 159 | Logging::setLevel("Module1", LogLevel::DEBUG); |
| 160 | logFromModule1(); |
| 161 | |
| 162 | Logging::flush(); |
| 163 | BOOST_CHECK(os.is_equal( |
| 164 | LOG_SYSTIME_STR + " DEBUG: [Module1] debug1\n" + |
| 165 | LOG_SYSTIME_STR + " INFO: [Module1] info1\n" + |
| 166 | LOG_SYSTIME_STR + " WARNING: [Module1] warn1\n" + |
| 167 | LOG_SYSTIME_STR + " ERROR: [Module1] error1\n" + |
| 168 | LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" |
| 169 | )); |
| 170 | } |
| 171 | |
| 172 | BOOST_AUTO_TEST_CASE(Trace) |
| 173 | { |
| 174 | Logging::setLevel("Module1", LogLevel::TRACE); |
| 175 | logFromModule1(); |
| 176 | |
| 177 | Logging::flush(); |
| 178 | BOOST_CHECK(os.is_equal( |
| 179 | LOG_SYSTIME_STR + " TRACE: [Module1] trace1\n" + |
| 180 | LOG_SYSTIME_STR + " DEBUG: [Module1] debug1\n" + |
| 181 | LOG_SYSTIME_STR + " INFO: [Module1] info1\n" + |
| 182 | LOG_SYSTIME_STR + " WARNING: [Module1] warn1\n" + |
| 183 | LOG_SYSTIME_STR + " ERROR: [Module1] error1\n" + |
| 184 | LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" |
| 185 | )); |
| 186 | } |
| 187 | |
| 188 | BOOST_AUTO_TEST_CASE(All) |
| 189 | { |
| 190 | Logging::setLevel("Module1", LogLevel::ALL); |
| 191 | logFromModule1(); |
| 192 | |
| 193 | Logging::flush(); |
| 194 | BOOST_CHECK(os.is_equal( |
| 195 | LOG_SYSTIME_STR + " TRACE: [Module1] trace1\n" + |
| 196 | LOG_SYSTIME_STR + " DEBUG: [Module1] debug1\n" + |
| 197 | LOG_SYSTIME_STR + " INFO: [Module1] info1\n" + |
| 198 | LOG_SYSTIME_STR + " WARNING: [Module1] warn1\n" + |
| 199 | LOG_SYSTIME_STR + " ERROR: [Module1] error1\n" + |
| 200 | LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" |
| 201 | )); |
| 202 | } |
| 203 | |
| 204 | BOOST_AUTO_TEST_SUITE_END() // Severity |
| 205 | |
| 206 | BOOST_AUTO_TEST_CASE(SameNameLoggers) |
| 207 | { |
| 208 | Logging::setLevel("Module1", LogLevel::WARN); |
| 209 | logFromModule1(); |
| 210 | logFromNewLogger("Module1"); |
| 211 | |
| 212 | Logging::flush(); |
| 213 | BOOST_CHECK(os.is_equal( |
| 214 | LOG_SYSTIME_STR + " WARNING: [Module1] warn1\n" + |
| 215 | LOG_SYSTIME_STR + " ERROR: [Module1] error1\n" + |
| 216 | LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" + |
| 217 | LOG_SYSTIME_STR + " WARNING: [Module1] warnModule1\n" + |
| 218 | LOG_SYSTIME_STR + " ERROR: [Module1] errorModule1\n" + |
| 219 | LOG_SYSTIME_STR + " FATAL: [Module1] fatalModule1\n" |
| 220 | )); |
| 221 | } |
| 222 | |
| 223 | BOOST_AUTO_TEST_CASE(LateRegistration) |
| 224 | { |
| 225 | BOOST_CHECK_NO_THROW(Logging::setLevel("Module3", LogLevel::DEBUG)); |
| 226 | logFromNewLogger("Module3"); |
| 227 | |
| 228 | Logging::flush(); |
| 229 | BOOST_CHECK(os.is_equal( |
| 230 | LOG_SYSTIME_STR + " DEBUG: [Module3] debugModule3\n" + |
| 231 | LOG_SYSTIME_STR + " INFO: [Module3] infoModule3\n" + |
| 232 | LOG_SYSTIME_STR + " WARNING: [Module3] warnModule3\n" + |
| 233 | LOG_SYSTIME_STR + " ERROR: [Module3] errorModule3\n" + |
| 234 | LOG_SYSTIME_STR + " FATAL: [Module3] fatalModule3\n" |
| 235 | )); |
| 236 | } |
| 237 | |
| 238 | BOOST_AUTO_TEST_SUITE(DefaultSeverity) |
| 239 | |
| 240 | BOOST_AUTO_TEST_CASE(Unset) |
| 241 | { |
| 242 | logFromModule1(); |
| 243 | logFromModule2(); |
| 244 | |
| 245 | Logging::flush(); |
| 246 | BOOST_CHECK(os.is_equal( |
| 247 | LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" + |
| 248 | LOG_SYSTIME_STR + " FATAL: [Module2] fatal2\n" |
| 249 | )); |
| 250 | } |
| 251 | |
| 252 | BOOST_AUTO_TEST_CASE(NoOverride) |
| 253 | { |
| 254 | Logging::setLevel("*", LogLevel::WARN); |
| 255 | logFromModule1(); |
| 256 | logFromModule2(); |
| 257 | |
| 258 | Logging::flush(); |
| 259 | BOOST_CHECK(os.is_equal( |
| 260 | LOG_SYSTIME_STR + " WARNING: [Module1] warn1\n" + |
| 261 | LOG_SYSTIME_STR + " ERROR: [Module1] error1\n" + |
| 262 | LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" + |
| 263 | LOG_SYSTIME_STR + " WARNING: [Module2] warn2\n" + |
| 264 | LOG_SYSTIME_STR + " ERROR: [Module2] error2\n" + |
| 265 | LOG_SYSTIME_STR + " FATAL: [Module2] fatal2\n" |
| 266 | )); |
| 267 | } |
| 268 | |
| 269 | BOOST_AUTO_TEST_CASE(Override) |
| 270 | { |
| 271 | Logging::setLevel("*", LogLevel::WARN); |
| 272 | Logging::setLevel("Module2", LogLevel::DEBUG); |
| 273 | logFromModule1(); |
| 274 | logFromModule2(); |
| 275 | |
| 276 | Logging::flush(); |
| 277 | BOOST_CHECK(os.is_equal( |
| 278 | LOG_SYSTIME_STR + " WARNING: [Module1] warn1\n" + |
| 279 | LOG_SYSTIME_STR + " ERROR: [Module1] error1\n" + |
| 280 | LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" + |
| 281 | LOG_SYSTIME_STR + " DEBUG: [Module2] debug2\n" + |
| 282 | LOG_SYSTIME_STR + " INFO: [Module2] info2\n" + |
| 283 | LOG_SYSTIME_STR + " WARNING: [Module2] warn2\n" + |
| 284 | LOG_SYSTIME_STR + " ERROR: [Module2] error2\n" + |
| 285 | LOG_SYSTIME_STR + " FATAL: [Module2] fatal2\n" |
| 286 | )); |
| 287 | } |
| 288 | |
| 289 | BOOST_AUTO_TEST_SUITE_END() // DefaultSeverity |
| 290 | |
| 291 | BOOST_AUTO_TEST_SUITE(SeverityConfig) |
| 292 | |
| 293 | BOOST_AUTO_TEST_CASE(SetEmpty) |
| 294 | { |
| 295 | Logging::setLevel(""); |
dmcoomes | e062a18 | 2017-06-12 11:10:31 -0500 | [diff] [blame] | 296 | const auto& prefixMap = Logging::get().getLevels(); |
| 297 | BOOST_CHECK_EQUAL(prefixMap.size(), 0); |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 298 | logFromModule1(); |
| 299 | logFromModule2(); |
| 300 | |
| 301 | Logging::flush(); |
| 302 | BOOST_CHECK(os.is_equal( |
| 303 | LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" + |
| 304 | LOG_SYSTIME_STR + " FATAL: [Module2] fatal2\n" |
| 305 | )); |
| 306 | } |
| 307 | |
| 308 | BOOST_AUTO_TEST_CASE(SetDefault) |
| 309 | { |
| 310 | Logging::setLevel("*=WARN"); |
dmcoomes | e062a18 | 2017-06-12 11:10:31 -0500 | [diff] [blame] | 311 | const auto& prefixMap = Logging::get().getLevels(); |
| 312 | // "*" is treated as "" internally |
| 313 | BOOST_CHECK_EQUAL(prefixMap.size(), 1); |
| 314 | BOOST_CHECK_EQUAL(prefixMap.at(""), LogLevel::WARN); |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 315 | logFromModule1(); |
| 316 | logFromModule2(); |
| 317 | |
| 318 | Logging::flush(); |
| 319 | BOOST_CHECK(os.is_equal( |
| 320 | LOG_SYSTIME_STR + " WARNING: [Module1] warn1\n" + |
| 321 | LOG_SYSTIME_STR + " ERROR: [Module1] error1\n" + |
| 322 | LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" + |
| 323 | LOG_SYSTIME_STR + " WARNING: [Module2] warn2\n" + |
| 324 | LOG_SYSTIME_STR + " ERROR: [Module2] error2\n" + |
| 325 | LOG_SYSTIME_STR + " FATAL: [Module2] fatal2\n" |
| 326 | )); |
| 327 | } |
| 328 | |
| 329 | BOOST_AUTO_TEST_CASE(SetModule) |
| 330 | { |
| 331 | Logging::setLevel("Module1=ERROR"); |
dmcoomes | e062a18 | 2017-06-12 11:10:31 -0500 | [diff] [blame] | 332 | const auto& prefixMap = Logging::get().getLevels(); |
| 333 | BOOST_CHECK_EQUAL(prefixMap.size(), 1); |
| 334 | BOOST_CHECK_EQUAL(prefixMap.at("Module1"), LogLevel::ERROR); |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 335 | logFromModule1(); |
| 336 | logFromModule2(); |
| 337 | |
| 338 | Logging::flush(); |
| 339 | BOOST_CHECK(os.is_equal( |
| 340 | LOG_SYSTIME_STR + " ERROR: [Module1] error1\n" + |
| 341 | LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" + |
| 342 | LOG_SYSTIME_STR + " FATAL: [Module2] fatal2\n" |
| 343 | )); |
| 344 | } |
| 345 | |
| 346 | BOOST_AUTO_TEST_CASE(SetOverride) |
| 347 | { |
| 348 | Logging::setLevel("*=WARN:Module2=DEBUG"); |
dmcoomes | e062a18 | 2017-06-12 11:10:31 -0500 | [diff] [blame] | 349 | const auto& prefixMap = Logging::get().getLevels(); |
| 350 | BOOST_CHECK_EQUAL(prefixMap.size(), 2); |
| 351 | // "*" is treated as "" internally |
| 352 | BOOST_CHECK_EQUAL(prefixMap.at(""), LogLevel::WARN); |
| 353 | BOOST_CHECK_EQUAL(prefixMap.at("Module2"), LogLevel::DEBUG); |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 354 | logFromModule1(); |
| 355 | logFromModule2(); |
| 356 | |
| 357 | Logging::flush(); |
| 358 | BOOST_CHECK(os.is_equal( |
| 359 | LOG_SYSTIME_STR + " WARNING: [Module1] warn1\n" + |
| 360 | LOG_SYSTIME_STR + " ERROR: [Module1] error1\n" + |
| 361 | LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" + |
| 362 | LOG_SYSTIME_STR + " DEBUG: [Module2] debug2\n" + |
| 363 | LOG_SYSTIME_STR + " INFO: [Module2] info2\n" + |
| 364 | LOG_SYSTIME_STR + " WARNING: [Module2] warn2\n" + |
| 365 | LOG_SYSTIME_STR + " ERROR: [Module2] error2\n" + |
| 366 | LOG_SYSTIME_STR + " FATAL: [Module2] fatal2\n" |
| 367 | )); |
| 368 | } |
| 369 | |
| 370 | BOOST_AUTO_TEST_CASE(SetTwice) |
| 371 | { |
| 372 | Logging::setLevel("*=WARN"); |
| 373 | Logging::setLevel("Module2=DEBUG"); |
dmcoomes | e062a18 | 2017-06-12 11:10:31 -0500 | [diff] [blame] | 374 | const auto& prefixMap = Logging::get().getLevels(); |
| 375 | BOOST_CHECK_EQUAL(prefixMap.size(), 2); |
| 376 | // "*" is treated as "" internally |
| 377 | BOOST_CHECK_EQUAL(prefixMap.at(""), LogLevel::WARN); |
| 378 | BOOST_CHECK_EQUAL(prefixMap.at("Module2"), LogLevel::DEBUG); |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 379 | logFromModule1(); |
| 380 | logFromModule2(); |
| 381 | |
| 382 | Logging::flush(); |
| 383 | BOOST_CHECK(os.is_equal( |
| 384 | LOG_SYSTIME_STR + " WARNING: [Module1] warn1\n" + |
| 385 | LOG_SYSTIME_STR + " ERROR: [Module1] error1\n" + |
| 386 | LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" + |
| 387 | LOG_SYSTIME_STR + " DEBUG: [Module2] debug2\n" + |
| 388 | LOG_SYSTIME_STR + " INFO: [Module2] info2\n" + |
| 389 | LOG_SYSTIME_STR + " WARNING: [Module2] warn2\n" + |
| 390 | LOG_SYSTIME_STR + " ERROR: [Module2] error2\n" + |
| 391 | LOG_SYSTIME_STR + " FATAL: [Module2] fatal2\n" |
| 392 | )); |
| 393 | } |
| 394 | |
| 395 | BOOST_AUTO_TEST_CASE(Reset) |
| 396 | { |
| 397 | Logging::setLevel("Module2=DEBUG"); |
| 398 | Logging::setLevel("*=ERROR"); |
dmcoomes | e062a18 | 2017-06-12 11:10:31 -0500 | [diff] [blame] | 399 | const auto& prefixMap = Logging::get().getLevels(); |
| 400 | BOOST_CHECK_EQUAL(prefixMap.size(), 1); |
| 401 | // "*" is treated as "" internally |
| 402 | BOOST_CHECK_EQUAL(prefixMap.at(""), LogLevel::ERROR); |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 403 | logFromModule1(); |
| 404 | logFromModule2(); |
| 405 | |
| 406 | Logging::flush(); |
| 407 | BOOST_CHECK(os.is_equal( |
| 408 | LOG_SYSTIME_STR + " ERROR: [Module1] error1\n" + |
| 409 | LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" + |
| 410 | LOG_SYSTIME_STR + " ERROR: [Module2] error2\n" + |
| 411 | LOG_SYSTIME_STR + " FATAL: [Module2] fatal2\n" |
| 412 | )); |
| 413 | } |
| 414 | |
| 415 | BOOST_AUTO_TEST_CASE(Malformed) |
| 416 | { |
| 417 | BOOST_CHECK_THROW(Logging::setLevel("Module1=INVALID-LEVEL"), std::invalid_argument); |
| 418 | BOOST_CHECK_THROW(Logging::setLevel("Module1-MISSING-EQUAL-SIGN"), std::invalid_argument); |
| 419 | } |
| 420 | |
dmcoomes | e062a18 | 2017-06-12 11:10:31 -0500 | [diff] [blame] | 421 | BOOST_AUTO_TEST_CASE(SetFilter) |
| 422 | { |
| 423 | Logging::setLevel("*=FATAL:fm.*=DEBUG"); |
| 424 | const auto& prefixMap = Logging::get().getLevels(); |
| 425 | BOOST_CHECK_EQUAL(prefixMap.size(), 2); |
| 426 | // "*" is treated as "" internally |
| 427 | BOOST_CHECK_EQUAL(prefixMap.at(""), LogLevel::FATAL); |
| 428 | // "name.*" is treated as "name." internally |
| 429 | BOOST_CHECK_EQUAL(prefixMap.at("fm."), LogLevel::DEBUG); |
| 430 | logFromModule1(); |
| 431 | logFromFilterModule(); |
| 432 | |
| 433 | Logging::flush(); |
| 434 | BOOST_CHECK(os.is_equal( |
| 435 | LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" + |
| 436 | LOG_SYSTIME_STR + " DEBUG: [fm.FilterModule] debugFM\n" + |
| 437 | LOG_SYSTIME_STR + " INFO: [fm.FilterModule] infoFM\n" + |
| 438 | LOG_SYSTIME_STR + " WARNING: [fm.FilterModule] warnFM\n" + |
| 439 | LOG_SYSTIME_STR + " ERROR: [fm.FilterModule] errorFM\n" + |
| 440 | LOG_SYSTIME_STR + " FATAL: [fm.FilterModule] fatalFM\n" |
| 441 | )); |
| 442 | } |
| 443 | |
| 444 | BOOST_AUTO_TEST_CASE(SetOverrideFilter) |
| 445 | { |
| 446 | Logging::setLevel("*=FATAL:fm.FilterModule=DEBUG"); |
| 447 | Logging::setLevel("fm.*", LogLevel::INFO); |
| 448 | const auto& prefixMap = Logging::get().getLevels(); |
| 449 | BOOST_CHECK_EQUAL(prefixMap.size(), 2); |
| 450 | // "*" is treated as "" internally |
| 451 | BOOST_CHECK_EQUAL(prefixMap.at(""), LogLevel::FATAL); |
| 452 | // "name.*" is treated as "name." internally |
| 453 | BOOST_CHECK_EQUAL(prefixMap.at("fm."), LogLevel::INFO); |
| 454 | logFromModule1(); |
| 455 | logFromFilterModule(); |
| 456 | |
| 457 | Logging::flush(); |
| 458 | BOOST_CHECK(os.is_equal( |
| 459 | LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" + |
| 460 | LOG_SYSTIME_STR + " INFO: [fm.FilterModule] infoFM\n" + |
| 461 | LOG_SYSTIME_STR + " WARNING: [fm.FilterModule] warnFM\n" + |
| 462 | LOG_SYSTIME_STR + " ERROR: [fm.FilterModule] errorFM\n" + |
| 463 | LOG_SYSTIME_STR + " FATAL: [fm.FilterModule] fatalFM\n" |
| 464 | )); |
| 465 | } |
| 466 | |
| 467 | BOOST_AUTO_TEST_CASE(FindPrefixRule) |
| 468 | { |
| 469 | Logging::setLevel("*=FATAL:fm.a.*=ERROR:fm.a.b=INFO"); |
| 470 | logFromNewLogger("fm.a.b"); |
| 471 | logFromNewLogger("fm.a.b.c"); |
| 472 | logFromNewLogger("fm.a.b.d"); |
| 473 | logFromNewLogger("fm.b"); |
| 474 | |
| 475 | Logging::flush(); |
| 476 | BOOST_CHECK(os.is_equal( |
| 477 | LOG_SYSTIME_STR + " INFO: [fm.a.b] infofm.a.b\n" + |
| 478 | LOG_SYSTIME_STR + " WARNING: [fm.a.b] warnfm.a.b\n" + |
| 479 | LOG_SYSTIME_STR + " ERROR: [fm.a.b] errorfm.a.b\n" + |
| 480 | LOG_SYSTIME_STR + " FATAL: [fm.a.b] fatalfm.a.b\n" + |
| 481 | LOG_SYSTIME_STR + " ERROR: [fm.a.b.c] errorfm.a.b.c\n" + |
| 482 | LOG_SYSTIME_STR + " FATAL: [fm.a.b.c] fatalfm.a.b.c\n" + |
| 483 | LOG_SYSTIME_STR + " ERROR: [fm.a.b.d] errorfm.a.b.d\n" + |
| 484 | LOG_SYSTIME_STR + " FATAL: [fm.a.b.d] fatalfm.a.b.d\n" + |
| 485 | LOG_SYSTIME_STR + " FATAL: [fm.b] fatalfm.b\n" |
| 486 | )); |
| 487 | } |
| 488 | |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 489 | BOOST_AUTO_TEST_SUITE_END() // SeverityConfig |
| 490 | |
| 491 | BOOST_AUTO_TEST_CASE(ChangeDestination) |
| 492 | { |
| 493 | logFromModule1(); |
| 494 | |
| 495 | auto os2 = make_shared<output_test_stream>(); |
| 496 | Logging::setDestination(os2); |
| 497 | weak_ptr<output_test_stream> os2weak(os2); |
| 498 | os2.reset(); |
| 499 | |
| 500 | logFromModule2(); |
| 501 | |
| 502 | Logging::flush(); |
| 503 | os2 = os2weak.lock(); |
| 504 | BOOST_REQUIRE(os2 != nullptr); |
| 505 | |
| 506 | BOOST_CHECK(os.is_equal( |
| 507 | LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" |
| 508 | )); |
| 509 | BOOST_CHECK(os2->is_equal( |
| 510 | LOG_SYSTIME_STR + " FATAL: [Module2] fatal2\n" |
| 511 | )); |
| 512 | |
| 513 | os2.reset(); |
| 514 | Logging::setDestination(os); |
| 515 | BOOST_CHECK(os2weak.expired()); |
| 516 | } |
| 517 | |
| 518 | BOOST_AUTO_TEST_SUITE_END() // TestLogging |
| 519 | BOOST_AUTO_TEST_SUITE_END() // Util |
| 520 | |
| 521 | } // namespace tests |
| 522 | } // namespace util |
| 523 | } // namespace ndn |