1//
2// LoggerTest.cpp
3//
4// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
5// and Contributors.
6//
7// SPDX-License-Identifier: BSL-1.0
8//
9
10
11#include "LoggerTest.h"
12#include "Poco/CppUnit/TestCaller.h"
13#include "Poco/CppUnit/TestSuite.h"
14#include "Poco/Logger.h"
15#include "Poco/AutoPtr.h"
16#include "TestChannel.h"
17
18
19using Poco::Logger;
20using Poco::Channel;
21using Poco::Message;
22using Poco::AutoPtr;
23
24
25LoggerTest::LoggerTest(const std::string& rName): CppUnit::TestCase(rName)
26{
27}
28
29
30LoggerTest::~LoggerTest()
31{
32}
33
34
35void LoggerTest::testLogger()
36{
37 AutoPtr<TestChannel> pChannel = new TestChannel;
38 Logger& root = Logger::root();
39 root.setChannel(pChannel);
40
41 assertTrue (root.getLevel() == Message::PRIO_INFORMATION);
42 assertTrue (root.is(Message::PRIO_INFORMATION));
43 assertTrue (root.fatal());
44 assertTrue (root.critical());
45 assertTrue (root.error());
46 assertTrue (root.warning());
47 assertTrue (root.notice());
48 assertTrue (root.information());
49 assertTrue (!root.debug());
50 assertTrue (!root.trace());
51
52 root.information("Informational message");
53 assertTrue (pChannel->list().size() == 1);
54 root.warning("Warning message");
55 assertTrue (pChannel->list().size() == 2);
56 root.debug("Debug message");
57 assert (pChannel->list().size() == 2);
58
59 Logger& logger1 = Logger::get("Logger1");
60 Logger& logger2 = Logger::get("Logger2");
61 Logger& logger11 = Logger::get("Logger1.Logger1");
62 Logger& logger12 = Logger::get("Logger1.Logger2");
63 Logger& logger21 = Logger::get("Logger2.Logger1");
64 Logger& logger22 = Logger::get("Logger2.Logger2");
65
66 std::vector<std::string> loggers;
67 Logger::names(loggers);
68 assertTrue (loggers.size() == 7);
69 assertTrue (loggers[0] == "");
70 assertTrue (loggers[1] == "Logger1");
71 assertTrue (loggers[2] == "Logger1.Logger1");
72 assertTrue (loggers[3] == "Logger1.Logger2");
73 assertTrue (loggers[4] == "Logger2");
74 assertTrue (loggers[5] == "Logger2.Logger1");
75 assertTrue (loggers[6] == "Logger2.Logger2");
76
77 Logger::setLevel("Logger1", Message::PRIO_DEBUG);
78 assertTrue (logger1.is(Message::PRIO_DEBUG));
79 assertTrue (logger11.is(Message::PRIO_DEBUG));
80 assertTrue (logger12.is(Message::PRIO_DEBUG));
81 assertTrue (!logger2.is(Message::PRIO_DEBUG));
82 assertTrue (!logger21.is(Message::PRIO_DEBUG));
83 assertTrue (!logger22.is(Message::PRIO_DEBUG));
84 assertTrue (logger11.is(Message::PRIO_INFORMATION));
85 assertTrue (logger12.is(Message::PRIO_INFORMATION));
86 assertTrue (logger21.is(Message::PRIO_INFORMATION));
87 assertTrue (logger22.is(Message::PRIO_INFORMATION));
88
89 Logger::setLevel("Logger2.Logger1", Message::PRIO_ERROR);
90 assertTrue (logger1.is(Message::PRIO_DEBUG));
91 assertTrue (logger11.is(Message::PRIO_DEBUG));
92 assertTrue (logger12.is(Message::PRIO_DEBUG));
93 assertTrue (!logger21.is(Message::PRIO_DEBUG));
94 assertTrue (!logger22.is(Message::PRIO_DEBUG));
95 assertTrue (logger11.is(Message::PRIO_INFORMATION));
96 assertTrue (logger12.is(Message::PRIO_INFORMATION));
97 assertTrue (logger21.is(Message::PRIO_ERROR));
98 assertTrue (logger22.is(Message::PRIO_INFORMATION));
99
100 Logger::setLevel("", Message::PRIO_WARNING);
101 assertTrue (root.getLevel() == Message::PRIO_WARNING);
102 assertTrue (logger1.getLevel() == Message::PRIO_WARNING);
103 assertTrue (logger11.getLevel() == Message::PRIO_WARNING);
104 assertTrue (logger12.getLevel() == Message::PRIO_WARNING);
105 assertTrue (logger1.getLevel() == Message::PRIO_WARNING);
106 assertTrue (logger21.getLevel() == Message::PRIO_WARNING);
107 assertTrue (logger22.getLevel() == Message::PRIO_WARNING);
108
109 AutoPtr<TestChannel> pChannel2 = new TestChannel;
110 Logger::setChannel("Logger2", pChannel2);
111 assertTrue (pChannel.get() == root.getChannel().get());
112 assertTrue (pChannel.get() == logger1.getChannel().get());
113 assertTrue (pChannel.get() == logger11.getChannel().get());
114 assertTrue (pChannel.get() == logger12.getChannel().get());
115 assertTrue (pChannel2.get() == logger2.getChannel().get());
116 assertTrue (pChannel2.get() == logger21.getChannel().get());
117 assertTrue (pChannel2.get() == logger22.getChannel().get());
118
119 root.setLevel(Message::PRIO_TRACE);
120 pChannel->list().clear();
121 root.trace("trace");
122 assertTrue (pChannel->list().begin()->getPriority() == Message::PRIO_TRACE);
123 pChannel->list().clear();
124 root.debug("debug");
125 assertTrue (pChannel->list().begin()->getPriority() == Message::PRIO_DEBUG);
126 pChannel->list().clear();
127 root.information("information");
128 assertTrue (pChannel->list().begin()->getPriority() == Message::PRIO_INFORMATION);
129 pChannel->list().clear();
130 root.notice("notice");
131 assertTrue (pChannel->list().begin()->getPriority() == Message::PRIO_NOTICE);
132 pChannel->list().clear();
133 root.warning("warning");
134 assertTrue (pChannel->list().begin()->getPriority() == Message::PRIO_WARNING);
135 pChannel->list().clear();
136 root.error("error");
137 assertTrue (pChannel->list().begin()->getPriority() == Message::PRIO_ERROR);
138 pChannel->list().clear();
139 root.critical("critical");
140 assertTrue (pChannel->list().begin()->getPriority() == Message::PRIO_CRITICAL);
141 pChannel->list().clear();
142 root.fatal("fatal");
143 assertTrue (pChannel->list().begin()->getPriority() == Message::PRIO_FATAL);
144
145 root.setLevel("1");
146 assertTrue (root.getLevel() == Message::PRIO_FATAL);
147 root.setLevel("8");
148 assertTrue (root.getLevel() == Message::PRIO_TRACE);
149 try
150 {
151 root.setLevel("0");
152 assertTrue (0);
153 }
154 catch(Poco::InvalidArgumentException&)
155 {
156 }
157 try
158 {
159 root.setLevel("9");
160 assertTrue (0);
161 }
162 catch(Poco::InvalidArgumentException&)
163 {
164 }
165}
166
167
168void LoggerTest::testFormat()
169{
170 std::string str = Logger::format("$0$1", "foo", "bar");
171 assertTrue (str == "foobar");
172 str = Logger::format("foo$0", "bar");
173 assertTrue (str == "foobar");
174 str = Logger::format("the amount is $$ $0", "100");
175 assertTrue (str == "the amount is $ 100");
176 str = Logger::format("$0$1$2", "foo", "bar");
177 assertTrue (str == "foobar");
178 str = Logger::format("$foo$0", "bar");
179 assertTrue (str == "$foobar");
180 str = Logger::format("$0", "1");
181 assertTrue (str == "1");
182 str = Logger::format("$0$1", "1", "2");
183 assertTrue (str == "12");
184 str = Logger::format("$0$1$2", "1", "2", "3");
185 assertTrue (str == "123");
186 str = Logger::format("$0$1$2$3", "1", "2", "3", "4");
187 assertTrue (str == "1234");
188}
189
190void LoggerTest::testFormatAny()
191{
192 AutoPtr<TestChannel> pChannel = new TestChannel;
193 Logger& root = Logger::root();
194 root.setChannel(pChannel);
195
196 root.error("%s%s", std::string("foo"), std::string("bar"));
197 assertTrue (pChannel->getLastMessage().getText() == "foobar");
198
199 root.error("foo%s", std::string("bar"));
200 assertTrue (pChannel->getLastMessage().getText() == "foobar");
201
202 root.error("the amount is %% %d", 100);
203 assertTrue (pChannel->getLastMessage().getText() == "the amount is % 100");
204
205 root.error("%d", 1);
206 assertTrue (pChannel->getLastMessage().getText() == "1");
207
208 root.error("%d%d", 1, 2);
209 assertTrue (pChannel->getLastMessage().getText() == "12");
210
211 root.error("%d%d%d", 1, 2, 3);
212 assertTrue (pChannel->getLastMessage().getText() == "123");
213
214 root.error("%d%d%d%d", 1, 2, 3, 4);
215 assertTrue (pChannel->getLastMessage().getText() == "1234");
216
217 root.error("%d%d%d%d%d", 1, 2, 3, 4, 5);
218 assertTrue (pChannel->getLastMessage().getText() == "12345");
219
220 root.error("%d%d%d%d%d%d", 1, 2, 3, 4, 5, 6);
221 assertTrue (pChannel->getLastMessage().getText() == "123456");
222
223 root.error("%d%d%d%d%d%d%d", 1, 2, 3, 4, 5, 6, 7);
224 assertTrue (pChannel->getLastMessage().getText() == "1234567");
225
226 root.error("%d%d%d%d%d%d%d%d", 1, 2, 3, 4, 5, 6, 7, 8);
227 assertTrue (pChannel->getLastMessage().getText() == "12345678");
228
229 root.error("%d%d%d%d%d%d%d%d%d", 1, 2, 3, 4, 5, 6, 7, 8, 9);
230 assertTrue (pChannel->getLastMessage().getText() == "123456789");
231
232 root.error("%d%d%d%d%d%d%d%d%d%d", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
233 assertTrue (pChannel->getLastMessage().getText() == "12345678910");
234
235 root.error("%d%d%d%d%d%d%d%d%d%d%d", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
236 assertTrue (pChannel->getLastMessage().getText() == "1234567891011");
237
238 root.error("%d%d%d%d%d%d%d%d%d%d%d%d", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
239 assertTrue (pChannel->getLastMessage().getText() == "123456789101112");
240
241 root.error("%d%d%d%d%d%d%d%d%d%d%d%d%d", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13);
242 assertTrue (pChannel->getLastMessage().getText() == "12345678910111213");
243
244 root.error("%d%d%d%d%d%d%d%d%d%d%d%d%d%d", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14);
245 assertTrue (pChannel->getLastMessage().getText() == "1234567891011121314");
246
247 root.error("%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
248 assertTrue (pChannel->getLastMessage().getText() == "123456789101112131415");
249}
250
251
252void LoggerTest::testDump()
253{
254 AutoPtr<TestChannel> pChannel = new TestChannel;
255 Logger& root = Logger::root();
256 root.setChannel(pChannel);
257 root.setLevel(Message::PRIO_INFORMATION);
258
259 char buffer1[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05};
260 root.dump("test", buffer1, sizeof(buffer1));
261 assertTrue (pChannel->list().empty());
262
263 root.setLevel(Message::PRIO_DEBUG);
264 root.dump("test", buffer1, sizeof(buffer1));
265
266 std::string msg = pChannel->list().begin()->getText();
267 assertTrue (msg == "test\n0000 00 01 02 03 04 05 ......");
268 pChannel->clear();
269
270 char buffer2[] = {
271 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
272 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
273 };
274 root.dump("", buffer2, sizeof(buffer2));
275 msg = pChannel->list().begin()->getText();
276 assertTrue (msg == "0000 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F ................");
277 pChannel->clear();
278
279 char buffer3[] = {
280 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
281 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
282 0x20, 0x41, 0x42, 0x1f, 0x7f, 0x7e
283 };
284 root.dump("", buffer3, sizeof(buffer3));
285 msg = pChannel->list().begin()->getText();
286 assertTrue (msg == "0000 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F ................\n"
287 "0010 20 41 42 1F 7F 7E AB..~");
288 pChannel->clear();
289}
290
291
292void LoggerTest::setUp()
293{
294 Logger::shutdown();
295}
296
297
298void LoggerTest::tearDown()
299{
300}
301
302
303CppUnit::Test* LoggerTest::suite()
304{
305 CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("LoggerTest");
306
307 CppUnit_addTest(pSuite, LoggerTest, testLogger);
308 CppUnit_addTest(pSuite, LoggerTest, testFormat);
309 CppUnit_addTest(pSuite, LoggerTest, testFormatAny);
310 CppUnit_addTest(pSuite, LoggerTest, testDump);
311
312 return pSuite;
313}
314