log4cplus  2.1.0
loggingmacros.h
Go to the documentation of this file.
1 // -*- C++ -*-
2 // Module: Log4CPLUS
3 // File: loggingmacros.h
4 // Created: 8/2003
5 // Author: Tad E. Smith
6 //
7 //
8 // Copyright 2003-2017 Tad E. Smith
9 //
10 // Licensed under the Apache License, Version 2.0 (the "License");
11 // you may not use this file except in compliance with the License.
12 // You may obtain a copy of the License at
13 //
14 // http://www.apache.org/licenses/LICENSE-2.0
15 //
16 // Unless required by applicable law or agreed to in writing, software
17 // distributed under the License is distributed on an "AS IS" BASIS,
18 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 // See the License for the specific language governing permissions and
20 // limitations under the License.
21 
25 #ifndef LOG4CPLUS_LOGGING_MACROS_HEADER_
26 #define LOG4CPLUS_LOGGING_MACROS_HEADER_
27 
28 #include <log4cplus/config.hxx>
29 
30 #if defined (LOG4CPLUS_HAVE_PRAGMA_ONCE)
31 #pragma once
32 #endif
33 
34 #include <log4cplus/streams.h>
35 #include <log4cplus/logger.h>
37 #include <log4cplus/tracelogger.h>
38 #include <sstream>
39 #include <utility>
40 
41 
42 #if defined(_MSC_VER)
43 #define LOG4CPLUS_SUPPRESS_DOWHILE_WARNING() \
44  __pragma (warning (push)) \
45  __pragma (warning (disable:4127))
46 
47 #define LOG4CPLUS_RESTORE_DOWHILE_WARNING() \
48  __pragma (warning (pop))
49 
50 #else
51 #define LOG4CPLUS_SUPPRESS_DOWHILE_WARNING() /* empty */
52 #define LOG4CPLUS_RESTORE_DOWHILE_WARNING() /* empty */
53 
54 #endif
55 
56 #define LOG4CPLUS_DOWHILE_NOTHING() \
57  LOG4CPLUS_SUPPRESS_DOWHILE_WARNING() \
58  do { } while (0) \
59  LOG4CPLUS_RESTORE_DOWHILE_WARNING()
60 
61 #if defined(LOG4CPLUS_DISABLE_FATAL) && !defined(LOG4CPLUS_DISABLE_ERROR)
62 #define LOG4CPLUS_DISABLE_ERROR
63 #endif
64 #if defined(LOG4CPLUS_DISABLE_ERROR) && !defined(LOG4CPLUS_DISABLE_WARN)
65 #define LOG4CPLUS_DISABLE_WARN
66 #endif
67 #if defined(LOG4CPLUS_DISABLE_WARN) && !defined(LOG4CPLUS_DISABLE_INFO)
68 #define LOG4CPLUS_DISABLE_INFO
69 #endif
70 #if defined(LOG4CPLUS_DISABLE_INFO) && !defined(LOG4CPLUS_DISABLE_DEBUG)
71 #define LOG4CPLUS_DISABLE_DEBUG
72 #endif
73 #if defined(LOG4CPLUS_DISABLE_DEBUG) && !defined(LOG4CPLUS_DISABLE_TRACE)
74 #define LOG4CPLUS_DISABLE_TRACE
75 #endif
76 
77 
78 namespace log4cplus
79 {
80 
81 namespace detail
82 {
83 
84 
85 inline
86 Logger
87 macros_get_logger (Logger const & logger)
88 {
89  return logger;
90 }
91 
92 
93 inline
94 Logger const &
96 {
97  return logger;
98 }
99 
100 
101 inline
102 Logger
104 {
105  return std::move (logger);
106 }
107 
108 inline
109 Logger
110 macros_get_logger (tstring const & logger)
111 {
112  return Logger::getInstance (logger);
113 }
114 
115 
116 inline
117 Logger
118 macros_get_logger (tchar const * logger)
119 {
120  return Logger::getInstance (logger);
121 }
122 
123 
125 
126 
130  log4cplus::LogLevel, log4cplus::tstring const &, char const *, int,
131  char const *);
133  log4cplus::LogLevel, log4cplus::tchar const *, char const *, int,
134  char const *);
135 
136 
137 
138 } // namespace detail
139 
140 } // namespace log4cplus
141 
142 
143 #undef LOG4CPLUS_MACRO_FUNCTION
144 #define LOG4CPLUS_MACRO_FUNCTION() nullptr
145 #if ! defined (LOG4CPLUS_DISABLE_FUNCTION_MACRO)
146 # if defined (LOG4CPLUS_HAVE_FUNCSIG_MACRO)
147 # undef LOG4CPLUS_MACRO_FUNCTION
148 # define LOG4CPLUS_MACRO_FUNCTION() __FUNCSIG__
149 # elif defined (LOG4CPLUS_HAVE_PRETTY_FUNCTION_MACRO)
150 # undef LOG4CPLUS_MACRO_FUNCTION
151 # define LOG4CPLUS_MACRO_FUNCTION() __PRETTY_FUNCTION__
152 # elif defined (LOG4CPLUS_HAVE_FUNCTION_MACRO)
153 # undef LOG4CPLUS_MACRO_FUNCTION
154 # define LOG4CPLUS_MACRO_FUNCTION() __FUNCTION__
155 # elif defined (LOG4CPLUS_HAVE_FUNC_SYMBOL)
156 # undef LOG4CPLUS_MACRO_FUNCTION
157 # define LOG4CPLUS_MACRO_FUNCTION() __func__
158 # endif
159 #endif
160 
161 #undef LOG4CPLUS_MACRO_FILE
162 #define LOG4CPLUS_MACRO_FILE() nullptr
163 #if ! defined (LOG4CPLUS_DISABLE_FILE_MACRO)
164 # undef LOG4CPLUS_MACRO_FILE
165 # define LOG4CPLUS_MACRO_FILE() __FILE__
166 #endif
167 
168 
169 // Make TRACE and DEBUG log level unlikely and INFO, WARN, ERROR and
170 // FATAL log level likely.
171 #define LOG4CPLUS_MACRO_TRACE_LOG_LEVEL(pred) \
172  LOG4CPLUS_UNLIKELY (pred)
173 #define LOG4CPLUS_MACRO_DEBUG_LOG_LEVEL(pred) \
174  LOG4CPLUS_UNLIKELY (pred)
175 #define LOG4CPLUS_MACRO_INFO_LOG_LEVEL(pred) \
176  LOG4CPLUS_LIKELY (pred)
177 #define LOG4CPLUS_MACRO_WARN_LOG_LEVEL(pred) \
178  LOG4CPLUS_LIKELY (pred)
179 #define LOG4CPLUS_MACRO_ERROR_LOG_LEVEL(pred) \
180  LOG4CPLUS_LIKELY (pred)
181 #define LOG4CPLUS_MACRO_FATAL_LOG_LEVEL(pred) \
182  LOG4CPLUS_LIKELY (pred)
183 
184 
186 #define LOG4CPLUS_MACRO_LOGLEVEL_PRED(pred, logLevel) \
187  LOG4CPLUS_MACRO_ ## logLevel (pred)
188 
189 
190 // Either use temporary instances of ostringstream
191 // and snprintf_buf, or use thread-local instances.
192 #if defined (LOG4CPLUS_MACRO_DISABLE_TLS)
193 # define LOG4CPLUS_MACRO_INSTANTIATE_OSTRINGSTREAM(var) \
194  log4cplus::tostringstream var
195 
196 # define LOG4CPLUS_MACRO_INSTANTIATE_SNPRINTF_BUF(var) \
197  log4cplus::helpers::snprintf_buf var
198 
199 #else
200 # define LOG4CPLUS_MACRO_INSTANTIATE_OSTRINGSTREAM(var) \
201  log4cplus::tostringstream & var \
202  = log4cplus::detail::get_macro_body_oss ()
203 
204 # define LOG4CPLUS_MACRO_INSTANTIATE_SNPRINTF_BUF(var) \
205  log4cplus::helpers::snprintf_buf & var \
206  = log4cplus::detail::get_macro_body_snprintf_buf ()
207 
208 #endif
209 
210 
211 #define LOG4CPLUS_MACRO_BODY(logger, logEvent, logLevel) \
212  LOG4CPLUS_SUPPRESS_DOWHILE_WARNING() \
213  do { \
214  log4cplus::Logger const & _l \
215  = log4cplus::detail::macros_get_logger (logger); \
216  if (LOG4CPLUS_MACRO_LOGLEVEL_PRED ( \
217  _l.isEnabledFor (log4cplus::logLevel), logLevel)) { \
218  LOG4CPLUS_MACRO_INSTANTIATE_OSTRINGSTREAM (_log4cplus_buf); \
219  _log4cplus_buf << logEvent; \
220  log4cplus::detail::macro_forced_log (_l, \
221  log4cplus::logLevel, _log4cplus_buf.str(), \
222  LOG4CPLUS_MACRO_FILE (), __LINE__, \
223  LOG4CPLUS_MACRO_FUNCTION ()); \
224  } \
225  } while (0) \
226  LOG4CPLUS_RESTORE_DOWHILE_WARNING()
227 
228 
229 #define LOG4CPLUS_MACRO_STR_BODY(logger, logEvent, logLevel) \
230  LOG4CPLUS_SUPPRESS_DOWHILE_WARNING() \
231  do { \
232  log4cplus::Logger const & _l \
233  = log4cplus::detail::macros_get_logger (logger); \
234  if (LOG4CPLUS_MACRO_LOGLEVEL_PRED ( \
235  _l.isEnabledFor (log4cplus::logLevel), logLevel)) { \
236  log4cplus::detail::macro_forced_log (_l, \
237  log4cplus::logLevel, logEvent, \
238  LOG4CPLUS_MACRO_FILE (), __LINE__, \
239  LOG4CPLUS_MACRO_FUNCTION ()); \
240  } \
241  } while(0) \
242  LOG4CPLUS_RESTORE_DOWHILE_WARNING()
243 
244 #define LOG4CPLUS_MACRO_FMT_BODY(logger, logLevel, ...) \
245  LOG4CPLUS_SUPPRESS_DOWHILE_WARNING() \
246  do { \
247  log4cplus::Logger const & _l \
248  = log4cplus::detail::macros_get_logger (logger); \
249  if (LOG4CPLUS_MACRO_LOGLEVEL_PRED ( \
250  _l.isEnabledFor (log4cplus::logLevel), logLevel)) { \
251  LOG4CPLUS_MACRO_INSTANTIATE_SNPRINTF_BUF (_snpbuf); \
252  log4cplus::tchar const * _logEvent \
253  = _snpbuf.print (__VA_ARGS__); \
254  log4cplus::detail::macro_forced_log (_l, \
255  log4cplus::logLevel, _logEvent, \
256  LOG4CPLUS_MACRO_FILE (), __LINE__, \
257  LOG4CPLUS_MACRO_FUNCTION ()); \
258  } \
259  } while(0) \
260  LOG4CPLUS_RESTORE_DOWHILE_WARNING()
261 
268 #if !defined(LOG4CPLUS_DISABLE_TRACE)
269 #define LOG4CPLUS_TRACE_METHOD(logger, logEvent) \
270  log4cplus::TraceLogger _log4cplus_trace_logger(logger, logEvent, \
271  LOG4CPLUS_MACRO_FILE (), __LINE__, \
272  LOG4CPLUS_MACRO_FUNCTION ());
273 #define LOG4CPLUS_TRACE(logger, logEvent) \
274  LOG4CPLUS_MACRO_BODY (logger, logEvent, TRACE_LOG_LEVEL)
275 #define LOG4CPLUS_TRACE_STR(logger, logEvent) \
276  LOG4CPLUS_MACRO_STR_BODY (logger, logEvent, TRACE_LOG_LEVEL)
277 #define LOG4CPLUS_TRACE_FMT(logger, ...) \
278  LOG4CPLUS_MACRO_FMT_BODY (logger, TRACE_LOG_LEVEL, __VA_ARGS__)
279 
280 #else
281 #define LOG4CPLUS_TRACE_METHOD(logger, logEvent) LOG4CPLUS_DOWHILE_NOTHING()
282 #define LOG4CPLUS_TRACE(logger, logEvent) LOG4CPLUS_DOWHILE_NOTHING()
283 #define LOG4CPLUS_TRACE_STR(logger, logEvent) LOG4CPLUS_DOWHILE_NOTHING()
284 #define LOG4CPLUS_TRACE_FMT(logger, logFmt, ...) LOG4CPLUS_DOWHILE_NOTHING()
285 
286 #endif
287 
293 #if !defined(LOG4CPLUS_DISABLE_DEBUG)
294 #define LOG4CPLUS_DEBUG(logger, logEvent) \
295  LOG4CPLUS_MACRO_BODY (logger, logEvent, DEBUG_LOG_LEVEL)
296 #define LOG4CPLUS_DEBUG_STR(logger, logEvent) \
297  LOG4CPLUS_MACRO_STR_BODY (logger, logEvent, DEBUG_LOG_LEVEL)
298 #define LOG4CPLUS_DEBUG_FMT(logger, ...) \
299  LOG4CPLUS_MACRO_FMT_BODY (logger, DEBUG_LOG_LEVEL, __VA_ARGS__)
300 
301 #else
302 #define LOG4CPLUS_DEBUG(logger, logEvent) LOG4CPLUS_DOWHILE_NOTHING()
303 #define LOG4CPLUS_DEBUG_STR(logger, logEvent) LOG4CPLUS_DOWHILE_NOTHING()
304 #define LOG4CPLUS_DEBUG_FMT(logger, ...) LOG4CPLUS_DOWHILE_NOTHING()
305 
306 #endif
307 
313 #if !defined(LOG4CPLUS_DISABLE_INFO)
314 #define LOG4CPLUS_INFO(logger, logEvent) \
315  LOG4CPLUS_MACRO_BODY (logger, logEvent, INFO_LOG_LEVEL)
316 #define LOG4CPLUS_INFO_STR(logger, logEvent) \
317  LOG4CPLUS_MACRO_STR_BODY (logger, logEvent, INFO_LOG_LEVEL)
318 #define LOG4CPLUS_INFO_FMT(logger, ...) \
319  LOG4CPLUS_MACRO_FMT_BODY (logger, INFO_LOG_LEVEL, __VA_ARGS__)
320 
321 #else
322 #define LOG4CPLUS_INFO(logger, logEvent) LOG4CPLUS_DOWHILE_NOTHING()
323 #define LOG4CPLUS_INFO_STR(logger, logEvent) LOG4CPLUS_DOWHILE_NOTHING()
324 #define LOG4CPLUS_INFO_FMT(logger, ...) LOG4CPLUS_DOWHILE_NOTHING()
325 
326 #endif
327 
333 #if !defined(LOG4CPLUS_DISABLE_WARN)
334 #define LOG4CPLUS_WARN(logger, logEvent) \
335  LOG4CPLUS_MACRO_BODY (logger, logEvent, WARN_LOG_LEVEL)
336 #define LOG4CPLUS_WARN_STR(logger, logEvent) \
337  LOG4CPLUS_MACRO_STR_BODY (logger, logEvent, WARN_LOG_LEVEL)
338 #define LOG4CPLUS_WARN_FMT(logger, ...) \
339  LOG4CPLUS_MACRO_FMT_BODY (logger, WARN_LOG_LEVEL, __VA_ARGS__)
340 
341 #else
342 #define LOG4CPLUS_WARN(logger, logEvent) LOG4CPLUS_DOWHILE_NOTHING()
343 #define LOG4CPLUS_WARN_STR(logger, logEvent) LOG4CPLUS_DOWHILE_NOTHING()
344 #define LOG4CPLUS_WARN_FMT(logger, ...) LOG4CPLUS_DOWHILE_NOTHING()
345 
346 #endif
347 
353 #if !defined(LOG4CPLUS_DISABLE_ERROR)
354 #define LOG4CPLUS_ERROR(logger, logEvent) \
355  LOG4CPLUS_MACRO_BODY (logger, logEvent, ERROR_LOG_LEVEL)
356 #define LOG4CPLUS_ERROR_STR(logger, logEvent) \
357  LOG4CPLUS_MACRO_STR_BODY (logger, logEvent, ERROR_LOG_LEVEL)
358 #define LOG4CPLUS_ERROR_FMT(logger, ...) \
359  LOG4CPLUS_MACRO_FMT_BODY (logger, ERROR_LOG_LEVEL, __VA_ARGS__)
360 
361 #else
362 #define LOG4CPLUS_ERROR(logger, logEvent) LOG4CPLUS_DOWHILE_NOTHING()
363 #define LOG4CPLUS_ERROR_STR(logger, logEvent) LOG4CPLUS_DOWHILE_NOTHING()
364 #define LOG4CPLUS_ERROR_FMT(logger, ...) LOG4CPLUS_DOWHILE_NOTHING()
365 
366 #endif
367 
373 #if !defined(LOG4CPLUS_DISABLE_FATAL)
374 #define LOG4CPLUS_FATAL(logger, logEvent) \
375  LOG4CPLUS_MACRO_BODY (logger, logEvent, FATAL_LOG_LEVEL)
376 #define LOG4CPLUS_FATAL_STR(logger, logEvent) \
377  LOG4CPLUS_MACRO_STR_BODY (logger, logEvent, FATAL_LOG_LEVEL)
378 #define LOG4CPLUS_FATAL_FMT(logger, ...) \
379  LOG4CPLUS_MACRO_FMT_BODY (logger, FATAL_LOG_LEVEL, __VA_ARGS__)
380 
381 #else
382 #define LOG4CPLUS_FATAL(logger, logEvent) LOG4CPLUS_DOWHILE_NOTHING()
383 #define LOG4CPLUS_FATAL_STR(logger, logEvent) LOG4CPLUS_DOWHILE_NOTHING()
384 #define LOG4CPLUS_FATAL_FMT(logger, ...) LOG4CPLUS_DOWHILE_NOTHING()
385 
386 #endif
387 
389 #define LOG4CPLUS_ASSERT_STRINGIFY(X) #X
390 
394 #define LOG4CPLUS_ASSERT(logger, condition) \
395  LOG4CPLUS_SUPPRESS_DOWHILE_WARNING() \
396  do { \
397  if (LOG4CPLUS_UNLIKELY(! (condition))) \
398  LOG4CPLUS_FATAL_STR ((logger), \
399  LOG4CPLUS_TEXT ("failed condition: ") \
400  LOG4CPLUS_TEXT (LOG4CPLUS_ASSERT_STRINGIFY (condition))); \
401  } while (0) \
402  LOG4CPLUS_RESTORE_DOWHILE_WARNING()
403 
404 
405 #endif /* LOG4CPLUS_LOGGING_MACROS_HEADER_ */
This is the central class in the log4cplus package.
Definition: logger.h:71
static Logger getInstance(const log4cplus::tstring &name)
Retrieve a logger with name name.
This header defines the Logger class and the logging macros.
LOG4CPLUS_EXPORT log4cplus::helpers::snprintf_buf & get_macro_body_snprintf_buf()
LOG4CPLUS_EXPORT void clear_tostringstream(tostringstream &)
Logger macros_get_logger(Logger const &logger)
Definition: loggingmacros.h:87
LOG4CPLUS_EXPORT void macro_forced_log(log4cplus::Logger const &, log4cplus::LogLevel, log4cplus::tstring const &, char const *, int, char const *)
LOG4CPLUS_EXPORT log4cplus::tostringstream & get_macro_body_oss()
std::basic_string< tchar > tstring
Definition: tstring.h:39
std::basic_ostringstream< tchar > tostringstream
Definition: streams.h:42
char tchar
Definition: tchar.h:56
int LogLevel
Defines the minimum set of priorities recognized by the system, that is FATAL_LOG_LEVEL,...
Definition: loglevel.h:48
#define LOG4CPLUS_EXPORT
Definition: win32.h:141