modm API documentation
logger/logger.hpp
1 /*
2  * Copyright (c) 2009-2011, Fabian Greif
3  * Copyright (c) 2009-2010, 2013, Martin Rosekeit
4  * Copyright (c) 2010, 2013, Georgi Grinshpun
5  * Copyright (c) 2012, 2016, Niklas Hauser
6  * Copyright (c) 2013, 2015, Sascha Schade
7  * Copyright (c) 2015, Kevin Läufer
8  *
9  * This file is part of the modm project.
10  *
11  * This Source Code Form is subject to the terms of the Mozilla Public
12  * License, v. 2.0. If a copy of the MPL was not distributed with this
13  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
14  */
15 // ----------------------------------------------------------------------------
16 
17 #ifndef MODM_LOGGER_HPP
18 #define MODM_LOGGER_HPP
19 
20 #include <modm/architecture/utils.hpp>
21 #include <modm/io/iostream.hpp>
22 
23 #include "level.hpp"
24 #include "style.hpp"
25 #include "style_wrapper.hpp"
26 #include "style/prefix.hpp"
27 
28 namespace modm
29 {
30  /**
31  * \ingroup modm_debug
32  * \brief Logger
33  */
34  namespace log
35  {
36  /**
37  * \brief Interface to the Logger.
38  *
39  * It is used by the macros defined below. This class overloads
40  * the shift-operator so that it is possible to write different
41  * message types to the logger.
42  *
43  * \ingroup modm_debug
44  * \author Martin Rosekeit <martin.rosekeit@rwth-aachen.de>
45  */
46  class Logger : public ::modm::IOStream
47  {
48  public:
49  inline Logger(::modm::IODevice& outputDevice) :
50  IOStream(outputDevice)
51  {
52  }
53 
54  private:
55  Logger(const Logger&);
56 
57  Logger&
58  operator = (const Logger&);
59  };
60 
61  /**
62  * \name Output streams
63  *
64  * Don't use this instances directly! Prefer an access through the
65  * MODM_LOG_DEBUG, MODM_LOG_INFO, MODM_LOG_WARNING and
66  * MODM_LOG_ERROR macros.
67  *
68  * \ingroup modm_debug
69  */
70  //\{
71  extern Logger debug; ///< log device to take messages on DEBUG level
72  extern Logger info; ///< log device to take messages on INFO level
73  extern Logger warning; ///< log device to take messages on WARNING level
74  extern Logger error; ///< log device to take messages on ERROR level
75  //\}
76  }
77 }
78 
79 // these macros are defined like this to avoid the dangling else problem.
80 // if (condition)
81 // MODM_LOG_DEBUG << "string";
82 // else
83 // expression;
84 
85 /**
86  * \brief Turn off messages print
87  * \ingroup modm_debug
88  */
89 #define MODM_LOG_OFF
90  if ( true ){}
91  else modm::log::debug
92 
93 /**
94  * \brief Output stream for debug messages
95  * \ingroup modm_debug
96  */
97 #define MODM_LOG_DEBUG
98  if (MODM_LOG_LEVEL > modm::log::DEBUG){}
99  else modm::log::debug
100 
101 /**
102  * \brief Output stream for info messages
103  * \ingroup modm_debug
104  */
105 #define MODM_LOG_INFO
106  if (MODM_LOG_LEVEL > modm::log::INFO){}
107  else modm::log::info
108 
109 /**
110  * \brief Output stream for warnings
111  * \ingroup modm_debug
112  */
113 #define MODM_LOG_WARNING
114  if (MODM_LOG_LEVEL > modm::log::WARNING){}
115  else modm::log::warning
116 
117 /**
118  * \brief Output stream for error messages
119  * \ingroup modm_debug
120  */
121 #define MODM_LOG_ERROR
122  if (MODM_LOG_LEVEL > modm::log::ERROR){}
123  else modm::log::error
124 
125 #ifdef __DOXYGEN__
126 
127 /**
128  * \brief Filename of the current file
129  *
130  * In contrast to \c __FILE__ the path to the file is omitted (if this feature
131  * is available, otherwise this macro will resolve to \c __FILE__).
132  *
133  * \ingroup modm_debug
134  */
135 #define FILENAME
136 
137 /**
138  * \brief String containing the filename and the current line
139  *
140  * Will generate something like:
141  * \code
142  * "file.cpp (123) >> "
143  * \endcode
144  *
145  * This can be very useful the track the origin of log-messages:
146  * \code
147  * MODM_LOG_DEBUG << MODM_FILE_INFO
148  * << "... something has happened ..."
149  * << modm::endl;
150  * \endcode
151  *
152  * This will result into:
153  * \code
154  * file.cpp (123) >> ... something has happened ...
155  * \endcode
156  *
157  * \ingroup modm_debug
158  */
159 #define MODM_FILE_INFO
160 
161 #else // !__DOXYGEN__
162 
163 #ifndef BASENAME
164 # ifndef __BASE_FILE__
165 # define FILENAME __FILE__
166 # else
167 # define FILENAME __BASE_FILE__
168 # endif
169 #else
170 # define FILENAME MODM_STRINGIFY(BASENAME)
171 #endif
172 
173 #define MODM_FILE_INFO "[" FILENAME "(" MODM_STRINGIFY(__LINE__) ")] "
174 
175 #endif // __DOXYGEN__
176 #endif // MODM_LOGGER_HPP
Logger error
log device to take messages on ERROR level
Logger warning
log device to take messages on WARNING level
Logger info
log device to take messages on INFO level
Logger debug
log device to take messages on DEBUG level
IOStream(IODevice &odevice)
Definition: iostream.hpp:50
Definition: modm/src/modm/io/iodevice.hpp:26
Definition: iostream.hpp:39
Interface to the Logger.
Definition: logger/logger.hpp:46