]> git.lizzy.rs Git - dragonfireclient.git/blob - src/debug.h
Change Minetest-c55 to Minetest
[dragonfireclient.git] / src / debug.h
1 /*
2 Minetest
3 Copyright (C) 2010 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 <stdio.h>
24 #include <jmutex.h>
25 #include <jmutexautolock.h>
26 #include <iostream>
27 #include "irrlichttypes.h"
28 #include <irrMap.h>
29 #include "threads.h"
30 #include "gettime.h"
31 #include "exceptions.h"
32
33 #ifdef _WIN32
34         #define WIN32_LEAN_AND_MEAN
35         #include <windows.h>
36         #ifdef _MSC_VER
37                 #include <eh.h>
38         #endif
39 #else
40 #endif
41
42 // Whether to catch all std::exceptions.
43 // Assert will be called on such an event.
44 // In debug mode, leave these for the debugger and don't catch them.
45 #ifdef NDEBUG
46         #define CATCH_UNHANDLED_EXCEPTIONS 1
47 #else
48         #define CATCH_UNHANDLED_EXCEPTIONS 0
49 #endif
50
51 /*
52         Debug output
53 */
54
55 #define DTIME (getTimestamp()+": ")
56
57 #define DEBUGSTREAM_COUNT 2
58
59 extern FILE *g_debugstreams[DEBUGSTREAM_COUNT];
60
61 extern void debugstreams_init(bool disable_stderr, const char *filename);
62 extern void debugstreams_deinit();
63
64 #define DEBUGPRINT(...)\
65 {\
66         for(int i=0; i<DEBUGSTREAM_COUNT; i++)\
67         {\
68                 if(g_debugstreams[i] != NULL){\
69                         fprintf(g_debugstreams[i], __VA_ARGS__);\
70                         fflush(g_debugstreams[i]);\
71                 }\
72         }\
73 }
74
75 class Debugbuf : public std::streambuf
76 {
77 public:
78         Debugbuf(bool disable_stderr)
79         {
80                 m_disable_stderr = disable_stderr;
81         }
82
83         int overflow(int c)
84         {
85                 for(int i=0; i<DEBUGSTREAM_COUNT; i++)
86                 {
87                         if(g_debugstreams[i] == stderr && m_disable_stderr)
88                                 continue;
89                         if(g_debugstreams[i] != NULL)
90                                 (void)fwrite(&c, 1, 1, g_debugstreams[i]);
91                         //TODO: Is this slow?
92                         fflush(g_debugstreams[i]);
93                 }
94                 
95                 return c;
96         }
97         std::streamsize xsputn(const char *s, std::streamsize n)
98         {
99                 for(int i=0; i<DEBUGSTREAM_COUNT; i++)
100                 {
101                         if(g_debugstreams[i] == stderr && m_disable_stderr)
102                                 continue;
103                         if(g_debugstreams[i] != NULL)
104                                 (void)fwrite(s, 1, n, g_debugstreams[i]);
105                         //TODO: Is this slow?
106                         fflush(g_debugstreams[i]);
107                 }
108
109                 return n;
110         }
111         
112 private:
113         bool m_disable_stderr;
114 };
115
116 // This is used to redirect output to /dev/null
117 class Nullstream : public std::ostream {
118 public:
119         Nullstream():
120                 std::ostream(0)
121         {
122         }
123 private:
124 };
125
126 extern Debugbuf debugbuf;
127 extern std::ostream dstream;
128 extern std::ostream dstream_no_stderr;
129 extern Nullstream dummyout;
130
131 /*
132         Assert
133 */
134
135 __NORETURN extern void assert_fail(
136                 const char *assertion, const char *file,
137                 unsigned int line, const char *function);
138
139 #define ASSERT(expr)\
140         ((expr)\
141         ? (void)(0)\
142         : assert_fail(#expr, __FILE__, __LINE__, __FUNCTION_NAME))
143
144 #define assert(expr) ASSERT(expr)
145
146 /*
147         DebugStack
148 */
149
150 #define DEBUG_STACK_SIZE 50
151 #define DEBUG_STACK_TEXT_SIZE 300
152
153 struct DebugStack
154 {
155         DebugStack(threadid_t id);
156         void print(FILE *file, bool everything);
157         void print(std::ostream &os, bool everything);
158         
159         threadid_t threadid;
160         char stack[DEBUG_STACK_SIZE][DEBUG_STACK_TEXT_SIZE];
161         int stack_i; // Points to the lowest empty position
162         int stack_max_i; // Highest i that was seen
163 };
164
165 extern core::map<threadid_t, DebugStack*> g_debug_stacks;
166 extern JMutex g_debug_stacks_mutex;
167
168 extern void debug_stacks_init();
169 extern void debug_stacks_print_to(std::ostream &os);
170 extern void debug_stacks_print();
171
172 class DebugStacker
173 {
174 public:
175         DebugStacker(const char *text);
176         ~DebugStacker();
177
178 private:
179         DebugStack *m_stack;
180         bool m_overflowed;
181 };
182
183 #define DSTACK(msg)\
184         DebugStacker __debug_stacker(msg);
185
186 #define DSTACKF(...)\
187         char __buf[DEBUG_STACK_TEXT_SIZE];\
188         snprintf(__buf,\
189                         DEBUG_STACK_TEXT_SIZE, __VA_ARGS__);\
190         DebugStacker __debug_stacker(__buf);
191
192 /*
193         Packet counter
194 */
195
196 class PacketCounter
197 {
198 public:
199         PacketCounter()
200         {
201         }
202
203         void add(u16 command)
204         {
205                 core::map<u16, u16>::Node *n = m_packets.find(command);
206                 if(n == NULL)
207                 {
208                         m_packets[command] = 1;
209                 }
210                 else
211                 {
212                         n->setValue(n->getValue()+1);
213                 }
214         }
215
216         void clear()
217         {
218                 for(core::map<u16, u16>::Iterator
219                                 i = m_packets.getIterator();
220                                 i.atEnd() == false; i++)
221                 {
222                         i.getNode()->setValue(0);
223                 }
224         }
225
226         void print(std::ostream &o)
227         {
228                 for(core::map<u16, u16>::Iterator
229                                 i = m_packets.getIterator();
230                                 i.atEnd() == false; i++)
231                 {
232                         o<<"cmd "<<i.getNode()->getKey()
233                                         <<" count "<<i.getNode()->getValue()
234                                         <<std::endl;
235                 }
236         }
237
238 private:
239         // command, count
240         core::map<u16, u16> m_packets;
241 };
242
243 /*
244         These should be put into every thread
245 */
246
247 #if CATCH_UNHANDLED_EXCEPTIONS == 1
248         #define BEGIN_PORTABLE_DEBUG_EXCEPTION_HANDLER try{
249         #define END_PORTABLE_DEBUG_EXCEPTION_HANDLER(logstream)\
250                 }catch(std::exception &e){\
251                         logstream<<"ERROR: An unhandled exception occurred: "\
252                                         <<e.what()<<std::endl;\
253                         assert(0);\
254                 }
255         #ifdef _WIN32 // Windows
256                 #ifdef _MSC_VER // MSVC
257 void se_trans_func(unsigned int, EXCEPTION_POINTERS*);
258
259 class FatalSystemException : public BaseException
260 {
261 public:
262         FatalSystemException(const char *s):
263                 BaseException(s)
264         {}
265 };
266                         #define BEGIN_DEBUG_EXCEPTION_HANDLER \
267                                 BEGIN_PORTABLE_DEBUG_EXCEPTION_HANDLER\
268                                 _set_se_translator(se_trans_func);
269
270                         #define END_DEBUG_EXCEPTION_HANDLER(logstream) \
271                                 END_PORTABLE_DEBUG_EXCEPTION_HANDLER(logstream)
272                 #else // Probably mingw
273                         #define BEGIN_DEBUG_EXCEPTION_HANDLER\
274                                 BEGIN_PORTABLE_DEBUG_EXCEPTION_HANDLER
275                         #define END_DEBUG_EXCEPTION_HANDLER(logstream)\
276                                 END_PORTABLE_DEBUG_EXCEPTION_HANDLER(logstream)
277                 #endif
278         #else // Posix
279                 #define BEGIN_DEBUG_EXCEPTION_HANDLER\
280                         BEGIN_PORTABLE_DEBUG_EXCEPTION_HANDLER
281                 #define END_DEBUG_EXCEPTION_HANDLER(logstream)\
282                         END_PORTABLE_DEBUG_EXCEPTION_HANDLER(logstream)
283         #endif
284 #else
285         // Dummy ones
286         #define BEGIN_DEBUG_EXCEPTION_HANDLER
287         #define END_DEBUG_EXCEPTION_HANDLER(logstream)
288 #endif
289
290 #endif // DEBUG_HEADER
291
292