]> git.lizzy.rs Git - dragonfireclient.git/blob - src/debug.h
Refactor logging
[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 #define DTIME ""
93
94 /*
95         DebugStack
96 */
97
98 #define DEBUG_STACK_SIZE 50
99 #define DEBUG_STACK_TEXT_SIZE 300
100
101 extern void debug_stacks_init();
102 extern void debug_stacks_print_to(std::ostream &os);
103 extern void debug_stacks_print();
104
105 struct DebugStack;
106 class DebugStacker
107 {
108 public:
109         DebugStacker(const char *text);
110         ~DebugStacker();
111
112 private:
113         DebugStack *m_stack;
114         bool m_overflowed;
115 };
116
117 #define DSTACK(msg) \
118         DebugStacker __debug_stacker(msg);
119
120 #define DSTACKF(...) \
121         char __buf[DEBUG_STACK_TEXT_SIZE];                   \
122         snprintf(__buf, DEBUG_STACK_TEXT_SIZE, __VA_ARGS__); \
123         DebugStacker __debug_stacker(__buf);
124
125 /*
126         These should be put into every thread
127 */
128
129 #if CATCH_UNHANDLED_EXCEPTIONS == 1
130         #define BEGIN_DEBUG_EXCEPTION_HANDLER try {
131         #define END_DEBUG_EXCEPTION_HANDLER(logstream)           \
132                 } catch (std::exception &e) {                        \
133                         logstream << "An unhandled exception occurred: " \
134                                 << e.what() << std::endl;                    \
135                         FATAL_ERROR(e.what());                           \
136                 }
137 #else
138         // Dummy ones
139         #define BEGIN_DEBUG_EXCEPTION_HANDLER
140         #define END_DEBUG_EXCEPTION_HANDLER(logstream)
141 #endif
142
143 #endif // DEBUG_HEADER
144
145