3 Copyright (C) 2010 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
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.
46 #define CATCH_UNHANDLED_EXCEPTIONS 1
48 #define CATCH_UNHANDLED_EXCEPTIONS 0
55 #define DTIME (getTimestamp()+": ")
57 #define DEBUGSTREAM_COUNT 2
59 extern FILE *g_debugstreams[DEBUGSTREAM_COUNT];
61 extern void debugstreams_init(bool disable_stderr, const char *filename);
62 extern void debugstreams_deinit();
64 #define DEBUGPRINT(...)\
66 for(int i=0; i<DEBUGSTREAM_COUNT; i++)\
68 if(g_debugstreams[i] != NULL){\
69 fprintf(g_debugstreams[i], __VA_ARGS__);\
70 fflush(g_debugstreams[i]);\
75 class Debugbuf : public std::streambuf
78 Debugbuf(bool disable_stderr)
80 m_disable_stderr = disable_stderr;
85 for(int i=0; i<DEBUGSTREAM_COUNT; i++)
87 if(g_debugstreams[i] == stderr && m_disable_stderr)
89 if(g_debugstreams[i] != NULL)
90 (void)fwrite(&c, 1, 1, g_debugstreams[i]);
92 fflush(g_debugstreams[i]);
97 std::streamsize xsputn(const char *s, std::streamsize n)
99 for(int i=0; i<DEBUGSTREAM_COUNT; i++)
101 if(g_debugstreams[i] == stderr && m_disable_stderr)
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]);
113 bool m_disable_stderr;
116 // This is used to redirect output to /dev/null
117 class Nullstream : public std::ostream {
126 extern Debugbuf debugbuf;
127 extern std::ostream dstream;
128 extern std::ostream dstream_no_stderr;
129 extern Nullstream dummyout;
135 __NORETURN extern void assert_fail(
136 const char *assertion, const char *file,
137 unsigned int line, const char *function);
139 #define ASSERT(expr)\
142 : assert_fail(#expr, __FILE__, __LINE__, __FUNCTION_NAME))
144 #define assert(expr) ASSERT(expr)
150 #define DEBUG_STACK_SIZE 50
151 #define DEBUG_STACK_TEXT_SIZE 300
155 DebugStack(threadid_t id);
156 void print(FILE *file, bool everything);
157 void print(std::ostream &os, bool everything);
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
165 extern core::map<threadid_t, DebugStack*> g_debug_stacks;
166 extern JMutex g_debug_stacks_mutex;
168 extern void debug_stacks_init();
169 extern void debug_stacks_print_to(std::ostream &os);
170 extern void debug_stacks_print();
175 DebugStacker(const char *text);
184 DebugStacker __debug_stacker(msg);
186 #define DSTACKF(...)\
187 char __buf[DEBUG_STACK_TEXT_SIZE];\
189 DEBUG_STACK_TEXT_SIZE, __VA_ARGS__);\
190 DebugStacker __debug_stacker(__buf);
203 void add(u16 command)
205 core::map<u16, u16>::Node *n = m_packets.find(command);
208 m_packets[command] = 1;
212 n->setValue(n->getValue()+1);
218 for(core::map<u16, u16>::Iterator
219 i = m_packets.getIterator();
220 i.atEnd() == false; i++)
222 i.getNode()->setValue(0);
226 void print(std::ostream &o)
228 for(core::map<u16, u16>::Iterator
229 i = m_packets.getIterator();
230 i.atEnd() == false; i++)
232 o<<"cmd "<<i.getNode()->getKey()
233 <<" count "<<i.getNode()->getValue()
240 core::map<u16, u16> m_packets;
244 These should be put into every thread
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;\
255 #ifdef _WIN32 // Windows
256 #ifdef _MSC_VER // MSVC
257 void se_trans_func(unsigned int, EXCEPTION_POINTERS*);
259 class FatalSystemException : public BaseException
262 FatalSystemException(const char *s):
266 #define BEGIN_DEBUG_EXCEPTION_HANDLER \
267 BEGIN_PORTABLE_DEBUG_EXCEPTION_HANDLER\
268 _set_se_translator(se_trans_func);
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)
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)
286 #define BEGIN_DEBUG_EXCEPTION_HANDLER
287 #define END_DEBUG_EXCEPTION_HANDLER(logstream)
290 #endif // DEBUG_HEADER