]> git.lizzy.rs Git - dragonfireclient.git/blob - src/debug.h
RemotePlayer/LocalPlayer Player base class proper separation (code cleanup) (patch...
[dragonfireclient.git] / src / debug.h
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 #include "log.h"
28
29 #if (defined(WIN32) || defined(_WIN32_WCE))
30         #define WIN32_LEAN_AND_MEAN
31         #ifndef _WIN32_WINNT
32                 #define _WIN32_WINNT 0x0501
33         #endif
34         #include <windows.h>
35         #ifdef _MSC_VER
36                 #include <eh.h>
37         #endif
38         #define NORETURN __declspec(noreturn)
39         #define FUNCTION_NAME __FUNCTION__
40 #else
41         #define NORETURN __attribute__ ((__noreturn__))
42         #define FUNCTION_NAME __PRETTY_FUNCTION__
43 #endif
44
45 // Whether to catch all std::exceptions.
46 // When "catching", the program will abort with an error message.
47 // In debug mode, leave these for the debugger and don't catch them.
48 #ifdef NDEBUG
49         #define CATCH_UNHANDLED_EXCEPTIONS 1
50 #else
51         #define CATCH_UNHANDLED_EXCEPTIONS 0
52 #endif
53
54 /* Abort program execution immediately
55  */
56 NORETURN extern void fatal_error_fn(
57                 const char *msg, const char *file,
58                 unsigned int line, const char *function);
59
60 #define FATAL_ERROR(msg) \
61         fatal_error_fn((msg), __FILE__, __LINE__, FUNCTION_NAME)
62
63 #define FATAL_ERROR_IF(expr, msg) \
64         ((expr) \
65         ? fatal_error_fn((msg), __FILE__, __LINE__, FUNCTION_NAME) \
66         : (void)(0))
67
68 /*
69         sanity_check()
70         Equivalent to assert() but persists in Release builds (i.e. when NDEBUG is
71         defined)
72 */
73
74 NORETURN extern void sanity_check_fn(
75                 const char *assertion, const char *file,
76                 unsigned int line, const char *function);
77
78 #define SANITY_CHECK(expr) \
79         ((expr) \
80         ? (void)(0) \
81         : sanity_check_fn(#expr, __FILE__, __LINE__, FUNCTION_NAME))
82
83 #define sanity_check(expr) SANITY_CHECK(expr)
84
85
86 void debug_set_exception_handler();
87
88 /*
89         DebugStack
90 */
91
92 #define DEBUG_STACK_SIZE 50
93 #define DEBUG_STACK_TEXT_SIZE 300
94
95 extern void debug_stacks_init();
96 extern void debug_stacks_print_to(std::ostream &os);
97 extern void debug_stacks_print();
98
99 struct DebugStack;
100 class DebugStacker
101 {
102 public:
103         DebugStacker(const char *text);
104         ~DebugStacker();
105
106 private:
107         DebugStack *m_stack;
108         bool m_overflowed;
109 };
110
111 #define DSTACK(msg) \
112         DebugStacker __debug_stacker(msg);
113
114 #define DSTACKF(...) \
115         char __buf[DEBUG_STACK_TEXT_SIZE];                   \
116         snprintf(__buf, DEBUG_STACK_TEXT_SIZE, __VA_ARGS__); \
117         DebugStacker __debug_stacker(__buf);
118
119 /*
120         These should be put into every thread
121 */
122
123 #if CATCH_UNHANDLED_EXCEPTIONS == 1
124         #define BEGIN_DEBUG_EXCEPTION_HANDLER try {
125         #define END_DEBUG_EXCEPTION_HANDLER                        \
126                 } catch (std::exception &e) {                          \
127                         errorstream << "An unhandled exception occurred: " \
128                                 << e.what() << std::endl;                      \
129                         FATAL_ERROR(e.what());                             \
130                 }
131 #else
132         // Dummy ones
133         #define BEGIN_DEBUG_EXCEPTION_HANDLER
134         #define END_DEBUG_EXCEPTION_HANDLER
135 #endif
136
137 #endif // DEBUG_HEADER
138
139