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"
35 #define WIN32_LEAN_AND_MEAN
37 #define _WIN32_WINNT 0x0500
46 // Whether to catch all std::exceptions.
47 // Assert will be called on such an event.
48 // In debug mode, leave these for the debugger and don't catch them.
50 #define CATCH_UNHANDLED_EXCEPTIONS 1
52 #define CATCH_UNHANDLED_EXCEPTIONS 0
59 #define DTIME (getTimestamp()+": ")
61 #define DEBUGSTREAM_COUNT 2
63 extern FILE *g_debugstreams[DEBUGSTREAM_COUNT];
65 extern void debugstreams_init(bool disable_stderr, const char *filename);
66 extern void debugstreams_deinit();
68 #define DEBUGPRINT(...)\
70 for(int i=0; i<DEBUGSTREAM_COUNT; i++)\
72 if(g_debugstreams[i] != NULL){\
73 fprintf(g_debugstreams[i], __VA_ARGS__);\
74 fflush(g_debugstreams[i]);\
79 class Debugbuf : public std::streambuf
82 Debugbuf(bool disable_stderr)
84 m_disable_stderr = disable_stderr;
89 for(int i=0; i<DEBUGSTREAM_COUNT; i++)
91 if(g_debugstreams[i] == stderr && m_disable_stderr)
93 if(g_debugstreams[i] != NULL)
94 (void)fwrite(&c, 1, 1, g_debugstreams[i]);
96 fflush(g_debugstreams[i]);
101 std::streamsize xsputn(const char *s, std::streamsize n)
103 for(int i=0; i<DEBUGSTREAM_COUNT; i++)
105 if(g_debugstreams[i] == stderr && m_disable_stderr)
107 if(g_debugstreams[i] != NULL)
108 (void)fwrite(s, 1, n, g_debugstreams[i]);
109 //TODO: Is this slow?
110 fflush(g_debugstreams[i]);
117 bool m_disable_stderr;
120 // This is used to redirect output to /dev/null
121 class Nullstream : public std::ostream {
130 extern Debugbuf debugbuf;
131 extern std::ostream dstream;
132 extern std::ostream dstream_no_stderr;
133 extern Nullstream dummyout;
139 __NORETURN extern void assert_fail(
140 const char *assertion, const char *file,
141 unsigned int line, const char *function);
143 #define ASSERT(expr)\
146 : assert_fail(#expr, __FILE__, __LINE__, __FUNCTION_NAME))
148 #define assert(expr) ASSERT(expr)
154 #define DEBUG_STACK_SIZE 50
155 #define DEBUG_STACK_TEXT_SIZE 300
159 DebugStack(threadid_t id);
160 void print(FILE *file, bool everything);
161 void print(std::ostream &os, bool everything);
164 char stack[DEBUG_STACK_SIZE][DEBUG_STACK_TEXT_SIZE];
165 int stack_i; // Points to the lowest empty position
166 int stack_max_i; // Highest i that was seen
169 extern std::map<threadid_t, DebugStack*> g_debug_stacks;
170 extern JMutex g_debug_stacks_mutex;
172 extern void debug_stacks_init();
173 extern void debug_stacks_print_to(std::ostream &os);
174 extern void debug_stacks_print();
179 DebugStacker(const char *text);
188 DebugStacker __debug_stacker(msg);
190 #define DSTACKF(...)\
191 char __buf[DEBUG_STACK_TEXT_SIZE];\
193 DEBUG_STACK_TEXT_SIZE, __VA_ARGS__);\
194 DebugStacker __debug_stacker(__buf);
207 void add(u16 command)
209 std::map<u16, u16>::iterator n = m_packets.find(command);
210 if(n == m_packets.end())
212 m_packets[command] = 1;
222 for(std::map<u16, u16>::iterator
223 i = m_packets.begin();
224 i != m_packets.end(); ++i)
230 void print(std::ostream &o)
232 for(std::map<u16, u16>::iterator
233 i = m_packets.begin();
234 i != m_packets.end(); ++i)
237 <<" count "<<i->second
244 std::map<u16, u16> m_packets;
248 These should be put into every thread
251 #if CATCH_UNHANDLED_EXCEPTIONS == 1
252 #define BEGIN_PORTABLE_DEBUG_EXCEPTION_HANDLER try{
253 #define END_PORTABLE_DEBUG_EXCEPTION_HANDLER(logstream)\
254 }catch(std::exception &e){\
255 logstream<<"ERROR: An unhandled exception occurred: "\
256 <<e.what()<<std::endl;\
259 #ifdef _WIN32 // Windows
260 #ifdef _MSC_VER // MSVC
261 void se_trans_func(unsigned int, EXCEPTION_POINTERS*);
263 class FatalSystemException : public BaseException
266 FatalSystemException(const char *s):
270 #define BEGIN_DEBUG_EXCEPTION_HANDLER \
271 BEGIN_PORTABLE_DEBUG_EXCEPTION_HANDLER\
272 _set_se_translator(se_trans_func);
274 #define END_DEBUG_EXCEPTION_HANDLER(logstream) \
275 END_PORTABLE_DEBUG_EXCEPTION_HANDLER(logstream)
276 #else // Probably mingw
277 #define BEGIN_DEBUG_EXCEPTION_HANDLER\
278 BEGIN_PORTABLE_DEBUG_EXCEPTION_HANDLER
279 #define END_DEBUG_EXCEPTION_HANDLER(logstream)\
280 END_PORTABLE_DEBUG_EXCEPTION_HANDLER(logstream)
283 #define BEGIN_DEBUG_EXCEPTION_HANDLER\
284 BEGIN_PORTABLE_DEBUG_EXCEPTION_HANDLER
285 #define END_DEBUG_EXCEPTION_HANDLER(logstream)\
286 END_PORTABLE_DEBUG_EXCEPTION_HANDLER(logstream)
290 #define BEGIN_DEBUG_EXCEPTION_HANDLER
291 #define END_DEBUG_EXCEPTION_HANDLER(logstream)
294 #endif // DEBUG_HEADER