Replace inet_aton() with inet_pton() to parse IPs correctly (#8825)
[libi2ncommon] / test / test_logging.cpp
... / ...
CommitLineData
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*/
20/** @file
21 * test cases for loggin module.
22 *
23 * (c) Copyright 2007 by Intra2net AG
24 */
25
26//#define NOISEDEBUG
27
28#include <string>
29#include <iostream>
30#include <iomanip>
31#include <exception>
32
33#define BOOST_TEST_DYN_LINK
34#include <boost/test/unit_test.hpp>
35
36#include <logfunc.hpp>
37#include <tracefunc.hpp>
38#include <filefunc.hxx>
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
47using namespace I2n;
48
49namespace {
50
51Logger::PartLogger module_logger(HERE);
52
53} // eo namespace <anonymous>
54
55class TestLoggingFixture
56{
57public:
58 std::string LogFile;
59
60 TestLoggingFixture()
61 {
62 LogFile = "zzUnitTest.log";
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);
71
72 if (file_exists(LogFile))
73 unlink(LogFile);
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();
87 Logger::enable_log_file(LogFile);
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
105
106
107BOOST_AUTO_TEST_CASE(TestScopeTrace1)
108{
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();
115 Logger::enable_log_file(LogFile);
116
117 SCOPETRACKER();
118
119 log.notice() << "Stream test notice msg";
120 {
121 SCOPETRACKER(); // #2
122 {
123 SCOPETRACKER(); // #3
124 {
125 SCOPETRACKER(); // #4
126 }
127 }
128 {
129 SCOPETRACKER(); // #3
130 }
131 }
132} // eo TestScopeTrace1()
133
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();
149 Logger::enable_log_file(LogFile);
150
151 recursive_function();
152}
153
154BOOST_AUTO_TEST_SUITE_END()