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