KrisLibrary  1.0.0
errors.h
1 #ifndef ERRORS_H
2 #define ERRORS_H
3 
4 #include <KrisLibrary/Logger.h>
5 #include <stdio.h>
6 #include <stdarg.h>
7 
8 /* Aborts and asserts don't give a proper stack trace on cygwin. */
9 #ifdef CYGWIN
10 
11 #ifdef NDEBUG /* required by ANSI standard */
12  #define Assert(p) ((void)0)
13 #else
14 
15  #ifdef __STDC__
16  #define Assert(e) ((e) ? (void)0 : __Assert(__FILE__, __LINE__, #e))
17  #else /* PCC */
18  #define Assert(e) ((e) ? (void)0 : __Assert(__FILE__, __LINE__, "e"))
19  #endif
20 
21 #endif // NDEBUG
22 
23 void Abort();
24 
25 inline void __Assert(const char * file, int line, const char * e)
26 {
27  LOG4CXX_FATAL(KrisLibrary::logger(),"Assertion \""<<e<<"\" failed: file \""<<file<<"\", line "<<line);
28  Abort();
29 }
30 
31 
32 #else
33 
34  #include "assert.h"
35  #include "stdlib.h"
36  #define Assert assert
37  #define Abort abort
38  #define __Assert __assert
39 
40 #endif //CYGWIN
41 
42 inline void RaiseErrorFmt(const char* func, const char* file, int line, const char* fmt, ...)
43 {
44  LOG4CXX_ERROR(KrisLibrary::logger(),"Error in "<< func<<" ("<<file<<":"<<line);
45  va_list args;
46  va_start(args, fmt);
47  char buf[1024];
48  vsnprintf(buf, 1024, fmt, args);
49  LOG4CXX_FATAL(KrisLibrary::logger(),buf);
50  Abort();
51 }
52 
53 inline void RaiseErrorFmt(const char* fmt,...)
54 {
55  LOG4CXX_ERROR(KrisLibrary::logger(),"Error (unknown function): ");
56  va_list args;
57  va_start(args, fmt);
58  char buf[1024];
59  vsnprintf(buf, 1024, fmt, args);
60  LOG4CXX_FATAL(KrisLibrary::logger(),buf);
61  Abort();
62 }
63 
64 
65 inline void RaiseError(const char* func, const char* file, int line, const char* text)
66 {
67  LOG4CXX_FATAL(KrisLibrary::logger(),"Error in "<< func<<" ("<<file<<":"<<line<<"): "<<text);
68  Abort();
69 }
70 
71 
72 
73 //the following is bending over backwards to support MS's lack of
74 //variable argument macro support
75 
76 #ifdef HAVE_PRETTY_FUNCTION
77 #define WHERE_AM_I __PRETTY_FUNCTION__, __FILE__, __LINE__
78 #else
79 #define WHERE_AM_I __FUNCTION__, __FILE__, __LINE__
80 #endif
81 
82 //Error1 is guaranteed to print line numbers with a single-argument error
83 #define FatalError1(text) RaiseError(WHERE_AM_I,text)
84 
85 #if HAVE_VARARGS_MACROS
86 #define FatalError(fmt,...) RaiseErrorFmt(WHERE_AM_I,fmt,__VA_ARGS__)
87 #else
88 //if no variable arguments, can't get any line info
89 #define FatalError RaiseErrorFmt
90 #endif
91 
92 #define PrintLocation(file) fprintf(file,"%s (%s:%d): ", WHERE_AM_I)
93 #define AssertNotReached() RaiseError(WHERE_AM_I,"Code should not be reached")
94 
95 #endif
96 
The logging system used in KrisLibrary.