2 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
3 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
4 // Stefan Bund <g0dil@berlios.de>
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the
18 // Free Software Foundation, Inc.,
19 // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 \brief Logger public header */
24 /** \defgroup logger The SENF Logger
26 The Loggger infrastructure shall implement a highly flexible compile- and run-time configurable
27 logging infrastructure supporting multiple streams, user definable log areas and fine grained
28 log levels. Logging can be configured at compile and runtime on any combination of above
29 parameters. The library supports a host of log targets and messages can be routed into multiple
30 targets at the same time. To allow concise usage of the library, a utility to define logging
31 defaults for any scope is provided.
33 An important basic concept of the library is, that most of the macros take a variable number of
34 arguments. Since this is not supported in the needed manner by the C++ preprocessor, the
35 arguments are encoded into a <a
36 href="http://www.boost.org/libs/preprocessor/doc/index.html">Boost.Preprocessor</a> like
40 SENF_LOG( (senf::log::Debug)(senf::log::NOTICE)(FroblizerArea)("The log message") );
43 The last sequence element always is the log message. Before that we have a number of log
44 parameters <i>in arbitrary order</i>. Since giving all the parameters in every log message is
45 to verbose, there are two helpful constructs to reduce the verbosity. Using \ref SENF_LOG_DEFAULTS it
46 is possible to define the default logging parameters to be used within a given scope. Using
47 \ref SENF_LOG_DEF_ALIAS you can define an alias (which is a scoped symbol) as an arbitrary
48 combination of parameters.
51 SENF_LOG_DEF_STREAM(userLog);
55 // Define a new log area
56 SENF_LOG_DEF_AREA(FroblizerArea);
58 // Set default log parameters for this scope
59 SENF_LOG_DEFAULTS( (senf::log::Debug) (senf::log::NOTICE) (FroblizerArea) );
61 // Define an alias for emergency messages to the sysadmin.
62 // The log area is inherited from the default at the place, where this
63 // alias is used *not* where it is defined
64 SENF_LOG_DEF_ALIAS(LogEmerg, (userLog) (senf::log::CRITICAL));
72 void Froblizer::froblize()
74 SENF_LOG(("This is the Debug stream at level NOTICE in the FroblizeArea"));
75 SENF_LOG((senf::log::WARNING) ("Same stream and area but at warning level"));
76 SENF_LOG((LogEmerg) ("This goes to the userLog at level CRITICAL in the FroblizerArea"));
79 void Froblizer::test()
81 // Change the default log level for this method. stream and area are taken
82 // from the next scope up
83 SENF_LOG_DEFAULTS((senf::log::DEBUG));
85 SENF_LOG(("Log to Debug stream in Froblizer area however at DEBUG level"));
89 Currently, the library is not implemented in any way. The interface has been defined up to a
90 point and we have dummy implementations of the 'in-code' part of the interface. This is the
91 part, which is called throughout the code. The configuration API is defined but we don't even
92 have a template implementation. However, this allows starting to use the SENF Logger in newly
93 developed code. Even though this code will unconditionally log everything to \c std::cerr for
94 now and errors in the parameter specification will not be caught (since they are just ignored)
95 the logging should work automatically as advertised as soon as the logger is completely
98 \implementation I would have much preferred a more C++ like implementation. However given the
100 \li Flexible configuration at compile and runtime
101 \li Concise usage and simple interface
102 \li Zero overhead for compile-time disabled log messages
104 I did not find any implementation which was not either completely convoluted, unusable or
105 slow. So I turned to a macro based implementation which can provide all the design goals stated
114 #include <boost/preprocessor/seq/size.hpp>
115 #include <boost/preprocessor/dec.hpp>
116 #include <boost/preprocessor/seq/elem.hpp>
118 //#include "Logger.mpp"
119 ///////////////////////////////hh.p////////////////////////////////////////
123 # ifndef _senf_LOG_STREAM
124 # define _senf_LOG_STREAM std::cerr
127 /// \addtogroup logger
130 /** \brief Write log message
132 This macro will write it's last argument to the log stream. The last argument must be an
133 expression which will be placed after a streaming \c operator<< (like
134 <i>some-log-sttream</i> \c << <i>last-macro-arg</i>).
136 SENF_LOG((parameters...)("log message " << args << ...));
141 # define SENF_LOG(args) \
142 _senf_LOG_STREAM << BOOST_PP_SEQ_ELEM(BOOST_PP_DEC(BOOST_PP_SEQ_SIZE(args)),args) \
145 /** \brief Enable block based on logging parameters
147 This macro is like SENF_LOG, however instead of writing a simple message, this macro allows
148 to specify a complete block of code to be executed if the log message is enabled.
150 SENF_LOG_BLOCK((parameters...)({
151 // arbitrary code using 'log' for logging
152 log << "log message";
158 # define SENF_LOG_BLOCK(args) \
160 std::ostream & log (_senf_LOG_STREAM); \
161 BOOST_PP_SEQ_ELEM(BOOST_PP_DEC(BOOST_PP_SEQ_SIZE(args)),args) \
165 /** \brief Set scope default log parameters
167 Sets the default log parameters for the current scope
169 SENF_LOG_DEFAULTS((parameters...));
174 # define SENF_LOG_DEFAULTS(args)
176 /** \brief Define log area
178 Defines a new log area named \a area. The area is defined as a symbol in the current scope.
182 # define SENF_LOG_DEF_AREA(area)
184 /** \brief Define log stream
186 Defines a new log stream named \a stream. The stream is defined as a symbol in the current
191 # define SENF_LOG_DEF_STREAM(stream)
193 /** \brief Define log parameter alias
195 Defines a new parameter alias named \a alias as an alias for the parameters in \a args. The
196 alias is defined as a symbol in the current scope.
200 # define SENF_LOG_DEF_ALIAS(alias,args)
206 ///////////////////////////////hh.e////////////////////////////////////////
207 //#include "Logger.cci"
208 //#include "Logger.ct"
209 //#include "Logger.cti"
216 // c-file-style: "senf"
217 // indent-tabs-mode: nil
218 // ispell-local-dictionary: "american"
219 // compile-command: "scons -u test"
220 // comment-column: 40