some documentation updates
[senf.git] / Utils / Logger.hh
1 // $Id$
2 //
3 // Copyright (C) 2007
4 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
5 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
6 //     Stefan Bund <g0dil@berlios.de>
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
23 /** \file
24     \brief Logger public header */
25
26 /** \defgroup logger The SENF Logger
27
28     The Loggger infrastructure shall implement a highly flexible compile- and run-time configurable
29     logging infrastructure supporting multiple streams, user definable log areas and fine grained
30     log levels. Logging can be configured at compile and runtime on any combination of above
31     parameters. The library supports a host of log targets and messages can be routed into multiple
32     targets at the same time. To allow concise usage of the library, a utility to define logging
33     defaults for any scope is provided.
34
35     An important basic concept of the library is, that most of the macros take a variable number of
36     arguments. Since this is not supported in the needed manner by the C++ preprocessor, the
37     arguments are encoded into a <a
38     href="http://www.boost.org/libs/preprocessor/doc/index.html">Boost.Preprocessor</a> like
39     sequence:
40
41     \code
42     SENF_LOG( (senf::log::Debug)(senf::log::NOTICE)(FroblizerArea)("The log message") );
43     \endcode
44
45     The last sequence element always is the log message. Before that we have a number of log
46     parameters <i>in arbitrary order</i>. Since giving all the parameters in every log message is
47     to verbose, there are two helpful constructs to reduce the verbosity. Using \ref SENF_LOG_DEFAULTS it
48     is possible to define the default logging parameters to be used within a given scope. Using
49     \ref SENF_LOG_DEF_ALIAS you can define an alias (which is a scoped symbol) as an arbitrary
50     combination of parameters.
51
52     \code
53     SENF_LOG_DEF_STREAM(userLog);
54
55     class Froblizer
56     {
57         // Define a new log area
58         SENF_LOG_DEF_AREA(FroblizerArea);
59
60         // Set default log parameters for this scope
61         SENF_LOG_DEFAULTS( (senf::log::Debug) (senf::log::NOTICE) (FroblizerArea) );
62
63         // Define an alias for emergency messages to the sysadmin.
64         // The log area is inherited from the default at the place, where this
65         // alias is used *not* where it is defined
66         SENF_LOG_DEF_ALIAS(LogEmerg, (userLog) (senf::log::CRITICAL));
67
68         void test();
69
70     public:
71         void froblize();
72     };
73
74     void Froblizer::froblize()
75     {
76         SENF_LOG(("This is the Debug stream at level NOTICE in the FroblizeArea"));
77         SENF_LOG((senf::log::WARNING) ("Same stream and area but at warning level"));
78         SENF_LOG((LogEmerg) ("This goes to the userLog at level CRITICAL in the FroblizerArea"));
79     }
80
81     void Froblizer::test()
82     {
83         // Change the default log level for this method. stream and area are taken
84         // from the next scope up
85         SENF_LOG_DEFAULTS((senf::log::DEBUG));
86
87         SENF_LOG(("Log to Debug stream in Froblizer area however at DEBUG level"));
88     }
89     \endcode
90
91     Currently, the library is not implemented in any way. The interface has been defined up to a
92     point and we have dummy implementations of the 'in-code' part of the interface. This is the
93     part, which is called throughout the code. The configuration API is defined but we don't even
94     have a template implementation. However, this allows starting to use the SENF Logger in newly
95     developed code. Even though this code will unconditionally log everything to \c std::cerr for
96     now and errors in the parameter specification will not be caught (since they are just ignored)
97     the logging should work automatically as advertised as soon as the logger is completely
98     implemented.
99
100     \implementation I would have much preferred a more C++ like implementation. However given the
101     design goals
102     \li Flexible configuration at compile and runtime
103     \li Concise usage and simple interface
104     \li Zero overhead for compile-time disabled log messages
105
106     I did not find any implementation which was not either completely convoluted, unusable or
107     slow. So I turned to a macro based implementation which can provide all the design goals stated
108     above.
109  */
110
111 #ifndef HH_Logger_
112 #define HH_Logger_ 1
113
114 // Custom includes
115 #include <iostream>
116 #include <boost/preprocessor/seq/size.hpp>
117 #include <boost/preprocessor/dec.hpp>
118 #include <boost/preprocessor/seq/elem.hpp>
119
120 //#include "Logger.mpp"
121 ///////////////////////////////hh.p////////////////////////////////////////
122
123 namespace senf {
124
125 #   ifndef _senf_LOG_STREAM
126 #     define _senf_LOG_STREAM std::cerr
127 #   endif
128
129     /// \addtogroup logger
130     /// @{
131
132     /** \brief Write log message
133
134         This macro will write it's last argument to the log stream. The last argument must be an
135         expression which will be placed after a streaming \c operator<< (like
136         <i>some-log-sttream</i> \c << <i>last-macro-arg</i>).
137         \code
138         SENF_LOG((parameters...)("log message " << args << ...));
139         \endcode
140
141         \hideinitializer
142      */
143 #   define SENF_LOG(args)                                                                       \
144         _senf_LOG_STREAM << BOOST_PP_SEQ_ELEM(BOOST_PP_DEC(BOOST_PP_SEQ_SIZE(args)),args)       \
145                          << std::endl;
146
147     /** \brief Enable block based on logging parameters
148
149         This macro is like SENF_LOG, however instead of writing a simple message, this macro allows
150         to specify a complete block of code to be executed if the log message is enabled.
151         \code
152         SENF_LOG_BLOCK((parameters...)({
153            // arbitrary code using 'log' for logging
154            log << "log message";
155         }));
156         \endcode
157
158         \hideinitializer
159      */
160 #   define SENF_LOG_BLOCK(args)                                                 \
161         do {                                                                    \
162             std::ostream & log (_senf_LOG_STREAM);                              \
163             BOOST_PP_SEQ_ELEM(BOOST_PP_DEC(BOOST_PP_SEQ_SIZE(args)),args)       \
164             log << std::endl;                                                   \
165         } while (0)
166
167     /** \brief Set scope default log parameters
168
169         Sets the default log parameters for the current scope
170         \code
171         SENF_LOG_DEFAULTS((parameters...));
172         \endcode
173
174         \hideinitializer
175      */
176 #   define SENF_LOG_DEFAULTS(args)
177
178     /** \brief Define log area
179
180         Defines a new log area named \a area. The area is defined as a symbol in the current scope.
181
182         \hideinitializer
183      */
184 #   define SENF_LOG_DEF_AREA(area)
185
186     /** \brief Define log stream
187
188         Defines a new log stream named \a stream. The stream is defined as a symbol in the current
189         scope.
190
191         \hideinitializer
192      */
193 #   define SENF_LOG_DEF_STREAM(stream)
194
195     /** \brief Define log parameter alias
196
197         Defines a new parameter alias named \a alias as an alias for the parameters in \a args. The
198         alias is defined as a symbol in the current scope.
199
200         \hideinitializer
201      */
202 #   define SENF_LOG_DEF_ALIAS(alias,args)
203
204     /// @}
205
206 }
207
208 ///////////////////////////////hh.e////////////////////////////////////////
209 //#include "Logger.cci"
210 //#include "Logger.ct"
211 //#include "Logger.cti"
212 #endif
213
214 \f
215 // Local Variables:
216 // mode: c++
217 // fill-column: 100
218 // c-file-style: "senf"
219 // indent-tabs-mode: nil
220 // ispell-local-dictionary: "american"
221 // compile-command: "scons -u test"
222 // comment-column: 40
223 // End: