Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Error.hh
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003-2009 (see file CONTACT for details)
5 Multimediale Systeme der Informationsverarbeitung
6 Institut fuer Informatik
7 Christian-Albrechts-Universitaet Kiel
8 
9 
10 BIAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14 
15 BIAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with BIAS; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 
25 
26 #ifndef __BIASERROR_HH__
27 #define __BIASERROR_HH__
28 
29 
30 /**
31 DEPRECATED macros:
32 - unused in BIAS and MIP and conflict with THROW macro from OpenEXR. (Jan Woetzel 2005/09/06)
33 - THROW, THROW_LIST
34 - DEF_EXCEPTIONS
35 - BIAS_CHECK_NAN
36 */
37 
38 
39 
40 // Jan Woetzel
41 #include "bias_config.h"
42 
43 // assertions are also switched of if COMPILE_NDEBUG is defined
44 #include <assert.h>
45 #include <iostream>
46 #include <sstream>
47 #include <string>
48 /// exit, abort, EXIT_FAILURE,
49 #include <stdlib.h>
50 // required for BIASABORT and BIASEXIT macros (jw)
51 #ifdef WIN32
52 # include <Base/Debug/biasabort.hh>
53 # include <Base/Debug/biasexit.hh>
54 # include <windows.h> // required for OutputDebugString (jw)
55 # ifdef BIAS_HAVE_WXWIDGETS
56 # include <wx/msw/winundef.h>
57 # endif
58 #endif
59 
60 
61 
62 #ifndef ERROR_STREAM
63 # ifdef WIN32
64 // only cout goes to simple W32 console directly (without rdbuf etc.) (JW)
65 # define ERROR_STREAM std::cout
66 # else // WIN32
67 // cout and cout goes to console on Linux
68 # define ERROR_STREAM std::cerr
69 # endif // WIN32
70 #endif // ERROR_STREAM
71 
72 
73 
74 /** @brief macro to display the complete function name a line of code belongs to.
75 See MSDN or g++ docu for details.
76 Works with g++ 3.3 and MS Visual Studo 7.1
77 @author Jan Woetzel */
78 #ifdef WIN32
79 # define FUNCNAME __FUNCTION__
80 #else // WIN32
81 # define FUNCNAME __PRETTY_FUNCTION__
82 #endif // WIN32
83 
84 
85 
86 /** set a breakpoint programmatically
87 This code is handled as a breakpoint exception.
88 Thus a messagebox will pop up where you can choose
89 - to "break" (which means use this like a normal breakpoint)
90 - or "ignore" to continue skipping the breakpoint.
91 Depends on BIAS_DEBUg and not COMPILE_DEBUG,
92 thus works for Debug *and* Release build!
93 Useful to set a breakpoint in code,
94 e.g. in addition to an exit or failure return
95 for MS VS IDE Debugging. VS 71 debugger
96 see http://www.highprogrammer.com/alan/windev/visualstudio.html
97 and http://msdn2.microsoft.com/en-us/library//f408b4et.aspx
98 @author Jan Woetzel */
99 #if defined(BIAS_DEBUG) && defined(WIN32) && defined(_MSC_VER)
100 //#include <intrin.h>
101 //# define BIASBREAK __asm { int 3 }
102 # define BIASBREAK {__debugbreak();}
103 #else
104 # define BIASBREAK {}
105 #endif
106 
107 
108 
109 /** @brief macro for error handling
110 Puts message in Debug mode to a destination and adds file+line for "click+jump" in Xemacs on Linux and Visual Studio on Windows.
111 - The output s written to cerr
112 - *AND* DebugConsole on WIN32
113 - thus you can click and jump to code.
114 In particular you can start main *and* WinMain applications in VS71 debugger and click directly in the "output window" to jump to code.
115 No output is generated on Release compilation to maximize speed.
116 The name of the temporary stringstream has to be unique due to the scope !!!
117 @author Jan Woetzel */
118 #if defined(BIAS_DEBUG) && defined(WIN32)
119 
120 # define BIASERR(arg) { \
121  ERROR_STREAM<<std::flush<<"Error in "<<__FILE__<<":"<<__LINE__<<" : "<<FUNCNAME<<std::endl \
122  <<" "<<arg<<std::endl<<std::flush; \
123  std::stringstream ss_BIASERR_unique4713; \
124  ss_BIASERR_unique4713<<std::endl<<__FILE__<<"("<<__LINE__<<") : "<<FUNCNAME<<std::endl \
125  <<" BIAS Error: "<<arg<<std::endl; \
126  OutputDebugStringA( ss_BIASERR_unique4713.str().c_str() ); \
127 }
128 
129 /** macro for warning msg. including file+line for click and jump
130 The temporaries require unique names due to the scope limitation.
131 @author Jan Woetzel */
132 # define BIASWARN(arg) { \
133  ERROR_STREAM<<std::flush<<"Warning in "<<__FILE__<<":"<<__LINE__<<" : "<<std::endl \
134  <<" "<<arg<<std::endl<<std::flush; \
135  std::stringstream ss_BIASWARN_unique4193; \
136  ss_BIASWARN_unique4193<<std::endl<<__FILE__<<"("<<__LINE__<<") : "<<std::endl \
137  <<" BIAS Warning: "<<arg<<std::endl; \
138  OutputDebugStringA( ss_BIASWARN_unique4193.str().c_str() ); \
139 }
140 
141 #else // defined(BIAS_DEBUG) && defined(WIN32)
142 
143 // all other: output format for Emacs click and jump (JW)
144 # define BIASERR(arg) { \
145  ERROR_STREAM<<std::flush<<"Error in "<<__FILE__<<":"<<__LINE__<<" : "<<FUNCNAME<<"\n" \
146  <<" "<<arg<<std::endl<<std::flush; \
147 }
148 
149 # define BIASWARN(arg) { \
150  ERROR_STREAM<<std::flush<<"Warning in "<<__FILE__<<":"<<__LINE__<<" :\n\t" \
151  <<" "<<arg<<std::endl<<std::flush; \
152 }
153 
154 #endif // defined(BIAS_DEBUG) && defined(WIN32)
155 
156 
157 
158 /** Issue a warning only once and only in debug mode at runtime.
159 Avoids flooding with warning noise.
160 Particularly useful to issue a runtime warning in an inner loop.
161 @author Jan Woetzel */
162 #if defined (BIAS_DEBUG) && defined(WIN32)
163 
164 # define BIASWARNONCE(arg) { \
165  static bool _oncerun_BIASWARNONCE_localscope_=false;\
166  if (!_oncerun_BIASWARNONCE_localscope_) { \
167  ERROR_STREAM<<std::flush<<"Warning once in "<<__FILE__<<":"<<__LINE__<<" :"<<std::endl \
168  <<"\t"<<arg<<std::endl<<std::flush; \
169  std::stringstream ss_BIASWARN_unique4193; \
170  ss_BIASWARN_unique4193<<std::endl<<__FILE__<<"("<<__LINE__<<") : "<<std::endl \
171  <<"\tBIAS Warning: "<<arg<<std::endl; \
172  OutputDebugStringA( ss_BIASWARN_unique4193.str().c_str() ); \
173  _oncerun_BIASWARNONCE_localscope_ = true;\
174  }\
175 }
176 
177 #elif defined (BIAS_DEBUG) && !defined(WIN32)
178 
179 # define BIASWARNONCE(arg) { \
180  static bool _oncerun_BIASWARNONCE_localscope_=false;\
181  if (!_oncerun_BIASWARNONCE_localscope_) { \
182  ERROR_STREAM<<std::flush<<"Warning once in "<<__FILE__<<":"<<__LINE__<<" :\n\t" \
183  <<"\t"<<arg<<std::endl<<std::flush; \
184  _oncerun_BIASWARNONCE_localscope_ = true;\
185  }\
186 }
187 
188 #else // ! BIAS_DEBUG
189 
190 # define BIASWARNONCE(arg)
191 
192 #endif // WIN32
193 
194 
195 /** macro around new catching exceptions */
196 #define BIASNEW(arg1,arg2)\
197  try {arg1= new arg2;} catch (std::bad_alloc){ BIASERR("new failed");};
198 
199 
200 /** @brief macro to be used instead of abort()
201 \todo: call MIP::biasabort instead of abort - but this introduces one library dependency.
202 @author Jan Woetzel **/
203 #if defined(WIN32) && defined(_MSC_VER)
204 #define BIASABORT { \
205  std::cout<<std::flush; std::cerr<<std::flush \
206  <<__FILE__<<"("<<__LINE__<<") : aborting."<<std::endl<<std::flush; \
207  std::stringstream ss_BIASABORT_unique_1375; \
208  ss_BIASABORT_unique_1375<<std::endl \
209  <<__FILE__<<"("<<__LINE__<<") : aborting."<<std::endl; \
210  OutputDebugStringA( ss_BIASABORT_unique_1375.str().c_str() ); \
211  __debugbreak(); \
212  BIAS::biasabort(); \
213 }
214 #else
215 #define BIASABORT { \
216  std::cout<<std::flush; std::cerr<<std::flush \
217  <<__FILE__<<", "<<__LINE__<<" aborting."<<std::endl<<std::flush; \
218  abort(); \
219 }
220 #endif
221 
222 
223 
224 /** @brief macro to be used instead of assert()
225 Expands to output on debug and on release:
226 -Linux release nothing,
227 -Windows release: Attention!
228 Assume that condition holds and let the compiler exploit
229 this knowledge for optimization.
230 Be warned: This may unintentionally optimize away your error handling code!
231 see http://msdn2.microsoft.com/en-us/library//1b3fsfxw.aspx
232 @author Jan Woetzel **/
233 #if defined(BIAS_DEBUG) && defined(WIN32) && defined(COMPILE_DEBUG) && defined(_MSC_VER) && !defined(COMPILE_NDEBUG)
234 // Windows and Microsoft compiler, break in debugger
235 # define BIASASSERT(cond) { \
236  if (!(cond)) { \
237  std::cout<<std::flush; \
238  std::cerr<<std::flush; \
239  std::cerr \
240  <<__FILE__<<"("<<__LINE__<<") : BIASASSERT failed. "<<std::endl<<std::flush; \
241  std::stringstream ss_BIASASSERT_unique_1142; \
242  ss_BIASASSERT_unique_1142<<std::endl \
243  <<__FILE__<<"("<<__LINE__<<") : BIASASSERT failed. "<<std::endl; \
244  OutputDebugStringA( ss_BIASASSERT_unique_1142.str().c_str() ); \
245  __debugbreak(); \
246  }; \
247  assert(cond); \
248 }
249 #define BIASASSERT_ISACTIVE
250 
251 #elif defined(BIAS_DEBUG) && defined(WIN32) && defined(COMPILE_DEBUG) && !defined(_MSC_VER) && !defined(COMPILE_NDEBUG)
252 // Windows - but possibly not Micrsoft compiler, drop __debugbreak
253 # define BIASASSERT(cond) { \
254  if (!(cond)) { \
255  std::cout<<std::flush; \
256  std::cerr<<std::flush; \
257  std::cerr \
258  <<__FILE__<<"("<<__LINE__<<") : BIASASSERT failed. "<<std::endl<<std::flush; \
259  std::stringstream ss_BIASASSERT_unique_1142; \
260  ss_BIASASSERT_unique_1142<<std::endl \
261  <<__FILE__<<"("<<__LINE__<<") : BIASASSERT failed. "<<std::endl; \
262  OutputDebugStringA( ss_BIASASSERT_unique_1142.str().c_str() ); \
263  }; \
264  assert(cond); \
265 }
266 #define BIASASSERT_ISACTIVE
267 
268 #elif defined(BIAS_DEBUG) && !defined(WIN32) && !defined(COMPILE_NDEBUG)
269 // Linux - xemacs output format
270 # define BIASASSERT(cond) { \
271  if (!(cond)) { \
272  std::cout<<std::flush; \
273  std::cerr<<std::flush; \
274  std::cerr \
275  <<__FILE__<<","<<__LINE__<<" : BIASASSERT failed. "<<std::endl<<std::flush; \
276  }; \
277  assert(cond); \
278 }
279 #define BIASASSERT_ISACTIVE
280 
281 /// to be discussed for opimizing builds - assume conditions hold. (JW)
282 //#elif !defined(BIAS_DEBUG) && defined(WIN32)
283 //# define BIASASSERT(cond) __assume(cond)
284 
285 #else
286 # define BIASASSERT(cond)
287 #endif
288 
289 
290 /** @brief macro to be used instead of exit(int)
291 Sample usage:
292 \verbatim
293  BIASEXIT(EXIT_FAILIRE, "my short description");
294 \endverbatim
295 @autor Jan Woetzel **/
296 #if defined(WIN32) && defined(_MSC_VER)
297 #define BIASEXIT(exitvalue_, mesg_) { \
298  std::cerr<<std::flush<<std::endl<<"exit request in "<<__FILE__<<","<<__LINE__<<" : ret="<<exitvalue_<<" "<<std::endl<<mesg_<<std::endl; \
299  __debugbreak(); \
300  BIAS::biasexit(exitvalue_); \
301 }
302 
303 #else // WIN32
304 
305 #define BIASEXIT(exitvalue_, mesg_) { \
306  std::cerr<<std::flush<<std::endl<<"exit request in "<<__FILE__<<","<<__LINE__<<" : ret="<<exitvalue_<<" "<<std::endl<<mesg_<<std::endl; \
307  exit(exitvalue_); \
308 }
309 #endif // WIN32
310 
311 
312 /** verbose writing calling information to screen in Debug mode.
313 Typical usage:
314 \verbatim
315 void MyClass::MyFunction(...)
316 {
317  CALLINFO; ...
318 }
319 \endverbatim
320 @author Jan Woetzel */
321 #ifdef BIAS_DEBUG
322 # define CALLINFO {std::cout<<"called "<<__FILE__<<", "<<__LINE__<<": "<<FUNCNAME<<std::endl<<std::flush;}
323 #else //BIAS_DEBUG
324 # define CALLINFO
325 #endif // BIAS_DEBUG
326 
327 
328 
329 /** alias for pthread C interface for mutex locking */
330 #define BIAS_MUTEX_LOCK(arg) pthread_mutex_lock(arg);
331 #define BIAS_MUTEX_UNLOCK(arg) pthread_mutex_unlock(arg);
332 
333 
334 
335 /** DEPRECATED - JW 12/2005
336 - unused in BIAS,MIP,S2DT
337 - bad style, please use templated isnan/isinf etc. */
338 #define BIAS_CHECK_NAN BIAS_CHECK_NAN_is_deprecated_because_never_used_see_BIAS_Error_hh
339 
340 
341 /** DEPRECATED - JW 12/2005
342 not used (at least in BIAS,MIP,SCan2DTracking... ) */
343 /*
344 //#define DEF_EXCEPTION(theClass, theText) \
345 //class theClass : public Exception { theClass() : Exception(theText) {} };
346 */
347 //#define DEF_EXCEPTION DEF_EXCEPTION_is_deprecated_beause_never_used_see_BIAS_Error_hh
348 
349 
350 /**
351 not finished... to be continued... (JW)
352 checked_cast uses fast static_cast in Release build
353 but checks cast with an ASSERT in Debug.
354 Typical usage:
355 class Foo { ... };
356 class Bar : public Foo { ... };
357 Foo * pFoo = new Bar;
358 Bar * pBar = checked_cast<Bar *>(pFoo);
359 see inspiration source: http://www.highprogrammer.com/alan/windev/visualstudio.html
360 and http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmex/html/vclrf__try_cast.asp
361 and http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclang/html/_pluslang_Casting_Operators.asp
362 -- does not (yet) work as expected, furterh work required (JW) --
363 @author Jan Woetzel 2005 */
364 /*
365 //template <class destT, class srcT>
366 //TypeTo checked_cast(srcT p)
367 //{
368 // BIASASSERT(dynamic_cast<destT>(p));
369 // return static_cast<destT>(p);
370 //};
371 */
372 
373 #endif // __BIASERROR_HH__
374