blob: c2491adc7c022aa4fa3a279b925262ba712367d9 [file] [log] [blame]
Alexander Afanasyev59a42ec2014-09-02 18:21:16 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014, Regents of the University of California.
4 *
5 * This file is part of NDNS (Named Data Networking Domain Name Service).
6 * See AUTHORS.md for complete list of NDNS authors and contributors.
7 *
8 * NDNS is free software: you can redistribute it and/or modify it under the terms
9 * of the GNU General Public License as published by the Free Software Foundation,
10 * either version 3 of the License, or (at your option) any later version.
11 *
12 * NDNS is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * NDNS, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "logger.hpp"
Shock Jiang3c723182014-09-10 16:41:18 -070021#include "../boost-test.hpp"
Shock Jiang3016c982014-11-11 11:35:17 -080022#include "config.hpp"
Alexander Afanasyev59a42ec2014-09-02 18:21:16 -070023
24#include <fstream>
25
26#include <boost/algorithm/string.hpp>
27#include <boost/algorithm/string/classification.hpp>
28#include <boost/filesystem.hpp>
29
30namespace ndn {
31namespace ndns {
32namespace tests {
33
34BOOST_AUTO_TEST_SUITE(Logger)
35
36class Fixture
37{
38public:
39 Fixture()
40 : m_logConfigFilename(boost::filesystem::unique_path().native())
41 , m_logFilename(boost::filesystem::unique_path().native())
42 {
43 }
44
45 ~Fixture()
46 {
47 boost::filesystem::remove(boost::filesystem::path(getLogConfigFilename()));
48 boost::filesystem::remove(boost::filesystem::path(getLogFilename()));
49
50 log::init("unit-tests.log4cxx");
51 }
52
53 const std::string&
54 getLogConfigFilename()
55 {
56 return m_logConfigFilename;
57 }
58
59 const std::string&
60 getLogFilename()
61 {
62 return m_logFilename;
63 }
64
65 void
66 verifyOutput(const std::string expected[], size_t nExpected)
67 {
68 std::ifstream is(getLogFilename().c_str());
69 std::string buffer((std::istreambuf_iterator<char>(is)),
70 (std::istreambuf_iterator<char>()));
71
72 std::vector<std::string> components;
73 boost::split(components, buffer, boost::is_any_of(" ,\n"));
74
75 // expected + number of timestamps (one per log statement) + trailing newline of last statement
76 BOOST_REQUIRE_EQUAL(components.size(), nExpected);
77
78 for (size_t i = 0; i < nExpected; ++i) {
79 if (expected[i] == "")
80 continue;
81
82 BOOST_CHECK_EQUAL(components[i], expected[i]);
83 }
84 }
85
86private:
87 std::string m_logConfigFilename;
88 std::string m_logFilename;
89};
90
91BOOST_FIXTURE_TEST_CASE(AllLevels, Fixture)
92{
93 {
94 std::ofstream of(getLogConfigFilename().c_str());
95 of << "log4j.rootLogger=TRACE, FILE\n"
96 << "log4j.appender.FILE=org.apache.log4j.FileAppender\n"
97 << "log4j.appender.FILE.layout=org.apache.log4j.PatternLayout\n"
98 << "log4j.appender.FILE.File=" << getLogFilename() << "\n"
99 << "log4j.appender.FILE.ImmediateFlush=true\n"
100 << "log4j.appender.FILE.layout.ConversionPattern=%d{HH:mm:ss} %p %c{1} - %m%n\n";
101 }
102
103 log::init(getLogConfigFilename());
104
105 NDNS_LOG_INIT("DefaultConfig");
106
107 NDNS_LOG_TRACE("trace-message-JHGFDSR^1");
108 NDNS_LOG_DEBUG("debug-message-IGg2474fdksd-fo-" << 15 << 16 << 17);
109 NDNS_LOG_INFO("info-message-Jjxjshj13");
110 NDNS_LOG_WARN("warning-message-XXXhdhd11" << 1 <<"x");
111 NDNS_LOG_ERROR("error-message-!#$&^%$#@");
112 NDNS_LOG_FATAL("fatal-message-JJSjaamcng");
113
114 const std::string EXPECTED[] =
115 {
116 "", "TRACE", "DefaultConfig", "-", "trace-message-JHGFDSR^1",
117 "", "DEBUG", "DefaultConfig", "-", "debug-message-IGg2474fdksd-fo-151617",
118 "", "INFO", "DefaultConfig", "-", "info-message-Jjxjshj13",
119 "", "WARN", "DefaultConfig", "-", "warning-message-XXXhdhd111x",
120 "", "ERROR", "DefaultConfig", "-", "error-message-!#$&^%$#@",
121 "", "FATAL", "DefaultConfig", "-", "fatal-message-JJSjaamcng",
122 "",
123 };
124
125 verifyOutput(EXPECTED, sizeof(EXPECTED) / sizeof(std::string));
126}
127
128BOOST_FIXTURE_TEST_CASE(UpToInfo, Fixture)
129{
130 {
131 std::ofstream of(getLogConfigFilename().c_str());
132 of << "log4j.rootLogger=INFO, FILE\n"
133 << "log4j.appender.FILE=org.apache.log4j.FileAppender\n"
134 << "log4j.appender.FILE.layout=org.apache.log4j.PatternLayout\n"
135 << "log4j.appender.FILE.File=" << getLogFilename() << "\n"
136 << "log4j.appender.FILE.ImmediateFlush=true\n"
137 << "log4j.appender.FILE.layout.ConversionPattern=%d{HH:mm:ss} %p %c{1} - %m%n\n";
138 }
139
140 log::init(getLogConfigFilename());
141
142 NDNS_LOG_INIT("DefaultConfig");
143
144 NDNS_LOG_TRACE("trace-message-JHGFDSR^1");
145 NDNS_LOG_DEBUG("debug-message-IGg2474fdksd-fo-" << 15 << 16 << 17);
146 NDNS_LOG_INFO("info-message-Jjxjshj13");
147 NDNS_LOG_WARN("warning-message-XXXhdhd11" << 1 <<"x");
148 NDNS_LOG_ERROR("error-message-!#$&^%$#@");
149 NDNS_LOG_FATAL("fatal-message-JJSjaamcng");
150
151 const std::string EXPECTED[] =
152 {
153 "", "INFO", "DefaultConfig", "-", "info-message-Jjxjshj13",
154 "", "WARN", "DefaultConfig", "-", "warning-message-XXXhdhd111x",
155 "", "ERROR", "DefaultConfig", "-", "error-message-!#$&^%$#@",
156 "", "FATAL", "DefaultConfig", "-", "fatal-message-JJSjaamcng",
157 "",
158 };
159
160 verifyOutput(EXPECTED, sizeof(EXPECTED) / sizeof(std::string));
161}
162
163BOOST_FIXTURE_TEST_CASE(OnlySelectedModule, Fixture)
164{
165 {
166 std::ofstream of(getLogConfigFilename().c_str());
167 of << "log4j.rootLogger=OFF, FILE\n"
168 << "log4j.appender.FILE=org.apache.log4j.FileAppender\n"
169 << "log4j.appender.FILE.layout=org.apache.log4j.PatternLayout\n"
170 << "log4j.appender.FILE.File=" << getLogFilename() << "\n"
171 << "log4j.appender.FILE.ImmediateFlush=true\n"
172 << "log4j.appender.FILE.layout.ConversionPattern=%d{HH:mm:ss} %p %c - %m%n\n"
173 << "log4j.logger.SelectedModule=TRACE\n"
174 << "log4j.logger.SelectedModule.Submodule=FATAL\n";
175 }
176
177 log::init(getLogConfigFilename());
178
179 {
180 NDNS_LOG_INIT("DefaultConfig");
181
182 NDNS_LOG_TRACE("trace-message-JHGFDSR^1");
183 NDNS_LOG_DEBUG("debug-message-IGg2474fdksd-fo-" << 15 << 16 << 17);
184 NDNS_LOG_INFO("info-message-Jjxjshj13");
185 NDNS_LOG_WARN("warning-message-XXXhdhd11" << 1 <<"x");
186 NDNS_LOG_ERROR("error-message-!#$&^%$#@");
187 NDNS_LOG_FATAL("fatal-message-JJSjaamcng");
188 }
189
190 {
191 NDNS_LOG_INIT("SelectedModule");
192
193 NDNS_LOG_TRACE("trace-message-JHGFDSR^1");
194 NDNS_LOG_DEBUG("debug-message-IGg2474fdksd-fo-" << 15 << 16 << 17);
195 NDNS_LOG_INFO("info-message-Jjxjshj13");
196 NDNS_LOG_WARN("warning-message-XXXhdhd11" << 1 <<"x");
197 NDNS_LOG_ERROR("error-message-!#$&^%$#@");
198 NDNS_LOG_FATAL("fatal-message-JJSjaamcng");
199 }
200
201 {
202 NDNS_LOG_INIT("SelectedModule.Submodule");
203
204 NDNS_LOG_TRACE("trace-message-JHGFDSR^1");
205 NDNS_LOG_DEBUG("debug-message-IGg2474fdksd-fo-" << 15 << 16 << 17);
206 NDNS_LOG_INFO("info-message-Jjxjshj13");
207 NDNS_LOG_WARN("warning-message-XXXhdhd11" << 1 <<"x");
208 NDNS_LOG_ERROR("error-message-!#$&^%$#@");
209 NDNS_LOG_FATAL("fatal-message-JJSjaamcng");
210 }
211
212 const std::string EXPECTED[] =
213 {
214 "", "TRACE", "SelectedModule", "-", "trace-message-JHGFDSR^1",
215 "", "DEBUG", "SelectedModule", "-", "debug-message-IGg2474fdksd-fo-151617",
216 "", "INFO", "SelectedModule", "-", "info-message-Jjxjshj13",
217 "", "WARN", "SelectedModule", "-", "warning-message-XXXhdhd111x",
218 "", "ERROR", "SelectedModule", "-", "error-message-!#$&^%$#@",
219 "", "FATAL", "SelectedModule", "-", "fatal-message-JJSjaamcng",
220 "", "FATAL", "SelectedModule.Submodule", "-", "fatal-message-JJSjaamcng",
221 "",
222 };
223
224 verifyOutput(EXPECTED, sizeof(EXPECTED) / sizeof(std::string));
225}
226
227
228BOOST_AUTO_TEST_SUITE_END()
229
230} // namespace tests
231} // namespace ndns
232} // namespace ndn