]> git.lizzy.rs Git - dragonfireclient.git/blob - src/debug.h
C++ modernize: Pragma once (#6264)
[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 #pragma once
21
22 #include <iostream>
23 #include <exception>
24 #include <assert.h>
25 #include "gettime.h"
26 #include "log.h"
27
28 #ifdef _WIN32
29         #ifndef _WIN32_WINNT
30                 #define _WIN32_WINNT 0x0501
31         #endif
32         #include <windows.h>
33         #ifdef _MSC_VER
34                 #include <eh.h>
35         #endif
36         #define NORETURN __declspec(noreturn)
37         #define FUNCTION_NAME __FUNCTION__
38 #else
39         #define NORETURN __attribute__ ((__noreturn__))
40         #define FUNCTION_NAME __PRETTY_FUNCTION__
41 #endif
42
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.
46 #ifdef NDEBUG
47         #define CATCH_UNHANDLED_EXCEPTIONS 1
48 #else
49         #define CATCH_UNHANDLED_EXCEPTIONS 0
50 #endif
51
52 /* Abort program execution immediately
53  */
54 NORETURN extern void fatal_error_fn(
55                 const char *msg, const char *file,
56                 unsigned int line, const char *function);
57
58 #define FATAL_ERROR(msg) \
59         fatal_error_fn((msg), __FILE__, __LINE__, FUNCTION_NAME)
60
61 #define FATAL_ERROR_IF(expr, msg) \
62         ((expr) \
63         ? fatal_error_fn((msg), __FILE__, __LINE__, FUNCTION_NAME) \
64         : (void)(0))
65
66 /*
67         sanity_check()
68         Equivalent to assert() but persists in Release builds (i.e. when NDEBUG is
69         defined)
70 */
71
72 NORETURN extern void sanity_check_fn(
73                 const char *assertion, const char *file,
74                 unsigned int line, const char *function);
75
76 #define SANITY_CHECK(expr) \
77         ((expr) \
78         ? (void)(0) \
79         : sanity_check_fn(#expr, __FILE__, __LINE__, FUNCTION_NAME))
80
81 #define sanity_check(expr) SANITY_CHECK(expr)
82
83
84 void debug_set_exception_handler();
85
86 /*
87         DebugStack
88 */
89
90 #define DEBUG_STACK_SIZE 50
91 #define DEBUG_STACK_TEXT_SIZE 300
92
93 extern void debug_stacks_print_to(std::ostream &os);
94 extern void debug_stacks_print();
95
96 struct DebugStack;
97 class DebugStacker
98 {
99 public:
100         DebugStacker(const char *text);
101         ~DebugStacker();
102
103 private:
104         DebugStack *m_stack;
105         bool m_overflowed;
106 };
107
108 #define DSTACK(msg) \
109         DebugStacker __debug_stacker(msg);
110
111 #define DSTACKF(...) \
112         char __buf[DEBUG_STACK_TEXT_SIZE];                   \
113         snprintf(__buf, DEBUG_STACK_TEXT_SIZE, __VA_ARGS__); \
114         DebugStacker __debug_stacker(__buf);
115
116 /*
117         These should be put into every thread
118 */
119
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());                             \
127                 }
128 #else
129         // Dummy ones
130         #define BEGIN_DEBUG_EXCEPTION_HANDLER
131         #define END_DEBUG_EXCEPTION_HANDLER
132 #endif