source: rrlib_logging/messages.h @ 103:b2191850c420

Last change on this file since 103:b2191850c420 was 103:b2191850c420, checked in by Tobias Föhst <foehst@…>, 7 years ago

Added RRLIB_LOG_THROW macros

File size: 12.4 KB
Line 
1//
2// You received this file as part of RRLib
3// Robotics Research Library
4//
5// Copyright (C) Finroc GbR (finroc.org)
6//
7// This program is free software; you can redistribute it and/or
8// modify it under the terms of the GNU General Public License
9// as published by the Free Software Foundation; either version 2
10// of the License, or (at your option) any later version.
11//
12// This program is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15// GNU General Public License for more details.
16//
17// You should have received a copy of the GNU General Public License
18// along with this program; if not, write to the Free Software
19// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20//
21//----------------------------------------------------------------------
22/*!\file    rrlib/logging/messages.h
23 *
24 * \author  Tobias Foehst
25 *
26 * \date    2010-06-16
27 *
28 * \brief   Contains macro definitions for using the RRLib logging framework
29 *
30 * \b Log Messages
31 *
32 * A few words for messages.h
33 *
34 */
35//----------------------------------------------------------------------
36#ifndef __rrlib__logging__messages_h__
37#define __rrlib__logging__messages_h__
38
39//----------------------------------------------------------------------
40// External includes (system with <>, local with "")
41//----------------------------------------------------------------------
42
43//----------------------------------------------------------------------
44// Internal includes with ""
45//----------------------------------------------------------------------
46#define __rrlib__logging__include_guard__
47
48#include "rrlib/logging/log_levels.h"
49#include "rrlib/logging/default_log_description.h"
50#include "rrlib/logging/messages/implementation.h"
51
52#undef __rrlib__logging__include_guard__
53
54//----------------------------------------------------------------------
55// Namespace declaration
56//----------------------------------------------------------------------
57// The macro interface and default GetLogDescription function must live
58// within the global namespace
59
60//----------------------------------------------------------------------
61// Forward declarations / typedefs / enums
62//----------------------------------------------------------------------
63
64//----------------------------------------------------------------------
65// Macros for internal use
66//----------------------------------------------------------------------
67#define __RRLIB_LOG_PRINT__(domain, level, args...) \
68  do \
69  { \
70    if ((level) <= domain.MaxMessageLevel()) \
71    { \
72      rrlib::logging::Print(domain, GetLogDescription(), __FUNCTION__, __FILE__, __LINE__, level, args); \
73    } \
74  } while(0) \
75     
76#define __RRLIB_LOG_PRINT_STATIC__(domain, level, args...) \
77  do \
78  { \
79    if ((level) <= domain.MaxMessageLevel()) \
80    { \
81      rrlib::logging::Print(domain, "<static>", __FUNCTION__, __FILE__, __LINE__, level, args); \
82    } \
83  } while(0) \
84     
85#define __RRLIB_LOG_PRINTF__(domain, level, args...) \
86  do \
87  { \
88    if ((level) <= domain.MaxMessageLevel()) \
89    { \
90      rrlib::logging::PrintFormatted(domain, GetLogDescription(), __FUNCTION__, __FILE__, __LINE__, level, args); \
91    } \
92  } while(0) \
93     
94#define __RRLIB_LOG_PRINTF_STATIC__(domain, level, args...) \
95  do \
96  { \
97    if ((level) <= domain.MaxMessageLevel()) \
98    { \
99      rrlib::logging::PrintFormatted(domain, "<static>", __FUNCTION__, __FILE__, __LINE__, level, args); \
100    } \
101  } while(0) \
102     
103#define __EXPAND_LEVEL__(level) rrlib::logging::tLogLevel::level
104
105//----------------------------------------------------------------------
106// The macro interface to the logging library
107//----------------------------------------------------------------------
108#ifdef RRLIB_LOGGING_LESS_OUTPUT
109
110/*! Macro to print messages using stream semantics
111 *
112 * \param level    The level of the message
113 * \param args     The data to be put into the underlying stream
114 */
115#define RRLIB_LOG_PRINT(level, args...) \
116  do \
117  { \
118    if ((__EXPAND_LEVEL__(level)) <= rrlib::logging::tLogLevel::DEBUG) \
119    { \
120      __RRLIB_LOG_PRINT__(rrlib::logging::GetConfiguration(__FILE__), __EXPAND_LEVEL__(level), args); \
121    } \
122  } while (0) \
123     
124/*! Macro to print messages to explicitly specified domain using stream semantics
125 *
126 * \param domain   The domain the message should be printed to
127 * \param level    The level of the message
128 * \param args     The data to be put into the underlying stream
129 */
130#define RRLIB_LOG_PRINT_TO(domain, level, args...) \
131  do \
132  { \
133    if ((__EXPAND_LEVEL__(level)) <= rrlib::logging::tLogLevel::DEBUG) \
134    { \
135      __RRLIB_LOG_PRINT__(rrlib::logging::GetConfiguration(__FILE__, #domain), __EXPAND_LEVEL__(level), args); \
136    } \
137  } while (0) \
138     
139/*! Macro to print messages using stream semantics from static context
140 *
141 * \param level    The level of the message
142 * \param args     The data to be put into the underlying stream
143 */
144#define RRLIB_LOG_PRINT_STATIC(level, args...) \
145  do \
146  { \
147    if ((__EXPAND_LEVEL__(level)) <= rrlib::logging::tLogLevel::DEBUG) \
148    { \
149      __RRLIB_LOG_PRINT_STATIC__(rrlib::logging::GetConfiguration(__FILE__), __EXPAND_LEVEL__(level), args); \
150    } \
151  } while (0) \
152     
153/*! Macro to print messages to explicitly specified domain using stream semantics from static context
154 *
155 * \param domain   The domain the message should be printed to
156 * \param level    The level of the message
157 * \param args     The data to be put into the underlying stream
158 */
159#define RRLIB_LOG_PRINT_STATIC_TO(domain, level, args...) \
160  do \
161  { \
162    if ((__EXPAND_LEVEL__(level)) <= rrlib::logging::tLogLevel::DEBUG) \
163    { \
164      __RRLIB_LOG_PRINT_STATIC__(rrlib::logging::GetConfiguration(__FILE__, #domain), __EXPAND_LEVEL__(level), args); \
165    } \
166  } while (0) \
167     
168/*! Macro to print messages using printf semantics
169 *
170 * \param level    The level of the message
171 * \param args     The format string for printf and the optional arguments to be printed
172 */
173#define RRLIB_LOG_PRINTF(level, args...) \
174  do \
175  { \
176    if ((__EXPAND_LEVEL__(level)) <= rrlib::logging::tLogLevel::DEBUG) \
177    { \
178      __RRLIB_LOG_PRINTF__(rrlib::logging::GetConfiguration(__FILE__), __EXPAND_LEVEL__(level), args); \
179    } \
180  } while (0) \
181     
182/*! Macro to print messages to explicitly specified domain using printf semantics
183 *
184 * \param domain   The domain the message should be printed to
185 * \param level    The level of the message
186 * \param args     The format string for printf and the optional arguments to be printed
187 */
188#define RRLIB_LOG_PRINTF_TO(domain, level, args...) \
189  do \
190  { \
191    if ((__EXPAND_LEVEL__(level)) <= rrlib::logging::tLogLevel::DEBUG) \
192    { \
193      __RRLIB_LOG_PRINTF__(rrlib::logging::GetConfiguration(__FILE__, #domain), __EXPAND_LEVEL__(level), args); \
194    } \
195  } while (0) \
196     
197/*! Macro to print messages using printf semantics from static context
198 *
199 * \param level    The level of the message
200 * \param args     The format string for printf and the optional arguments to be printed
201 */
202#define RRLIB_LOG_PRINTF_STATIC(level, args...) \
203  do \
204  { \
205    if ((__EXPAND_LEVEL__(level)) <= rrlib::logging::tLogLevel::DEBUG) \
206    { \
207      __RRLIB_LOG_PRINTF_STATIC__(rrlib::logging::GetConfiguration(__FILE__), __EXPAND_LEVEL__(level), args); \
208    } \
209  } while (0) \
210     
211/*! Macro to print messages to explicitly specified domain using printf semantics from static context
212 *
213 * \param domain   The domain the message should be printed to
214 * \param level    The level of the message
215 * \param args     The format string for printf and the optional arguments to be printed
216 */
217#define RRLIB_LOG_PRINTF_STATIC_TO(domain, level, args...) \
218  do \
219  { \
220    if ((__EXPAND_LEVEL__(level)) <= rrlib::logging::tLogLevel::DEBUG) \
221    { \
222      __RRLIB_LOG_PRINTF_STATIC__(rrlib::logging::GetConfiguration(__FILE__, #domain), __EXPAND_LEVEL__(level), args); \
223    } \
224  } while (0) \
225     
226#else
227
228/*! Macro to print messages using stream semantics
229 *
230 * \param level    The level of the message
231 * \param args     The data to be put into the underlying stream
232 */
233#define RRLIB_LOG_PRINT(level, args...) \
234  __RRLIB_LOG_PRINT__(rrlib::logging::GetConfiguration(__FILE__), __EXPAND_LEVEL__(level), args) \
235   
236/*! Macro to print messages to explicitly specified domain using stream semantics
237 *
238 * \param domain   The domain the message should be printed to
239 * \param level    The level of the message
240 * \param args     The data to be put into the underlying stream
241 */
242#define RRLIB_LOG_PRINT_TO(domain, level, args...) \
243  __RRLIB_LOG_PRINT__(rrlib::logging::GetConfiguration(__FILE__, #domain), __EXPAND_LEVEL__(level), args) \
244   
245/*! Macro to print messages using stream semantics static context
246 *
247 * \param level    The level of the message
248 * \param args     The data to be put into the underlying stream
249 */
250#define RRLIB_LOG_PRINT_STATIC(level, args...) \
251  __RRLIB_LOG_PRINT_STATIC__(rrlib::logging::GetConfiguration(__FILE__), __EXPAND_LEVEL__(level), args) \
252   
253/*! Macro to print messages to explicitly specified using stream semantics static context
254 *
255 * \param domain   The domain the message should be printed to
256 * \param level    The level of the message
257 * \param args     The data to be put into the underlying stream
258 */
259#define RRLIB_LOG_PRINT_STATIC_TO(domain, level, args...) \
260  __RRLIB_LOG_PRINT_STATIC__(rrlib::logging::GetConfiguration(__FILE__, #domain), __EXPAND_LEVEL__(level), args) \
261   
262/*! Macro to print messages using printf semantics
263 *
264 * \param level    The level of the message
265 * \param args     The format string for printf and the optional arguments to be printed
266 */
267#define RRLIB_LOG_PRINTF(level, args...) \
268  __RRLIB_LOG_PRINTF__(rrlib::logging::GetConfiguration(__FILE__), __EXPAND_LEVEL__(level), args) \
269   
270/*! Macro to print messages to explicitly specified using printf semantics
271 *
272 * \param domain   The domain the message should be printed to
273 * \param level    The level of the message
274 * \param args     The format string for printf and the optional arguments to be printed
275 */
276#define RRLIB_LOG_PRINTF_TO(domain, level, args...) \
277  __RRLIB_LOG_PRINTF__(rrlib::logging::GetConfiguration(__FILE__, #domain), __EXPAND_LEVEL__(level), args) \
278   
279/*! Macro to print messages using printf semantics from static context
280 *
281 * \param level    The level of the message
282 * \param args     The format string for printf and the optional arguments to be printed
283 */
284#define RRLIB_LOG_PRINTF_STATIC(level, args...) \
285  __RRLIB_LOG_PRINTF_STATIC__(rrlib::logging::GetConfiguration(__FILE__), __EXPAND_LEVEL__(level), args) \
286   
287/*! Macro to print messages to explicitly specified using printf semantics from static context
288 *
289 * \param domain   The domain the message should be printed to
290 * \param level    The level of the message
291 * \param args     The format string for printf and the optional arguments to be printed
292 */
293#define RRLIB_LOG_PRINTF_STATIC_TO(domain, level, args...) \
294  __RRLIB_LOG_PRINTF_STATIC__(rrlib::logging::GetConfiguration(__FILE__, #domain), __EXPAND_LEVEL__(level), args) \
295   
296#endif
297
298/*! Macro to throw and log exceptions in one line
299 *
300 * \param exception   The exception to be thrown
301 */
302#define RRLIB_LOG_THROW(exception) \
303  do \
304  { \
305    RRLIB_LOG_PRINT(ERROR, exception); \
306    throw exception; \
307  } while (0) \
308     
309/*! Macro to throw and log exceptions to explicitly specified domain in one line
310 *
311 * \param domain      The domain the message should be printed to
312 * \param exception   The exception to be thrown
313 */
314#define RRLIB_LOG_THROW_TO(domain, exception) \
315  do \
316  { \
317    RRLIB_LOG_PRINT_TO(domain, ERROR, exception); \
318    throw exception; \
319  } while (0) \
320     
321/*! Macro to throw and log exceptions in one line from static context
322 *
323 * \param exception   The exception to be thrown
324 */
325#define RRLIB_LOG_THROW_STATIC(exception) \
326  do \
327  { \
328    RRLIB_LOG_PRINT_STATIC(ERROR, exception); \
329    throw exception; \
330  } while (0) \
331     
332/*! Macro to throw and log exceptions to explicitly specified domain in one line from static context
333 *
334 * \param domain      The domain the message should be printed to
335 * \param exception   The exception to be thrown
336 */
337#define RRLIB_LOG_THROW_STATIC_TO(domain, exception) \
338  do \
339  { \
340    RRLIB_LOG_PRINT_STATIC_TO(domain, ERROR, exception); \
341    throw exception; \
342  } while (0) \
343     
344
345// The default global GetLogDescription definition
346inline const char *GetLogDescription()
347{
348  return rrlib::logging::default_log_description;
349}
350
351
352#endif
Note: See TracBrowser for help on using the repository browser.