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"
35 #define DTIME (getTimestamp()+": ")
37 #define DEBUGSTREAM_COUNT 2
39 extern FILE *g_debugstreams[DEBUGSTREAM_COUNT];
41 extern void debugstreams_init(bool disable_stderr, const char *filename);
42 extern void debugstreams_deinit();
44 #define DEBUGPRINT(...)\
46 for(int i=0; i<DEBUGSTREAM_COUNT; i++)\
48 if(g_debugstreams[i] != NULL){\
49 fprintf(g_debugstreams[i], __VA_ARGS__);\
50 fflush(g_debugstreams[i]);\
55 class Debugbuf : public std::streambuf
58 Debugbuf(bool disable_stderr)
60 m_disable_stderr = disable_stderr;
65 for(int i=0; i<DEBUGSTREAM_COUNT; i++)
67 if(g_debugstreams[i] == stderr && m_disable_stderr)
69 if(g_debugstreams[i] != NULL)
70 fwrite(&c, 1, 1, g_debugstreams[i]);
72 fflush(g_debugstreams[i]);
77 int xsputn(const char *s, int n)
79 for(int i=0; i<DEBUGSTREAM_COUNT; i++)
81 if(g_debugstreams[i] == stderr && m_disable_stderr)
83 if(g_debugstreams[i] != NULL)
84 fwrite(s, 1, n, g_debugstreams[i]);
86 fflush(g_debugstreams[i]);
93 bool m_disable_stderr;
96 // This is used to redirect output to /dev/null
97 class Nullstream : public std::ostream {
106 extern Debugbuf debugbuf;
107 extern std::ostream dstream;
108 extern std::ostream dstream_no_stderr;
109 extern Nullstream dummyout;
115 __NORETURN extern void assert_fail(
116 const char *assertion, const char *file,
117 unsigned int line, const char *function);
119 #define ASSERT(expr)\
122 : assert_fail(#expr, __FILE__, __LINE__, __FUNCTION_NAME))
124 #define assert(expr) ASSERT(expr)
130 #define DEBUG_STACK_SIZE 50
131 #define DEBUG_STACK_TEXT_SIZE 300
135 DebugStack(threadid_t id);
136 void print(FILE *file, bool everything);
139 char stack[DEBUG_STACK_SIZE][DEBUG_STACK_TEXT_SIZE];
140 int stack_i; // Points to the lowest empty position
141 int stack_max_i; // Highest i that was seen
144 extern core::map<threadid_t, DebugStack*> g_debug_stacks;
145 extern JMutex g_debug_stacks_mutex;
147 extern void debug_stacks_init();
148 extern void debug_stacks_print();
153 DebugStacker(const char *text);
162 char __buf[DEBUG_STACK_TEXT_SIZE];\
164 DEBUG_STACK_TEXT_SIZE, __VA_ARGS__);\
165 DebugStacker __debug_stacker(__buf);
178 void add(u16 command)
180 core::map<u16, u16>::Node *n = m_packets.find(command);
183 m_packets[command] = 1;
187 n->setValue(n->getValue()+1);
193 for(core::map<u16, u16>::Iterator
194 i = m_packets.getIterator();
195 i.atEnd() == false; i++)
197 i.getNode()->setValue(0);
201 void print(std::ostream &o)
203 for(core::map<u16, u16>::Iterator
204 i = m_packets.getIterator();
205 i.atEnd() == false; i++)
207 o<<"cmd "<<i.getNode()->getKey()
208 <<" count "<<i.getNode()->getValue()
215 core::map<u16, u16> m_packets;
219 #endif // DEBUG_HEADER