3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
25 #include <jmutexautolock.h>
27 #include "irrlichttypes.h"
31 #include "exceptions.h"
34 #define WIN32_LEAN_AND_MEAN
36 #define _WIN32_WINNT 0x0500
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.
49 #define CATCH_UNHANDLED_EXCEPTIONS 1
51 #define CATCH_UNHANDLED_EXCEPTIONS 0
58 #define DTIME (getTimestamp()+": ")
60 #define DEBUGSTREAM_COUNT 2
62 extern FILE *g_debugstreams[DEBUGSTREAM_COUNT];
64 extern void debugstreams_init(bool disable_stderr, const char *filename);
65 extern void debugstreams_deinit();
67 #define DEBUGPRINT(...)\
69 for(int i=0; i<DEBUGSTREAM_COUNT; i++)\
71 if(g_debugstreams[i] != NULL){\
72 fprintf(g_debugstreams[i], __VA_ARGS__);\
73 fflush(g_debugstreams[i]);\
78 class Debugbuf : public std::streambuf
81 Debugbuf(bool disable_stderr)
83 m_disable_stderr = disable_stderr;
88 for(int i=0; i<DEBUGSTREAM_COUNT; i++)
90 if(g_debugstreams[i] == stderr && m_disable_stderr)
92 if(g_debugstreams[i] != NULL)
93 (void)fwrite(&c, 1, 1, g_debugstreams[i]);
95 fflush(g_debugstreams[i]);
100 std::streamsize xsputn(const char *s, std::streamsize n)
102 for(int i=0; i<DEBUGSTREAM_COUNT; i++)
104 if(g_debugstreams[i] == stderr && m_disable_stderr)
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]);
116 bool m_disable_stderr;
119 // This is used to redirect output to /dev/null
120 class Nullstream : public std::ostream {
129 extern Debugbuf debugbuf;
130 extern std::ostream dstream;
131 extern std::ostream dstream_no_stderr;
132 extern Nullstream dummyout;
138 __NORETURN extern void assert_fail(
139 const char *assertion, const char *file,
140 unsigned int line, const char *function);
142 #define ASSERT(expr)\
145 : assert_fail(#expr, __FILE__, __LINE__, __FUNCTION_NAME))
147 #define assert(expr) ASSERT(expr)
153 #define DEBUG_STACK_SIZE 50
154 #define DEBUG_STACK_TEXT_SIZE 300
158 DebugStack(threadid_t id);
159 void print(FILE *file, bool everything);
160 void print(std::ostream &os, bool everything);
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
168 extern core::map<threadid_t, DebugStack*> g_debug_stacks;
169 extern JMutex g_debug_stacks_mutex;
171 extern void debug_stacks_init();
172 extern void debug_stacks_print_to(std::ostream &os);
173 extern void debug_stacks_print();
178 DebugStacker(const char *text);
187 DebugStacker __debug_stacker(msg);
189 #define DSTACKF(...)\
190 char __buf[DEBUG_STACK_TEXT_SIZE];\
192 DEBUG_STACK_TEXT_SIZE, __VA_ARGS__);\
193 DebugStacker __debug_stacker(__buf);
206 void add(u16 command)
208 core::map<u16, u16>::Node *n = m_packets.find(command);
211 m_packets[command] = 1;
215 n->setValue(n->getValue()+1);
221 for(core::map<u16, u16>::Iterator
222 i = m_packets.getIterator();
223 i.atEnd() == false; i++)
225 i.getNode()->setValue(0);
229 void print(std::ostream &o)
231 for(core::map<u16, u16>::Iterator
232 i = m_packets.getIterator();
233 i.atEnd() == false; i++)
235 o<<"cmd "<<i.getNode()->getKey()
236 <<" count "<<i.getNode()->getValue()
243 core::map<u16, u16> m_packets;
247 These should be put into every thread
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;\
258 #ifdef _WIN32 // Windows
259 #ifdef _MSC_VER // MSVC
260 void se_trans_func(unsigned int, EXCEPTION_POINTERS*);
262 class FatalSystemException : public BaseException
265 FatalSystemException(const char *s):
269 #define BEGIN_DEBUG_EXCEPTION_HANDLER \
270 BEGIN_PORTABLE_DEBUG_EXCEPTION_HANDLER\
271 _set_se_translator(se_trans_func);
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)
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)
289 #define BEGIN_DEBUG_EXCEPTION_HANDLER
290 #define END_DEBUG_EXCEPTION_HANDLER(logstream)
293 #endif // DEBUG_HEADER