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 General Public License as published by
7 the Free Software Foundation; either version 2 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 General Public License for more details.
15 You should have received a copy of the GNU 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 "common_irrlicht.h"
30 #include "constants.h"
31 #include "exceptions.h"
34 #define WIN32_LEAN_AND_MEAN
44 #define DTIME (getTimestamp()+": ")
46 #define DEBUGSTREAM_COUNT 2
48 extern FILE *g_debugstreams[DEBUGSTREAM_COUNT];
50 extern void debugstreams_init(bool disable_stderr, const char *filename);
51 extern void debugstreams_deinit();
53 #define DEBUGPRINT(...)\
55 for(int i=0; i<DEBUGSTREAM_COUNT; i++)\
57 if(g_debugstreams[i] != NULL){\
58 fprintf(g_debugstreams[i], __VA_ARGS__);\
59 fflush(g_debugstreams[i]);\
64 class Debugbuf : public std::streambuf
67 Debugbuf(bool disable_stderr)
69 m_disable_stderr = disable_stderr;
74 for(int i=0; i<DEBUGSTREAM_COUNT; i++)
76 if(g_debugstreams[i] == stderr && m_disable_stderr)
78 if(g_debugstreams[i] != NULL)
79 (void)fwrite(&c, 1, 1, g_debugstreams[i]);
81 fflush(g_debugstreams[i]);
86 int xsputn(const char *s, int n)
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(s, 1, n, g_debugstreams[i]);
95 fflush(g_debugstreams[i]);
102 bool m_disable_stderr;
105 // This is used to redirect output to /dev/null
106 class Nullstream : public std::ostream {
115 extern Debugbuf debugbuf;
116 extern std::ostream dstream;
117 extern std::ostream dstream_no_stderr;
118 extern Nullstream dummyout;
124 __NORETURN extern void assert_fail(
125 const char *assertion, const char *file,
126 unsigned int line, const char *function);
128 #define ASSERT(expr)\
131 : assert_fail(#expr, __FILE__, __LINE__, __FUNCTION_NAME))
133 #define assert(expr) ASSERT(expr)
139 #define DEBUG_STACK_SIZE 50
140 #define DEBUG_STACK_TEXT_SIZE 300
144 DebugStack(threadid_t id);
145 void print(FILE *file, bool everything);
148 char stack[DEBUG_STACK_SIZE][DEBUG_STACK_TEXT_SIZE];
149 int stack_i; // Points to the lowest empty position
150 int stack_max_i; // Highest i that was seen
153 extern core::map<threadid_t, DebugStack*> g_debug_stacks;
154 extern JMutex g_debug_stacks_mutex;
156 extern void debug_stacks_init();
157 extern void debug_stacks_print();
162 DebugStacker(const char *text);
171 char __buf[DEBUG_STACK_TEXT_SIZE];\
173 DEBUG_STACK_TEXT_SIZE, __VA_ARGS__);\
174 DebugStacker __debug_stacker(__buf);
187 void add(u16 command)
189 core::map<u16, u16>::Node *n = m_packets.find(command);
192 m_packets[command] = 1;
196 n->setValue(n->getValue()+1);
202 for(core::map<u16, u16>::Iterator
203 i = m_packets.getIterator();
204 i.atEnd() == false; i++)
206 i.getNode()->setValue(0);
210 void print(std::ostream &o)
212 for(core::map<u16, u16>::Iterator
213 i = m_packets.getIterator();
214 i.atEnd() == false; i++)
216 o<<"cmd "<<i.getNode()->getKey()
217 <<" count "<<i.getNode()->getValue()
224 core::map<u16, u16> m_packets;
228 These should be put into every thread
231 #if CATCH_UNHANDLED_EXCEPTIONS == 1
232 #define BEGIN_PORTABLE_DEBUG_EXCEPTION_HANDLER try{
233 #define END_PORTABLE_DEBUG_EXCEPTION_HANDLER\
234 }catch(std::exception &e){\
235 dstream<<std::endl<<DTIME\
236 <<"ERROR: An unhandled exception occurred: "\
237 <<e.what()<<std::endl;\
240 #ifdef _WIN32 // Windows
241 #ifdef _MSC_VER // MSVC
242 void se_trans_func(unsigned int, EXCEPTION_POINTERS*);
244 class FatalSystemException : public BaseException
247 FatalSystemException(const char *s):
251 #define BEGIN_DEBUG_EXCEPTION_HANDLER \
252 BEGIN_PORTABLE_DEBUG_EXCEPTION_HANDLER\
253 _set_se_translator(se_trans_func);
255 #define END_DEBUG_EXCEPTION_HANDLER \
256 END_PORTABLE_DEBUG_EXCEPTION_HANDLER
257 #else // Probably mingw
258 #define BEGIN_DEBUG_EXCEPTION_HANDLER\
259 BEGIN_PORTABLE_DEBUG_EXCEPTION_HANDLER
260 #define END_DEBUG_EXCEPTION_HANDLER\
261 END_PORTABLE_DEBUG_EXCEPTION_HANDLER
264 #define BEGIN_DEBUG_EXCEPTION_HANDLER\
265 BEGIN_PORTABLE_DEBUG_EXCEPTION_HANDLER
266 #define END_DEBUG_EXCEPTION_HANDLER\
267 END_PORTABLE_DEBUG_EXCEPTION_HANDLER
271 #define BEGIN_DEBUG_EXCEPTION_HANDLER
272 #define END_DEBUG_EXCEPTION_HANDLER
275 #endif // DEBUG_HEADER