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