cf2dc3d9d9075ea5ada12f3c85dd3f95a1a5f2a8
[libi2ncommon] / test / test_logging.cpp
1 /*
2 The software in this package is distributed under the GNU General
3 Public License version 2 (with a special exception described below).
4
5 A copy of GNU General Public License (GPL) is included in this distribution,
6 in the file COPYING.GPL.
7
8 As a special exception, if other files instantiate templates or use macros
9 or inline functions from this file, or you compile this file and link it
10 with other works to produce a work based on this file, this file
11 does not by itself cause the resulting work to be covered
12 by the GNU General Public License.
13
14 However the source code for this file must still be made available
15 in accordance with section (3) of the GNU General Public License.
16
17 This exception does not invalidate any other reasons why a work based
18 on 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
39
40 #ifdef NOISEDEBUG
41 #define DOUT(msg) std::cout << msg << std::endl
42 #else
43 #define DOUT(msg) do {} while (0)
44 #endif
45
46 using namespace I2n;
47
48 namespace {
49
50 Logger::PartLogger module_logger(HERE);
51
52 } // eo namespace <anonymous>
53
54 class TestLoggingFixture
55 {
56 public:
57     TestLoggingFixture()
58     {
59     }
60
61     ~TestLoggingFixture()
62     {
63         Logger::set_log_level(0);
64         Logger::enable_syslog(false);
65         Logger::enable_stderr_log(false);
66         Logger::enable_log_file(false);
67     }
68 };
69
70 BOOST_FIXTURE_TEST_SUITE(TestLogging, TestLoggingFixture)
71
72 BOOST_AUTO_TEST_CASE(Syslog1)
73 {
74     //Logger::enable_syslog("I2N Unittest", Logger::Facility::User );
75     Logger::enable_syslog( Logger::Facility::User );
76     Logger::PartLogger log(__func__);
77
78     Logger::set_log_level( 7 );
79     Logger::enable_stderr_log();
80     Logger::enable_log_file("zzUnitTest.log");
81
82     log.error("Test error msg");
83     log.error() << "Stream test error msg #" << 2 << ".";
84     log.warning() << "Stream test warning msg";
85     log.notice() << "Stream test notice msg";
86     log.info() << "Stream test info msg";
87     log.debug() << "Stream test debug msg";
88
89     log.debug()
90         << "multiline log message" << std::endl
91         << "this (second) line should be indented" << std::endl
92         << "and this also!" << std::endl;
93     log.debug(HERE) << "This should have a source info";
94
95     module_logger.debug(HERE) << "module level debug message with source loc info";
96 } // eo Syslog1()
97
98
99
100 BOOST_AUTO_TEST_CASE(TestScopeTrace1)
101 {
102     //Logger::enable_syslog("I2N Unittest", Logger::Facility::User );
103     Logger::enable_syslog( Logger::Facility::User );
104     Logger::PartLogger log(__func__);
105
106     Logger::set_log_level( 7 );
107     Logger::enable_stderr_log();
108     Logger::enable_log_file("zzUnitTest.log");
109
110     SCOPETRACKER();
111
112     log.notice() << "Stream test notice msg";
113     {
114         SCOPETRACKER(); // #2
115         {
116             SCOPETRACKER(); // #3
117             {
118                 SCOPETRACKER(); // #4
119             }
120         }
121         {
122             SCOPETRACKER(); // #3
123         }
124     }
125 } // eo TestScopeTrace1()
126
127 void recursive_function(int level=0)
128 {
129     SCOPETRACKER();
130
131     if (level < 10)
132         recursive_function(++level);
133 }
134
135 BOOST_AUTO_TEST_CASE(TestRecursiveScopeTracker)
136 {
137     Logger::enable_syslog( Logger::Facility::User );
138     Logger::PartLogger log(__func__);
139
140     Logger::set_log_level( 7 );
141     Logger::enable_stderr_log();
142     Logger::enable_log_file("zzUnitTest.log");
143
144     recursive_function();
145 }
146
147 BOOST_AUTO_TEST_SUITE_END()