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"
30 #include "exceptions.h"
33 #define WIN32_LEAN_AND_MEAN
41 // Whether to catch all std::exceptions.
42 // Assert will be called on such an event.
43 // In debug mode, leave these for the debugger and don't catch them.
45 #define CATCH_UNHANDLED_EXCEPTIONS 1
47 #define CATCH_UNHANDLED_EXCEPTIONS 0
54 #define DTIME (getTimestamp()+": ")
56 #define DEBUGSTREAM_COUNT 2
58 extern FILE *g_debugstreams[DEBUGSTREAM_COUNT];
60 extern void debugstreams_init(bool disable_stderr, const char *filename);
61 extern void debugstreams_deinit();
63 #define DEBUGPRINT(...)\
65 for(int i=0; i<DEBUGSTREAM_COUNT; i++)\
67 if(g_debugstreams[i] != NULL){\
68 fprintf(g_debugstreams[i], __VA_ARGS__);\
69 fflush(g_debugstreams[i]);\
74 class Debugbuf : public std::streambuf
77 Debugbuf(bool disable_stderr)
79 m_disable_stderr = disable_stderr;
84 for(int i=0; i<DEBUGSTREAM_COUNT; i++)
86 if(g_debugstreams[i] == stderr && m_disable_stderr)
88 if(g_debugstreams[i] != NULL)
89 (void)fwrite(&c, 1, 1, g_debugstreams[i]);
91 fflush(g_debugstreams[i]);
96 std::streamsize xsputn(const char *s, std::streamsize n)
98 for(int i=0; i<DEBUGSTREAM_COUNT; i++)
100 if(g_debugstreams[i] == stderr && m_disable_stderr)
102 if(g_debugstreams[i] != NULL)
103 (void)fwrite(s, 1, n, g_debugstreams[i]);
104 //TODO: Is this slow?
105 fflush(g_debugstreams[i]);
112 bool m_disable_stderr;
115 // This is used to redirect output to /dev/null
116 class Nullstream : public std::ostream {
125 extern Debugbuf debugbuf;
126 extern std::ostream dstream;
127 extern std::ostream dstream_no_stderr;
128 extern Nullstream dummyout;
134 __NORETURN extern void assert_fail(
135 const char *assertion, const char *file,
136 unsigned int line, const char *function);
138 #define ASSERT(expr)\
141 : assert_fail(#expr, __FILE__, __LINE__, __FUNCTION_NAME))
143 #define assert(expr) ASSERT(expr)
149 #define DEBUG_STACK_SIZE 50
150 #define DEBUG_STACK_TEXT_SIZE 300
154 DebugStack(threadid_t id);
155 void print(FILE *file, bool everything);
156 void print(std::ostream &os, bool everything);
159 char stack[DEBUG_STACK_SIZE][DEBUG_STACK_TEXT_SIZE];
160 int stack_i; // Points to the lowest empty position
161 int stack_max_i; // Highest i that was seen
164 extern core::map<threadid_t, DebugStack*> g_debug_stacks;
165 extern JMutex g_debug_stacks_mutex;
167 extern void debug_stacks_init();
168 extern void debug_stacks_print_to(std::ostream &os);
169 extern void debug_stacks_print();
174 DebugStacker(const char *text);
183 DebugStacker __debug_stacker(msg);
185 #define DSTACKF(...)\
186 char __buf[DEBUG_STACK_TEXT_SIZE];\
188 DEBUG_STACK_TEXT_SIZE, __VA_ARGS__);\
189 DebugStacker __debug_stacker(__buf);
202 void add(u16 command)
204 core::map<u16, u16>::Node *n = m_packets.find(command);
207 m_packets[command] = 1;
211 n->setValue(n->getValue()+1);
217 for(core::map<u16, u16>::Iterator
218 i = m_packets.getIterator();
219 i.atEnd() == false; i++)
221 i.getNode()->setValue(0);
225 void print(std::ostream &o)
227 for(core::map<u16, u16>::Iterator
228 i = m_packets.getIterator();
229 i.atEnd() == false; i++)
231 o<<"cmd "<<i.getNode()->getKey()
232 <<" count "<<i.getNode()->getValue()
239 core::map<u16, u16> m_packets;
243 These should be put into every thread
246 #if CATCH_UNHANDLED_EXCEPTIONS == 1
247 #define BEGIN_PORTABLE_DEBUG_EXCEPTION_HANDLER try{
248 #define END_PORTABLE_DEBUG_EXCEPTION_HANDLER(logstream)\
249 }catch(std::exception &e){\
250 logstream<<"ERROR: An unhandled exception occurred: "\
251 <<e.what()<<std::endl;\
254 #ifdef _WIN32 // Windows
255 #ifdef _MSC_VER // MSVC
256 void se_trans_func(unsigned int, EXCEPTION_POINTERS*);
258 class FatalSystemException : public BaseException
261 FatalSystemException(const char *s):
265 #define BEGIN_DEBUG_EXCEPTION_HANDLER \
266 BEGIN_PORTABLE_DEBUG_EXCEPTION_HANDLER\
267 _set_se_translator(se_trans_func);
269 #define END_DEBUG_EXCEPTION_HANDLER(logstream) \
270 END_PORTABLE_DEBUG_EXCEPTION_HANDLER(logstream)
271 #else // Probably mingw
272 #define BEGIN_DEBUG_EXCEPTION_HANDLER\
273 BEGIN_PORTABLE_DEBUG_EXCEPTION_HANDLER
274 #define END_DEBUG_EXCEPTION_HANDLER(logstream)\
275 END_PORTABLE_DEBUG_EXCEPTION_HANDLER(logstream)
278 #define BEGIN_DEBUG_EXCEPTION_HANDLER\
279 BEGIN_PORTABLE_DEBUG_EXCEPTION_HANDLER
280 #define END_DEBUG_EXCEPTION_HANDLER(logstream)\
281 END_PORTABLE_DEBUG_EXCEPTION_HANDLER(logstream)
285 #define BEGIN_DEBUG_EXCEPTION_HANDLER
286 #define END_DEBUG_EXCEPTION_HANDLER(logstream)
289 #endif // DEBUG_HEADER