Utils/Logger: Reorganize source code
[senf.git] / Utils / Logger / Log.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 Log public header */
25
26 #ifndef HH_Log_
27 #define HH_Log_ 1
28
29 // Custom includes
30 #include <boost/preprocessor/seq/size.hpp>
31 #include <boost/preprocessor/dec.hpp>
32 #include <boost/preprocessor/seq/elem.hpp>
33 #include <boost/preprocessor/seq/pop_back.hpp>
34 #include "Parameters.hh"
35
36 //#include "Log.mpp"
37 #include "Log.ih"
38 ///////////////////////////////hh.p////////////////////////////////////////
39
40 /** \defgroup logging Logging commands
41
42     The logging library provides several commands to create log messages. All these macro commands
43     take a variable number of arguments. Since this is not supported in a usable way by the C++
44     preprocessor, the arguments are encoded into a <a
45     href="http://www.boost.org/libs/preprocessor/doc/index.html">Boost.Preprocessor</a> like
46     sequence:
47
48     \code
49     SENF_LOG( (senf::log::Debug)(senf::log::NOTICE)(FroblizerArea)("The log message") );
50     \endcode
51
52     For each log message, the following information is needed:
53     \li The <em>log stream</em>,
54     \li the <em>log area</em>,
55     \li the <em>log level</em>,
56     \li and the log message itself
57     
58     These parameters may be specified <i>in arbitrary order</i> and even multiple times in the
59     parameter sequence. If some argument type occurs multiple times, the last occurrence wins. If
60     any one of the parameters is not specified, it's current default value will be used.
61
62     This current default value is set using \ref SENF_LOG_DEFAULT_STREAM, \ref SENF_LOG_DEFAULT_AREA
63     and \ref SENF_LOG_DEFAULT_LEVEL respectively. These macros set the default stream, area and/or
64     level of the current scope. The logging library defines the global defaults for these values to
65     be \c senf::log::Debug (\e stream), senf::log::DefaultArea (\e area), and senf::log::NONE (\e
66     level).
67
68     There is one special log level, senf::log::NONE. If the log level is set to this value, the log
69     level will be set from the stream provided default value.
70     
71
72     All these parameters must be <em>compile time constants</em> (they are all types, so it's
73     difficult form them to be something else).
74  */
75
76 ///\ingroup logging
77 ///\{
78
79 ///\name Generating log messages
80 ///\{
81
82 /** \brief Write log message
83
84     This macro will write it's last argument to the log stream. The last argument must be an
85     expression which will be placed after a streaming \c operator<< (like
86     <i>some-log-stream</i> \c << <i>last-macro-arg</i>).
87     \code
88     SENF_LOG((parameters...)("log message " << args << ...));
89     \endcode
90
91     \hideinitializer
92  */
93 #define SENF_LOG(args)                                                                            \
94     SENF_LOG_BLOCK_( SENF_LOG_MERGE_PARAMETERS(BOOST_PP_SEQ_POP_BACK(args)),                      \
95                      { log << BOOST_PP_SEQ_ELEM(BOOST_PP_DEC(BOOST_PP_SEQ_SIZE(args)),args); })
96
97 /** \brief Write log message (template context)
98
99     This macro is used like \ref SENF_LOG() if called from a template context
100
101     \hideinitializer
102  */
103 #define SENF_LOG_TPL(args)                                                                        \
104     SENF_LOG_BLOCK_( SENF_LOG_MERGE_PARAMETERS_TPL(BOOST_PP_SEQ_POP_BACK(args)),                  \
105                      { log << BOOST_PP_SEQ_ELEM(BOOST_PP_DEC(BOOST_PP_SEQ_SIZE(args)),args); })
106
107 /** \brief Enable block based on logging parameters
108
109     This macro is like SENF_LOG, however instead of writing a simple message, this macro allows
110     to specify a complete block of code to be executed if the log message is enabled.
111     \code
112     SENF_LOG_BLOCK((parameters...)({
113        // arbitrary code using 'log' for logging
114        log << "log message";
115     }));
116     \endcode
117
118     \hideinitializer
119  */
120 #define SENF_LOG_BLOCK(args)                                                                      \
121     SENF_LOG_BLOCK_( SENF_LOG_MERGE_PARAMETERS(BOOST_PP_SEQ_POP_BACK(args)),                      \
122                      BOOST_PP_SEQ_ELEM(BOOST_PP_DEC(BOOST_PP_SEQ_SIZE(args)),args))
123
124 /** \brief Write log message (template context)
125
126     This macro is used like \ref SENF_LOG_BLOCK() if called from a template context
127
128     \hideinitializer
129  */
130 #define SENF_LOG_BLOCK_TPL(args)                                                                  \
131     SENF_LOG_BLOCK_( SENF_LOG_MERGE_PARAMETERS_TPL(BOOST_PP_SEQ_POP_BACK(args)),                  \
132                      BOOST_PP_SEQ_ELEM(BOOST_PP_DEC(BOOST_PP_SEQ_SIZE(args)),args))
133
134 ///\}
135 ///\}
136
137 ///////////////////////////////hh.e////////////////////////////////////////
138 //#include "Log.cci"
139 //#include "Log.ct"
140 //#include "Log.cti"
141 #endif
142
143 \f
144 // Local Variables:
145 // mode: c++
146 // fill-column: 100
147 // comment-column: 40
148 // c-file-style: "senf"
149 // indent-tabs-mode: nil
150 // ispell-local-dictionary: "american"
151 // compile-command: "scons -u test"
152 // End: