Make ScopeTracker thread safe via TLS (thread local storage)
[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>
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
b6fb82a2
RP
46using namespace I2n;
47
48namespace {
49
50Logger::PartLogger module_logger(HERE);
51
52} // eo namespace <anonymous>
53
9fe0853b
TJ
54class TestLoggingFixture
55{
56public:
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
70BOOST_FIXTURE_TEST_SUITE(TestLogging, TestLoggingFixture)
71
72BOOST_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
b6fb82a2
RP
98
99
9fe0853b 100BOOST_AUTO_TEST_CASE(TestScopeTrace1)
b6fb82a2 101{
9fe0853b
TJ
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
b6fb82a2 115 {
9fe0853b 116 SCOPETRACKER(); // #3
b6fb82a2 117 {
9fe0853b 118 SCOPETRACKER(); // #4
b6fb82a2 119 }
9fe0853b
TJ
120 }
121 {
122 SCOPETRACKER(); // #3
123 }
124 }
125} // eo TestScopeTrace1()
b6fb82a2 126
f4fcef65
TJ
127void recursive_function(int level=0)
128{
129 SCOPETRACKER();
130
131 if (level < 10)
132 recursive_function(++level);
133}
134
135BOOST_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
9fe0853b 147BOOST_AUTO_TEST_SUITE_END()