072e89387f3141f8e65cd3f8bc751c09a9d22ecf
[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 ///////////////////////////////hh.p////////////////////////////////////////
38
39 /** \brief Write log message
40
41     This macro will write it's last argument to the log stream. The last argument must be an
42     expression which will be placed after a streaming \c operator<< (like
43     <i>some-log-sttream</i> \c << <i>last-macro-arg</i>).
44     \code
45     SENF_LOG((parameters...)("log message " << args << ...));
46     \endcode
47
48     \hideinitializer
49  */
50 #define SENF_LOG(args)                                                                            \
51     SENF_LOG_BLOCK_( SENF_LOG_MERGE_PARAMETERS(BOOST_PP_SEQ_POP_BACK(args)),                      \
52                      { log << BOOST_PP_SEQ_ELEM(BOOST_PP_DEC(BOOST_PP_SEQ_SIZE(args)),args); })
53
54 #define SENF_LOG_TPL(args)                                                                        \
55     SENF_LOG_BLOCK_( SENF_LOG_MERGE_PARAMETERS_TPL(BOOST_PP_SEQ_POP_BACK(args)),                  \
56                      { log << BOOST_PP_SEQ_ELEM(BOOST_PP_DEC(BOOST_PP_SEQ_SIZE(args)),args); })
57
58 /** \brief Enable block based on logging parameters
59
60     This macro is like SENF_LOG, however instead of writing a simple message, this macro allows
61     to specify a complete block of code to be executed if the log message is enabled.
62     \code
63     SENF_LOG_BLOCK((parameters...)({
64        // arbitrary code using 'log' for logging
65        log << "log message";
66     }));
67     \endcode
68
69     \hideinitializer
70  */
71 #define SENF_LOG_BLOCK(args)                                                                      \
72     SENF_LOG_BLOCK_( SENF_LOG_MERGE_PARAMETERS(BOOST_PP_SEQ_POP_BACK(args)),                      \
73                      BOOST_PP_SEQ_ELEM(BOOST_PP_DEC(BOOST_PP_SEQ_SIZE(args)),args))
74
75 #define SENF_LOG_BLOCK_TPL(args)                                                                  \
76     SENF_LOG_BLOCK_( SENF_LOG_MERGE_PARAMETERS_TPL(BOOST_PP_SEQ_POP_BACK(args)),                  \
77                      BOOST_PP_SEQ_ELEM(BOOST_PP_DEC(BOOST_PP_SEQ_SIZE(args)),args))
78
79 #define SENF_LOG_BLOCK_(parameters, block)                                                        \
80     do {                                                                                          \
81         if (parameters::compile_enabled && parameters::enabled()) {                               \
82             std::ostream & log (parameters::log_stream());                                         \
83             do block while(0);                                                                    \
84             log << std::endl;                                                                     \
85         }                                                                                         \
86     } while(0) 
87
88 ///////////////////////////////hh.e////////////////////////////////////////
89 //#include "Log.cci"
90 //#include "Log.ct"
91 //#include "Log.cti"
92 #endif
93
94 \f
95 // Local Variables:
96 // mode: c++
97 // fill-column: 100
98 // comment-column: 40
99 // c-file-style: "senf"
100 // indent-tabs-mode: nil
101 // ispell-local-dictionary: "american"
102 // compile-command: "scons -u test"
103 // End: