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.
29 #if (defined(WIN32) || defined(_WIN32_WCE))
30 #define WIN32_LEAN_AND_MEAN
32 #define _WIN32_WINNT 0x0501
38 #define __NORETURN __declspec(noreturn)
39 #define __FUNCTION_NAME __FUNCTION__
40 #define NORETURN __declspec(noreturn)
41 #define FUNCTION_NAME __FUNCTION__
43 #define __NORETURN __attribute__ ((__noreturn__))
44 #define __FUNCTION_NAME __PRETTY_FUNCTION__
45 #define NORETURN __attribute__ ((__noreturn__))
46 #define FUNCTION_NAME __PRETTY_FUNCTION__
49 // Whether to catch all std::exceptions.
50 // When "catching", the program will abort with an error message.
51 // In debug mode, leave these for the debugger and don't catch them.
53 #define CATCH_UNHANDLED_EXCEPTIONS 1
55 #define CATCH_UNHANDLED_EXCEPTIONS 0
58 /* Abort program execution immediately
60 __NORETURN extern void fatal_error_fn(
61 const char *msg, const char *file,
62 unsigned int line, const char *function);
64 #define FATAL_ERROR(msg) \
65 fatal_error_fn((msg), __FILE__, __LINE__, __FUNCTION_NAME)
67 #define FATAL_ERROR_IF(expr, msg) \
69 ? fatal_error_fn((msg), __FILE__, __LINE__, __FUNCTION_NAME) \
74 Equivalent to assert() but persists in Release builds (i.e. when NDEBUG is
78 __NORETURN extern void sanity_check_fn(
79 const char *assertion, const char *file,
80 unsigned int line, const char *function);
82 #define SANITY_CHECK(expr) \
85 : sanity_check_fn(#expr, __FILE__, __LINE__, __FUNCTION_NAME))
87 #define sanity_check(expr) SANITY_CHECK(expr)
90 void debug_set_exception_handler();
98 #define DEBUG_STACK_SIZE 50
99 #define DEBUG_STACK_TEXT_SIZE 300
101 extern void debug_stacks_init();
102 extern void debug_stacks_print_to(std::ostream &os);
103 extern void debug_stacks_print();
109 DebugStacker(const char *text);
117 #define DSTACK(msg) \
118 DebugStacker __debug_stacker(msg);
120 #define DSTACKF(...) \
121 char __buf[DEBUG_STACK_TEXT_SIZE]; \
122 snprintf(__buf, DEBUG_STACK_TEXT_SIZE, __VA_ARGS__); \
123 DebugStacker __debug_stacker(__buf);
126 These should be put into every thread
129 #if CATCH_UNHANDLED_EXCEPTIONS == 1
130 #define BEGIN_DEBUG_EXCEPTION_HANDLER try {
131 #define END_DEBUG_EXCEPTION_HANDLER(logstream) \
132 } catch (std::exception &e) { \
133 logstream << "An unhandled exception occurred: " \
134 << e.what() << std::endl; \
135 FATAL_ERROR(e.what()); \
139 #define BEGIN_DEBUG_EXCEPTION_HANDLER
140 #define END_DEBUG_EXCEPTION_HANDLER(logstream)
143 #endif // DEBUG_HEADER