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