3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #if (defined(WIN32) || defined(_WIN32_WCE))
28 #define WIN32_LEAN_AND_MEAN
30 #define _WIN32_WINNT 0x0501
36 #define __NORETURN __declspec(noreturn)
37 #define __FUNCTION_NAME __FUNCTION__
39 #define __NORETURN __attribute__ ((__noreturn__))
40 #define __FUNCTION_NAME __PRETTY_FUNCTION__
43 // Whether to catch all std::exceptions.
44 // Assert will be called on such an event.
45 // In debug mode, leave these for the debugger and don't catch them.
47 #define CATCH_UNHANDLED_EXCEPTIONS 1
49 #define CATCH_UNHANDLED_EXCEPTIONS 0
56 #define DTIME (getTimestamp()+": ")
58 extern void debugstreams_init(bool disable_stderr, const char *filename);
59 extern void debugstreams_deinit();
61 // This is used to redirect output to /dev/null
62 class Nullstream : public std::ostream {
71 extern std::ostream dstream;
72 extern std::ostream dstream_no_stderr;
73 extern Nullstream dummyout;
79 __NORETURN extern void assert_fail(
80 const char *assertion, const char *file,
81 unsigned int line, const char *function);
86 : assert_fail(#expr, __FILE__, __LINE__, __FUNCTION_NAME))
88 #define assert(expr) ASSERT(expr)
94 #define DEBUG_STACK_SIZE 50
95 #define DEBUG_STACK_TEXT_SIZE 300
97 extern void debug_stacks_init();
98 extern void debug_stacks_print_to(std::ostream &os);
99 extern void debug_stacks_print();
105 DebugStacker(const char *text);
114 DebugStacker __debug_stacker(msg);
116 #define DSTACKF(...)\
117 char __buf[DEBUG_STACK_TEXT_SIZE];\
119 DEBUG_STACK_TEXT_SIZE, __VA_ARGS__);\
120 DebugStacker __debug_stacker(__buf);
123 These should be put into every thread
126 #if CATCH_UNHANDLED_EXCEPTIONS == 1
127 #define BEGIN_PORTABLE_DEBUG_EXCEPTION_HANDLER try{
128 #define END_PORTABLE_DEBUG_EXCEPTION_HANDLER(logstream)\
129 }catch(std::exception &e){\
130 logstream<<"ERROR: An unhandled exception occurred: "\
131 <<e.what()<<std::endl;\
134 #ifdef _WIN32 // Windows
135 #ifdef _MSC_VER // MSVC
136 void se_trans_func(unsigned int, EXCEPTION_POINTERS*);
137 #define BEGIN_DEBUG_EXCEPTION_HANDLER \
138 BEGIN_PORTABLE_DEBUG_EXCEPTION_HANDLER\
139 _set_se_translator(se_trans_func);
141 #define END_DEBUG_EXCEPTION_HANDLER(logstream) \
142 END_PORTABLE_DEBUG_EXCEPTION_HANDLER(logstream)
143 #else // Probably mingw
144 #define BEGIN_DEBUG_EXCEPTION_HANDLER\
145 BEGIN_PORTABLE_DEBUG_EXCEPTION_HANDLER
146 #define END_DEBUG_EXCEPTION_HANDLER(logstream)\
147 END_PORTABLE_DEBUG_EXCEPTION_HANDLER(logstream)
150 #define BEGIN_DEBUG_EXCEPTION_HANDLER\
151 BEGIN_PORTABLE_DEBUG_EXCEPTION_HANDLER
152 #define END_DEBUG_EXCEPTION_HANDLER(logstream)\
153 END_PORTABLE_DEBUG_EXCEPTION_HANDLER(logstream)
157 #define BEGIN_DEBUG_EXCEPTION_HANDLER
158 #define END_DEBUG_EXCEPTION_HANDLER(logstream)
161 #endif // DEBUG_HEADER