Merge branch 'daemon-ext'
[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 #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
47 using namespace I2n;
48
49 namespace {
50
51 Logger::PartLogger module_logger(HERE);
52
53 } // eo namespace <anonymous>
54
55 class TestLoggingFixture
56 {
57 public:
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
77 BOOST_FIXTURE_TEST_SUITE(TestLogging, TestLoggingFixture)
78
79 BOOST_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
107 BOOST_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
134 void recursive_function(int level=0)
135 {
136     SCOPETRACKER();
137
138     if (level < 10)
139         recursive_function(++level);
140 }
141
142 BOOST_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
154 BOOST_AUTO_TEST_SUITE_END()