a45ba466f1ce18bc7bac9e774c91693abddd4c29
[libi2ncommon] / test / test_logging.cpp
1 /** @file
2  * test cases for loggin module.
3  *
4  * (c) Copyright 2007 by Intra2net AG
5  * 
6  * info@intra2net.com
7  */
8
9 //#define NOISEDEBUG
10
11 #include <string>
12 #include <iostream>
13 #include <iomanip>
14 #include <exception>
15
16 #define BOOST_TEST_DYN_LINK
17 #include <boost/test/unit_test.hpp>
18
19 #include <logfunc.hpp>
20 #include <tracefunc.hpp>
21
22
23 #ifdef NOISEDEBUG
24 #define DOUT(msg) std::cout << msg << std::endl
25 #else
26 #define DOUT(msg) do {} while (0)
27 #endif
28
29 using namespace I2n;
30
31 namespace {
32
33 Logger::PartLogger module_logger(HERE);
34
35 } // eo namespace <anonymous>
36
37 class TestLoggingFixture
38 {
39 public:
40     TestLoggingFixture()
41     {
42     }
43
44     ~TestLoggingFixture()
45     {
46         Logger::set_log_level(0);
47         Logger::enable_syslog(false);
48         Logger::enable_stderr_log(false);
49         Logger::enable_log_file(false);
50     }
51 };
52
53 BOOST_FIXTURE_TEST_SUITE(TestLogging, TestLoggingFixture)
54
55 BOOST_AUTO_TEST_CASE(Syslog1)
56 {
57     //Logger::enable_syslog("I2N Unittest", Logger::Facility::User );
58     Logger::enable_syslog( Logger::Facility::User );
59     Logger::PartLogger log(__func__);
60
61     Logger::set_log_level( 7 );
62     Logger::enable_stderr_log();
63     Logger::enable_log_file("zzUnitTest.log");
64
65     log.error("Test error msg");
66     log.error() << "Stream test error msg #" << 2 << ".";
67     log.warning() << "Stream test warning msg";
68     log.notice() << "Stream test notice msg";
69     log.info() << "Stream test info msg";
70     log.debug() << "Stream test debug msg";
71
72     log.debug()
73         << "multiline log message" << std::endl
74         << "this (second) line should be indented" << std::endl
75         << "and this also!" << std::endl;
76     log.debug(HERE) << "This should have a source info";
77
78     module_logger.debug(HERE) << "module level debug message with source loc info";
79 } // eo Syslog1()
80
81
82
83 BOOST_AUTO_TEST_CASE(TestScopeTrace1)
84 {
85     //Logger::enable_syslog("I2N Unittest", Logger::Facility::User );
86     Logger::enable_syslog( Logger::Facility::User );
87     Logger::PartLogger log(__func__);
88
89     Logger::set_log_level( 7 );
90     Logger::enable_stderr_log();
91     Logger::enable_log_file("zzUnitTest.log");
92
93     SCOPETRACKER();
94
95     log.notice() << "Stream test notice msg";
96     {
97         SCOPETRACKER(); // #2
98         {
99             SCOPETRACKER(); // #3
100             {
101                 SCOPETRACKER(); // #4
102             }
103         }
104         {
105             SCOPETRACKER(); // #3
106         }
107     }
108 } // eo TestScopeTrace1()
109
110 BOOST_AUTO_TEST_SUITE_END()