]> git.lizzy.rs Git - minetest.git/blob - src/debug.h
Fix msvc2012 build
[minetest.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 "gettime.h"
26
27 #if (defined(WIN32) || defined(_WIN32_WCE))
28         #define WIN32_LEAN_AND_MEAN
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 // Assert will be called on such an event.
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 /*
53         Debug output
54 */
55
56 #define DTIME (getTimestamp()+": ")
57
58 extern void debugstreams_init(bool disable_stderr, const char *filename);
59 extern void debugstreams_deinit();
60
61 // This is used to redirect output to /dev/null
62 class Nullstream : public std::ostream {
63 public:
64         Nullstream():
65                 std::ostream(0)
66         {
67         }
68 private:
69 };
70
71 extern std::ostream dstream;
72 extern std::ostream dstream_no_stderr;
73 extern Nullstream dummyout;
74
75 /*
76         Include assert.h and immediately undef assert so that it can't override
77         our assert later on. leveldb/slice.h is a notable offender.
78 */
79
80 #include <assert.h>
81 #undef assert
82
83 /*
84         Assert
85 */
86
87 __NORETURN extern void assert_fail(
88                 const char *assertion, const char *file,
89                 unsigned int line, const char *function);
90
91 #define ASSERT(expr)\
92         ((expr)\
93         ? (void)(0)\
94         : assert_fail(#expr, __FILE__, __LINE__, __FUNCTION_NAME))
95
96 #define assert(expr) ASSERT(expr)
97
98 /*
99         DebugStack
100 */
101
102 #define DEBUG_STACK_SIZE 50
103 #define DEBUG_STACK_TEXT_SIZE 300
104
105 extern void debug_stacks_init();
106 extern void debug_stacks_print_to(std::ostream &os);
107 extern void debug_stacks_print();
108
109 struct DebugStack;
110 class DebugStacker
111 {
112 public:
113         DebugStacker(const char *text);
114         ~DebugStacker();
115
116 private:
117         DebugStack *m_stack;
118         bool m_overflowed;
119 };
120
121 #define DSTACK(msg)\
122         DebugStacker __debug_stacker(msg);
123
124 #define DSTACKF(...)\
125         char __buf[DEBUG_STACK_TEXT_SIZE];\
126         snprintf(__buf,\
127                         DEBUG_STACK_TEXT_SIZE, __VA_ARGS__);\
128         DebugStacker __debug_stacker(__buf);
129
130 /*
131         These should be put into every thread
132 */
133
134 #if CATCH_UNHANDLED_EXCEPTIONS == 1
135         #define BEGIN_PORTABLE_DEBUG_EXCEPTION_HANDLER try{
136         #define END_PORTABLE_DEBUG_EXCEPTION_HANDLER(logstream)\
137                 }catch(std::exception &e){\
138                         logstream<<"ERROR: An unhandled exception occurred: "\
139                                         <<e.what()<<std::endl;\
140                         assert(0);\
141                 }
142         #ifdef _WIN32 // Windows
143                 #ifdef _MSC_VER // MSVC
144 void se_trans_func(unsigned int, EXCEPTION_POINTERS*);
145                         #define BEGIN_DEBUG_EXCEPTION_HANDLER \
146                                 BEGIN_PORTABLE_DEBUG_EXCEPTION_HANDLER\
147                                 _set_se_translator(se_trans_func);
148
149                         #define END_DEBUG_EXCEPTION_HANDLER(logstream) \
150                                 END_PORTABLE_DEBUG_EXCEPTION_HANDLER(logstream)
151                 #else // Probably mingw
152                         #define BEGIN_DEBUG_EXCEPTION_HANDLER\
153                                 BEGIN_PORTABLE_DEBUG_EXCEPTION_HANDLER
154                         #define END_DEBUG_EXCEPTION_HANDLER(logstream)\
155                                 END_PORTABLE_DEBUG_EXCEPTION_HANDLER(logstream)
156                 #endif
157         #else // Posix
158                 #define BEGIN_DEBUG_EXCEPTION_HANDLER\
159                         BEGIN_PORTABLE_DEBUG_EXCEPTION_HANDLER
160                 #define END_DEBUG_EXCEPTION_HANDLER(logstream)\
161                         END_PORTABLE_DEBUG_EXCEPTION_HANDLER(logstream)
162         #endif
163 #else
164         // Dummy ones
165         #define BEGIN_DEBUG_EXCEPTION_HANDLER
166         #define END_DEBUG_EXCEPTION_HANDLER(logstream)
167 #endif
168
169 #endif // DEBUG_HEADER
170
171