File: logconfiguration-test.cpp

package info (click to toggle)
cxxtools 3.0.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,960 kB
  • sloc: cpp: 68,550; ansic: 15,641; sh: 4,239; makefile: 841
file content (263 lines) | stat: -rw-r--r-- 11,972 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*
 * Copyright (C) 2015 Tommi Maekitalo
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * As a special exception, you may use this file as part of a free
 * software library without restriction. Specifically, if other files
 * instantiate templates or use macros or inline functions from this
 * file, or you compile this file and link it with other files to
 * produce an executable, this file does not by itself cause the
 * resulting executable to be covered by the GNU General Public
 * License. This exception does not however invalidate any other
 * reasons why the executable file might be covered by the GNU Library
 * General Public License.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "cxxtools/log.h"
#include "cxxtools/unit/testsuite.h"
#include "cxxtools/unit/registertest.h"
#include <sstream>
#include <cxxtools/properties.h>

log_define("cxxtools.test.logconfiguration")

class LogconfigurationTest : public cxxtools::unit::TestSuite
{
  public:
    LogconfigurationTest()
    : cxxtools::unit::TestSuite("logconfiguration")
    {
      registerMethod("logLevelTest", *this, &LogconfigurationTest::logLevelTest);
      registerMethod("logLevelWithTraceTest", *this, &LogconfigurationTest::logLevelWithTraceTest);
      registerMethod("logSingleFlagTest", *this, &LogconfigurationTest::logSingleFlagTest);
      registerMethod("logFlagsTest", *this, &LogconfigurationTest::logFlagsTest);
      registerMethod("rootLevelTest", *this, &LogconfigurationTest::rootLevelTest);
      registerMethod("hierachicalTest", *this, &LogconfigurationTest::hierachicalTest);
      registerMethod("convertLogFlagsTest", *this, &LogconfigurationTest::convertLogFlagsTest);
    }

    void logLevelTest();
    void logLevelWithTraceTest();
    void logSingleFlagTest();
    void logFlagsTest();
    void rootLevelTest();
    void hierachicalTest();
    void convertLogFlagsTest();
};

void LogconfigurationTest::logLevelTest()
{
  std::istringstream properties(
    "rootlogger=WARN\n"
    "logger.fatal=FATAL\n"
    "logger.error=ERROR\n"
    "logger.warn=WARN\n"
    "logger.info=INFO\n"
    "logger.debug=DEBUG\n"
    "logger.fine=FINE\n"
    "logger.finer=FINER\n"
    "logger.finest=FINEST\n"
    "logger.trace=TRACE\n");

  cxxtools::LogConfiguration config;
  properties >> cxxtools::Properties(config);

  cxxtools::Logger rootLogger("", config.rootFlags());
  CXXTOOLS_UNIT_ASSERT(rootLogger.isEnabled(cxxtools::Logger::LOG_WARN));
  CXXTOOLS_UNIT_ASSERT(rootLogger.isEnabled(cxxtools::Logger::LOG_ERROR));
  CXXTOOLS_UNIT_ASSERT(!rootLogger.isEnabled(cxxtools::Logger::LOG_INFO));

  cxxtools::Logger fatalLogger("fatal", config.logFlags("fatal"));
  CXXTOOLS_UNIT_ASSERT(fatalLogger.isEnabled(cxxtools::Logger::LOG_FATAL));
  CXXTOOLS_UNIT_ASSERT(!fatalLogger.isEnabled(cxxtools::Logger::LOG_ERROR));

  CXXTOOLS_UNIT_ASSERT_EQUALS(config.logFlags("fatal"),  0x01);
  CXXTOOLS_UNIT_ASSERT_EQUALS(config.logFlags("error"),  0x03);
  CXXTOOLS_UNIT_ASSERT_EQUALS(config.logFlags("warn"),   0x07);
  CXXTOOLS_UNIT_ASSERT_EQUALS(config.logFlags("info"),   0x0f);
  CXXTOOLS_UNIT_ASSERT_EQUALS(config.logFlags("debug"),  0x1f);
  CXXTOOLS_UNIT_ASSERT_EQUALS(config.logFlags("fine"),   0x1f);
  CXXTOOLS_UNIT_ASSERT_EQUALS(config.logFlags("finer"),  0x3f);
  CXXTOOLS_UNIT_ASSERT_EQUALS(config.logFlags("finest"), 0x7f);
  CXXTOOLS_UNIT_ASSERT_EQUALS(config.logFlags("trace"),  0x9f);
}

void LogconfigurationTest::logLevelWithTraceTest()
{
  std::istringstream properties(
    "rootlogger=WARN\n"
    "logger.fatal=TFATAL\n"
    "logger.error=TERROR\n"
    "logger.warn=TWARN\n"
    "logger.info=TINFO\n"
    "logger.debug=TDEBUG\n"
    "logger.fine=TFINE\n"
    "logger.finer=TFINER\n"
    "logger.finest=TFINEST\n");

  cxxtools::LogConfiguration config;
  properties >> cxxtools::Properties(config);

  CXXTOOLS_UNIT_ASSERT_EQUALS(config.logFlags("fatal"),  0x81);
  CXXTOOLS_UNIT_ASSERT_EQUALS(config.logFlags("error"),  0x83);
  CXXTOOLS_UNIT_ASSERT_EQUALS(config.logFlags("warn"),   0x87);
  CXXTOOLS_UNIT_ASSERT_EQUALS(config.logFlags("info"),   0x8f);
  CXXTOOLS_UNIT_ASSERT_EQUALS(config.logFlags("debug"),  0x9f);
  CXXTOOLS_UNIT_ASSERT_EQUALS(config.logFlags("fine"),   0x9f);
  CXXTOOLS_UNIT_ASSERT_EQUALS(config.logFlags("finer"),  0xbf);
  CXXTOOLS_UNIT_ASSERT_EQUALS(config.logFlags("finest"), 0xff);
}

void LogconfigurationTest::logSingleFlagTest()
{
  std::istringstream properties(
    "rootlogger=WARN\n"
    "logger.fatal=|FATAL\n"
    "logger.error=|ERROR\n"
    "logger.warn=|WARN\n"
    "logger.info=|INFO\n"
    "logger.debug=|DEBUG\n"
    "logger.fine=|FINE\n"
    "logger.finer=|FINER\n"
    "logger.finest=|FINEST\n"
    "logger.trace=|TRACE\n");

  cxxtools::LogConfiguration config;
  properties >> cxxtools::Properties(config);

  CXXTOOLS_UNIT_ASSERT_EQUALS(config.logFlags("fatal"),  cxxtools::Logger::LOG_FATAL);
  CXXTOOLS_UNIT_ASSERT_EQUALS(config.logFlags("error"),  cxxtools::Logger::LOG_ERROR);
  CXXTOOLS_UNIT_ASSERT_EQUALS(config.logFlags("warn"),   cxxtools::Logger::LOG_WARN);
  CXXTOOLS_UNIT_ASSERT_EQUALS(config.logFlags("info"),   cxxtools::Logger::LOG_INFO);
  CXXTOOLS_UNIT_ASSERT_EQUALS(config.logFlags("debug"),  cxxtools::Logger::LOG_DEBUG);
  CXXTOOLS_UNIT_ASSERT_EQUALS(config.logFlags("fine"),   cxxtools::Logger::LOG_FINE);
  CXXTOOLS_UNIT_ASSERT_EQUALS(config.logFlags("finer"),  cxxtools::Logger::LOG_FINER);
  CXXTOOLS_UNIT_ASSERT_EQUALS(config.logFlags("finest"), cxxtools::Logger::LOG_FINEST);
  CXXTOOLS_UNIT_ASSERT_EQUALS(config.logFlags("trace"),  cxxtools::Logger::LOG_TRACE);
}

void LogconfigurationTest::logFlagsTest()
{
  std::istringstream properties(
    "rootlogger=WARN\n"
    "logger.cat1=WARN|INFO\n"
    "logger.cat2=TRACE|D\n"
    "logger.cat3=I|T\n");

  cxxtools::LogConfiguration config;
  properties >> cxxtools::Properties(config);

  CXXTOOLS_UNIT_ASSERT_EQUALS(config.logFlags("cat1"), cxxtools::Logger::LOG_WARN | cxxtools::Logger::LOG_INFO);
  CXXTOOLS_UNIT_ASSERT_EQUALS(config.logFlags("cat2"), cxxtools::Logger::LOG_DEBUG | cxxtools::Logger::LOG_TRACE);
  CXXTOOLS_UNIT_ASSERT_EQUALS(config.logFlags("cat3"), cxxtools::Logger::LOG_INFO | cxxtools::Logger::LOG_TRACE);
}

void LogconfigurationTest::rootLevelTest()
{
  std::istringstream properties(
    "rootlogger=WARN\n"
    "logger.cat1=INFO\n");

  cxxtools::LogConfiguration config;
  properties >> cxxtools::Properties(config);

  cxxtools::Logger cat1("cat1", config.logFlags("cat1"));
  cxxtools::Logger cat2("cat2", config.logFlags("cat2"));

  CXXTOOLS_UNIT_ASSERT(cat1.isEnabled(cxxtools::Logger::LOG_WARN));
  CXXTOOLS_UNIT_ASSERT(cat1.isEnabled(cxxtools::Logger::LOG_INFO));
  CXXTOOLS_UNIT_ASSERT(!cat1.isEnabled(cxxtools::Logger::LOG_DEBUG));

  CXXTOOLS_UNIT_ASSERT(cat2.isEnabled(cxxtools::Logger::LOG_WARN));
  CXXTOOLS_UNIT_ASSERT(!cat2.isEnabled(cxxtools::Logger::LOG_INFO));
  CXXTOOLS_UNIT_ASSERT(!cat2.isEnabled(cxxtools::Logger::LOG_DEBUG));
}

void LogconfigurationTest::hierachicalTest()
{
  std::istringstream properties(
    "rootlogger=WARN\n"
    "logger.cat1=INFO\n"
    "logger.cat1.sub1=ERROR\n"
    "logger.cat2.sub3=|INFO\n");

  cxxtools::LogConfiguration config;
  properties >> cxxtools::Properties(config);

  cxxtools::Logger cat1("cat1", config.logFlags("cat1"));
  cxxtools::Logger cat2("cat2", config.logFlags("cat2"));
  cxxtools::Logger sub1("sub1", config.logFlags("cat1.sub1"));
  cxxtools::Logger sub2("sub2", config.logFlags("cat1.sub2"));
  cxxtools::Logger sub3("sub3", config.logFlags("cat2.sub3"));

  CXXTOOLS_UNIT_ASSERT(cat1.isEnabled(cxxtools::Logger::LOG_WARN));
  CXXTOOLS_UNIT_ASSERT(cat1.isEnabled(cxxtools::Logger::LOG_INFO));
  CXXTOOLS_UNIT_ASSERT(!cat1.isEnabled(cxxtools::Logger::LOG_DEBUG));

  CXXTOOLS_UNIT_ASSERT(cat2.isEnabled(cxxtools::Logger::LOG_WARN));
  CXXTOOLS_UNIT_ASSERT(!cat2.isEnabled(cxxtools::Logger::LOG_INFO));
  CXXTOOLS_UNIT_ASSERT(!cat2.isEnabled(cxxtools::Logger::LOG_DEBUG));

  CXXTOOLS_UNIT_ASSERT(!sub1.isEnabled(cxxtools::Logger::LOG_WARN));
  CXXTOOLS_UNIT_ASSERT(!sub1.isEnabled(cxxtools::Logger::LOG_INFO));
  CXXTOOLS_UNIT_ASSERT(!sub1.isEnabled(cxxtools::Logger::LOG_DEBUG));

  CXXTOOLS_UNIT_ASSERT(sub2.isEnabled(cxxtools::Logger::LOG_WARN));
  CXXTOOLS_UNIT_ASSERT(sub2.isEnabled(cxxtools::Logger::LOG_INFO));
  CXXTOOLS_UNIT_ASSERT(!sub2.isEnabled(cxxtools::Logger::LOG_DEBUG));

  CXXTOOLS_UNIT_ASSERT(!sub3.isEnabled(cxxtools::Logger::LOG_WARN));
  CXXTOOLS_UNIT_ASSERT(sub3.isEnabled(cxxtools::Logger::LOG_INFO));
  CXXTOOLS_UNIT_ASSERT(!sub3.isEnabled(cxxtools::Logger::LOG_DEBUG));

}

void LogconfigurationTest::convertLogFlagsTest()
{
  CXXTOOLS_UNIT_ASSERT_EQUALS(cxxtools::LogConfiguration::strToLogFlags("|FATAL"),  0x01);
  CXXTOOLS_UNIT_ASSERT_EQUALS(cxxtools::LogConfiguration::strToLogFlags("FATAL"),  0x01);
  CXXTOOLS_UNIT_ASSERT_EQUALS(cxxtools::LogConfiguration::strToLogFlags("TFATAL"),  0x81);
  CXXTOOLS_UNIT_ASSERT_EQUALS(cxxtools::LogConfiguration::strToLogFlags("F"),  0x01);
  CXXTOOLS_UNIT_ASSERT_EQUALS(cxxtools::LogConfiguration::strToLogFlags("TF"),  0x81);
  CXXTOOLS_UNIT_ASSERT_EQUALS(cxxtools::LogConfiguration::strToLogFlags("|ERROR"),  0x02);
  CXXTOOLS_UNIT_ASSERT_EQUALS(cxxtools::LogConfiguration::strToLogFlags("ERROR"),  0x03);
  CXXTOOLS_UNIT_ASSERT_EQUALS(cxxtools::LogConfiguration::strToLogFlags("TERROR"),  0x83);
  CXXTOOLS_UNIT_ASSERT_EQUALS(cxxtools::LogConfiguration::strToLogFlags("E"),  0x03);
  CXXTOOLS_UNIT_ASSERT_EQUALS(cxxtools::LogConfiguration::strToLogFlags("ERROR|FATAL"),  0x03);
  CXXTOOLS_UNIT_ASSERT_EQUALS(cxxtools::LogConfiguration::strToLogFlags("|WARN"),   0x04);
  CXXTOOLS_UNIT_ASSERT_EQUALS(cxxtools::LogConfiguration::strToLogFlags("ERROR|WARN"),   0x06);
  CXXTOOLS_UNIT_ASSERT_EQUALS(cxxtools::LogConfiguration::strToLogFlags("WARN"),   0x07);
  CXXTOOLS_UNIT_ASSERT_EQUALS(cxxtools::LogConfiguration::strToLogFlags("W"),   0x07);
  CXXTOOLS_UNIT_ASSERT_EQUALS(cxxtools::LogConfiguration::strToLogFlags("|INFO"),   0x08);
  CXXTOOLS_UNIT_ASSERT_EQUALS(cxxtools::LogConfiguration::strToLogFlags("INFO"),   0x0f);
  CXXTOOLS_UNIT_ASSERT_EQUALS(cxxtools::LogConfiguration::strToLogFlags("|DEBUG"),  0x10);
  CXXTOOLS_UNIT_ASSERT_EQUALS(cxxtools::LogConfiguration::strToLogFlags("DEBUG"),  0x1f);
  CXXTOOLS_UNIT_ASSERT_EQUALS(cxxtools::LogConfiguration::strToLogFlags("D"),  0x1f);
  CXXTOOLS_UNIT_ASSERT_EQUALS(cxxtools::LogConfiguration::strToLogFlags("D|I"),  0x18);
  CXXTOOLS_UNIT_ASSERT_EQUALS(cxxtools::LogConfiguration::strToLogFlags("|FINE"),   0x10);
  CXXTOOLS_UNIT_ASSERT_EQUALS(cxxtools::LogConfiguration::strToLogFlags("FINE"),   0x1f);
  CXXTOOLS_UNIT_ASSERT_EQUALS(cxxtools::LogConfiguration::strToLogFlags("|FINER"),  0x20);
  CXXTOOLS_UNIT_ASSERT_EQUALS(cxxtools::LogConfiguration::strToLogFlags("FINER"),  0x3f);
  CXXTOOLS_UNIT_ASSERT_EQUALS(cxxtools::LogConfiguration::strToLogFlags("|FINEST"), 0x40);
  CXXTOOLS_UNIT_ASSERT_EQUALS(cxxtools::LogConfiguration::strToLogFlags("FINEST"), 0x7f);
  CXXTOOLS_UNIT_ASSERT_EQUALS(cxxtools::LogConfiguration::strToLogFlags("|TRACE"),  0x80);
  CXXTOOLS_UNIT_ASSERT_EQUALS(cxxtools::LogConfiguration::strToLogFlags("TRACE"),  0x9f);

  CXXTOOLS_UNIT_ASSERT_THROW(cxxtools::LogConfiguration::strToLogFlags("blah"), std::runtime_error);
}


cxxtools::unit::RegisterTest<LogconfigurationTest> register_LogconfigurationTest;