base64 encoder/decoder: Add parameter to control linefeed handling
[libi2ncommon] / test / test_logging.cpp
CommitLineData
0e23f538
TJ
1/*
2The software in this package is distributed under the GNU General
3Public License version 2 (with a special exception described below).
4
5A copy of GNU General Public License (GPL) is included in this distribution,
6in the file COPYING.GPL.
7
8As a special exception, if other files instantiate templates or use macros
9or inline functions from this file, or you compile this file and link it
10with other works to produce a work based on this file, this file
11does not by itself cause the resulting work to be covered
12by the GNU General Public License.
13
14However the source code for this file must still be made available
15in accordance with section (3) of the GNU General Public License.
16
17This exception does not invalidate any other reasons why a work based
18on this file might be covered by the GNU General Public License.
19*/
b6fb82a2
RP
20/** @file
21 * test cases for loggin module.
22 *
23 * (c) Copyright 2007 by Intra2net AG
b6fb82a2
RP
24 */
25
26//#define NOISEDEBUG
27
28#include <string>
29#include <iostream>
30#include <iomanip>
31#include <exception>
32
9fe0853b
TJ
33#define BOOST_TEST_DYN_LINK
34#include <boost/test/unit_test.hpp>
b6fb82a2
RP
35
36#include <logfunc.hpp>
37#include <tracefunc.hpp>
c489769c 38#include <filefunc.hxx>
b6fb82a2
RP
39
40
41#ifdef NOISEDEBUG
42#define DOUT(msg) std::cout << msg << std::endl
43#else
44#define DOUT(msg) do {} while (0)
45#endif
46
b6fb82a2
RP
47using namespace I2n;
48
49namespace {
50
51Logger::PartLogger module_logger(HERE);
52
53} // eo namespace <anonymous>
54
9fe0853b
TJ
55class TestLoggingFixture
56{
57public:
c489769c
CH
58 std::string LogFile;
59
9fe0853b
TJ
60 TestLoggingFixture()
61 {
c489769c 62 LogFile = "zzUnitTest.log";
9fe0853b
TJ
63 }
64
65 ~TestLoggingFixture()
66 {
67 Logger::set_log_level(0);
68 Logger::enable_syslog(false);
69 Logger::enable_stderr_log(false);
70 Logger::enable_log_file(false);
c489769c
CH
71
72 if (file_exists(LogFile))
73 unlink(LogFile);
9fe0853b
TJ
74 }
75};
76
77BOOST_FIXTURE_TEST_SUITE(TestLogging, TestLoggingFixture)
78
79BOOST_AUTO_TEST_CASE(Syslog1)
80{
81 //Logger::enable_syslog("I2N Unittest", Logger::Facility::User );
82 Logger::enable_syslog( Logger::Facility::User );
83 Logger::PartLogger log(__func__);
84
85 Logger::set_log_level( 7 );
86 Logger::enable_stderr_log();
c489769c 87 Logger::enable_log_file(LogFile);
9fe0853b
TJ
88
89 log.error("Test error msg");
90 log.error() << "Stream test error msg #" << 2 << ".";
91 log.warning() << "Stream test warning msg";
92 log.notice() << "Stream test notice msg";
93 log.info() << "Stream test info msg";
94 log.debug() << "Stream test debug msg";
95
96 log.debug()
97 << "multiline log message" << std::endl
98 << "this (second) line should be indented" << std::endl
99 << "and this also!" << std::endl;
100 log.debug(HERE) << "This should have a source info";
101
102 module_logger.debug(HERE) << "module level debug message with source loc info";
103} // eo Syslog1()
104
b6fb82a2
RP
105
106
9fe0853b 107BOOST_AUTO_TEST_CASE(TestScopeTrace1)
b6fb82a2 108{
9fe0853b
TJ
109 //Logger::enable_syslog("I2N Unittest", Logger::Facility::User );
110 Logger::enable_syslog( Logger::Facility::User );
111 Logger::PartLogger log(__func__);
112
113 Logger::set_log_level( 7 );
114 Logger::enable_stderr_log();
c489769c 115 Logger::enable_log_file(LogFile);
9fe0853b
TJ
116
117 SCOPETRACKER();
118
119 log.notice() << "Stream test notice msg";
120 {
121 SCOPETRACKER(); // #2
b6fb82a2 122 {
9fe0853b 123 SCOPETRACKER(); // #3
b6fb82a2 124 {
9fe0853b 125 SCOPETRACKER(); // #4
b6fb82a2 126 }
9fe0853b
TJ
127 }
128 {
129 SCOPETRACKER(); // #3
130 }
131 }
132} // eo TestScopeTrace1()
b6fb82a2 133
f4fcef65
TJ
134void recursive_function(int level=0)
135{
136 SCOPETRACKER();
137
138 if (level < 10)
139 recursive_function(++level);
140}
141
142BOOST_AUTO_TEST_CASE(TestRecursiveScopeTracker)
143{
144 Logger::enable_syslog( Logger::Facility::User );
145 Logger::PartLogger log(__func__);
146
147 Logger::set_log_level( 7 );
148 Logger::enable_stderr_log();
c489769c 149 Logger::enable_log_file(LogFile);
f4fcef65
TJ
150
151 recursive_function();
152}
153
9fe0853b 154BOOST_AUTO_TEST_SUITE_END()