4 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
5 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
6 // Stefan Bund <g0dil@berlios.de>
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.
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.
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.
24 \brief Logger public header */
26 /** \defgroup logger The SENF Logger
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.
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
42 SENF_LOG( (senf::log::Debug)(senf::log::NOTICE)(FroblizerArea)("The log message") );
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.
53 SENF_LOG_DEF_STREAM(userLog);
57 // Define a new log area
58 SENF_LOG_DEF_AREA(FroblizerArea);
60 // Set default log parameters for this scope
61 SENF_LOG_DEFAULTS( (senf::log::Debug) (senf::log::NOTICE) (FroblizerArea) );
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));
74 void Froblizer::froblize()
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"));
81 void Froblizer::test()
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));
87 SENF_LOG(("Log to Debug stream in Froblizer area however at DEBUG level"));
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
100 \implementation I would have much preferred a more C++ like implementation. However given the
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
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
116 #include <boost/preprocessor/seq/size.hpp>
117 #include <boost/preprocessor/dec.hpp>
118 #include <boost/preprocessor/seq/elem.hpp>
120 //#include "Logger.mpp"
121 ///////////////////////////////hh.p////////////////////////////////////////
125 # ifndef _senf_LOG_STREAM
126 # define _senf_LOG_STREAM std::cerr
129 /// \addtogroup logger
132 /** \brief Write log message
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>).
138 SENF_LOG((parameters...)("log message " << args << ...));
143 # define SENF_LOG(args) \
144 _senf_LOG_STREAM << BOOST_PP_SEQ_ELEM(BOOST_PP_DEC(BOOST_PP_SEQ_SIZE(args)),args) \
147 /** \brief Enable block based on logging parameters
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.
152 SENF_LOG_BLOCK((parameters...)({
153 // arbitrary code using 'log' for logging
154 log << "log message";
160 # define SENF_LOG_BLOCK(args) \
162 std::ostream & log (_senf_LOG_STREAM); \
163 BOOST_PP_SEQ_ELEM(BOOST_PP_DEC(BOOST_PP_SEQ_SIZE(args)),args) \
167 /** \brief Set scope default log parameters
169 Sets the default log parameters for the current scope
171 SENF_LOG_DEFAULTS((parameters...));
176 # define SENF_LOG_DEFAULTS(args)
178 /** \brief Define log area
180 Defines a new log area named \a area. The area is defined as a symbol in the current scope.
184 # define SENF_LOG_DEF_AREA(area)
186 /** \brief Define log stream
188 Defines a new log stream named \a stream. The stream is defined as a symbol in the current
193 # define SENF_LOG_DEF_STREAM(stream)
195 /** \brief Define log parameter alias
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.
202 # define SENF_LOG_DEF_ALIAS(alias,args)
208 ///////////////////////////////hh.e////////////////////////////////////////
209 //#include "Logger.cci"
210 //#include "Logger.ct"
211 //#include "Logger.cti"
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