9a2e282f5c2d7eba4c5f69c670aa5f5b272aa89e
[oweals/minetest.git] / src / debug.h
1 /*
2 Minetest-c55
3 Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
20
21 /*
22         Debug stuff
23 */
24
25 #ifndef DEBUG_HEADER
26 #define DEBUG_HEADER
27
28 #include <stdio.h>
29 #include <jmutex.h>
30 #include <jmutexautolock.h>
31 #include <iostream>
32 #include "common_irrlicht.h"
33
34 /*
35         Compatibility stuff
36 */
37
38 #if (defined(WIN32) || defined(_WIN32_WCE))
39 typedef DWORD threadid_t;
40 #define __NORETURN __declspec(noreturn)
41 #define __FUNCTION_NAME __FUNCTION__
42 #else
43 typedef pthread_t threadid_t;
44 #define __NORETURN __attribute__ ((__noreturn__))
45 #define __FUNCTION_NAME __PRETTY_FUNCTION__
46 #endif
47
48 inline threadid_t get_current_thread_id()
49 {
50 #if (defined(WIN32) || defined(_WIN32_WCE))
51         return GetCurrentThreadId();
52 #else
53         return pthread_self();
54 #endif
55 }
56
57 /*
58         Debug output
59 */
60
61 #define DEBUGSTREAM_COUNT 2
62
63 extern FILE *g_debugstreams[DEBUGSTREAM_COUNT];
64
65 extern void debugstreams_init(bool disable_stderr, const char *filename);
66 extern void debugstreams_deinit();
67
68 #define DEBUGPRINT(...)\
69 {\
70         for(int i=0; i<DEBUGSTREAM_COUNT; i++)\
71         {\
72                 if(g_debugstreams[i] != NULL){\
73                         fprintf(g_debugstreams[i], __VA_ARGS__);\
74                         fflush(g_debugstreams[i]);\
75                 }\
76         }\
77 }
78
79 class Debugbuf : public std::streambuf
80 {
81 public:
82         Debugbuf(bool disable_stderr)
83         {
84                 m_disable_stderr = disable_stderr;
85         }
86
87         int overflow(int c)
88         {
89                 for(int i=0; i<DEBUGSTREAM_COUNT; i++)
90                 {
91                         if(g_debugstreams[i] == stderr && m_disable_stderr)
92                                 continue;
93                         if(g_debugstreams[i] != NULL)
94                                 fwrite(&c, 1, 1, g_debugstreams[i]);
95                         //TODO: Is this slow?
96                         fflush(g_debugstreams[i]);
97                 }
98                 
99                 return c;
100         }
101         int xsputn(const char *s, int n)
102         {
103                 for(int i=0; i<DEBUGSTREAM_COUNT; i++)
104                 {
105                         if(g_debugstreams[i] == stderr && m_disable_stderr)
106                                 continue;
107                         if(g_debugstreams[i] != NULL)
108                                 fwrite(s, 1, n, g_debugstreams[i]);
109                         //TODO: Is this slow?
110                         fflush(g_debugstreams[i]);
111                 }
112
113                 return n;
114         }
115         
116 private:
117         bool m_disable_stderr;
118 };
119
120 // This is used to redirect output to /dev/null
121 class Nullstream : public std::ostream {
122 public:
123         Nullstream():
124                 std::ostream(0)
125         {
126         }
127 private:
128 };
129
130 extern Debugbuf debugbuf;
131 extern std::ostream dstream;
132 extern std::ostream dstream_no_stderr;
133 extern Nullstream dummyout;
134
135 /*
136         Assert
137 */
138
139 __NORETURN extern void assert_fail(
140                 const char *assertion, const char *file,
141                 unsigned int line, const char *function);
142
143 #define ASSERT(expr)\
144         ((expr)\
145         ? (void)(0)\
146         : assert_fail(#expr, __FILE__, __LINE__, __FUNCTION_NAME))
147
148 #define assert(expr) ASSERT(expr)
149
150 /*
151         DebugStack
152 */
153
154 #define DEBUG_STACK_SIZE 50
155 #define DEBUG_STACK_TEXT_SIZE 300
156
157 struct DebugStack
158 {
159         DebugStack(threadid_t id);
160         void print(FILE *file, bool everything);
161         
162         threadid_t threadid;
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
166 };
167
168 extern core::map<threadid_t, DebugStack*> g_debug_stacks;
169 extern JMutex g_debug_stacks_mutex;
170
171 extern void debug_stacks_init();
172 extern void debug_stacks_print();
173
174 class DebugStacker
175 {
176 public:
177         DebugStacker(const char *text);
178         ~DebugStacker();
179
180 private:
181         DebugStack *m_stack;
182         bool m_overflowed;
183 };
184
185 #define DSTACK(...)\
186         char __buf[DEBUG_STACK_TEXT_SIZE];\
187         snprintf(__buf,\
188                         DEBUG_STACK_TEXT_SIZE, __VA_ARGS__);\
189         DebugStacker __debug_stacker(__buf);
190
191 /*
192         Packet counter
193 */
194
195 class PacketCounter
196 {
197 public:
198         PacketCounter()
199         {
200         }
201
202         void add(u16 command)
203         {
204                 core::map<u16, u16>::Node *n = m_packets.find(command);
205                 if(n == NULL)
206                 {
207                         m_packets[command] = 1;
208                 }
209                 else
210                 {
211                         n->setValue(n->getValue()+1);
212                 }
213         }
214
215         void clear()
216         {
217                 for(core::map<u16, u16>::Iterator
218                                 i = m_packets.getIterator();
219                                 i.atEnd() == false; i++)
220                 {
221                         i.getNode()->setValue(0);
222                 }
223         }
224
225         void print(std::ostream &o)
226         {
227                 for(core::map<u16, u16>::Iterator
228                                 i = m_packets.getIterator();
229                                 i.atEnd() == false; i++)
230                 {
231                         o<<"cmd "<<i.getNode()->getKey()
232                                         <<" count "<<i.getNode()->getValue()
233                                         <<std::endl;
234                 }
235         }
236
237 private:
238         // command, count
239         core::map<u16, u16> m_packets;
240 };
241
242
243 #endif // DEBUG_HEADER
244
245