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  va_end(args);
50  LOG4CXX_FATAL(KrisLibrary::logger(),buf);
51  Abort();
52 }
53 
54 inline void RaiseErrorFmt(const char* fmt,...)
55 {
56  LOG4CXX_ERROR(KrisLibrary::logger(),"Error (unknown function): ");
57  va_list args;
58  va_start(args, fmt);
59  char buf[1024];
60  vsnprintf(buf, 1024, fmt, args);
61  va_end(args);
62  LOG4CXX_FATAL(KrisLibrary::logger(),buf);
63  Abort();
64 }
65 
66 
67 inline void RaiseError(const char* func, const char* file, int line, const char* text)
68 {
69  LOG4CXX_FATAL(KrisLibrary::logger(),"Error in "<< func<<" ("<<file<<":"<<line<<"): "<<text);
70  Abort();
71 }
72 
73 
74 
75 //the following is bending over backwards to support MS's lack of
76 //variable argument macro support
77 
78 #ifdef HAVE_PRETTY_FUNCTION
79 #define WHERE_AM_I __PRETTY_FUNCTION__, __FILE__, __LINE__
80 #else
81 #define WHERE_AM_I __FUNCTION__, __FILE__, __LINE__
82 #endif
83 
84 //Error1 is guaranteed to print line numbers with a single-argument error
85 #define FatalError1(text) RaiseError(WHERE_AM_I,text)
86 
87 #if HAVE_VARARGS_MACROS
88 #define FatalError(fmt,...) RaiseErrorFmt(WHERE_AM_I,fmt,__VA_ARGS__)
89 #else
90 //if no variable arguments, can't get any line info
91 #define FatalError RaiseErrorFmt
92 #endif
93 
94 #define PrintLocation(file) fprintf(file,"%s (%s:%d): ", WHERE_AM_I)
95 #define AssertNotReached() RaiseError(WHERE_AM_I,"Code should not be reached")
96 
97 #endif
98 
The logging system used in KrisLibrary.