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