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" |
| 24 | |
Davide Pesavento | 0c145ec | 2018-04-10 21:33:57 -0400 | [diff] [blame] | 25 | #include "../unit-test-time-fixture.hpp" |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 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 | void |
| 35 | logFromModule1(); |
| 36 | |
| 37 | void |
| 38 | logFromModule2(); |
| 39 | |
| 40 | void |
dmcoomes | e062a18 | 2017-06-12 11:10:31 -0500 | [diff] [blame] | 41 | logFromFilterModule(); |
| 42 | |
| 43 | static void |
Davide Pesavento | 1c9c96c | 2018-04-25 10:45:08 -0400 | [diff] [blame] | 44 | logFromNewLogger(const char* moduleName) |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 45 | { |
Davide Pesavento | 0c145ec | 2018-04-10 21:33:57 -0400 | [diff] [blame] | 46 | Logger logger(moduleName); |
| 47 | auto ndn_cxx_getLogger = [&logger] () -> Logger& { return logger; }; |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 48 | |
Junxiao Shi | 1fe7ce5 | 2016-08-08 05:48:02 +0000 | [diff] [blame] | 49 | NDN_LOG_TRACE("trace" << moduleName); |
| 50 | NDN_LOG_DEBUG("debug" << moduleName); |
| 51 | NDN_LOG_INFO("info" << moduleName); |
| 52 | NDN_LOG_WARN("warn" << moduleName); |
| 53 | NDN_LOG_ERROR("error" << moduleName); |
| 54 | NDN_LOG_FATAL("fatal" << moduleName); |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 55 | |
| 56 | BOOST_CHECK(Logging::get().removeLogger(logger)); |
| 57 | } |
| 58 | |
Davide Pesavento | 0c145ec | 2018-04-10 21:33:57 -0400 | [diff] [blame] | 59 | namespace ns1 { |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 60 | |
Davide Pesavento | 0c145ec | 2018-04-10 21:33:57 -0400 | [diff] [blame] | 61 | NDN_LOG_INIT(ndn.util.tests.ns1); |
| 62 | |
| 63 | static void |
| 64 | logFromNamespace1() |
| 65 | { |
| 66 | NDN_LOG_INFO("hello world from ns1"); |
| 67 | } |
| 68 | |
| 69 | } // namespace ns1 |
| 70 | |
| 71 | namespace ns2 { |
| 72 | |
| 73 | NDN_LOG_INIT(ndn.util.tests.ns2); |
| 74 | |
| 75 | static void |
| 76 | logFromNamespace2() |
| 77 | { |
| 78 | NDN_LOG_INFO("hi there from ns2"); |
| 79 | } |
| 80 | |
| 81 | } // namespace ns2 |
| 82 | |
| 83 | class ClassWithLogger |
| 84 | { |
| 85 | public: |
| 86 | void |
| 87 | logFromConstMemberFunction() const |
| 88 | { |
| 89 | NDN_LOG_INFO("const member function"); |
| 90 | } |
| 91 | |
| 92 | static void |
| 93 | logFromStaticMemberFunction() |
| 94 | { |
| 95 | NDN_LOG_INFO("static member function"); |
| 96 | } |
| 97 | |
| 98 | private: |
Davide Pesavento | 1c9c96c | 2018-04-25 10:45:08 -0400 | [diff] [blame] | 99 | NDN_LOG_MEMBER_DECL(); |
Davide Pesavento | 0c145ec | 2018-04-10 21:33:57 -0400 | [diff] [blame] | 100 | }; |
| 101 | |
Davide Pesavento | 1c9c96c | 2018-04-25 10:45:08 -0400 | [diff] [blame] | 102 | NDN_LOG_MEMBER_INIT(ClassWithLogger, ndn.util.tests.ClassWithLogger); |
| 103 | |
Davide Pesavento | 0c145ec | 2018-04-10 21:33:57 -0400 | [diff] [blame] | 104 | template<class T, class U> |
| 105 | class ClassTemplateWithLogger |
| 106 | { |
| 107 | public: |
| 108 | void |
| 109 | logFromMemberFunction() |
| 110 | { |
| 111 | NDN_LOG_INFO("class template non-static member function"); |
| 112 | } |
| 113 | |
| 114 | static void |
| 115 | logFromStaticMemberFunction() |
| 116 | { |
| 117 | NDN_LOG_INFO("class template static member function"); |
| 118 | } |
| 119 | |
| 120 | private: |
| 121 | NDN_LOG_MEMBER_DECL(); |
| 122 | }; |
| 123 | |
| 124 | // Technically this declaration is not necessary in this case, |
| 125 | // but we want to test that the macro expands to well-formed code |
| 126 | NDN_LOG_MEMBER_DECL_SPECIALIZED((ClassTemplateWithLogger<int, double>)); |
| 127 | |
| 128 | NDN_LOG_MEMBER_INIT_SPECIALIZED((ClassTemplateWithLogger<int, double>), ndn.util.tests.Specialized1); |
| 129 | NDN_LOG_MEMBER_INIT_SPECIALIZED((ClassTemplateWithLogger<int, std::string>), ndn.util.tests.Specialized2); |
| 130 | |
| 131 | const time::microseconds LOG_SYSTIME(1468108800311239LL); |
| 132 | const std::string LOG_SYSTIME_STR("1468108800.311239"); |
| 133 | |
| 134 | class LoggingFixture : public ndn::tests::UnitTestTimeFixture |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 135 | { |
| 136 | protected: |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 137 | LoggingFixture() |
dmcoomes | e062a18 | 2017-06-12 11:10:31 -0500 | [diff] [blame] | 138 | : m_oldEnabledLevel(Logging::get().getLevels()) |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 139 | , m_oldDestination(Logging::get().getDestination()) |
| 140 | { |
| 141 | this->systemClock->setNow(LOG_SYSTIME); |
| 142 | Logging::get().resetLevels(); |
| 143 | Logging::setDestination(os); |
| 144 | } |
| 145 | |
| 146 | ~LoggingFixture() |
| 147 | { |
dmcoomes | e062a18 | 2017-06-12 11:10:31 -0500 | [diff] [blame] | 148 | Logging::get().setLevelImpl(m_oldEnabledLevel); |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 149 | Logging::setDestination(m_oldDestination); |
| 150 | } |
| 151 | |
| 152 | protected: |
Davide Pesavento | 1c9c96c | 2018-04-25 10:45:08 -0400 | [diff] [blame] | 153 | boost::test_tools::output_test_stream os; |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 154 | |
| 155 | private: |
dmcoomes | e062a18 | 2017-06-12 11:10:31 -0500 | [diff] [blame] | 156 | std::unordered_map<std::string, LogLevel> m_oldEnabledLevel; |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 157 | shared_ptr<std::ostream> m_oldDestination; |
| 158 | }; |
| 159 | |
| 160 | BOOST_AUTO_TEST_SUITE(Util) |
| 161 | BOOST_FIXTURE_TEST_SUITE(TestLogging, LoggingFixture) |
| 162 | |
Alexander Afanasyev | 354f382 | 2017-03-27 15:26:41 -0500 | [diff] [blame] | 163 | BOOST_AUTO_TEST_CASE(GetLoggerNames) |
| 164 | { |
Davide Pesavento | 1c9c96c | 2018-04-25 10:45:08 -0400 | [diff] [blame] | 165 | // check that all names are registered from the start even if |
| 166 | // logger instances are lazily created on first use |
| 167 | auto n = Logging::getLoggerNames().size(); |
| 168 | NDN_LOG_TRACE("GetLoggerNames"); |
Davide Pesavento | 0c145ec | 2018-04-10 21:33:57 -0400 | [diff] [blame] | 169 | auto names = Logging::getLoggerNames(); |
Davide Pesavento | 1c9c96c | 2018-04-25 10:45:08 -0400 | [diff] [blame] | 170 | BOOST_CHECK_EQUAL(names.size(), n); |
Alexander Afanasyev | 354f382 | 2017-03-27 15:26:41 -0500 | [diff] [blame] | 171 | BOOST_CHECK_EQUAL(names.count("ndn.util.tests.Logging"), 1); |
| 172 | } |
| 173 | |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 174 | BOOST_AUTO_TEST_SUITE(Severity) |
| 175 | |
| 176 | BOOST_AUTO_TEST_CASE(None) |
| 177 | { |
| 178 | Logging::setLevel("Module1", LogLevel::NONE); |
| 179 | logFromModule1(); |
| 180 | |
| 181 | Logging::flush(); |
| 182 | BOOST_CHECK(os.is_equal( |
| 183 | LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" |
| 184 | )); |
| 185 | } |
| 186 | |
| 187 | BOOST_AUTO_TEST_CASE(Error) |
| 188 | { |
| 189 | Logging::setLevel("Module1", LogLevel::ERROR); |
| 190 | logFromModule1(); |
| 191 | |
| 192 | Logging::flush(); |
| 193 | BOOST_CHECK(os.is_equal( |
| 194 | LOG_SYSTIME_STR + " ERROR: [Module1] error1\n" + |
| 195 | LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" |
| 196 | )); |
| 197 | } |
| 198 | |
| 199 | BOOST_AUTO_TEST_CASE(Warn) |
| 200 | { |
| 201 | Logging::setLevel("Module1", LogLevel::WARN); |
| 202 | logFromModule1(); |
| 203 | |
| 204 | Logging::flush(); |
| 205 | BOOST_CHECK(os.is_equal( |
| 206 | LOG_SYSTIME_STR + " WARNING: [Module1] warn1\n" + |
| 207 | LOG_SYSTIME_STR + " ERROR: [Module1] error1\n" + |
| 208 | LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" |
| 209 | )); |
| 210 | } |
| 211 | |
| 212 | BOOST_AUTO_TEST_CASE(Info) |
| 213 | { |
| 214 | Logging::setLevel("Module1", LogLevel::INFO); |
| 215 | logFromModule1(); |
| 216 | |
| 217 | Logging::flush(); |
| 218 | BOOST_CHECK(os.is_equal( |
| 219 | LOG_SYSTIME_STR + " INFO: [Module1] info1\n" + |
| 220 | LOG_SYSTIME_STR + " WARNING: [Module1] warn1\n" + |
| 221 | LOG_SYSTIME_STR + " ERROR: [Module1] error1\n" + |
| 222 | LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" |
| 223 | )); |
| 224 | } |
| 225 | |
| 226 | BOOST_AUTO_TEST_CASE(Debug) |
| 227 | { |
| 228 | Logging::setLevel("Module1", LogLevel::DEBUG); |
| 229 | logFromModule1(); |
| 230 | |
| 231 | Logging::flush(); |
| 232 | BOOST_CHECK(os.is_equal( |
| 233 | LOG_SYSTIME_STR + " DEBUG: [Module1] debug1\n" + |
| 234 | LOG_SYSTIME_STR + " INFO: [Module1] info1\n" + |
| 235 | LOG_SYSTIME_STR + " WARNING: [Module1] warn1\n" + |
| 236 | LOG_SYSTIME_STR + " ERROR: [Module1] error1\n" + |
| 237 | LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" |
| 238 | )); |
| 239 | } |
| 240 | |
| 241 | BOOST_AUTO_TEST_CASE(Trace) |
| 242 | { |
| 243 | Logging::setLevel("Module1", LogLevel::TRACE); |
| 244 | logFromModule1(); |
| 245 | |
| 246 | Logging::flush(); |
| 247 | BOOST_CHECK(os.is_equal( |
| 248 | LOG_SYSTIME_STR + " TRACE: [Module1] trace1\n" + |
| 249 | LOG_SYSTIME_STR + " DEBUG: [Module1] debug1\n" + |
| 250 | LOG_SYSTIME_STR + " INFO: [Module1] info1\n" + |
| 251 | LOG_SYSTIME_STR + " WARNING: [Module1] warn1\n" + |
| 252 | LOG_SYSTIME_STR + " ERROR: [Module1] error1\n" + |
| 253 | LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" |
| 254 | )); |
| 255 | } |
| 256 | |
| 257 | BOOST_AUTO_TEST_CASE(All) |
| 258 | { |
| 259 | Logging::setLevel("Module1", LogLevel::ALL); |
| 260 | logFromModule1(); |
| 261 | |
| 262 | Logging::flush(); |
| 263 | BOOST_CHECK(os.is_equal( |
| 264 | LOG_SYSTIME_STR + " TRACE: [Module1] trace1\n" + |
| 265 | LOG_SYSTIME_STR + " DEBUG: [Module1] debug1\n" + |
| 266 | LOG_SYSTIME_STR + " INFO: [Module1] info1\n" + |
| 267 | LOG_SYSTIME_STR + " WARNING: [Module1] warn1\n" + |
| 268 | LOG_SYSTIME_STR + " ERROR: [Module1] error1\n" + |
| 269 | LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" |
| 270 | )); |
| 271 | } |
| 272 | |
| 273 | BOOST_AUTO_TEST_SUITE_END() // Severity |
| 274 | |
Davide Pesavento | 0c145ec | 2018-04-10 21:33:57 -0400 | [diff] [blame] | 275 | BOOST_AUTO_TEST_CASE(NamespaceLogger) |
| 276 | { |
| 277 | Logging::setLevel("ndn.util.tests.ns1", LogLevel::INFO); |
| 278 | Logging::setLevel("ndn.util.tests.ns2", LogLevel::DEBUG); |
| 279 | |
| 280 | const auto& levels = Logging::get().getLevels(); |
| 281 | BOOST_CHECK_EQUAL(levels.size(), 2); |
| 282 | BOOST_CHECK_EQUAL(levels.at("ndn.util.tests.ns1"), LogLevel::INFO); |
| 283 | BOOST_CHECK_EQUAL(levels.at("ndn.util.tests.ns2"), LogLevel::DEBUG); |
| 284 | |
Davide Pesavento | 1c9c96c | 2018-04-25 10:45:08 -0400 | [diff] [blame] | 285 | const auto& names = Logging::getLoggerNames(); |
| 286 | BOOST_CHECK_EQUAL(names.count("ndn.util.tests.ns1"), 1); |
| 287 | BOOST_CHECK_EQUAL(names.count("ndn.util.tests.ns2"), 1); |
| 288 | |
Davide Pesavento | 0c145ec | 2018-04-10 21:33:57 -0400 | [diff] [blame] | 289 | ns1::logFromNamespace1(); |
| 290 | ns2::logFromNamespace2(); |
| 291 | |
| 292 | Logging::flush(); |
| 293 | BOOST_CHECK(os.is_equal( |
| 294 | LOG_SYSTIME_STR + " INFO: [ndn.util.tests.ns1] hello world from ns1\n" + |
| 295 | LOG_SYSTIME_STR + " INFO: [ndn.util.tests.ns2] hi there from ns2\n" |
| 296 | )); |
| 297 | } |
| 298 | |
| 299 | BOOST_AUTO_TEST_CASE(MemberLogger) |
| 300 | { |
| 301 | Logging::setLevel("ndn.util.tests.ClassWithLogger", LogLevel::INFO); |
| 302 | Logging::setLevel("ndn.util.tests.Specialized1", LogLevel::INFO); |
| 303 | // ndn.util.tests.Specialized2 is not enabled |
| 304 | |
Davide Pesavento | 1c9c96c | 2018-04-25 10:45:08 -0400 | [diff] [blame] | 305 | const auto& names = Logging::getLoggerNames(); |
| 306 | BOOST_CHECK_EQUAL(names.count("ndn.util.tests.ClassWithLogger"), 1); |
| 307 | BOOST_CHECK_EQUAL(names.count("ndn.util.tests.Specialized1"), 1); |
| 308 | BOOST_CHECK_EQUAL(names.count("ndn.util.tests.Specialized2"), 1); |
| 309 | |
Davide Pesavento | 0c145ec | 2018-04-10 21:33:57 -0400 | [diff] [blame] | 310 | ClassWithLogger::logFromStaticMemberFunction(); |
| 311 | ClassWithLogger{}.logFromConstMemberFunction(); |
| 312 | |
| 313 | Logging::flush(); |
| 314 | BOOST_CHECK(os.is_equal( |
| 315 | LOG_SYSTIME_STR + " INFO: [ndn.util.tests.ClassWithLogger] static member function\n" + |
| 316 | LOG_SYSTIME_STR + " INFO: [ndn.util.tests.ClassWithLogger] const member function\n" |
| 317 | )); |
| 318 | |
| 319 | ClassTemplateWithLogger<int, double>::logFromStaticMemberFunction(); |
| 320 | ClassTemplateWithLogger<int, double>{}.logFromMemberFunction(); |
| 321 | ClassTemplateWithLogger<int, std::string>::logFromStaticMemberFunction(); |
| 322 | ClassTemplateWithLogger<int, std::string>{}.logFromMemberFunction(); |
| 323 | |
| 324 | Logging::flush(); |
| 325 | BOOST_CHECK(os.is_equal( |
| 326 | LOG_SYSTIME_STR + " INFO: [ndn.util.tests.Specialized1] class template static member function\n" + |
| 327 | LOG_SYSTIME_STR + " INFO: [ndn.util.tests.Specialized1] class template non-static member function\n" |
| 328 | )); |
| 329 | } |
| 330 | |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 331 | BOOST_AUTO_TEST_CASE(SameNameLoggers) |
| 332 | { |
| 333 | Logging::setLevel("Module1", LogLevel::WARN); |
| 334 | logFromModule1(); |
| 335 | logFromNewLogger("Module1"); |
| 336 | |
Davide Pesavento | 1c9c96c | 2018-04-25 10:45:08 -0400 | [diff] [blame] | 337 | BOOST_CHECK_EQUAL(Logging::getLoggerNames().count("Module1"), 1); |
| 338 | |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 339 | Logging::flush(); |
| 340 | BOOST_CHECK(os.is_equal( |
| 341 | LOG_SYSTIME_STR + " WARNING: [Module1] warn1\n" + |
| 342 | LOG_SYSTIME_STR + " ERROR: [Module1] error1\n" + |
| 343 | LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" + |
| 344 | LOG_SYSTIME_STR + " WARNING: [Module1] warnModule1\n" + |
| 345 | LOG_SYSTIME_STR + " ERROR: [Module1] errorModule1\n" + |
| 346 | LOG_SYSTIME_STR + " FATAL: [Module1] fatalModule1\n" |
| 347 | )); |
| 348 | } |
| 349 | |
| 350 | BOOST_AUTO_TEST_CASE(LateRegistration) |
| 351 | { |
Davide Pesavento | 0c145ec | 2018-04-10 21:33:57 -0400 | [diff] [blame] | 352 | Logging::setLevel("Module3", LogLevel::DEBUG); |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 353 | logFromNewLogger("Module3"); |
| 354 | |
| 355 | Logging::flush(); |
| 356 | BOOST_CHECK(os.is_equal( |
| 357 | LOG_SYSTIME_STR + " DEBUG: [Module3] debugModule3\n" + |
| 358 | LOG_SYSTIME_STR + " INFO: [Module3] infoModule3\n" + |
| 359 | LOG_SYSTIME_STR + " WARNING: [Module3] warnModule3\n" + |
| 360 | LOG_SYSTIME_STR + " ERROR: [Module3] errorModule3\n" + |
| 361 | LOG_SYSTIME_STR + " FATAL: [Module3] fatalModule3\n" |
| 362 | )); |
| 363 | } |
| 364 | |
| 365 | BOOST_AUTO_TEST_SUITE(DefaultSeverity) |
| 366 | |
| 367 | BOOST_AUTO_TEST_CASE(Unset) |
| 368 | { |
| 369 | logFromModule1(); |
| 370 | logFromModule2(); |
| 371 | |
| 372 | Logging::flush(); |
| 373 | BOOST_CHECK(os.is_equal( |
| 374 | LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" + |
| 375 | LOG_SYSTIME_STR + " FATAL: [Module2] fatal2\n" |
| 376 | )); |
| 377 | } |
| 378 | |
| 379 | BOOST_AUTO_TEST_CASE(NoOverride) |
| 380 | { |
| 381 | Logging::setLevel("*", LogLevel::WARN); |
| 382 | logFromModule1(); |
| 383 | logFromModule2(); |
| 384 | |
| 385 | Logging::flush(); |
| 386 | BOOST_CHECK(os.is_equal( |
| 387 | LOG_SYSTIME_STR + " WARNING: [Module1] warn1\n" + |
| 388 | LOG_SYSTIME_STR + " ERROR: [Module1] error1\n" + |
| 389 | LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" + |
| 390 | LOG_SYSTIME_STR + " WARNING: [Module2] warn2\n" + |
| 391 | LOG_SYSTIME_STR + " ERROR: [Module2] error2\n" + |
| 392 | LOG_SYSTIME_STR + " FATAL: [Module2] fatal2\n" |
| 393 | )); |
| 394 | } |
| 395 | |
| 396 | BOOST_AUTO_TEST_CASE(Override) |
| 397 | { |
| 398 | Logging::setLevel("*", LogLevel::WARN); |
| 399 | Logging::setLevel("Module2", LogLevel::DEBUG); |
| 400 | logFromModule1(); |
| 401 | logFromModule2(); |
| 402 | |
| 403 | Logging::flush(); |
| 404 | BOOST_CHECK(os.is_equal( |
| 405 | LOG_SYSTIME_STR + " WARNING: [Module1] warn1\n" + |
| 406 | LOG_SYSTIME_STR + " ERROR: [Module1] error1\n" + |
| 407 | LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" + |
| 408 | LOG_SYSTIME_STR + " DEBUG: [Module2] debug2\n" + |
| 409 | LOG_SYSTIME_STR + " INFO: [Module2] info2\n" + |
| 410 | LOG_SYSTIME_STR + " WARNING: [Module2] warn2\n" + |
| 411 | LOG_SYSTIME_STR + " ERROR: [Module2] error2\n" + |
| 412 | LOG_SYSTIME_STR + " FATAL: [Module2] fatal2\n" |
| 413 | )); |
| 414 | } |
| 415 | |
| 416 | BOOST_AUTO_TEST_SUITE_END() // DefaultSeverity |
| 417 | |
| 418 | BOOST_AUTO_TEST_SUITE(SeverityConfig) |
| 419 | |
| 420 | BOOST_AUTO_TEST_CASE(SetEmpty) |
| 421 | { |
| 422 | Logging::setLevel(""); |
dmcoomes | e062a18 | 2017-06-12 11:10:31 -0500 | [diff] [blame] | 423 | const auto& prefixMap = Logging::get().getLevels(); |
| 424 | BOOST_CHECK_EQUAL(prefixMap.size(), 0); |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 425 | logFromModule1(); |
| 426 | logFromModule2(); |
| 427 | |
| 428 | Logging::flush(); |
| 429 | BOOST_CHECK(os.is_equal( |
| 430 | LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" + |
| 431 | LOG_SYSTIME_STR + " FATAL: [Module2] fatal2\n" |
| 432 | )); |
| 433 | } |
| 434 | |
| 435 | BOOST_AUTO_TEST_CASE(SetDefault) |
| 436 | { |
| 437 | Logging::setLevel("*=WARN"); |
dmcoomes | e062a18 | 2017-06-12 11:10:31 -0500 | [diff] [blame] | 438 | const auto& prefixMap = Logging::get().getLevels(); |
| 439 | // "*" is treated as "" internally |
| 440 | BOOST_CHECK_EQUAL(prefixMap.size(), 1); |
| 441 | BOOST_CHECK_EQUAL(prefixMap.at(""), LogLevel::WARN); |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 442 | logFromModule1(); |
| 443 | logFromModule2(); |
| 444 | |
| 445 | Logging::flush(); |
| 446 | BOOST_CHECK(os.is_equal( |
| 447 | LOG_SYSTIME_STR + " WARNING: [Module1] warn1\n" + |
| 448 | LOG_SYSTIME_STR + " ERROR: [Module1] error1\n" + |
| 449 | LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" + |
| 450 | LOG_SYSTIME_STR + " WARNING: [Module2] warn2\n" + |
| 451 | LOG_SYSTIME_STR + " ERROR: [Module2] error2\n" + |
| 452 | LOG_SYSTIME_STR + " FATAL: [Module2] fatal2\n" |
| 453 | )); |
| 454 | } |
| 455 | |
| 456 | BOOST_AUTO_TEST_CASE(SetModule) |
| 457 | { |
| 458 | Logging::setLevel("Module1=ERROR"); |
dmcoomes | e062a18 | 2017-06-12 11:10:31 -0500 | [diff] [blame] | 459 | const auto& prefixMap = Logging::get().getLevels(); |
| 460 | BOOST_CHECK_EQUAL(prefixMap.size(), 1); |
| 461 | BOOST_CHECK_EQUAL(prefixMap.at("Module1"), LogLevel::ERROR); |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 462 | logFromModule1(); |
| 463 | logFromModule2(); |
| 464 | |
| 465 | Logging::flush(); |
| 466 | BOOST_CHECK(os.is_equal( |
| 467 | LOG_SYSTIME_STR + " ERROR: [Module1] error1\n" + |
| 468 | LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" + |
| 469 | LOG_SYSTIME_STR + " FATAL: [Module2] fatal2\n" |
| 470 | )); |
| 471 | } |
| 472 | |
| 473 | BOOST_AUTO_TEST_CASE(SetOverride) |
| 474 | { |
| 475 | Logging::setLevel("*=WARN:Module2=DEBUG"); |
dmcoomes | e062a18 | 2017-06-12 11:10:31 -0500 | [diff] [blame] | 476 | const auto& prefixMap = Logging::get().getLevels(); |
| 477 | BOOST_CHECK_EQUAL(prefixMap.size(), 2); |
| 478 | // "*" is treated as "" internally |
| 479 | BOOST_CHECK_EQUAL(prefixMap.at(""), LogLevel::WARN); |
| 480 | BOOST_CHECK_EQUAL(prefixMap.at("Module2"), LogLevel::DEBUG); |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 481 | logFromModule1(); |
| 482 | logFromModule2(); |
| 483 | |
| 484 | Logging::flush(); |
| 485 | BOOST_CHECK(os.is_equal( |
| 486 | LOG_SYSTIME_STR + " WARNING: [Module1] warn1\n" + |
| 487 | LOG_SYSTIME_STR + " ERROR: [Module1] error1\n" + |
| 488 | LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" + |
| 489 | LOG_SYSTIME_STR + " DEBUG: [Module2] debug2\n" + |
| 490 | LOG_SYSTIME_STR + " INFO: [Module2] info2\n" + |
| 491 | LOG_SYSTIME_STR + " WARNING: [Module2] warn2\n" + |
| 492 | LOG_SYSTIME_STR + " ERROR: [Module2] error2\n" + |
| 493 | LOG_SYSTIME_STR + " FATAL: [Module2] fatal2\n" |
| 494 | )); |
| 495 | } |
| 496 | |
| 497 | BOOST_AUTO_TEST_CASE(SetTwice) |
| 498 | { |
| 499 | Logging::setLevel("*=WARN"); |
| 500 | Logging::setLevel("Module2=DEBUG"); |
dmcoomes | e062a18 | 2017-06-12 11:10:31 -0500 | [diff] [blame] | 501 | const auto& prefixMap = Logging::get().getLevels(); |
| 502 | BOOST_CHECK_EQUAL(prefixMap.size(), 2); |
| 503 | // "*" is treated as "" internally |
| 504 | BOOST_CHECK_EQUAL(prefixMap.at(""), LogLevel::WARN); |
| 505 | BOOST_CHECK_EQUAL(prefixMap.at("Module2"), LogLevel::DEBUG); |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 506 | logFromModule1(); |
| 507 | logFromModule2(); |
| 508 | |
| 509 | Logging::flush(); |
| 510 | BOOST_CHECK(os.is_equal( |
| 511 | LOG_SYSTIME_STR + " WARNING: [Module1] warn1\n" + |
| 512 | LOG_SYSTIME_STR + " ERROR: [Module1] error1\n" + |
| 513 | LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" + |
| 514 | LOG_SYSTIME_STR + " DEBUG: [Module2] debug2\n" + |
| 515 | LOG_SYSTIME_STR + " INFO: [Module2] info2\n" + |
| 516 | LOG_SYSTIME_STR + " WARNING: [Module2] warn2\n" + |
| 517 | LOG_SYSTIME_STR + " ERROR: [Module2] error2\n" + |
| 518 | LOG_SYSTIME_STR + " FATAL: [Module2] fatal2\n" |
| 519 | )); |
| 520 | } |
| 521 | |
| 522 | BOOST_AUTO_TEST_CASE(Reset) |
| 523 | { |
| 524 | Logging::setLevel("Module2=DEBUG"); |
| 525 | Logging::setLevel("*=ERROR"); |
dmcoomes | e062a18 | 2017-06-12 11:10:31 -0500 | [diff] [blame] | 526 | const auto& prefixMap = Logging::get().getLevels(); |
| 527 | BOOST_CHECK_EQUAL(prefixMap.size(), 1); |
| 528 | // "*" is treated as "" internally |
| 529 | BOOST_CHECK_EQUAL(prefixMap.at(""), LogLevel::ERROR); |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 530 | logFromModule1(); |
| 531 | logFromModule2(); |
| 532 | |
| 533 | Logging::flush(); |
| 534 | BOOST_CHECK(os.is_equal( |
| 535 | LOG_SYSTIME_STR + " ERROR: [Module1] error1\n" + |
| 536 | LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" + |
| 537 | LOG_SYSTIME_STR + " ERROR: [Module2] error2\n" + |
| 538 | LOG_SYSTIME_STR + " FATAL: [Module2] fatal2\n" |
| 539 | )); |
| 540 | } |
| 541 | |
| 542 | BOOST_AUTO_TEST_CASE(Malformed) |
| 543 | { |
| 544 | BOOST_CHECK_THROW(Logging::setLevel("Module1=INVALID-LEVEL"), std::invalid_argument); |
| 545 | BOOST_CHECK_THROW(Logging::setLevel("Module1-MISSING-EQUAL-SIGN"), std::invalid_argument); |
| 546 | } |
| 547 | |
dmcoomes | e062a18 | 2017-06-12 11:10:31 -0500 | [diff] [blame] | 548 | BOOST_AUTO_TEST_CASE(SetFilter) |
| 549 | { |
| 550 | Logging::setLevel("*=FATAL:fm.*=DEBUG"); |
| 551 | const auto& prefixMap = Logging::get().getLevels(); |
| 552 | BOOST_CHECK_EQUAL(prefixMap.size(), 2); |
| 553 | // "*" is treated as "" internally |
| 554 | BOOST_CHECK_EQUAL(prefixMap.at(""), LogLevel::FATAL); |
| 555 | // "name.*" is treated as "name." internally |
| 556 | BOOST_CHECK_EQUAL(prefixMap.at("fm."), LogLevel::DEBUG); |
| 557 | logFromModule1(); |
| 558 | logFromFilterModule(); |
| 559 | |
| 560 | Logging::flush(); |
| 561 | BOOST_CHECK(os.is_equal( |
| 562 | LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" + |
| 563 | LOG_SYSTIME_STR + " DEBUG: [fm.FilterModule] debugFM\n" + |
| 564 | LOG_SYSTIME_STR + " INFO: [fm.FilterModule] infoFM\n" + |
| 565 | LOG_SYSTIME_STR + " WARNING: [fm.FilterModule] warnFM\n" + |
| 566 | LOG_SYSTIME_STR + " ERROR: [fm.FilterModule] errorFM\n" + |
| 567 | LOG_SYSTIME_STR + " FATAL: [fm.FilterModule] fatalFM\n" |
| 568 | )); |
| 569 | } |
| 570 | |
| 571 | BOOST_AUTO_TEST_CASE(SetOverrideFilter) |
| 572 | { |
| 573 | Logging::setLevel("*=FATAL:fm.FilterModule=DEBUG"); |
| 574 | Logging::setLevel("fm.*", LogLevel::INFO); |
| 575 | const auto& prefixMap = Logging::get().getLevels(); |
| 576 | BOOST_CHECK_EQUAL(prefixMap.size(), 2); |
| 577 | // "*" is treated as "" internally |
| 578 | BOOST_CHECK_EQUAL(prefixMap.at(""), LogLevel::FATAL); |
| 579 | // "name.*" is treated as "name." internally |
| 580 | BOOST_CHECK_EQUAL(prefixMap.at("fm."), LogLevel::INFO); |
| 581 | logFromModule1(); |
| 582 | logFromFilterModule(); |
| 583 | |
| 584 | Logging::flush(); |
| 585 | BOOST_CHECK(os.is_equal( |
| 586 | LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" + |
| 587 | LOG_SYSTIME_STR + " INFO: [fm.FilterModule] infoFM\n" + |
| 588 | LOG_SYSTIME_STR + " WARNING: [fm.FilterModule] warnFM\n" + |
| 589 | LOG_SYSTIME_STR + " ERROR: [fm.FilterModule] errorFM\n" + |
| 590 | LOG_SYSTIME_STR + " FATAL: [fm.FilterModule] fatalFM\n" |
| 591 | )); |
| 592 | } |
| 593 | |
| 594 | BOOST_AUTO_TEST_CASE(FindPrefixRule) |
| 595 | { |
| 596 | Logging::setLevel("*=FATAL:fm.a.*=ERROR:fm.a.b=INFO"); |
| 597 | logFromNewLogger("fm.a.b"); |
| 598 | logFromNewLogger("fm.a.b.c"); |
| 599 | logFromNewLogger("fm.a.b.d"); |
| 600 | logFromNewLogger("fm.b"); |
| 601 | |
| 602 | Logging::flush(); |
| 603 | BOOST_CHECK(os.is_equal( |
| 604 | LOG_SYSTIME_STR + " INFO: [fm.a.b] infofm.a.b\n" + |
| 605 | LOG_SYSTIME_STR + " WARNING: [fm.a.b] warnfm.a.b\n" + |
| 606 | LOG_SYSTIME_STR + " ERROR: [fm.a.b] errorfm.a.b\n" + |
| 607 | LOG_SYSTIME_STR + " FATAL: [fm.a.b] fatalfm.a.b\n" + |
| 608 | LOG_SYSTIME_STR + " ERROR: [fm.a.b.c] errorfm.a.b.c\n" + |
| 609 | LOG_SYSTIME_STR + " FATAL: [fm.a.b.c] fatalfm.a.b.c\n" + |
| 610 | LOG_SYSTIME_STR + " ERROR: [fm.a.b.d] errorfm.a.b.d\n" + |
| 611 | LOG_SYSTIME_STR + " FATAL: [fm.a.b.d] fatalfm.a.b.d\n" + |
| 612 | LOG_SYSTIME_STR + " FATAL: [fm.b] fatalfm.b\n" |
| 613 | )); |
| 614 | } |
| 615 | |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 616 | BOOST_AUTO_TEST_SUITE_END() // SeverityConfig |
| 617 | |
| 618 | BOOST_AUTO_TEST_CASE(ChangeDestination) |
| 619 | { |
Davide Pesavento | 1c9c96c | 2018-04-25 10:45:08 -0400 | [diff] [blame] | 620 | using boost::test_tools::output_test_stream; |
| 621 | |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 622 | logFromModule1(); |
| 623 | |
| 624 | auto os2 = make_shared<output_test_stream>(); |
| 625 | Logging::setDestination(os2); |
| 626 | weak_ptr<output_test_stream> os2weak(os2); |
| 627 | os2.reset(); |
| 628 | |
| 629 | logFromModule2(); |
| 630 | |
| 631 | Logging::flush(); |
| 632 | os2 = os2weak.lock(); |
| 633 | BOOST_REQUIRE(os2 != nullptr); |
| 634 | |
| 635 | BOOST_CHECK(os.is_equal( |
| 636 | LOG_SYSTIME_STR + " FATAL: [Module1] fatal1\n" |
| 637 | )); |
| 638 | BOOST_CHECK(os2->is_equal( |
| 639 | LOG_SYSTIME_STR + " FATAL: [Module2] fatal2\n" |
| 640 | )); |
| 641 | |
| 642 | os2.reset(); |
| 643 | Logging::setDestination(os); |
| 644 | BOOST_CHECK(os2weak.expired()); |
| 645 | } |
| 646 | |
| 647 | BOOST_AUTO_TEST_SUITE_END() // TestLogging |
| 648 | BOOST_AUTO_TEST_SUITE_END() // Util |
| 649 | |
| 650 | } // namespace tests |
| 651 | } // namespace util |
| 652 | } // namespace ndn |