blob: 0aa2e47af8bb767733d42d8756a2d36e9534ecc8 [file] [log] [blame]
Ilya Moiseenkoa807e652014-01-28 11:51:01 -08001/* -*- 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 DiBenedettobf6a93d2014-03-21 14:03:02 -060010
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080011#include <boost/test/unit_test.hpp>
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060012
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080013#include <iostream>
14
Junxiao Shid9ee45c2014-02-27 15:38:11 -070015#include "tests/test-common.hpp"
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080016
17namespace nfd {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070018namespace tests {
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080019
Junxiao Shid9ee45c2014-02-27 15:38:11 -070020BOOST_FIXTURE_TEST_SUITE(CoreLogger, BaseFixture)
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080021
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060022class LoggerFixture : protected BaseFixture
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080023{
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060024public:
Alexander Afanasyev66886812014-01-31 14:48:48 -080025 LoggerFixture()
26 : m_savedBuf(std::cerr.rdbuf())
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060027 , m_savedLevel(LoggerFactory::getInstance().getDefaultLevel())
Alexander Afanasyev66886812014-01-31 14:48:48 -080028 {
29 std::cerr.rdbuf(m_buffer.rdbuf());
30 }
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080031
Alexander Afanasyev66886812014-01-31 14:48:48 -080032 ~LoggerFixture()
33 {
34 std::cerr.rdbuf(m_savedBuf);
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060035 LoggerFactory::getInstance().setDefaultLevel(m_savedLevel);
Alexander Afanasyev66886812014-01-31 14:48:48 -080036 }
Alexander Afanasyevb84cc922014-02-24 17:52:58 -080037
Alexander Afanasyev66886812014-01-31 14:48:48 -080038 std::stringstream m_buffer;
39 std::streambuf* m_savedBuf;
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060040 LogLevel m_savedLevel;
Alexander Afanasyev66886812014-01-31 14:48:48 -080041};
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080042
Alexander Afanasyev66886812014-01-31 14:48:48 -080043BOOST_FIXTURE_TEST_CASE(Basic, LoggerFixture)
44{
45 NFD_LOG_INIT("BasicTests");
Alexander Afanasyevb84cc922014-02-24 17:52:58 -080046 g_logger.setLogLevel(LOG_ALL);
47
Alexander Afanasyev66886812014-01-31 14:48:48 -080048 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 Afanasyevb84cc922014-02-24 17:52:58 -080051 NFD_LOG_INFO("info message Jjxjshj13");
Alexander Afanasyev66886812014-01-31 14:48:48 -080052 NFD_LOG_ERROR("error message !#$&^%$#@");
53 NFD_LOG_FATAL("fatal message JJSjaamcng");
Alexander Afanasyevb84cc922014-02-24 17:52:58 -080054
Alexander Afanasyev66886812014-01-31 14:48:48 -080055 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 Moiseenkoa807e652014-01-28 11:51:01 -080063}
64
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060065BOOST_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
95BOOST_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
124static void
125testModuleBPrint()
126{
127 NFD_LOG_INIT("TestModuleB");
128 NFD_LOG_DEBUG("debug message IGg2474fdksd fo " << 15 << 16 << 17);
129}
130
131BOOST_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
157BOOST_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
185static bool
186checkError(const LoggerFactory::Error& error, const std::string& expected)
187{
188 return error.what() == expected;
189}
190
191BOOST_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
209BOOST_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 Afanasyev66886812014-01-31 14:48:48 -0800227class InClassLogger : public LoggerFixture
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800228{
Alexander Afanasyev66886812014-01-31 14:48:48 -0800229public:
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600230
Alexander Afanasyevb84cc922014-02-24 17:52:58 -0800231 InClassLogger()
232 {
233 g_logger.setLogLevel(LOG_ALL);
234 }
235
Alexander Afanasyev66886812014-01-31 14:48:48 -0800236 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 Afanasyevb84cc922014-02-24 17:52:58 -0800242 NFD_LOG_INFO("info message Jjxjshj13");
Alexander Afanasyev66886812014-01-31 14:48:48 -0800243 NFD_LOG_ERROR("error message !#$&^%$#@");
244 NFD_LOG_FATAL("fatal message JJSjaamcng");
245 }
Alexander Afanasyevb84cc922014-02-24 17:52:58 -0800246
Alexander Afanasyev66886812014-01-31 14:48:48 -0800247private:
248 NFD_LOG_INCLASS_DECLARE();
249};
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800250
Alexander Afanasyev66886812014-01-31 14:48:48 -0800251NFD_LOG_INCLASS_DEFINE(InClassLogger, "InClassLogger");
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800252
Alexander Afanasyev66886812014-01-31 14:48:48 -0800253BOOST_FIXTURE_TEST_CASE(InClass, InClassLogger)
254{
255 writeLogs();
Alexander Afanasyevb84cc922014-02-24 17:52:58 -0800256
Alexander Afanasyev66886812014-01-31 14:48:48 -0800257 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 Moiseenkoa807e652014-01-28 11:51:01 -0800265}
266
Alexander Afanasyev66886812014-01-31 14:48:48 -0800267
268template<class T>
269class InClassTemplateLogger : public LoggerFixture
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800270{
Alexander Afanasyev66886812014-01-31 14:48:48 -0800271public:
Alexander Afanasyevb84cc922014-02-24 17:52:58 -0800272 InClassTemplateLogger()
273 {
274 g_logger.setLogLevel(LOG_ALL);
275 }
276
Alexander Afanasyev66886812014-01-31 14:48:48 -0800277 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 Afanasyevb84cc922014-02-24 17:52:58 -0800283 NFD_LOG_INFO("info message Jjxjshj13");
Alexander Afanasyev66886812014-01-31 14:48:48 -0800284 NFD_LOG_ERROR("error message !#$&^%$#@");
285 NFD_LOG_FATAL("fatal message JJSjaamcng");
286 }
Alexander Afanasyevb84cc922014-02-24 17:52:58 -0800287
Alexander Afanasyev66886812014-01-31 14:48:48 -0800288private:
289 NFD_LOG_INCLASS_DECLARE();
290};
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800291
Alexander Afanasyev66886812014-01-31 14:48:48 -0800292NFD_LOG_INCLASS_TEMPLATE_DEFINE(InClassTemplateLogger, "GenericInClassTemplateLogger");
293NFD_LOG_INCLASS_TEMPLATE_SPECIALIZATION_DEFINE(InClassTemplateLogger, int, "IntInClassLogger");
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800294
Alexander Afanasyev66886812014-01-31 14:48:48 -0800295BOOST_FIXTURE_TEST_CASE(GenericInTemplatedClass, InClassTemplateLogger<bool>)
296{
297 writeLogs();
Alexander Afanasyevb84cc922014-02-24 17:52:58 -0800298
Alexander Afanasyev66886812014-01-31 14:48:48 -0800299 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 Moiseenkoa807e652014-01-28 11:51:01 -0800307}
308
Alexander Afanasyev66886812014-01-31 14:48:48 -0800309BOOST_FIXTURE_TEST_CASE(SpecializedInTemplatedClass, InClassTemplateLogger<int>)
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800310{
Alexander Afanasyev66886812014-01-31 14:48:48 -0800311 writeLogs();
Alexander Afanasyevb84cc922014-02-24 17:52:58 -0800312
Alexander Afanasyev66886812014-01-31 14:48:48 -0800313 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 Moiseenkoa807e652014-01-28 11:51:01 -0800321}
322
323BOOST_AUTO_TEST_SUITE_END()
324
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700325} // namespace tests
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800326} // namespace nfd