]> git.lizzy.rs Git - dragonfireclient.git/blob - src/debug.h
Fix msvc annoyances (#5963)
[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 #ifdef _WIN32
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 // When "catching", the program will abort with an error message.
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 /* Abort program execution immediately
54  */
55 NORETURN extern void fatal_error_fn(
56                 const char *msg, const char *file,
57                 unsigned int line, const char *function);
58
59 #define FATAL_ERROR(msg) \
60         fatal_error_fn((msg), __FILE__, __LINE__, FUNCTION_NAME)
61
62 #define FATAL_ERROR_IF(expr, msg) \
63         ((expr) \
64         ? fatal_error_fn((msg), __FILE__, __LINE__, FUNCTION_NAME) \
65         : (void)(0))
66
67 /*
68         sanity_check()
69         Equivalent to assert() but persists in Release builds (i.e. when NDEBUG is
70         defined)
71 */
72
73 NORETURN extern void sanity_check_fn(
74                 const char *assertion, const char *file,
75                 unsigned int line, const char *function);
76
77 #define SANITY_CHECK(expr) \
78         ((expr) \
79         ? (void)(0) \
80         : sanity_check_fn(#expr, __FILE__, __LINE__, FUNCTION_NAME))
81
82 #define sanity_check(expr) SANITY_CHECK(expr)
83
84
85 void debug_set_exception_handler();
86
87 /*
88         DebugStack
89 */
90
91 #define DEBUG_STACK_SIZE 50
92 #define DEBUG_STACK_TEXT_SIZE 300
93
94 extern void debug_stacks_print_to(std::ostream &os);
95 extern void debug_stacks_print();
96
97 struct DebugStack;
98 class DebugStacker
99 {
100 public:
101         DebugStacker(const char *text);
102         ~DebugStacker();
103
104 private:
105         DebugStack *m_stack;
106         bool m_overflowed;
107 };
108
109 #define DSTACK(msg) \
110         DebugStacker __debug_stacker(msg);
111
112 #define DSTACKF(...) \
113         char __buf[DEBUG_STACK_TEXT_SIZE];                   \
114         snprintf(__buf, DEBUG_STACK_TEXT_SIZE, __VA_ARGS__); \
115         DebugStacker __debug_stacker(__buf);
116
117 /*
118         These should be put into every thread
119 */
120
121 #if CATCH_UNHANDLED_EXCEPTIONS == 1
122         #define BEGIN_DEBUG_EXCEPTION_HANDLER try {
123         #define END_DEBUG_EXCEPTION_HANDLER                        \
124                 } catch (std::exception &e) {                          \
125                         errorstream << "An unhandled exception occurred: " \
126                                 << e.what() << std::endl;                      \
127                         FATAL_ERROR(e.what());                             \
128                 }
129 #else
130         // Dummy ones
131         #define BEGIN_DEBUG_EXCEPTION_HANDLER
132         #define END_DEBUG_EXCEPTION_HANDLER
133 #endif
134
135 #endif // DEBUG_HEADER
136
137