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.
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 // When "catching", the program will abort with an error message.
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
52 /* Abort program execution immediately
54 NORETURN extern void fatal_error_fn(
55 const char *msg, const char *file,
56 unsigned int line, const char *function);
58 #define FATAL_ERROR(msg) \
59 fatal_error_fn((msg), __FILE__, __LINE__, FUNCTION_NAME)
61 #define FATAL_ERROR_IF(expr, msg) \
63 ? fatal_error_fn((msg), __FILE__, __LINE__, FUNCTION_NAME) \
68 Equivalent to assert() but persists in Release builds (i.e. when NDEBUG is
72 NORETURN extern void sanity_check_fn(
73 const char *assertion, const char *file,
74 unsigned int line, const char *function);
76 #define SANITY_CHECK(expr) \
79 : sanity_check_fn(#expr, __FILE__, __LINE__, FUNCTION_NAME))
81 #define sanity_check(expr) SANITY_CHECK(expr)
84 void debug_set_exception_handler();
90 #define DEBUG_STACK_SIZE 50
91 #define DEBUG_STACK_TEXT_SIZE 300
93 extern void debug_stacks_print_to(std::ostream &os);
94 extern void debug_stacks_print();
100 DebugStacker(const char *text);
108 #define DSTACK(msg) \
109 DebugStacker __debug_stacker(msg);
111 #define DSTACKF(...) \
112 char __buf[DEBUG_STACK_TEXT_SIZE]; \
113 snprintf(__buf, DEBUG_STACK_TEXT_SIZE, __VA_ARGS__); \
114 DebugStacker __debug_stacker(__buf);
117 These should be put into every thread
120 #if CATCH_UNHANDLED_EXCEPTIONS == 1
121 #define BEGIN_DEBUG_EXCEPTION_HANDLER try {
122 #define END_DEBUG_EXCEPTION_HANDLER \
123 } catch (std::exception &e) { \
124 errorstream << "An unhandled exception occurred: " \
125 << e.what() << std::endl; \
126 FATAL_ERROR(e.what()); \
130 #define BEGIN_DEBUG_EXCEPTION_HANDLER
131 #define END_DEBUG_EXCEPTION_HANDLER