You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

414 lines
16 KiB

  1. // -*- C++ -*-
  2. // Module: Log4CPLUS
  3. // File: loggingmacros.h
  4. // Created: 8/2003
  5. // Author: Tad E. Smith
  6. //
  7. //
  8. // Copyright 2003-2010 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. /** @file
  22. * This header defines the logging macros. */
  23. #ifndef LOG4CPLUS_LOGGING_MACROS_HEADER_
  24. #define LOG4CPLUS_LOGGING_MACROS_HEADER_
  25. #include <log4cplus/config.hxx>
  26. #if defined (LOG4CPLUS_HAVE_PRAGMA_ONCE)
  27. #pragma once
  28. #endif
  29. #include <log4cplus/streams.h>
  30. #include <log4cplus/logger.h>
  31. #include <log4cplus/helpers/snprintf.h>
  32. #include <log4cplus/tracelogger.h>
  33. #include <sstream>
  34. #include <utility>
  35. #if defined(LOG4CPLUS_DISABLE_FATAL) && !defined(LOG4CPLUS_DISABLE_ERROR)
  36. #define LOG4CPLUS_DISABLE_ERROR
  37. #endif
  38. #if defined(LOG4CPLUS_DISABLE_ERROR) && !defined(LOG4CPLUS_DISABLE_WARN)
  39. #define LOG4CPLUS_DISABLE_WARN
  40. #endif
  41. #if defined(LOG4CPLUS_DISABLE_WARN) && !defined(LOG4CPLUS_DISABLE_INFO)
  42. #define LOG4CPLUS_DISABLE_INFO
  43. #endif
  44. #if defined(LOG4CPLUS_DISABLE_INFO) && !defined(LOG4CPLUS_DISABLE_DEBUG)
  45. #define LOG4CPLUS_DISABLE_DEBUG
  46. #endif
  47. #if defined(LOG4CPLUS_DISABLE_DEBUG) && !defined(LOG4CPLUS_DISABLE_TRACE)
  48. #define LOG4CPLUS_DISABLE_TRACE
  49. #endif
  50. namespace log4cplus
  51. {
  52. namespace detail
  53. {
  54. inline
  55. Logger
  56. macros_get_logger (Logger const & logger)
  57. {
  58. return logger;
  59. }
  60. inline
  61. Logger const &
  62. macros_get_logger (Logger & logger)
  63. {
  64. return logger;
  65. }
  66. #if defined (LOG4CPLUS_HAVE_RVALUE_REFS)
  67. inline
  68. Logger
  69. macros_get_logger (Logger && logger)
  70. {
  71. return std::move (logger);
  72. }
  73. #endif
  74. inline
  75. Logger
  76. macros_get_logger (tstring const & logger)
  77. {
  78. return Logger::getInstance (logger);
  79. }
  80. inline
  81. Logger
  82. macros_get_logger (tchar const * logger)
  83. {
  84. return Logger::getInstance (logger);
  85. }
  86. LOG4CPLUS_EXPORT void clear_tostringstream (tostringstream &);
  87. LOG4CPLUS_EXPORT log4cplus::tostringstream & get_macro_body_oss ();
  88. LOG4CPLUS_EXPORT log4cplus::helpers::snprintf_buf & get_macro_body_snprintf_buf ();
  89. LOG4CPLUS_EXPORT void macro_forced_log (log4cplus::Logger const &,
  90. log4cplus::LogLevel, log4cplus::tstring const &, char const *, int,
  91. char const *);
  92. } // namespace detail
  93. } // namespace log4cplus
  94. #undef LOG4CPLUS_MACRO_FUNCTION
  95. #define LOG4CPLUS_MACRO_FUNCTION() 0
  96. #if ! defined (LOG4CPLUS_DISABLE_FUNCTION_MACRO)
  97. # if defined (LOG4CPLUS_HAVE_FUNCSIG_MACRO)
  98. # undef LOG4CPLUS_MACRO_FUNCTION
  99. # define LOG4CPLUS_MACRO_FUNCTION() __FUNCSIG__
  100. # elif defined (LOG4CPLUS_HAVE_PRETTY_FUNCTION_MACRO)
  101. # undef LOG4CPLUS_MACRO_FUNCTION
  102. # define LOG4CPLUS_MACRO_FUNCTION() __PRETTY_FUNCTION__
  103. # elif defined (LOG4CPLUS_HAVE_FUNCTION_MACRO)
  104. # undef LOG4CPLUS_MACRO_FUNCTION
  105. # define LOG4CPLUS_MACRO_FUNCTION() __FUNCTION__
  106. # elif defined (LOG4CPLUS_HAVE_FUNC_SYMBOL)
  107. # undef LOG4CPLUS_MACRO_FUNCTION
  108. # define LOG4CPLUS_MACRO_FUNCTION() __func__
  109. # endif
  110. #endif
  111. // Make TRACE and DEBUG log level unlikely and INFO, WARN, ERROR and
  112. // FATAL log level likely.
  113. #define LOG4CPLUS_MACRO_TRACE_LOG_LEVEL(pred) \
  114. LOG4CPLUS_UNLIKELY (pred)
  115. #define LOG4CPLUS_MACRO_DEBUG_LOG_LEVEL(pred) \
  116. LOG4CPLUS_UNLIKELY (pred)
  117. #define LOG4CPLUS_MACRO_INFO_LOG_LEVEL(pred) \
  118. LOG4CPLUS_LIKELY (pred)
  119. #define LOG4CPLUS_MACRO_WARN_LOG_LEVEL(pred) \
  120. LOG4CPLUS_LIKELY (pred)
  121. #define LOG4CPLUS_MACRO_ERROR_LOG_LEVEL(pred) \
  122. LOG4CPLUS_LIKELY (pred)
  123. #define LOG4CPLUS_MACRO_FATAL_LOG_LEVEL(pred) \
  124. LOG4CPLUS_LIKELY (pred)
  125. //! Dispatch to LOG4CPLUS_MACRO_LOGLEVEL_* depending on log level.
  126. #define LOG4CPLUS_MACRO_LOGLEVEL_PRED(pred, logLevel) \
  127. LOG4CPLUS_MACRO_ ## logLevel (pred)
  128. #define LOG4CPLUS_MACRO_BODY(logger, logEvent, logLevel) \
  129. do { \
  130. log4cplus::Logger const & _l \
  131. = log4cplus::detail::macros_get_logger (logger); \
  132. if (LOG4CPLUS_MACRO_LOGLEVEL_PRED ( \
  133. _l.isEnabledFor (log4cplus::logLevel), logLevel)) { \
  134. log4cplus::tostringstream & _log4cplus_buf \
  135. = log4cplus::detail::get_macro_body_oss (); \
  136. _log4cplus_buf << logEvent; \
  137. log4cplus::detail::macro_forced_log (_l, \
  138. log4cplus::logLevel, _log4cplus_buf.str(), \
  139. __FILE__, __LINE__, LOG4CPLUS_MACRO_FUNCTION ()); \
  140. } \
  141. } while (0)
  142. #define LOG4CPLUS_MACRO_STR_BODY(logger, logEvent, logLevel) \
  143. do { \
  144. log4cplus::Logger const & _l \
  145. = log4cplus::detail::macros_get_logger (logger); \
  146. if (LOG4CPLUS_MACRO_LOGLEVEL_PRED ( \
  147. _l.isEnabledFor (log4cplus::logLevel), logLevel)) { \
  148. log4cplus::detail::macro_forced_log (_l, \
  149. log4cplus::logLevel, logEvent, \
  150. __FILE__, __LINE__, LOG4CPLUS_MACRO_FUNCTION ()); \
  151. } \
  152. } while(0)
  153. #if defined (LOG4CPLUS_HAVE_C99_VARIADIC_MACROS)
  154. #define LOG4CPLUS_MACRO_FMT_BODY(logger, logLevel, logFmt, ...) \
  155. do { \
  156. log4cplus::Logger const & _l \
  157. = log4cplus::detail::macros_get_logger (logger); \
  158. if (LOG4CPLUS_MACRO_LOGLEVEL_PRED ( \
  159. _l.isEnabledFor (log4cplus::logLevel), logLevel)) { \
  160. log4cplus::helpers::snprintf_buf & _snpbuf \
  161. = log4cplus::detail::get_macro_body_snprintf_buf (); \
  162. log4cplus::tchar const * _logEvent \
  163. = _snpbuf.print (logFmt, __VA_ARGS__); \
  164. log4cplus::detail::macro_forced_log (_l, \
  165. log4cplus::logLevel, _logEvent, \
  166. __FILE__, __LINE__, LOG4CPLUS_MACRO_FUNCTION ()); \
  167. } \
  168. } while(0)
  169. #elif defined (LOG4CPLUS_HAVE_GNU_VARIADIC_MACROS)
  170. #define LOG4CPLUS_MACRO_FMT_BODY(logger, logLevel, logFmt, logArgs...) \
  171. do { \
  172. log4cplus::Logger const & _l \
  173. = log4cplus::detail::macros_get_logger (logger); \
  174. if (LOG4CPLUS_MACRO_LOGLEVEL_PRED ( \
  175. _l.isEnabledFor (log4cplus::logLevel), logLevel)) { \
  176. log4cplus::helpers::snprintf_buf & _snpbuf \
  177. = log4cplus::detail::get_macro_body_snprintf_buf (); \
  178. log4cplus::tchar const * _logEvent \
  179. = _snpbuf.print (logFmt, logArgs); \
  180. log4cplus::detail::macro_forced_log (_l, \
  181. log4cplus::logLevel, _logEvent, \
  182. __FILE__, __LINE__, LOG4CPLUS_MACRO_FUNCTION ()); \
  183. } \
  184. } while(0)
  185. #endif
  186. /**
  187. * @def LOG4CPLUS_TRACE(logger, logEvent) This macro creates a
  188. * TraceLogger to log a TRACE_LOG_LEVEL message to <code>logger</code>
  189. * upon entry and exiting of a method.
  190. * <code>logEvent</code> will be streamed into an <code>ostream</code>.
  191. */
  192. #if !defined(LOG4CPLUS_DISABLE_TRACE)
  193. #define LOG4CPLUS_TRACE_METHOD(logger, logEvent) \
  194. log4cplus::TraceLogger _log4cplus_trace_logger(logger, logEvent, \
  195. __FILE__, __LINE__);
  196. #define LOG4CPLUS_TRACE(logger, logEvent) \
  197. LOG4CPLUS_MACRO_BODY (logger, logEvent, TRACE_LOG_LEVEL)
  198. #define LOG4CPLUS_TRACE_STR(logger, logEvent) \
  199. LOG4CPLUS_MACRO_STR_BODY (logger, logEvent, TRACE_LOG_LEVEL)
  200. #if defined (LOG4CPLUS_HAVE_C99_VARIADIC_MACROS)
  201. #define LOG4CPLUS_TRACE_FMT(logger, logFmt, ...) \
  202. LOG4CPLUS_MACRO_FMT_BODY (logger, TRACE_LOG_LEVEL, logFmt, __VA_ARGS__)
  203. #elif defined (LOG4CPLUS_HAVE_GNU_VARIADIC_MACROS)
  204. #define LOG4CPLUS_TRACE_FMT(logger, logFmt, logArgs...) \
  205. LOG4CPLUS_MACRO_FMT_BODY(logger, TRACE_LOG_LEVEL, logFmt, logArgs)
  206. #endif
  207. #else
  208. #define LOG4CPLUS_TRACE_METHOD(logger, logEvent) do { } while (0)
  209. #define LOG4CPLUS_TRACE(logger, logEvent) do { } while (0)
  210. #define LOG4CPLUS_TRACE_STR(logger, logEvent) do { } while (0)
  211. #if defined (LOG4CPLUS_HAVE_C99_VARIADIC_MACROS)
  212. #define LOG4CPLUS_TRACE_FMT(logger, logFmt, ...) do { } while (0)
  213. #elif defined (LOG4CPLUS_HAVE_GNU_VARIADIC_MACROS)
  214. #define LOG4CPLUS_TRACE_FMT(logger, logFmt, logArgs...) do { } while (0)
  215. #endif
  216. #endif
  217. /**
  218. * @def LOG4CPLUS_DEBUG(logger, logEvent) This macro is used to log a
  219. * DEBUG_LOG_LEVEL message to <code>logger</code>.
  220. * <code>logEvent</code> will be streamed into an <code>ostream</code>.
  221. */
  222. #if !defined(LOG4CPLUS_DISABLE_DEBUG)
  223. #define LOG4CPLUS_DEBUG(logger, logEvent) \
  224. LOG4CPLUS_MACRO_BODY (logger, logEvent, DEBUG_LOG_LEVEL)
  225. #define LOG4CPLUS_DEBUG_STR(logger, logEvent) \
  226. LOG4CPLUS_MACRO_STR_BODY (logger, logEvent, DEBUG_LOG_LEVEL)
  227. #if defined (LOG4CPLUS_HAVE_C99_VARIADIC_MACROS)
  228. #define LOG4CPLUS_DEBUG_FMT(logger, logFmt, ...) \
  229. LOG4CPLUS_MACRO_FMT_BODY (logger, DEBUG_LOG_LEVEL, logFmt, __VA_ARGS__)
  230. #elif defined (LOG4CPLUS_HAVE_GNU_VARIADIC_MACROS)
  231. #define LOG4CPLUS_DEBUG_FMT(logger, logFmt, logArgs...) \
  232. LOG4CPLUS_MACRO_FMT_BODY(logger, DEBUG_LOG_LEVEL, logFmt, logArgs)
  233. #endif
  234. #else
  235. #define LOG4CPLUS_DEBUG(logger, logEvent) do { } while (0)
  236. #define LOG4CPLUS_DEBUG_STR(logger, logEvent) do { } while (0)
  237. #if defined (LOG4CPLUS_HAVE_C99_VARIADIC_MACROS)
  238. #define LOG4CPLUS_DEBUG_FMT(logger, logFmt, ...) do { } while (0)
  239. #elif defined (LOG4CPLUS_HAVE_GNU_VARIADIC_MACROS)
  240. #define LOG4CPLUS_DEBUG_FMT(logger, logFmt, logArgs...) do { } while (0)
  241. #endif
  242. #endif
  243. /**
  244. * @def LOG4CPLUS_INFO(logger, logEvent) This macro is used to log a
  245. * INFO_LOG_LEVEL message to <code>logger</code>.
  246. * <code>logEvent</code> will be streamed into an <code>ostream</code>.
  247. */
  248. #if !defined(LOG4CPLUS_DISABLE_INFO)
  249. #define LOG4CPLUS_INFO(logger, logEvent) \
  250. LOG4CPLUS_MACRO_BODY (logger, logEvent, INFO_LOG_LEVEL)
  251. #define LOG4CPLUS_INFO_STR(logger, logEvent) \
  252. LOG4CPLUS_MACRO_STR_BODY (logger, logEvent, INFO_LOG_LEVEL)
  253. #if defined (LOG4CPLUS_HAVE_C99_VARIADIC_MACROS)
  254. #define LOG4CPLUS_INFO_FMT(logger, logFmt, ...) \
  255. LOG4CPLUS_MACRO_FMT_BODY (logger, INFO_LOG_LEVEL, logFmt, __VA_ARGS__)
  256. #elif defined (LOG4CPLUS_HAVE_GNU_VARIADIC_MACROS)
  257. #define LOG4CPLUS_INFO_FMT(logger, logFmt, logArgs...) \
  258. LOG4CPLUS_MACRO_FMT_BODY(logger, INFO_LOG_LEVEL, logFmt, logArgs)
  259. #endif
  260. #else
  261. #define LOG4CPLUS_INFO(logger, logEvent) do { } while (0)
  262. #define LOG4CPLUS_INFO_STR(logger, logEvent) do { } while (0)
  263. #if defined (LOG4CPLUS_HAVE_C99_VARIADIC_MACROS)
  264. #define LOG4CPLUS_INFO_FMT(logger, logFmt, ...) do { } while (0)
  265. #elif defined (LOG4CPLUS_HAVE_GNU_VARIADIC_MACROS)
  266. #define LOG4CPLUS_INFO_FMT(logger, logFmt, logArgs...) do { } while (0)
  267. #endif
  268. #endif
  269. /**
  270. * @def LOG4CPLUS_WARN(logger, logEvent) This macro is used to log a
  271. * WARN_LOG_LEVEL message to <code>logger</code>.
  272. * <code>logEvent</code> will be streamed into an <code>ostream</code>.
  273. */
  274. #if !defined(LOG4CPLUS_DISABLE_WARN)
  275. #define LOG4CPLUS_WARN(logger, logEvent) \
  276. LOG4CPLUS_MACRO_BODY (logger, logEvent, WARN_LOG_LEVEL)
  277. #define LOG4CPLUS_WARN_STR(logger, logEvent) \
  278. LOG4CPLUS_MACRO_STR_BODY (logger, logEvent, WARN_LOG_LEVEL)
  279. #if defined (LOG4CPLUS_HAVE_C99_VARIADIC_MACROS)
  280. #define LOG4CPLUS_WARN_FMT(logger, logFmt, ...) \
  281. LOG4CPLUS_MACRO_FMT_BODY (logger, WARN_LOG_LEVEL, logFmt, __VA_ARGS__)
  282. #elif defined (LOG4CPLUS_HAVE_GNU_VARIADIC_MACROS)
  283. #define LOG4CPLUS_WARN_FMT(logger, logFmt, logArgs...) \
  284. LOG4CPLUS_MACRO_FMT_BODY(logger, WARN_LOG_LEVEL, logFmt, logArgs)
  285. #endif
  286. #else
  287. #define LOG4CPLUS_WARN(logger, logEvent) do { } while (0)
  288. #define LOG4CPLUS_WARN_STR(logger, logEvent) do { } while (0)
  289. #if defined (LOG4CPLUS_HAVE_C99_VARIADIC_MACROS)
  290. #define LOG4CPLUS_WARN_FMT(logger, logFmt, ...) do { } while (0)
  291. #elif defined (LOG4CPLUS_HAVE_GNU_VARIADIC_MACROS)
  292. #define LOG4CPLUS_WARN_FMT(logger, logFmt, logArgs...) do { } while (0)
  293. #endif
  294. #endif
  295. /**
  296. * @def LOG4CPLUS_ERROR(logger, logEvent) This macro is used to log a
  297. * ERROR_LOG_LEVEL message to <code>logger</code>.
  298. * <code>logEvent</code> will be streamed into an <code>ostream</code>.
  299. */
  300. #if !defined(LOG4CPLUS_DISABLE_ERROR)
  301. #define LOG4CPLUS_ERROR(logger, logEvent) \
  302. LOG4CPLUS_MACRO_BODY (logger, logEvent, ERROR_LOG_LEVEL)
  303. #define LOG4CPLUS_ERROR_STR(logger, logEvent) \
  304. LOG4CPLUS_MACRO_STR_BODY (logger, logEvent, ERROR_LOG_LEVEL)
  305. #if defined (LOG4CPLUS_HAVE_C99_VARIADIC_MACROS)
  306. #define LOG4CPLUS_ERROR_FMT(logger, logFmt, ...) \
  307. LOG4CPLUS_MACRO_FMT_BODY (logger, ERROR_LOG_LEVEL, logFmt, __VA_ARGS__)
  308. #elif defined (LOG4CPLUS_HAVE_GNU_VARIADIC_MACROS)
  309. #define LOG4CPLUS_ERROR_FMT(logger, logFmt, logArgs...) \
  310. LOG4CPLUS_MACRO_FMT_BODY(logger, ERROR_LOG_LEVEL, logFmt, logArgs)
  311. #endif
  312. #else
  313. #define LOG4CPLUS_ERROR(logger, logEvent) do { } while (0)
  314. #define LOG4CPLUS_ERROR_STR(logger, logEvent) do { } while (0)
  315. #if defined (LOG4CPLUS_HAVE_C99_VARIADIC_MACROS)
  316. #define LOG4CPLUS_ERROR_FMT(logger, logFmt, ...) do { } while (0)
  317. #elif defined (LOG4CPLUS_HAVE_GNU_VARIADIC_MACROS)
  318. #define LOG4CPLUS_ERROR_FMT(logger, logFmt, logArgs...) do { } while (0)
  319. #endif
  320. #endif
  321. /**
  322. * @def LOG4CPLUS_FATAL(logger, logEvent) This macro is used to log a
  323. * FATAL_LOG_LEVEL message to <code>logger</code>.
  324. * <code>logEvent</code> will be streamed into an <code>ostream</code>.
  325. */
  326. #if !defined(LOG4CPLUS_DISABLE_FATAL)
  327. #define LOG4CPLUS_FATAL(logger, logEvent) \
  328. LOG4CPLUS_MACRO_BODY (logger, logEvent, FATAL_LOG_LEVEL)
  329. #define LOG4CPLUS_FATAL_STR(logger, logEvent) \
  330. LOG4CPLUS_MACRO_STR_BODY (logger, logEvent, FATAL_LOG_LEVEL)
  331. #if defined (LOG4CPLUS_HAVE_C99_VARIADIC_MACROS)
  332. #define LOG4CPLUS_FATAL_FMT(logger, logFmt, ...) \
  333. LOG4CPLUS_MACRO_FMT_BODY (logger, FATAL_LOG_LEVEL, logFmt, __VA_ARGS__)
  334. #elif defined (LOG4CPLUS_HAVE_GNU_VARIADIC_MACROS)
  335. #define LOG4CPLUS_FATAL_FMT(logger, logFmt, logArgs...) \
  336. LOG4CPLUS_MACRO_FMT_BODY(logger, FATAL_LOG_LEVEL, logFmt, logArgs)
  337. #endif
  338. #else
  339. #define LOG4CPLUS_FATAL(logger, logEvent) do { } while (0)
  340. #define LOG4CPLUS_FATAL_STR(logger, logEvent) do { } while (0)
  341. #if defined (LOG4CPLUS_HAVE_C99_VARIADIC_MACROS)
  342. #define LOG4CPLUS_FATAL_FMT(logger, logFmt, ...) do { } while (0)
  343. #elif defined (LOG4CPLUS_HAVE_GNU_VARIADIC_MACROS)
  344. #define LOG4CPLUS_FATAL_FMT(logger, logFmt, logArgs...) do { } while (0)
  345. #endif
  346. #endif
  347. #endif /* LOG4CPLUS_LOGGING_MACROS_HEADER_ */