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