Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (C) 2014 Named Data Networking Project |
| 4 | * See COPYING for copyright and distribution information. |
| 5 | * |
| 6 | * Author: Ilya Moiseenko <iliamo@ucla.edu> |
| 7 | */ |
| 8 | |
| 9 | #include "core/logger.hpp" |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 10 | |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 11 | #include <boost/test/unit_test.hpp> |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 12 | |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 13 | #include <iostream> |
| 14 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 15 | #include "tests/test-common.hpp" |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 16 | |
| 17 | namespace nfd { |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 18 | namespace tests { |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 19 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 20 | BOOST_FIXTURE_TEST_SUITE(CoreLogger, BaseFixture) |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 21 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 22 | class LoggerFixture : protected BaseFixture |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 23 | { |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 24 | public: |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 25 | LoggerFixture() |
| 26 | : m_savedBuf(std::cerr.rdbuf()) |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 27 | , m_savedLevel(LoggerFactory::getInstance().getDefaultLevel()) |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 28 | { |
| 29 | std::cerr.rdbuf(m_buffer.rdbuf()); |
| 30 | } |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 31 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 32 | ~LoggerFixture() |
| 33 | { |
| 34 | std::cerr.rdbuf(m_savedBuf); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 35 | LoggerFactory::getInstance().setDefaultLevel(m_savedLevel); |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 36 | } |
Alexander Afanasyev | b84cc92 | 2014-02-24 17:52:58 -0800 | [diff] [blame] | 37 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 38 | std::stringstream m_buffer; |
| 39 | std::streambuf* m_savedBuf; |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 40 | LogLevel m_savedLevel; |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 41 | }; |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 42 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 43 | BOOST_FIXTURE_TEST_CASE(Basic, LoggerFixture) |
| 44 | { |
| 45 | NFD_LOG_INIT("BasicTests"); |
Alexander Afanasyev | b84cc92 | 2014-02-24 17:52:58 -0800 | [diff] [blame] | 46 | g_logger.setLogLevel(LOG_ALL); |
| 47 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 48 | NFD_LOG_TRACE("trace message JHGFDSR^1"); |
| 49 | NFD_LOG_DEBUG("debug message IGg2474fdksd fo " << 15 << 16 << 17); |
| 50 | NFD_LOG_WARN("warning message XXXhdhd11" << 1 << "x"); |
Alexander Afanasyev | b84cc92 | 2014-02-24 17:52:58 -0800 | [diff] [blame] | 51 | NFD_LOG_INFO("info message Jjxjshj13"); |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 52 | NFD_LOG_ERROR("error message !#$&^%$#@"); |
| 53 | NFD_LOG_FATAL("fatal message JJSjaamcng"); |
Alexander Afanasyev | b84cc92 | 2014-02-24 17:52:58 -0800 | [diff] [blame] | 54 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 55 | BOOST_CHECK_EQUAL(m_buffer.str(), |
| 56 | "TRACE: [BasicTests] trace message JHGFDSR^1\n" |
| 57 | "DEBUG: [BasicTests] debug message IGg2474fdksd fo 151617\n" |
| 58 | "WARNING: [BasicTests] warning message XXXhdhd111x\n" |
| 59 | "INFO: [BasicTests] info message Jjxjshj13\n" |
| 60 | "ERROR: [BasicTests] error message !#$&^%$#@\n" |
| 61 | "FATAL: [BasicTests] fatal message JJSjaamcng\n" |
| 62 | ); |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 63 | } |
| 64 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 65 | BOOST_FIXTURE_TEST_CASE(ConfigureFactory, LoggerFixture) |
| 66 | { |
| 67 | NFD_LOG_INIT("ConfigureFactoryTests"); |
| 68 | |
| 69 | const std::string LOG_CONFIG = |
| 70 | "log\n" |
| 71 | "{\n" |
| 72 | " default_level INFO\n" |
| 73 | "}\n"; |
| 74 | |
| 75 | ConfigFile config; |
| 76 | LoggerFactory::getInstance().setConfigFile(config); |
| 77 | |
| 78 | config.parse(LOG_CONFIG, false, "LOG_CONFIG"); |
| 79 | |
| 80 | NFD_LOG_TRACE("trace message JHGFDSR^1"); |
| 81 | NFD_LOG_DEBUG("debug message IGg2474fdksd fo " << 15 << 16 << 17); |
| 82 | NFD_LOG_WARN("warning message XXXhdhd11" << 1 << "x"); |
| 83 | NFD_LOG_INFO("info message Jjxjshj13"); |
| 84 | NFD_LOG_ERROR("error message !#$&^%$#@"); |
| 85 | NFD_LOG_FATAL("fatal message JJSjaamcng"); |
| 86 | |
| 87 | BOOST_CHECK_EQUAL(m_buffer.str(), |
| 88 | "WARNING: [ConfigureFactoryTests] warning message XXXhdhd111x\n" |
| 89 | "INFO: [ConfigureFactoryTests] info message Jjxjshj13\n" |
| 90 | "ERROR: [ConfigureFactoryTests] error message !#$&^%$#@\n" |
| 91 | "FATAL: [ConfigureFactoryTests] fatal message JJSjaamcng\n" |
| 92 | ); |
| 93 | } |
| 94 | |
| 95 | BOOST_FIXTURE_TEST_CASE(TestNumberLevel, LoggerFixture) |
| 96 | { |
| 97 | NFD_LOG_INIT("TestNumberLevel"); |
| 98 | |
| 99 | const std::string LOG_CONFIG = |
| 100 | "log\n" |
| 101 | "{\n" |
| 102 | " default_level 2\n" // equivalent of WARN |
| 103 | "}\n"; |
| 104 | |
| 105 | ConfigFile config; |
| 106 | LoggerFactory::getInstance().setConfigFile(config); |
| 107 | |
| 108 | config.parse(LOG_CONFIG, false, "LOG_CONFIG"); |
| 109 | |
| 110 | NFD_LOG_TRACE("trace message JHGFDSR^1"); |
| 111 | NFD_LOG_DEBUG("debug message IGg2474fdksd fo " << 15 << 16 << 17); |
| 112 | NFD_LOG_WARN("warning message XXXhdhd11" << 1 << "x"); |
| 113 | NFD_LOG_INFO("info message Jjxjshj13"); |
| 114 | NFD_LOG_ERROR("error message !#$&^%$#@"); |
| 115 | NFD_LOG_FATAL("fatal message JJSjaamcng"); |
| 116 | |
| 117 | BOOST_CHECK_EQUAL(m_buffer.str(), |
| 118 | "WARNING: [TestNumberLevel] warning message XXXhdhd111x\n" |
| 119 | "ERROR: [TestNumberLevel] error message !#$&^%$#@\n" |
| 120 | "FATAL: [TestNumberLevel] fatal message JJSjaamcng\n" |
| 121 | ); |
| 122 | } |
| 123 | |
| 124 | static void |
| 125 | testModuleBPrint() |
| 126 | { |
| 127 | NFD_LOG_INIT("TestModuleB"); |
| 128 | NFD_LOG_DEBUG("debug message IGg2474fdksd fo " << 15 << 16 << 17); |
| 129 | } |
| 130 | |
| 131 | BOOST_FIXTURE_TEST_CASE(LimitModules, LoggerFixture) |
| 132 | { |
| 133 | NFD_LOG_INIT("TestModuleA"); |
| 134 | |
| 135 | const std::string LOG_CONFIG = |
| 136 | "log\n" |
| 137 | "{\n" |
| 138 | " default_level WARN\n" |
| 139 | "}\n"; |
| 140 | |
| 141 | ConfigFile config; |
| 142 | LoggerFactory::getInstance().setConfigFile(config); |
| 143 | |
| 144 | config.parse(LOG_CONFIG, false, "LOG_CONFIG"); |
| 145 | |
| 146 | // this should print |
| 147 | NFD_LOG_WARN("warning message XXXhdhd11" << 1 << "x"); |
| 148 | |
| 149 | // this should not because it's level is < WARN |
| 150 | testModuleBPrint(); |
| 151 | |
| 152 | BOOST_CHECK_EQUAL(m_buffer.str(), |
| 153 | "WARNING: [TestModuleA] warning message XXXhdhd111x\n" |
| 154 | ); |
| 155 | } |
| 156 | |
| 157 | BOOST_FIXTURE_TEST_CASE(ExplicitlySetModule, LoggerFixture) |
| 158 | { |
| 159 | NFD_LOG_INIT("TestModuleA"); |
| 160 | |
| 161 | const std::string LOG_CONFIG = |
| 162 | "log\n" |
| 163 | "{\n" |
| 164 | " default_level WARN\n" |
| 165 | " TestModuleB DEBUG\n" |
| 166 | "}\n"; |
| 167 | |
| 168 | ConfigFile config; |
| 169 | LoggerFactory::getInstance().setConfigFile(config); |
| 170 | |
| 171 | config.parse(LOG_CONFIG, false, "LOG_CONFIG"); |
| 172 | |
| 173 | // this should print |
| 174 | NFD_LOG_WARN("warning message XXXhdhd11" << 1 << "x"); |
| 175 | |
| 176 | // this too because its level is explicitly set to DEBUG |
| 177 | testModuleBPrint(); |
| 178 | |
| 179 | BOOST_CHECK_EQUAL(m_buffer.str(), |
| 180 | "WARNING: [TestModuleA] warning message XXXhdhd111x\n" |
| 181 | "DEBUG: [TestModuleB] debug message IGg2474fdksd fo 151617\n" |
| 182 | ); |
| 183 | } |
| 184 | |
| 185 | static bool |
| 186 | checkError(const LoggerFactory::Error& error, const std::string& expected) |
| 187 | { |
| 188 | return error.what() == expected; |
| 189 | } |
| 190 | |
| 191 | BOOST_FIXTURE_TEST_CASE(UnknownLevelString, LoggerFixture) |
| 192 | { |
| 193 | const std::string LOG_CONFIG = |
| 194 | "log\n" |
| 195 | "{\n" |
| 196 | " default_level TestMadeUpLevel\n" |
| 197 | "}\n"; |
| 198 | |
| 199 | ConfigFile config; |
| 200 | LoggerFactory::getInstance().setConfigFile(config); |
| 201 | |
| 202 | BOOST_REQUIRE_EXCEPTION(config.parse(LOG_CONFIG, false, "LOG_CONFIG"), |
| 203 | LoggerFactory::Error, |
| 204 | bind(&checkError, |
| 205 | _1, |
| 206 | "Unsupported logging level \"TestMadeUpLevel\"")); |
| 207 | } |
| 208 | |
| 209 | BOOST_FIXTURE_TEST_CASE(UnknownOption, LoggerFixture) |
| 210 | { |
| 211 | const std::string LOG_CONFIG = |
| 212 | "log\n" |
| 213 | "{\n" |
| 214 | " TestMadeUpOption\n" |
| 215 | "}\n"; |
| 216 | |
| 217 | ConfigFile config; |
| 218 | LoggerFactory::getInstance().setConfigFile(config); |
| 219 | |
| 220 | BOOST_REQUIRE_EXCEPTION(config.parse(LOG_CONFIG, false, "LOG_CONFIG"), |
| 221 | LoggerFactory::Error, |
| 222 | bind(&checkError, |
| 223 | _1, |
| 224 | "No logging level found for option \"TestMadeUpOption\"")); |
| 225 | } |
| 226 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 227 | class InClassLogger : public LoggerFixture |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 228 | { |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 229 | public: |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 230 | |
Alexander Afanasyev | b84cc92 | 2014-02-24 17:52:58 -0800 | [diff] [blame] | 231 | InClassLogger() |
| 232 | { |
| 233 | g_logger.setLogLevel(LOG_ALL); |
| 234 | } |
| 235 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 236 | void |
| 237 | writeLogs() |
| 238 | { |
| 239 | NFD_LOG_TRACE("trace message JHGFDSR^1"); |
| 240 | NFD_LOG_DEBUG("debug message IGg2474fdksd fo " << 15 << 16 << 17); |
| 241 | NFD_LOG_WARN("warning message XXXhdhd11" << 1 << "x"); |
Alexander Afanasyev | b84cc92 | 2014-02-24 17:52:58 -0800 | [diff] [blame] | 242 | NFD_LOG_INFO("info message Jjxjshj13"); |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 243 | NFD_LOG_ERROR("error message !#$&^%$#@"); |
| 244 | NFD_LOG_FATAL("fatal message JJSjaamcng"); |
| 245 | } |
Alexander Afanasyev | b84cc92 | 2014-02-24 17:52:58 -0800 | [diff] [blame] | 246 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 247 | private: |
| 248 | NFD_LOG_INCLASS_DECLARE(); |
| 249 | }; |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 250 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 251 | NFD_LOG_INCLASS_DEFINE(InClassLogger, "InClassLogger"); |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 252 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 253 | BOOST_FIXTURE_TEST_CASE(InClass, InClassLogger) |
| 254 | { |
| 255 | writeLogs(); |
Alexander Afanasyev | b84cc92 | 2014-02-24 17:52:58 -0800 | [diff] [blame] | 256 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 257 | BOOST_CHECK_EQUAL(m_buffer.str(), |
| 258 | "TRACE: [InClassLogger] trace message JHGFDSR^1\n" |
| 259 | "DEBUG: [InClassLogger] debug message IGg2474fdksd fo 151617\n" |
| 260 | "WARNING: [InClassLogger] warning message XXXhdhd111x\n" |
| 261 | "INFO: [InClassLogger] info message Jjxjshj13\n" |
| 262 | "ERROR: [InClassLogger] error message !#$&^%$#@\n" |
| 263 | "FATAL: [InClassLogger] fatal message JJSjaamcng\n" |
| 264 | ); |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 265 | } |
| 266 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 267 | |
| 268 | template<class T> |
| 269 | class InClassTemplateLogger : public LoggerFixture |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 270 | { |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 271 | public: |
Alexander Afanasyev | b84cc92 | 2014-02-24 17:52:58 -0800 | [diff] [blame] | 272 | InClassTemplateLogger() |
| 273 | { |
| 274 | g_logger.setLogLevel(LOG_ALL); |
| 275 | } |
| 276 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 277 | void |
| 278 | writeLogs() |
| 279 | { |
| 280 | NFD_LOG_TRACE("trace message JHGFDSR^1"); |
| 281 | NFD_LOG_DEBUG("debug message IGg2474fdksd fo " << 15 << 16 << 17); |
| 282 | NFD_LOG_WARN("warning message XXXhdhd11" << 1 << "x"); |
Alexander Afanasyev | b84cc92 | 2014-02-24 17:52:58 -0800 | [diff] [blame] | 283 | NFD_LOG_INFO("info message Jjxjshj13"); |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 284 | NFD_LOG_ERROR("error message !#$&^%$#@"); |
| 285 | NFD_LOG_FATAL("fatal message JJSjaamcng"); |
| 286 | } |
Alexander Afanasyev | b84cc92 | 2014-02-24 17:52:58 -0800 | [diff] [blame] | 287 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 288 | private: |
| 289 | NFD_LOG_INCLASS_DECLARE(); |
| 290 | }; |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 291 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 292 | NFD_LOG_INCLASS_TEMPLATE_DEFINE(InClassTemplateLogger, "GenericInClassTemplateLogger"); |
| 293 | NFD_LOG_INCLASS_TEMPLATE_SPECIALIZATION_DEFINE(InClassTemplateLogger, int, "IntInClassLogger"); |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 294 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 295 | BOOST_FIXTURE_TEST_CASE(GenericInTemplatedClass, InClassTemplateLogger<bool>) |
| 296 | { |
| 297 | writeLogs(); |
Alexander Afanasyev | b84cc92 | 2014-02-24 17:52:58 -0800 | [diff] [blame] | 298 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 299 | BOOST_CHECK_EQUAL(m_buffer.str(), |
| 300 | "TRACE: [GenericInClassTemplateLogger] trace message JHGFDSR^1\n" |
| 301 | "DEBUG: [GenericInClassTemplateLogger] debug message IGg2474fdksd fo 151617\n" |
| 302 | "WARNING: [GenericInClassTemplateLogger] warning message XXXhdhd111x\n" |
| 303 | "INFO: [GenericInClassTemplateLogger] info message Jjxjshj13\n" |
| 304 | "ERROR: [GenericInClassTemplateLogger] error message !#$&^%$#@\n" |
| 305 | "FATAL: [GenericInClassTemplateLogger] fatal message JJSjaamcng\n" |
| 306 | ); |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 307 | } |
| 308 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 309 | BOOST_FIXTURE_TEST_CASE(SpecializedInTemplatedClass, InClassTemplateLogger<int>) |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 310 | { |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 311 | writeLogs(); |
Alexander Afanasyev | b84cc92 | 2014-02-24 17:52:58 -0800 | [diff] [blame] | 312 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 313 | BOOST_CHECK_EQUAL(m_buffer.str(), |
| 314 | "TRACE: [IntInClassLogger] trace message JHGFDSR^1\n" |
| 315 | "DEBUG: [IntInClassLogger] debug message IGg2474fdksd fo 151617\n" |
| 316 | "WARNING: [IntInClassLogger] warning message XXXhdhd111x\n" |
| 317 | "INFO: [IntInClassLogger] info message Jjxjshj13\n" |
| 318 | "ERROR: [IntInClassLogger] error message !#$&^%$#@\n" |
| 319 | "FATAL: [IntInClassLogger] fatal message JJSjaamcng\n" |
| 320 | ); |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | BOOST_AUTO_TEST_SUITE_END() |
| 324 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 325 | } // namespace tests |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 326 | } // namespace nfd |