fw4spl
spyLog.hpp
Go to the documentation of this file.
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2018.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
39 #pragma once
40 
41 # define SPYLOG_ABORT() std::abort()
42 
43 # ifdef _DEBUG
44 
45 # ifdef WIN32
46 # include <windows.h>
47 # define DEBUG_BREAK() DebugBreak()
48 # else
49 # include <csignal>
50 # define DEBUG_BREAK() std::raise(SIGTRAP)
51 # endif
52 
53 # ifdef SPYLOG_ABORT
54 # undef SPYLOG_ABORT
55 # endif
56 # define SPYLOG_ABORT() DEBUG_BREAK()
57 
58 # endif
59 
60 # include <cassert>
61 # include <sstream>
62 
63 #include <boost/preprocessor/comparison/greater_equal.hpp>
64 #include <boost/preprocessor/control/expr_iif.hpp>
65 
66 # include "fwCore/log/SpyLogger.hpp"
67 # include "fwCore/log/ScopedMessage.hpp"
68 
69 // -----------------------------------------------------------------------------
70 
75 // -----------------------------------------------------------------------------
76 
77 # ifndef __FWCORE_EXPR_BLOCK
78 # define __FWCORE_EXPR_BLOCK(expr) do { expr } while (0)
79 # endif
80 
81 # define __FWCORE_IF(cond, code) if ( cond ) { code }
82 
83 # define OSL_LOG(log, loglevel, message) __FWCORE_EXPR_BLOCK( \
84  std::stringstream oslStr; \
85  oslStr << message; \
86  SL_ ## loglevel(log, oslStr.str()); \
87  )
88 
89 #ifdef SPYLOG_LEVEL
90 
91 #define __FWCORE_IF_ENABLED( level, expr ) \
92  BOOST_PP_EXPR_IIF( BOOST_PP_GREATER_EQUAL(SPYLOG_LEVEL, level), expr)
93 
94 #endif
95 
96 // -----------------------------------------------------------------------------
97 
98 # define SL_TRACE(log, message) __FWCORE_IF_ENABLED( 6, \
99  log.trace(message, __FILE__, __LINE__); \
100  )
101 # define OSL_TRACE(log, message) __FWCORE_IF_ENABLED( 6, \
102  OSL_LOG(log, TRACE, message); \
103  )
104 # define SL_TRACE_IF(log, message, cond) __FWCORE_IF_ENABLED( 6, \
105  __FWCORE_IF(cond, log.trace(message, __FILE__, \
106  __LINE__); ) \
107  )
108 # define OSL_TRACE_IF(log, message, cond) __FWCORE_IF_ENABLED( 6, \
109  __FWCORE_IF(cond, OSL_LOG(log, TRACE, message); ) \
110  )
111 
112 # define SL_DEBUG(log, message) __FWCORE_IF_ENABLED( 5, \
113  log.debug(message, __FILE__, __LINE__); \
114  )
115 # define OSL_DEBUG(log, message) __FWCORE_IF_ENABLED( 5, \
116  OSL_LOG(log, DEBUG, message); \
117  )
118 # define SL_DEBUG_IF(log, message, cond) __FWCORE_IF_ENABLED( 5, \
119  __FWCORE_IF(cond, log.debug(message, __FILE__, \
120  __LINE__); ) \
121  )
122 #define OSL_DEBUG_IF(log, message, cond) __FWCORE_IF_ENABLED( 5, \
123  __FWCORE_IF(cond, OSL_LOG(log, DEBUG, message); ) \
124  )
125 
126 #define SL_INFO(log, message) __FWCORE_IF_ENABLED( 4, \
127  log.info(message, __FILE__, __LINE__); \
128  )
129 #define OSL_INFO(log, message) __FWCORE_IF_ENABLED( 4, \
130  OSL_LOG(log, INFO, message); \
131  )
132 #define SL_INFO_IF(log, message, cond) __FWCORE_IF_ENABLED( 4, \
133  __FWCORE_IF(cond, log.info(message, __FILE__, __LINE__); ) \
134  )
135 #define OSL_INFO_IF(log, message, cond) __FWCORE_IF_ENABLED( 4, \
136  __FWCORE_IF(cond, OSL_LOG(log, INFO, message); ) \
137  )
138 
139 #define SL_WARN(log, message) __FWCORE_IF_ENABLED( 3, \
140  log.warn(message, __FILE__, __LINE__); \
141  )
142 #define OSL_WARN(log, message) __FWCORE_IF_ENABLED( 3, \
143  OSL_LOG(log, WARN, message); \
144  )
145 #define SL_WARN_IF(log, message, cond) __FWCORE_IF_ENABLED( 3, \
146  __FWCORE_IF(cond, log.warn(message, __FILE__, __LINE__); ) \
147  )
148 #define OSL_WARN_IF(log, message, cond) __FWCORE_IF_ENABLED( 3, \
149  __FWCORE_IF(cond, OSL_LOG(log, WARN, message); ) \
150  )
151 
152 #define SL_ERROR(log, message) __FWCORE_IF_ENABLED( 2, \
153  log.error(message, __FILE__, __LINE__); \
154  )
155 #define OSL_ERROR(log, message) __FWCORE_IF_ENABLED( 2, \
156  OSL_LOG(log, ERROR, message); \
157  )
158 #define SL_ERROR_IF(log, message, cond) __FWCORE_IF_ENABLED( 2, \
159  __FWCORE_IF(cond, log.error(message, __FILE__, __LINE__); ) \
160  )
161 #define OSL_ERROR_IF(log, message, cond) __FWCORE_IF_ENABLED( 2, \
162  __FWCORE_IF(cond, OSL_LOG(log, ERROR, message); ) \
163  )
164 
165 #define SL_FATAL(log, message) __FWCORE_IF_ENABLED( 1, \
166  log.fatal(message, __FILE__, __LINE__); \
167  SPYLOG_ABORT(); \
168  )
169 #define OSL_FATAL(log, message) __FWCORE_IF_ENABLED( 1, \
170  OSL_LOG(log, FATAL, message); \
171  SPYLOG_ABORT(); \
172  )
173 #define SL_FATAL_IF(log, message, cond) __FWCORE_IF_ENABLED( 1, \
174  __FWCORE_IF(cond, SL_FATAL(log, message); ) \
175  )
176 #define OSL_FATAL_IF(log, message, cond) __FWCORE_IF_ENABLED( 1, \
177  __FWCORE_IF(cond, OSL_FATAL(log, message); ) \
178  )
179 
180 // -----------------------------------------------------------------------------
181 
182 # ifdef _DEBUG
183 # ifdef WIN32
184 # define SL_ASSERT(log, message, cond) __FWCORE_EXPR_BLOCK( \
185  __FWCORE_IF(!(cond), \
186  std::stringstream oslStr1; \
187  oslStr1 << "Assertion '" << \
188  #cond << "' failed.\n" << message; \
189  log.fatal(oslStr1.str(), __FILE__, __LINE__); \
190  _CrtDbgReport(_CRT_ASSERT, __FILE__, __LINE__, NULL, "%s", oslStr1.str().c_str()); \
191  __debugbreak(); \
192  ))
193 # else
194 # define SL_ASSERT(log, message, cond) __FWCORE_EXPR_BLOCK( \
195  __FWCORE_IF(!(cond), \
196  std::stringstream oslStr1; \
197  oslStr1 << "Assertion '" << \
198  #cond << "' failed: " << message; \
199  log.fatal(oslStr1.str(), __FILE__, __LINE__); \
200  SPYLOG_ABORT(); \
201  ))
202 # endif
203 
204 # define OSL_ASSERT(log, message, cond) __FWCORE_EXPR_BLOCK( \
205  __FWCORE_IF(!(cond), \
206  std::stringstream oslStr; \
207  oslStr << message; \
208  SL_ASSERT(log, oslStr.str(), cond); \
209  ))
210 # else
211 # define SL_ASSERT(log, message, cond) // empty
212 # define OSL_ASSERT(log, message, cond) // empty
213 # endif
214 
215 // -----------------------------------------------------------------------------
216 
217 # define _SPYLOG_SPYLOGGER_ \
218  ::fwCore::log::SpyLogger::getSpyLogger()
219 
220 // -----------------------------------------------------------------------------
221 
228 # define SLM_TRACE(message) SL_TRACE(_SPYLOG_SPYLOGGER_, message)
229 
230 # define OSLM_TRACE(message) OSL_TRACE(_SPYLOG_SPYLOGGER_, message)
231 
232 # define SLM_TRACE_IF(message, cond) SL_TRACE_IF(_SPYLOG_SPYLOGGER_, message, cond)
233 
234 # define OSLM_TRACE_IF(message, cond) OSL_TRACE_IF(_SPYLOG_SPYLOGGER_, message, cond)
235 
239 # define SLM_DEBUG(message) SL_DEBUG(_SPYLOG_SPYLOGGER_, message)
240 
241 # define OSLM_DEBUG(message) OSL_DEBUG(_SPYLOG_SPYLOGGER_, message)
242 
243 # define SLM_DEBUG_IF(message, cond) SL_DEBUG_IF(_SPYLOG_SPYLOGGER_, message, cond)
244 
245 # define OSLM_DEBUG_IF(message, cond) OSL_DEBUG_IF(_SPYLOG_SPYLOGGER_, message, cond)
246 
250 # define SLM_INFO(message) SL_INFO(_SPYLOG_SPYLOGGER_, message)
251 
252 # define OSLM_INFO(message) OSL_INFO(_SPYLOG_SPYLOGGER_, message)
253 
254 # define SLM_INFO_IF(message, cond) SL_INFO_IF(_SPYLOG_SPYLOGGER_, message, cond)
255 
256 # define OSLM_INFO_IF(message, cond) OSL_INFO_IF(_SPYLOG_SPYLOGGER_, message, cond)
257 
261 # define SLM_WARN(message) SL_WARN(_SPYLOG_SPYLOGGER_, message)
262 
263 # define OSLM_WARN(message) OSL_WARN(_SPYLOG_SPYLOGGER_, message)
264 
265 # define SLM_WARN_IF(message, cond) SL_WARN_IF(_SPYLOG_SPYLOGGER_, message, cond)
266 
267 # define OSLM_WARN_IF(message, cond) OSL_WARN_IF(_SPYLOG_SPYLOGGER_, message, cond)
268 
272 # define SLM_ERROR(message) SL_ERROR(_SPYLOG_SPYLOGGER_, message)
273 
274 # define OSLM_ERROR(message) OSL_ERROR(_SPYLOG_SPYLOGGER_, message)
275 
276 # define SLM_ERROR_IF(message, cond) SL_ERROR_IF(_SPYLOG_SPYLOGGER_, message, cond)
277 
278 # define OSLM_ERROR_IF(message, cond) OSL_ERROR_IF(_SPYLOG_SPYLOGGER_, message, cond)
279 
283 # define SLM_FATAL(message) SL_FATAL(_SPYLOG_SPYLOGGER_, message)
284 
285 # define OSLM_FATAL(message) OSL_FATAL(_SPYLOG_SPYLOGGER_, message)
286 
287 # define SLM_FATAL_IF(message, cond) SL_FATAL_IF(_SPYLOG_SPYLOGGER_, message, cond)
288 
289 # define OSLM_FATAL_IF(message, cond) OSL_FATAL_IF(_SPYLOG_SPYLOGGER_, message, cond)
290 
294 #define OSLM_LOG(message) \
295  __FWCORE_EXPR_BLOCK( \
296  std::stringstream stream; \
297  stream << message; \
298  _SPYLOG_SPYLOGGER_.log(stream.str(), __FILE__, __LINE__); \
299  )
300 
308 # define SLM_ASSERT(message, cond) \
309  SL_ASSERT(_SPYLOG_SPYLOGGER_, message, cond)
310 # define OSLM_ASSERT(message, cond) \
311  OSL_ASSERT(_SPYLOG_SPYLOGGER_, message, cond)
312 
313 // -----------------------------------------------------------------------------
314 
315 # ifdef __GNUC__ // with GCC
316 # define SLM_PRETTY_FUNC() __PRETTY_FUNCTION__
317 # elif defined(_MSC_VER) // with MSC
318 # define SLM_PRETTY_FUNC() __FUNCSIG__
319 # else
320 # define SLM_PRETTY_FUNC() __func__
321 # endif
322 
328 # ifndef SPYLOG_TIMER
329 # define SLM_TRACE_FUNC() SLM_TRACE(SLM_PRETTY_FUNC())
330 # else
331 
332 # define SLM_TRACE_FUNC() __FWCORE_EXPR_BLOCK( \
333  ::fwCore::log::ScopedMessage __spylog__scoped__msg__( \
334  __FILE__, __LINE__, SLM_PRETTY_FUNC()); \
335  __spylog__scoped__msg__.use(); \
336  )
337 
338 # endif
339 
344 #define FW_DEPRECATED(oldFnName, newFnName, version) \
345  OSLM_ERROR( "[DEPRECATED] '" << oldFnName << "' is deprecated and will be removed in '" << version << "', use '" \
346  << newFnName << "' instead. It is still used by '" + this->getClassname() + "'." \
347  );
348 
352 #define FW_DEPRECATED_IF(oldFnName, newFnName, version, condition) \
353  OSLM_ERROR_IF("[DEPRECATED] '" << oldFnName << "' is deprecated and will be removed in '" << version << "', use '" \
354  << newFnName << "' instead. It is still used by '" + this->getClassname() + "'.", \
355  condition);
356 
360 #define FW_DEPRECATED_MSG(message, version) \
361  OSLM_ERROR( "[DEPRECATED] " << message << " It will be removed in '" << version << "'");
362 
366 #define FW_DEPRECATED_KEY(newKey, access, version) \
367  OSLM_ERROR( "[DEPRECATED] The key '" << newKey << "' is not correctly set. Please correct the configuration to " \
368  "set an '" << access << "' key named '" << newKey << "'. The support of the old key will be removed " \
369  "in '" << version << "'.");