-
Notifications
You must be signed in to change notification settings - Fork 38
/
tri_logger.hpp
137 lines (114 loc) · 4.15 KB
/
tri_logger.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
//
// Copyright (c) 2005 - 2010
// Seweryn Habdank-Wojewodzki
//
// Distributed under the Boost Software License, Version 1.0.
// ( copy at http://www.boost.org/LICENSE_1_0.txt )
#ifndef TRI_LOGGER_HPP_INCLUDED
#define TRI_LOGGER_HPP_INCLUDED 1
#include <ostream>
#include <memory>
namespace trivial_logger
{
namespace detail
{
class tri_logger_impl;
typedef char char_type;
}
// main trivial logger class
class tri_logger_t
{
public:
tri_logger_t ();
~tri_logger_t ();
/// return true if logger is activated
static bool is_activated();
/// activate and deactivate logger
static void activate ( bool const activated );
/// return reference to pointer to output stream
static ::std::ostream *& ostream_ptr();
private:
static std::auto_ptr<detail::tri_logger_impl> tl_impl;
// in this design logger should be noncopyable
tri_logger_t ( tri_logger_t const & );
tri_logger_t & operator= ( tri_logger_t const & );
};
// important funtion which helps solves
// "static initialisation fiasco" problem
// see:
// 1. S. Habdank-Wojewodzki, "C++ Trivial Logger", Overload 77, Feb 2007, pp.19-23
// 2. http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.13
// present solution is much better
extern tri_logger_t & tri_logger();
namespace detail
{
extern ::std::auto_ptr<tri_logger_t> tri_logger_out_ptr;
}
} // namespace trival_logger
// macro prints variable name and its value to the logger stream
#define TRI_LOG(name) \
do { if ( trivial_logger::tri_logger().is_activated() ){\
*trivial_logger::tri_logger().ostream_ptr() << __FILE__ \
<< " [" << __LINE__ << "] : " << #name \
<< " = " << (name) << ::std::endl;} }while(false)
// macro prints value of the variable to the logger stream
// useful for printing constant strings
#define TRI_LOG_STR(str) \
do { if ( trivial_logger::tri_logger().is_activated() ){\
*trivial_logger::tri_logger().ostream_ptr() << __FILE__ \
<< " [" << __LINE__ << "] : " << str \
<< ::std::endl;} }while(false)
// namespace for the trivial logger
namespace trivial_logger
{
// example how to create functions which operates on logger stream
// here are used templates for preparing function which is independent
// on the type, but this is not required
template < typename T1, typename T2, typename T3, typename T4 >
void put_debug_info ( tri_logger_t & log,
T1 const & t1, T2 const & t2, T3 const & t3, T4 const & t4 )
{
if ( log.is_activated() )
{
*(log.ostream_ptr()) << t1 << " (" << t2 << ") : ";
*(log.ostream_ptr()) << t3 << " = " << t4 << ::std::endl;
}
}
template < typename T >
void put_log_info ( tri_logger_t & log,
T const & t )
{
if ( log.is_activated() )
{
*(log.ostream_ptr()) << t << ::std::endl;
}
}
} // namespace trivial_logger
// macro shows how to write macros which using user-defined functions
#define TRI_LOG_FN(name) \
::trivial_logger::put_debug_info ( trivial_logger::tri_logger(),\
__FILE__, __LINE__, #name, (name) )
// below is a place for user defined logger formating data
// ...
#define TRI_LOG_INFO(name) \
::trivial_logger::put_log_info ( trivial_logger::tri_logger(), (name) )
// macros for switching off and on logger
#define TRI_LOG_ON() \
do { ::trivial_logger::tri_logger().activate ( true ); } while(false)
#define TRI_LOG_OFF() \
do { ::trivial_logger::tri_logger().activate ( false ); } while(false)
#if defined(CLEANTLOG)
#undef TRI_LOG
#undef TRI_LOG_ON
#undef TRI_LOG_OFF
#undef TRI_LOG_FN
#undef TRI_LOG_STR
#undef TRI_LOG_INFO
#define TRI_LOG(name) do{}while(false)
#define TRI_LOG_FN(name) do{}while(false)
#define TRI_LOG_ON() do{}while(false)
#define TRI_LOG_OFF() do{}while(false)
#define TRI_LOG_STR(str) do{}while(false)
#define TRI_LOG_INFO(str) do{}while(false)
#endif
#endif // TRI_LOGGER_HPP_INCLUDED