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