Line data Source code
1 : /*
2 : Minetest
3 : Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4 :
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.
9 :
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.
14 :
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.
18 : */
19 :
20 : #ifndef DEBUG_HEADER
21 : #define DEBUG_HEADER
22 :
23 : #include <iostream>
24 : #include <exception>
25 : #include <assert.h>
26 : #include "gettime.h"
27 :
28 : #if (defined(WIN32) || defined(_WIN32_WCE))
29 : #define WIN32_LEAN_AND_MEAN
30 : #ifndef _WIN32_WINNT
31 : #define _WIN32_WINNT 0x0501
32 : #endif
33 : #include <windows.h>
34 : #ifdef _MSC_VER
35 : #include <eh.h>
36 : #endif
37 : #define __NORETURN __declspec(noreturn)
38 : #define __FUNCTION_NAME __FUNCTION__
39 : #else
40 : #define __NORETURN __attribute__ ((__noreturn__))
41 : #define __FUNCTION_NAME __PRETTY_FUNCTION__
42 : #endif
43 :
44 : // Whether to catch all std::exceptions.
45 : // Assert will be called on such an event.
46 : // In debug mode, leave these for the debugger and don't catch them.
47 : #ifdef NDEBUG
48 : #define CATCH_UNHANDLED_EXCEPTIONS 1
49 : #else
50 : #define CATCH_UNHANDLED_EXCEPTIONS 0
51 : #endif
52 :
53 : /*
54 : Debug output
55 : */
56 :
57 : #define DTIME (getTimestamp()+": ")
58 :
59 : extern void debugstreams_init(bool disable_stderr, const char *filename);
60 : extern void debugstreams_deinit();
61 :
62 : // This is used to redirect output to /dev/null
63 1 : class Nullstream : public std::ostream {
64 : public:
65 1 : Nullstream():
66 1 : std::ostream(0)
67 : {
68 1 : }
69 : private:
70 : };
71 :
72 : extern std::ostream dstream;
73 : extern std::ostream dstream_no_stderr;
74 : extern Nullstream dummyout;
75 :
76 :
77 : /* Abort program execution immediately
78 : */
79 : __NORETURN extern void fatal_error_fn(
80 : const char *msg, const char *file,
81 : unsigned int line, const char *function);
82 :
83 : #define FATAL_ERROR(msg) \
84 : fatal_error_fn((msg), __FILE__, __LINE__, __FUNCTION_NAME)
85 :
86 : #define FATAL_ERROR_IF(expr, msg) \
87 : ((expr) \
88 : ? fatal_error_fn((msg), __FILE__, __LINE__, __FUNCTION_NAME) \
89 : : (void)(0))
90 :
91 : /*
92 : sanity_check()
93 : Equivalent to assert() but persists in Release builds (i.e. when NDEBUG is
94 : defined)
95 : */
96 :
97 : __NORETURN extern void sanity_check_fn(
98 : const char *assertion, const char *file,
99 : unsigned int line, const char *function);
100 :
101 : #define SANITY_CHECK(expr) \
102 : ((expr) \
103 : ? (void)(0) \
104 : : sanity_check_fn(#expr, __FILE__, __LINE__, __FUNCTION_NAME))
105 :
106 : #define sanity_check(expr) SANITY_CHECK(expr)
107 :
108 :
109 : void debug_set_exception_handler();
110 :
111 : /*
112 : DebugStack
113 : */
114 :
115 : #define DEBUG_STACK_SIZE 50
116 : #define DEBUG_STACK_TEXT_SIZE 300
117 :
118 : extern void debug_stacks_init();
119 : extern void debug_stacks_print_to(std::ostream &os);
120 : extern void debug_stacks_print();
121 :
122 : struct DebugStack;
123 : class DebugStacker
124 : {
125 : public:
126 : DebugStacker(const char *text);
127 : ~DebugStacker();
128 :
129 : private:
130 : DebugStack *m_stack;
131 : bool m_overflowed;
132 : };
133 :
134 : #define DSTACK(msg) \
135 : DebugStacker __debug_stacker(msg);
136 :
137 : #define DSTACKF(...) \
138 : char __buf[DEBUG_STACK_TEXT_SIZE]; \
139 : snprintf(__buf, DEBUG_STACK_TEXT_SIZE, __VA_ARGS__); \
140 : DebugStacker __debug_stacker(__buf);
141 :
142 : /*
143 : These should be put into every thread
144 : */
145 :
146 : #if CATCH_UNHANDLED_EXCEPTIONS == 1
147 : #define BEGIN_DEBUG_EXCEPTION_HANDLER try {
148 : #define END_DEBUG_EXCEPTION_HANDLER(logstream) \
149 : } catch (std::exception &e) { \
150 : logstream << "ERROR: An unhandled exception occurred: " \
151 : << e.what() << std::endl; \
152 : assert(0); \
153 : }
154 : #else
155 : // Dummy ones
156 : #define BEGIN_DEBUG_EXCEPTION_HANDLER
157 : #define END_DEBUG_EXCEPTION_HANDLER(logstream)
158 : #endif
159 :
160 : #endif // DEBUG_HEADER
161 :
162 :
|