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