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