Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame^] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2013-2016 Regents of the University of California. |
| 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 | */ |
| 21 | #include "util/logging.hpp" |
| 22 | #include "util/logger.hpp" |
| 23 | |
| 24 | #include "boost-test.hpp" |
| 25 | #include <boost/test/output_test_stream.hpp> |
| 26 | #include "../unit-test-time-fixture.hpp" |
| 27 | |
| 28 | namespace ndn { |
| 29 | namespace util { |
| 30 | namespace tests { |
| 31 | |
| 32 | using namespace ndn::tests; |
| 33 | using boost::test_tools::output_test_stream; |
| 34 | |
| 35 | void |
| 36 | logFromModule1(); |
| 37 | |
| 38 | void |
| 39 | logFromModule2(); |
| 40 | |
| 41 | void |
| 42 | logFromNewLogger(const std::string& moduleName) |
| 43 | { |
| 44 | // clang complains -Wreturn-stack-address on OSX-10.9 if Logger is allocated on stack |
| 45 | auto loggerPtr = make_unique<Logger>(moduleName); |
| 46 | Logger& logger = *loggerPtr; |
| 47 | |
| 48 | auto getNdnCxxLogger = [&logger] () -> Logger& { return logger; }; |
| 49 | NDN_CXX_LOG_TRACE("trace" << moduleName); |
| 50 | NDN_CXX_LOG_DEBUG("debug" << moduleName); |
| 51 | NDN_CXX_LOG_INFO("info" << moduleName); |
| 52 | NDN_CXX_LOG_WARN("warn" << moduleName); |
| 53 | NDN_CXX_LOG_ERROR("error" << moduleName); |
| 54 | NDN_CXX_LOG_FATAL("fatal" << moduleName); |
| 55 | |
| 56 | BOOST_CHECK(Logging::get().removeLogger(logger)); |
| 57 | } |
| 58 | |
| 59 | const time::system_clock::Duration LOG_SYSTIME = time::microseconds(1468108800311239LL); |
| 60 | const std::string LOG_SYSTIME_STR = "1468108800.311239"; |
| 61 | |
| 62 | class LoggingFixture : public UnitTestTimeFixture |
| 63 | { |
| 64 | protected: |
| 65 | explicit |
| 66 | LoggingFixture() |
| 67 | : m_oldLevels(Logging::get().getLevels()) |
| 68 | , m_oldDestination(Logging::get().getDestination()) |
| 69 | { |
| 70 | this->systemClock->setNow(LOG_SYSTIME); |
| 71 | Logging::get().resetLevels(); |
| 72 | Logging::setDestination(os); |
| 73 | } |
| 74 | |
| 75 | ~LoggingFixture() |
| 76 | { |
| 77 | Logging::setLevel(m_oldLevels); |
| 78 | Logging::setDestination(m_oldDestination); |
| 79 | } |
| 80 | |
| 81 | protected: |
| 82 | output_test_stream os; |
| 83 | |
| 84 | private: |
| 85 | std::string m_oldLevels; |
| 86 | shared_ptr<std::ostream> m_oldDestination; |
| 87 | }; |
| 88 | |
| 89 | BOOST_AUTO_TEST_SUITE(Util) |
| 90 | BOOST_FIXTURE_TEST_SUITE(TestLogging, LoggingFixture) |
| 91 | |
| 92 | BOOST_AUTO_TEST_SUITE(Severity) |
| 93 | |
| 94 | BOOST_AUTO_TEST_CASE(None) |
| 95 | { |
| 96 | Logging::setLevel("Module1", LogLevel::NONE); |
| 97 | logFromModule1(); |
| 98 | |
| 99 | Logging::flush(); |
| 100 | BOOST_CHECK(os.is_equal( |
| 101 | LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" |
| 102 | )); |
| 103 | } |
| 104 | |
| 105 | BOOST_AUTO_TEST_CASE(Error) |
| 106 | { |
| 107 | Logging::setLevel("Module1", LogLevel::ERROR); |
| 108 | logFromModule1(); |
| 109 | |
| 110 | Logging::flush(); |
| 111 | BOOST_CHECK(os.is_equal( |
| 112 | LOG_SYSTIME_STR + " ERROR: [Module1] error1\n" + |
| 113 | LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" |
| 114 | )); |
| 115 | } |
| 116 | |
| 117 | BOOST_AUTO_TEST_CASE(Warn) |
| 118 | { |
| 119 | Logging::setLevel("Module1", LogLevel::WARN); |
| 120 | logFromModule1(); |
| 121 | |
| 122 | Logging::flush(); |
| 123 | BOOST_CHECK(os.is_equal( |
| 124 | LOG_SYSTIME_STR + " WARNING: [Module1] warn1\n" + |
| 125 | LOG_SYSTIME_STR + " ERROR: [Module1] error1\n" + |
| 126 | LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" |
| 127 | )); |
| 128 | } |
| 129 | |
| 130 | BOOST_AUTO_TEST_CASE(Info) |
| 131 | { |
| 132 | Logging::setLevel("Module1", LogLevel::INFO); |
| 133 | logFromModule1(); |
| 134 | |
| 135 | Logging::flush(); |
| 136 | BOOST_CHECK(os.is_equal( |
| 137 | LOG_SYSTIME_STR + " INFO: [Module1] info1\n" + |
| 138 | LOG_SYSTIME_STR + " WARNING: [Module1] warn1\n" + |
| 139 | LOG_SYSTIME_STR + " ERROR: [Module1] error1\n" + |
| 140 | LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" |
| 141 | )); |
| 142 | } |
| 143 | |
| 144 | BOOST_AUTO_TEST_CASE(Debug) |
| 145 | { |
| 146 | Logging::setLevel("Module1", LogLevel::DEBUG); |
| 147 | logFromModule1(); |
| 148 | |
| 149 | Logging::flush(); |
| 150 | BOOST_CHECK(os.is_equal( |
| 151 | LOG_SYSTIME_STR + " DEBUG: [Module1] debug1\n" + |
| 152 | LOG_SYSTIME_STR + " INFO: [Module1] info1\n" + |
| 153 | LOG_SYSTIME_STR + " WARNING: [Module1] warn1\n" + |
| 154 | LOG_SYSTIME_STR + " ERROR: [Module1] error1\n" + |
| 155 | LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" |
| 156 | )); |
| 157 | } |
| 158 | |
| 159 | BOOST_AUTO_TEST_CASE(Trace) |
| 160 | { |
| 161 | Logging::setLevel("Module1", LogLevel::TRACE); |
| 162 | logFromModule1(); |
| 163 | |
| 164 | Logging::flush(); |
| 165 | BOOST_CHECK(os.is_equal( |
| 166 | LOG_SYSTIME_STR + " TRACE: [Module1] trace1\n" + |
| 167 | LOG_SYSTIME_STR + " DEBUG: [Module1] debug1\n" + |
| 168 | LOG_SYSTIME_STR + " INFO: [Module1] info1\n" + |
| 169 | LOG_SYSTIME_STR + " WARNING: [Module1] warn1\n" + |
| 170 | LOG_SYSTIME_STR + " ERROR: [Module1] error1\n" + |
| 171 | LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" |
| 172 | )); |
| 173 | } |
| 174 | |
| 175 | BOOST_AUTO_TEST_CASE(All) |
| 176 | { |
| 177 | Logging::setLevel("Module1", LogLevel::ALL); |
| 178 | logFromModule1(); |
| 179 | |
| 180 | Logging::flush(); |
| 181 | BOOST_CHECK(os.is_equal( |
| 182 | LOG_SYSTIME_STR + " TRACE: [Module1] trace1\n" + |
| 183 | LOG_SYSTIME_STR + " DEBUG: [Module1] debug1\n" + |
| 184 | LOG_SYSTIME_STR + " INFO: [Module1] info1\n" + |
| 185 | LOG_SYSTIME_STR + " WARNING: [Module1] warn1\n" + |
| 186 | LOG_SYSTIME_STR + " ERROR: [Module1] error1\n" + |
| 187 | LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" |
| 188 | )); |
| 189 | } |
| 190 | |
| 191 | BOOST_AUTO_TEST_SUITE_END() // Severity |
| 192 | |
| 193 | BOOST_AUTO_TEST_CASE(SameNameLoggers) |
| 194 | { |
| 195 | Logging::setLevel("Module1", LogLevel::WARN); |
| 196 | logFromModule1(); |
| 197 | logFromNewLogger("Module1"); |
| 198 | |
| 199 | Logging::flush(); |
| 200 | BOOST_CHECK(os.is_equal( |
| 201 | LOG_SYSTIME_STR + " WARNING: [Module1] warn1\n" + |
| 202 | LOG_SYSTIME_STR + " ERROR: [Module1] error1\n" + |
| 203 | LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" + |
| 204 | LOG_SYSTIME_STR + " WARNING: [Module1] warnModule1\n" + |
| 205 | LOG_SYSTIME_STR + " ERROR: [Module1] errorModule1\n" + |
| 206 | LOG_SYSTIME_STR + " FATAL: [Module1] fatalModule1\n" |
| 207 | )); |
| 208 | } |
| 209 | |
| 210 | BOOST_AUTO_TEST_CASE(LateRegistration) |
| 211 | { |
| 212 | BOOST_CHECK_NO_THROW(Logging::setLevel("Module3", LogLevel::DEBUG)); |
| 213 | logFromNewLogger("Module3"); |
| 214 | |
| 215 | Logging::flush(); |
| 216 | BOOST_CHECK(os.is_equal( |
| 217 | LOG_SYSTIME_STR + " DEBUG: [Module3] debugModule3\n" + |
| 218 | LOG_SYSTIME_STR + " INFO: [Module3] infoModule3\n" + |
| 219 | LOG_SYSTIME_STR + " WARNING: [Module3] warnModule3\n" + |
| 220 | LOG_SYSTIME_STR + " ERROR: [Module3] errorModule3\n" + |
| 221 | LOG_SYSTIME_STR + " FATAL: [Module3] fatalModule3\n" |
| 222 | )); |
| 223 | } |
| 224 | |
| 225 | BOOST_AUTO_TEST_SUITE(DefaultSeverity) |
| 226 | |
| 227 | BOOST_AUTO_TEST_CASE(Unset) |
| 228 | { |
| 229 | logFromModule1(); |
| 230 | logFromModule2(); |
| 231 | |
| 232 | Logging::flush(); |
| 233 | BOOST_CHECK(os.is_equal( |
| 234 | LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" + |
| 235 | LOG_SYSTIME_STR + " FATAL: [Module2] fatal2\n" |
| 236 | )); |
| 237 | } |
| 238 | |
| 239 | BOOST_AUTO_TEST_CASE(NoOverride) |
| 240 | { |
| 241 | Logging::setLevel("*", LogLevel::WARN); |
| 242 | logFromModule1(); |
| 243 | logFromModule2(); |
| 244 | |
| 245 | Logging::flush(); |
| 246 | BOOST_CHECK(os.is_equal( |
| 247 | LOG_SYSTIME_STR + " WARNING: [Module1] warn1\n" + |
| 248 | LOG_SYSTIME_STR + " ERROR: [Module1] error1\n" + |
| 249 | LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" + |
| 250 | LOG_SYSTIME_STR + " WARNING: [Module2] warn2\n" + |
| 251 | LOG_SYSTIME_STR + " ERROR: [Module2] error2\n" + |
| 252 | LOG_SYSTIME_STR + " FATAL: [Module2] fatal2\n" |
| 253 | )); |
| 254 | } |
| 255 | |
| 256 | BOOST_AUTO_TEST_CASE(Override) |
| 257 | { |
| 258 | Logging::setLevel("*", LogLevel::WARN); |
| 259 | Logging::setLevel("Module2", LogLevel::DEBUG); |
| 260 | logFromModule1(); |
| 261 | logFromModule2(); |
| 262 | |
| 263 | Logging::flush(); |
| 264 | BOOST_CHECK(os.is_equal( |
| 265 | LOG_SYSTIME_STR + " WARNING: [Module1] warn1\n" + |
| 266 | LOG_SYSTIME_STR + " ERROR: [Module1] error1\n" + |
| 267 | LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" + |
| 268 | LOG_SYSTIME_STR + " DEBUG: [Module2] debug2\n" + |
| 269 | LOG_SYSTIME_STR + " INFO: [Module2] info2\n" + |
| 270 | LOG_SYSTIME_STR + " WARNING: [Module2] warn2\n" + |
| 271 | LOG_SYSTIME_STR + " ERROR: [Module2] error2\n" + |
| 272 | LOG_SYSTIME_STR + " FATAL: [Module2] fatal2\n" |
| 273 | )); |
| 274 | } |
| 275 | |
| 276 | BOOST_AUTO_TEST_SUITE_END() // DefaultSeverity |
| 277 | |
| 278 | BOOST_AUTO_TEST_SUITE(SeverityConfig) |
| 279 | |
| 280 | BOOST_AUTO_TEST_CASE(SetEmpty) |
| 281 | { |
| 282 | Logging::setLevel(""); |
| 283 | BOOST_CHECK_EQUAL(Logging::get().getLevels(), ""); |
| 284 | logFromModule1(); |
| 285 | logFromModule2(); |
| 286 | |
| 287 | Logging::flush(); |
| 288 | BOOST_CHECK(os.is_equal( |
| 289 | LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" + |
| 290 | LOG_SYSTIME_STR + " FATAL: [Module2] fatal2\n" |
| 291 | )); |
| 292 | } |
| 293 | |
| 294 | BOOST_AUTO_TEST_CASE(SetDefault) |
| 295 | { |
| 296 | Logging::setLevel("*=WARN"); |
| 297 | BOOST_CHECK_EQUAL(Logging::get().getLevels(), "*=WARN"); |
| 298 | logFromModule1(); |
| 299 | logFromModule2(); |
| 300 | |
| 301 | Logging::flush(); |
| 302 | BOOST_CHECK(os.is_equal( |
| 303 | LOG_SYSTIME_STR + " WARNING: [Module1] warn1\n" + |
| 304 | LOG_SYSTIME_STR + " ERROR: [Module1] error1\n" + |
| 305 | LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" + |
| 306 | LOG_SYSTIME_STR + " WARNING: [Module2] warn2\n" + |
| 307 | LOG_SYSTIME_STR + " ERROR: [Module2] error2\n" + |
| 308 | LOG_SYSTIME_STR + " FATAL: [Module2] fatal2\n" |
| 309 | )); |
| 310 | } |
| 311 | |
| 312 | BOOST_AUTO_TEST_CASE(SetModule) |
| 313 | { |
| 314 | Logging::setLevel("Module1=ERROR"); |
| 315 | BOOST_CHECK_EQUAL(Logging::get().getLevels(), "Module1=ERROR"); |
| 316 | logFromModule1(); |
| 317 | logFromModule2(); |
| 318 | |
| 319 | Logging::flush(); |
| 320 | BOOST_CHECK(os.is_equal( |
| 321 | LOG_SYSTIME_STR + " ERROR: [Module1] error1\n" + |
| 322 | LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" + |
| 323 | LOG_SYSTIME_STR + " FATAL: [Module2] fatal2\n" |
| 324 | )); |
| 325 | } |
| 326 | |
| 327 | BOOST_AUTO_TEST_CASE(SetOverride) |
| 328 | { |
| 329 | Logging::setLevel("*=WARN:Module2=DEBUG"); |
| 330 | BOOST_CHECK_EQUAL(Logging::get().getLevels(), "*=WARN:Module2=DEBUG"); |
| 331 | logFromModule1(); |
| 332 | logFromModule2(); |
| 333 | |
| 334 | Logging::flush(); |
| 335 | BOOST_CHECK(os.is_equal( |
| 336 | LOG_SYSTIME_STR + " WARNING: [Module1] warn1\n" + |
| 337 | LOG_SYSTIME_STR + " ERROR: [Module1] error1\n" + |
| 338 | LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" + |
| 339 | LOG_SYSTIME_STR + " DEBUG: [Module2] debug2\n" + |
| 340 | LOG_SYSTIME_STR + " INFO: [Module2] info2\n" + |
| 341 | LOG_SYSTIME_STR + " WARNING: [Module2] warn2\n" + |
| 342 | LOG_SYSTIME_STR + " ERROR: [Module2] error2\n" + |
| 343 | LOG_SYSTIME_STR + " FATAL: [Module2] fatal2\n" |
| 344 | )); |
| 345 | } |
| 346 | |
| 347 | BOOST_AUTO_TEST_CASE(SetTwice) |
| 348 | { |
| 349 | Logging::setLevel("*=WARN"); |
| 350 | Logging::setLevel("Module2=DEBUG"); |
| 351 | BOOST_CHECK_EQUAL(Logging::get().getLevels(), "*=WARN:Module2=DEBUG"); |
| 352 | logFromModule1(); |
| 353 | logFromModule2(); |
| 354 | |
| 355 | Logging::flush(); |
| 356 | BOOST_CHECK(os.is_equal( |
| 357 | LOG_SYSTIME_STR + " WARNING: [Module1] warn1\n" + |
| 358 | LOG_SYSTIME_STR + " ERROR: [Module1] error1\n" + |
| 359 | LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" + |
| 360 | LOG_SYSTIME_STR + " DEBUG: [Module2] debug2\n" + |
| 361 | LOG_SYSTIME_STR + " INFO: [Module2] info2\n" + |
| 362 | LOG_SYSTIME_STR + " WARNING: [Module2] warn2\n" + |
| 363 | LOG_SYSTIME_STR + " ERROR: [Module2] error2\n" + |
| 364 | LOG_SYSTIME_STR + " FATAL: [Module2] fatal2\n" |
| 365 | )); |
| 366 | } |
| 367 | |
| 368 | BOOST_AUTO_TEST_CASE(Reset) |
| 369 | { |
| 370 | Logging::setLevel("Module2=DEBUG"); |
| 371 | Logging::setLevel("*=ERROR"); |
| 372 | BOOST_CHECK_EQUAL(Logging::get().getLevels(), "*=ERROR"); |
| 373 | logFromModule1(); |
| 374 | logFromModule2(); |
| 375 | |
| 376 | Logging::flush(); |
| 377 | BOOST_CHECK(os.is_equal( |
| 378 | LOG_SYSTIME_STR + " ERROR: [Module1] error1\n" + |
| 379 | LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" + |
| 380 | LOG_SYSTIME_STR + " ERROR: [Module2] error2\n" + |
| 381 | LOG_SYSTIME_STR + " FATAL: [Module2] fatal2\n" |
| 382 | )); |
| 383 | } |
| 384 | |
| 385 | BOOST_AUTO_TEST_CASE(Malformed) |
| 386 | { |
| 387 | BOOST_CHECK_THROW(Logging::setLevel("Module1=INVALID-LEVEL"), std::invalid_argument); |
| 388 | BOOST_CHECK_THROW(Logging::setLevel("Module1-MISSING-EQUAL-SIGN"), std::invalid_argument); |
| 389 | } |
| 390 | |
| 391 | BOOST_AUTO_TEST_SUITE_END() // SeverityConfig |
| 392 | |
| 393 | BOOST_AUTO_TEST_CASE(ChangeDestination) |
| 394 | { |
| 395 | logFromModule1(); |
| 396 | |
| 397 | auto os2 = make_shared<output_test_stream>(); |
| 398 | Logging::setDestination(os2); |
| 399 | weak_ptr<output_test_stream> os2weak(os2); |
| 400 | os2.reset(); |
| 401 | |
| 402 | logFromModule2(); |
| 403 | |
| 404 | Logging::flush(); |
| 405 | os2 = os2weak.lock(); |
| 406 | BOOST_REQUIRE(os2 != nullptr); |
| 407 | |
| 408 | BOOST_CHECK(os.is_equal( |
| 409 | LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" |
| 410 | )); |
| 411 | BOOST_CHECK(os2->is_equal( |
| 412 | LOG_SYSTIME_STR + " FATAL: [Module2] fatal2\n" |
| 413 | )); |
| 414 | |
| 415 | os2.reset(); |
| 416 | Logging::setDestination(os); |
| 417 | BOOST_CHECK(os2weak.expired()); |
| 418 | } |
| 419 | |
| 420 | BOOST_AUTO_TEST_SUITE_END() // TestLogging |
| 421 | BOOST_AUTO_TEST_SUITE_END() // Util |
| 422 | |
| 423 | } // namespace tests |
| 424 | } // namespace util |
| 425 | } // namespace ndn |