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.
30 #include <jmutexautolock.h>
32 #include "common_irrlicht.h"
38 #if (defined(WIN32) || defined(_WIN32_WCE))
39 typedef DWORD threadid_t;
40 #define __NORETURN __declspec(noreturn)
41 #define __FUNCTION_NAME __FUNCTION__
43 typedef pthread_t threadid_t;
44 #define __NORETURN __attribute__ ((__noreturn__))
45 #define __FUNCTION_NAME __PRETTY_FUNCTION__
48 inline threadid_t get_current_thread_id()
50 #if (defined(WIN32) || defined(_WIN32_WCE))
51 return GetCurrentThreadId();
53 return pthread_self();
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 fwrite(&c, 1, 1, g_debugstreams[i]);
96 fflush(g_debugstreams[i]);
101 int xsputn(const char *s, int 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 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);
163 char stack[DEBUG_STACK_SIZE][DEBUG_STACK_TEXT_SIZE];
164 int stack_i; // Points to the lowest empty position
165 int stack_max_i; // Highest i that was seen
168 extern core::map<threadid_t, DebugStack*> g_debug_stacks;
169 extern JMutex g_debug_stacks_mutex;
171 extern void debug_stacks_init();
172 extern void debug_stacks_print();
177 DebugStacker(const char *text);
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 #endif // DEBUG_HEADER