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