source: box/trunk/lib/common/Box.h @ 3101

Revision 3101, 5.9 KB checked in by chris, 4 weeks ago (diff)

Allow hiding specific exceptions to keep test output cleaner.

  • Property svn:eol-style set to native
Line 
1// --------------------------------------------------------------------------
2//
3// File
4//              Name:    Box.h
5//              Purpose: Main header file for the Box project
6//              Created: 2003/07/08
7//
8// --------------------------------------------------------------------------
9
10#ifndef BOX__H
11#define BOX__H
12
13// Use the same changes as gcc3 for gcc4
14#ifdef PLATFORM_GCC4
15        #define PLATFORM_GCC3
16#endif
17
18#include "BoxPlatform.h"
19
20#include <memory>
21
22// uncomment this line to enable full memory leak finding on all
23// malloc-ed blocks (at least, ones used by the STL)
24//#define MEMLEAKFINDER_FULL_MALLOC_MONITORING
25
26// Show backtraces on exceptions in release builds until further notice
27// (they are only logged at TRACE level anyway)
28#ifdef HAVE_EXECINFO_H
29        #define SHOW_BACKTRACE_ON_EXCEPTION
30#endif
31
32#ifdef SHOW_BACKTRACE_ON_EXCEPTION
33        #include "Utils.h"
34        #define OPTIONAL_DO_BACKTRACE DumpStackBacktrace();
35#else
36        #define OPTIONAL_DO_BACKTRACE
37#endif
38
39#include "CommonException.h"
40#include "Logging.h"
41
42#ifndef BOX_RELEASE_BUILD
43       
44        extern bool AssertFailuresToSyslog;
45        #define ASSERT_FAILS_TO_SYSLOG_ON {AssertFailuresToSyslog = true;}
46        void BoxDebugAssertFailed(const char *cond, const char *file, int line);
47        #define ASSERT(cond) \
48        { \
49                if(!(cond)) \
50                { \
51                        BoxDebugAssertFailed(#cond, __FILE__, __LINE__); \
52                        THROW_EXCEPTION_MESSAGE(CommonException, \
53                                AssertFailed, #cond); \
54                } \
55        }
56
57        // Note that syslog tracing is independent of BoxDebugTraceOn,
58        // but stdout tracing is not
59        extern bool BoxDebugTraceToSyslog;
60        #define TRACE_TO_SYSLOG(x) {BoxDebugTraceToSyslog = x;}
61        extern bool BoxDebugTraceToStdout;
62        #define TRACE_TO_STDOUT(x) {BoxDebugTraceToStdout = x;}
63
64        extern bool BoxDebugTraceOn;
65        int BoxDebug_printf(const char *format, ...);
66        int BoxDebugTrace(const char *format, ...);
67       
68        #ifndef PLATFORM_DISABLE_MEM_LEAK_TESTING
69                #define BOX_MEMORY_LEAK_TESTING
70        #endif
71       
72        // Exception names
73        #define EXCEPTION_CODENAMES_EXTENDED
74       
75#else
76        #define ASSERT_FAILS_TO_SYSLOG_ON
77        #define ASSERT(cond)
78
79        #define TRACE_TO_SYSLOG(x) {}
80        #define TRACE_TO_STDOUT(x) {}
81
82        // Box Backup builds release get extra information for exception logging
83        #define EXCEPTION_CODENAMES_EXTENDED
84        #define EXCEPTION_CODENAMES_EXTENDED_WITH_DESCRIPTION
85       
86#endif
87
88#ifdef BOX_MEMORY_LEAK_TESTING
89        // Memory leak testing
90        #include "MemLeakFinder.h"
91        #define DEBUG_NEW new(__FILE__,__LINE__)
92        #define MEMLEAKFINDER_NOT_A_LEAK(x)     memleakfinder_notaleak(x);
93        #define MEMLEAKFINDER_NO_LEAKS          MemLeakSuppressionGuard _guard;
94        #define MEMLEAKFINDER_INIT              memleakfinder_init();
95        #define MEMLEAKFINDER_START {memleakfinder_global_enable = true;}
96        #define MEMLEAKFINDER_STOP  {memleakfinder_global_enable = false;}
97#else
98        #define DEBUG_NEW new
99        #define MEMLEAKFINDER_NOT_A_LEAK(x)
100        #define MEMLEAKFINDER_NO_LEAKS
101        #define MEMLEAKFINDER_INIT
102        #define MEMLEAKFINDER_START
103        #define MEMLEAKFINDER_STOP
104#endif
105
106#define THROW_EXCEPTION(type, subtype) \
107        { \
108                if((!HideExceptionMessageGuard::ExceptionsHidden() \
109                        && !HideSpecificExceptionGuard::IsHidden( \
110                                type::ExceptionType, type::subtype)) \
111                        || Logging::Guard::IsGuardingFrom(Log::EVERYTHING)) \
112                { \
113                        std::auto_ptr<Logging::Guard> guard; \
114                        \
115                        if(Logging::Guard::IsGuardingFrom(Log::EVERYTHING)) \
116                        { \
117                                guard.reset(new Logging::Guard(Log::EVERYTHING)); \
118                        } \
119                        \
120                        OPTIONAL_DO_BACKTRACE \
121                        BOX_WARNING("Exception thrown: " \
122                                #type "(" #subtype ") " \
123                                "at " __FILE__ "(" << __LINE__ << ")") \
124                } \
125                throw type(type::subtype); \
126        }
127
128#define THROW_EXCEPTION_MESSAGE(type, subtype, message) \
129        { \
130                std::ostringstream _box_throw_line; \
131                _box_throw_line << message; \
132                if((!HideExceptionMessageGuard::ExceptionsHidden() \
133                        && !HideSpecificExceptionGuard::IsHidden( \
134                                type::ExceptionType, type::subtype)) \
135                        || Logging::Guard::IsGuardingFrom(Log::EVERYTHING)) \
136                { \
137                        std::auto_ptr<Logging::Guard> guard; \
138                        \
139                        if(Logging::Guard::IsGuardingFrom(Log::EVERYTHING)) \
140                        { \
141                                guard.reset(new Logging::Guard(Log::EVERYTHING)); \
142                        } \
143                        \
144                        OPTIONAL_DO_BACKTRACE \
145                        BOX_WARNING("Exception thrown: " \
146                                #type "(" #subtype ") (" << \
147                                _box_throw_line.str() << \
148                                ") at " __FILE__ ":" << __LINE__) \
149                } \
150                throw type(type::subtype, _box_throw_line.str()); \
151        }
152
153// extra macros for converting to network byte order
154#ifdef HAVE_NETINET_IN_H
155        #include <netinet/in.h>
156#endif
157
158// Always define a swap64 function, as it's useful.
159inline uint64_t box_swap64(uint64_t x)
160{
161        return ((x & 0xff) << 56 |
162                (x & 0xff00LL) << 40 |
163                (x & 0xff0000LL) << 24 |
164                (x & 0xff000000LL) << 8 |
165                (x & 0xff00000000LL) >> 8 |
166                (x & 0xff0000000000LL) >> 24 |
167                (x & 0xff000000000000LL) >> 40 |
168                (x & 0xff00000000000000LL) >> 56);
169}
170
171#ifdef WORDS_BIGENDIAN
172        #define box_hton64(x) (x)
173        #define box_ntoh64(x) (x)
174#elif defined(HAVE_BSWAP64)
175        #ifdef HAVE_SYS_ENDIAN_H
176                #include <sys/endian.h>
177        #endif
178        #ifdef HAVE_ASM_BYTEORDER_H
179                #include <asm/byteorder.h>
180        #endif
181
182        #define box_hton64(x) BSWAP64(x)
183        #define box_ntoh64(x) BSWAP64(x)
184#else
185        #define box_hton64(x) box_swap64(x)
186        #define box_ntoh64(x) box_swap64(x)
187#endif
188
189// overloaded auto-conversion functions
190inline uint64_t hton(uint64_t in) { return box_hton64(in); }
191inline uint32_t hton(uint32_t in) { return htonl(in); }
192inline uint16_t hton(uint16_t in) { return htons(in); }
193inline uint8_t  hton(uint8_t in)  { return in; }
194inline int64_t  hton(int64_t in)  { return box_hton64(in); }
195inline int32_t  hton(int32_t in)  { return htonl(in); }
196inline int16_t  hton(int16_t in)  { return htons(in); }
197inline int8_t   hton(int8_t in)   { return in; }
198inline uint64_t ntoh(uint64_t in) { return box_ntoh64(in); }
199inline uint32_t ntoh(uint32_t in) { return ntohl(in); }
200inline uint16_t ntoh(uint16_t in) { return ntohs(in); }
201inline uint8_t  ntoh(uint8_t in)  { return in; }
202inline int64_t  ntoh(int64_t in)  { return box_ntoh64(in); }
203inline int32_t  ntoh(int32_t in)  { return ntohl(in); }
204inline int16_t  ntoh(int16_t in)  { return ntohs(in); }
205inline int8_t   ntoh(int8_t in)   { return in; }
206
207#endif // BOX__H
208
Note: See TracBrowser for help on using the repository browser.