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
46 #define DTIME (getTimestamp()+": ")
48 #define DEBUGSTREAM_COUNT 2
50 extern FILE *g_debugstreams[DEBUGSTREAM_COUNT];
52 extern void debugstreams_init(bool disable_stderr, const char *filename);
53 extern void debugstreams_deinit();
55 #define DEBUGPRINT(...)\
57 for(int i=0; i<DEBUGSTREAM_COUNT; i++)\
59 if(g_debugstreams[i] != NULL){\
60 fprintf(g_debugstreams[i], __VA_ARGS__);\
61 fflush(g_debugstreams[i]);\
66 class Debugbuf : public std::streambuf
69 Debugbuf(bool disable_stderr)
71 m_disable_stderr = disable_stderr;
76 for(int i=0; i<DEBUGSTREAM_COUNT; i++)
78 if(g_debugstreams[i] == stderr && m_disable_stderr)
80 if(g_debugstreams[i] != NULL)
81 (void)fwrite(&c, 1, 1, g_debugstreams[i]);
83 fflush(g_debugstreams[i]);
88 std::streamsize xsputn(const char *s, std::streamsize n)
90 for(int i=0; i<DEBUGSTREAM_COUNT; i++)
92 if(g_debugstreams[i] == stderr && m_disable_stderr)
94 if(g_debugstreams[i] != NULL)
95 (void)fwrite(s, 1, n, g_debugstreams[i]);
97 fflush(g_debugstreams[i]);
104 bool m_disable_stderr;
107 // This is used to redirect output to /dev/null
108 class Nullstream : public std::ostream {
117 extern Debugbuf debugbuf;
118 extern std::ostream dstream;
119 extern std::ostream dstream_no_stderr;
120 extern Nullstream dummyout;
126 __NORETURN extern void assert_fail(
127 const char *assertion, const char *file,
128 unsigned int line, const char *function);
130 #define ASSERT(expr)\
133 : assert_fail(#expr, __FILE__, __LINE__, __FUNCTION_NAME))
135 #define assert(expr) ASSERT(expr)
141 #define DEBUG_STACK_SIZE 50
142 #define DEBUG_STACK_TEXT_SIZE 300
146 DebugStack(threadid_t id);
147 void print(FILE *file, bool everything);
150 char stack[DEBUG_STACK_SIZE][DEBUG_STACK_TEXT_SIZE];
151 int stack_i; // Points to the lowest empty position
152 int stack_max_i; // Highest i that was seen
155 extern core::map<threadid_t, DebugStack*> g_debug_stacks;
156 extern JMutex g_debug_stacks_mutex;
158 extern void debug_stacks_init();
159 extern void debug_stacks_print();
164 DebugStacker(const char *text);
173 DebugStacker __debug_stacker(msg);
175 #define DSTACKF(...)\
176 char __buf[DEBUG_STACK_TEXT_SIZE];\
178 DEBUG_STACK_TEXT_SIZE, __VA_ARGS__);\
179 DebugStacker __debug_stacker(__buf);
192 void add(u16 command)
194 core::map<u16, u16>::Node *n = m_packets.find(command);
197 m_packets[command] = 1;
201 n->setValue(n->getValue()+1);
207 for(core::map<u16, u16>::Iterator
208 i = m_packets.getIterator();
209 i.atEnd() == false; i++)
211 i.getNode()->setValue(0);
215 void print(std::ostream &o)
217 for(core::map<u16, u16>::Iterator
218 i = m_packets.getIterator();
219 i.atEnd() == false; i++)
221 o<<"cmd "<<i.getNode()->getKey()
222 <<" count "<<i.getNode()->getValue()
229 core::map<u16, u16> m_packets;
233 These should be put into every thread
236 #if CATCH_UNHANDLED_EXCEPTIONS == 1
237 #define BEGIN_PORTABLE_DEBUG_EXCEPTION_HANDLER try{
238 #define END_PORTABLE_DEBUG_EXCEPTION_HANDLER(logstream)\
239 }catch(std::exception &e){\
240 logstream<<"ERROR: An unhandled exception occurred: "\
241 <<e.what()<<std::endl;\
244 #ifdef _WIN32 // Windows
245 #ifdef _MSC_VER // MSVC
246 void se_trans_func(unsigned int, EXCEPTION_POINTERS*);
248 class FatalSystemException : public BaseException
251 FatalSystemException(const char *s):
255 #define BEGIN_DEBUG_EXCEPTION_HANDLER \
256 BEGIN_PORTABLE_DEBUG_EXCEPTION_HANDLER\
257 _set_se_translator(se_trans_func);
259 #define END_DEBUG_EXCEPTION_HANDLER(logstream) \
260 END_PORTABLE_DEBUG_EXCEPTION_HANDLER(logstream)
261 #else // Probably mingw
262 #define BEGIN_DEBUG_EXCEPTION_HANDLER\
263 BEGIN_PORTABLE_DEBUG_EXCEPTION_HANDLER
264 #define END_DEBUG_EXCEPTION_HANDLER(logstream)\
265 END_PORTABLE_DEBUG_EXCEPTION_HANDLER(logstream)
268 #define BEGIN_DEBUG_EXCEPTION_HANDLER\
269 BEGIN_PORTABLE_DEBUG_EXCEPTION_HANDLER
270 #define END_DEBUG_EXCEPTION_HANDLER(logstream)\
271 END_PORTABLE_DEBUG_EXCEPTION_HANDLER(logstream)
275 #define BEGIN_DEBUG_EXCEPTION_HANDLER
276 #define END_DEBUG_EXCEPTION_HANDLER(logstream)
279 #endif // DEBUG_HEADER