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