1 |
/* debugmodule.cpp |
---|
2 |
* Copyright (C) 2004,05 by Pieter Palmers, Daniel Wagner |
---|
3 |
* |
---|
4 |
* This file is part of FreeBob. |
---|
5 |
* |
---|
6 |
* FreeBob is free software; you can redistribute it and/or modify |
---|
7 |
* it under the terms of the GNU General Public License as published by |
---|
8 |
* the Free Software Foundation; either version 2 of the License, or |
---|
9 |
* (at your option) any later version. |
---|
10 |
* FreeBob 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 |
---|
16 |
* along with FreeBob; if not, write to the Free Software |
---|
17 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
---|
18 |
* MA 02111-1307 USA. |
---|
19 |
*/ |
---|
20 |
#include <stdarg.h> |
---|
21 |
#include "debugmodule.h" |
---|
22 |
|
---|
23 |
#undef setDebugLevel |
---|
24 |
#undef debugError |
---|
25 |
#undef debugPrint |
---|
26 |
#undef debugPrintShort |
---|
27 |
|
---|
28 |
//////////////////////////////////////////////////////////////// |
---|
29 |
DebugBase::DebugBase() |
---|
30 |
: m_level( 0 ) |
---|
31 |
{} |
---|
32 |
|
---|
33 |
DebugBase::~DebugBase() |
---|
34 |
{} |
---|
35 |
|
---|
36 |
//////////////////////////////////////////////////////////////// |
---|
37 |
DebugStandard::DebugStandard() |
---|
38 |
: DebugBase() |
---|
39 |
{} |
---|
40 |
|
---|
41 |
DebugStandard::~DebugStandard() |
---|
42 |
{} |
---|
43 |
|
---|
44 |
void |
---|
45 |
DebugStandard::error( const char* file, |
---|
46 |
const char* function, |
---|
47 |
unsigned int line, |
---|
48 |
const char* format, ... ) const |
---|
49 |
{ |
---|
50 |
va_list arg; |
---|
51 |
|
---|
52 |
va_start( arg, format ); |
---|
53 |
fprintf( stderr, "ERROR: %s %s %d: ", file, function, line ); |
---|
54 |
vfprintf( stderr, format, arg ); |
---|
55 |
va_end( arg ); |
---|
56 |
} |
---|
57 |
|
---|
58 |
void |
---|
59 |
DebugStandard::print( int level, |
---|
60 |
const char* file, |
---|
61 |
const char* function, |
---|
62 |
unsigned int line, |
---|
63 |
const char* format, ... ) const |
---|
64 |
{ |
---|
65 |
if (level & m_level) { |
---|
66 |
va_list arg; |
---|
67 |
|
---|
68 |
va_start( arg, format ); |
---|
69 |
printf( "DEBUG %s %s %d : ", file, function, line ); |
---|
70 |
vprintf( format, arg ); |
---|
71 |
va_end( arg ); |
---|
72 |
} |
---|
73 |
} |
---|
74 |
|
---|
75 |
void |
---|
76 |
DebugStandard::printShort( int level, |
---|
77 |
const char* format, ... ) const |
---|
78 |
{ |
---|
79 |
if (level & m_level) { |
---|
80 |
va_list arg; |
---|
81 |
|
---|
82 |
va_start( arg, format ); |
---|
83 |
vprintf ( format, arg ); |
---|
84 |
va_end( arg ); |
---|
85 |
} |
---|
86 |
} |
---|
87 |
|
---|
88 |
//////////////////////////////////////////////////////////////// |
---|
89 |
DebugAnsiColor::DebugAnsiColor() |
---|
90 |
: DebugBase() |
---|
91 |
{} |
---|
92 |
|
---|
93 |
DebugAnsiColor::~DebugAnsiColor() |
---|
94 |
{} |
---|
95 |
|
---|
96 |
void |
---|
97 |
DebugAnsiColor::error( const char* file, |
---|
98 |
const char* function, |
---|
99 |
unsigned int line, |
---|
100 |
const char* format, ... ) const |
---|
101 |
{ |
---|
102 |
va_list arg; |
---|
103 |
|
---|
104 |
va_start( arg, format ); |
---|
105 |
fprintf( stderr, "\033[37m\033[41mERROR: %s %s %d: ", |
---|
106 |
file, function, line ); |
---|
107 |
vfprintf( stderr, format, arg ); |
---|
108 |
fprintf( stderr, "\033[0m\033[49m" ); |
---|
109 |
va_end( arg ); |
---|
110 |
} |
---|
111 |
|
---|
112 |
void |
---|
113 |
DebugAnsiColor::print( int level, |
---|
114 |
const char* file, |
---|
115 |
const char* function, |
---|
116 |
unsigned int line, |
---|
117 |
const char* format, ... ) const |
---|
118 |
{ |
---|
119 |
if (level & m_level) { |
---|
120 |
va_list arg; |
---|
121 |
|
---|
122 |
va_start( arg, format ); |
---|
123 |
printf( "\033[37mDEBUG %s %s %d: %s" , |
---|
124 |
file, function, line, getLevelColor( level ) ); |
---|
125 |
vprintf( format, arg ); |
---|
126 |
printf( "\033[0m" ); |
---|
127 |
va_end( arg ); |
---|
128 |
} |
---|
129 |
} |
---|
130 |
|
---|
131 |
void |
---|
132 |
DebugAnsiColor::printShort( int level, |
---|
133 |
const char* format, ... ) const |
---|
134 |
{ |
---|
135 |
if (level & m_level) { |
---|
136 |
va_list arg; |
---|
137 |
|
---|
138 |
va_start( arg, format ); |
---|
139 |
printf( "%s", getLevelColor( level ) ); |
---|
140 |
vprintf( format, arg ); |
---|
141 |
printf( "\033[0m" ); |
---|
142 |
va_end( arg ); |
---|
143 |
} |
---|
144 |
} |
---|
145 |
|
---|
146 |
const char* |
---|
147 |
DebugAnsiColor::getLevelColor( int level ) const |
---|
148 |
{ |
---|
149 |
switch(level) { |
---|
150 |
case DEBUG_LEVEL_INFO: |
---|
151 |
return "\033[31m"; |
---|
152 |
break; |
---|
153 |
case DEBUG_LEVEL_DEVICE: |
---|
154 |
return "\033[32m"; |
---|
155 |
break; |
---|
156 |
case DEBUG_LEVEL_SUBUNIT: |
---|
157 |
return "\033[33m"; |
---|
158 |
break; |
---|
159 |
case DEBUG_LEVEL_DESCRIPTOR: |
---|
160 |
return "\033[34m"; // blue |
---|
161 |
break; |
---|
162 |
case DEBUG_LEVEL_INFOBLOCK: // purple |
---|
163 |
return "\033[35m"; |
---|
164 |
break; |
---|
165 |
case DEBUG_LEVEL_TRANSFERS: |
---|
166 |
return "\033[36m"; |
---|
167 |
break; |
---|
168 |
default: |
---|
169 |
return "\033[0m"; |
---|
170 |
break; |
---|
171 |
|
---|
172 |
} |
---|
173 |
return ""; |
---|
174 |
} |
---|
175 |
|
---|
176 |
//////////////////////////////////////////////////////////////// |
---|
177 |
DebugHtml::DebugHtml() |
---|
178 |
: DebugBase() |
---|
179 |
{} |
---|
180 |
|
---|
181 |
DebugHtml::~DebugHtml() |
---|
182 |
{} |
---|
183 |
|
---|
184 |
void |
---|
185 |
DebugHtml::error( const char* file, |
---|
186 |
const char* function, |
---|
187 |
unsigned int line, |
---|
188 |
const char* format, ... ) const |
---|
189 |
{ |
---|
190 |
va_list arg; |
---|
191 |
|
---|
192 |
va_start( arg, format ); |
---|
193 |
fprintf( stdout, "<P style='background: red;'><FONT color='white'>" |
---|
194 |
"ERROR: %s %s %d: ", file, function, line ); |
---|
195 |
vfprintf( stdout, format, arg ); |
---|
196 |
fprintf( stdout, "</FONT></P>" ); |
---|
197 |
va_end( arg ); |
---|
198 |
} |
---|
199 |
|
---|
200 |
void |
---|
201 |
DebugHtml::print( int level, |
---|
202 |
const char* file, |
---|
203 |
const char* function, |
---|
204 |
unsigned int line, |
---|
205 |
const char* format, ... ) const |
---|
206 |
{ |
---|
207 |
if (level & m_level) { |
---|
208 |
va_list arg; |
---|
209 |
|
---|
210 |
va_start( arg, format ); |
---|
211 |
printf( "<FONT color='darkGray'>" "DEBUG %s %s %d: %s", |
---|
212 |
file, function, line, getLevelColor( level ) ); |
---|
213 |
vprintf( format, arg ); |
---|
214 |
printf( "</FONT></FONT>" ); |
---|
215 |
va_end( arg ); |
---|
216 |
} |
---|
217 |
} |
---|
218 |
|
---|
219 |
void |
---|
220 |
DebugHtml::printShort( int level, const char* format, ... ) const |
---|
221 |
{ |
---|
222 |
if (level & m_level) { |
---|
223 |
va_list( arg ); |
---|
224 |
|
---|
225 |
va_start( arg, format ); |
---|
226 |
printf ( "%s", getLevelColor( level ) ); |
---|
227 |
vprintf( format, arg ); |
---|
228 |
printf( "</FONT>" ); |
---|
229 |
va_end( arg ); |
---|
230 |
} |
---|
231 |
} |
---|
232 |
|
---|
233 |
const char* |
---|
234 |
DebugHtml::getLevelColor( int level ) const |
---|
235 |
{ |
---|
236 |
switch( level ) { |
---|
237 |
case DEBUG_LEVEL_INFO: |
---|
238 |
return "<FONT color='darkRed'>"; |
---|
239 |
break; |
---|
240 |
case DEBUG_LEVEL_DEVICE: |
---|
241 |
return "<FONT color='darkGreen'>"; |
---|
242 |
break; |
---|
243 |
case DEBUG_LEVEL_SUBUNIT: |
---|
244 |
return "<FONT color='darkYellow'>"; |
---|
245 |
break; |
---|
246 |
case DEBUG_LEVEL_DESCRIPTOR: |
---|
247 |
return "<FONT color='darkBlue'>"; // blue |
---|
248 |
break; |
---|
249 |
case DEBUG_LEVEL_INFOBLOCK: // purple |
---|
250 |
return "<FONT color='darkMagenta'>"; |
---|
251 |
break; |
---|
252 |
case DEBUG_LEVEL_TRANSFERS: |
---|
253 |
return "<FONT color='darkCyan'>"; |
---|
254 |
break; |
---|
255 |
default: |
---|
256 |
return "<FONT color='black'>"; |
---|
257 |
break; |
---|
258 |
} |
---|
259 |
} |
---|
260 |
|
---|
261 |
//////////////////////////////////////////////////////////////// |
---|
262 |
//////////////////////////////////////////////////////////////// |
---|
263 |
|
---|
264 |
unsigned char |
---|
265 |
toAscii( unsigned char c ) |
---|
266 |
{ |
---|
267 |
if ( ( c > 31 ) && ( c < 126) ) { |
---|
268 |
return c; |
---|
269 |
} else { |
---|
270 |
return '.'; |
---|
271 |
} |
---|
272 |
} |
---|
273 |
|
---|
274 |
/* converts a quadlet to a uchar * buffer |
---|
275 |
* not implemented optimally, but clear |
---|
276 |
*/ |
---|
277 |
void |
---|
278 |
quadlet2char( quadlet_t quadlet, unsigned char* buff ) |
---|
279 |
{ |
---|
280 |
*(buff) = (quadlet>>24)&0xFF; |
---|
281 |
*(buff+1) = (quadlet>>16)&0xFF; |
---|
282 |
*(buff+2) = (quadlet>> 8)&0xFF; |
---|
283 |
*(buff+3) = (quadlet) &0xFF; |
---|
284 |
} |
---|
285 |
|
---|
286 |
void |
---|
287 |
hexDump( unsigned char *data_start, unsigned int length ) |
---|
288 |
{ |
---|
289 |
unsigned int i=0; |
---|
290 |
unsigned int byte_pos; |
---|
291 |
unsigned int bytes_left; |
---|
292 |
//printf("hexdump: %p %d\n",data_start,length); |
---|
293 |
|
---|
294 |
if ( length <= 0 ) { |
---|
295 |
return; |
---|
296 |
} |
---|
297 |
if ( length >= 7 ) { |
---|
298 |
for ( i = 0; i < (length-7); i += 8 ) { |
---|
299 |
printf( "%04X: %02X %02X %02X %02X %02X %02X %02X %02X " |
---|
300 |
"- [%c%c%c%c%c%c%c%c]\n", |
---|
301 |
|
---|
302 |
i, |
---|
303 |
|
---|
304 |
*(data_start+i+0), |
---|
305 |
*(data_start+i+1), |
---|
306 |
*(data_start+i+2), |
---|
307 |
*(data_start+i+3), |
---|
308 |
*(data_start+i+4), |
---|
309 |
*(data_start+i+5), |
---|
310 |
*(data_start+i+6), |
---|
311 |
*(data_start+i+7), |
---|
312 |
|
---|
313 |
toAscii( *(data_start+i+0) ), |
---|
314 |
toAscii( *(data_start+i+1) ), |
---|
315 |
toAscii( *(data_start+i+2) ), |
---|
316 |
toAscii( *(data_start+i+3) ), |
---|
317 |
toAscii( *(data_start+i+4) ), |
---|
318 |
toAscii( *(data_start+i+5) ), |
---|
319 |
toAscii( *(data_start+i+6) ), |
---|
320 |
toAscii( *(data_start+i+7) ) |
---|
321 |
); |
---|
322 |
} |
---|
323 |
} |
---|
324 |
byte_pos = i; |
---|
325 |
bytes_left = length - byte_pos; |
---|
326 |
|
---|
327 |
printf( "%04X:" ,i ); |
---|
328 |
for ( i = byte_pos; i < length; i += 1 ) { |
---|
329 |
printf( " %02X", *(data_start+i) ); |
---|
330 |
} |
---|
331 |
for ( i=0; i < 8-bytes_left; i+=1 ) { |
---|
332 |
printf( " " ); |
---|
333 |
} |
---|
334 |
|
---|
335 |
printf( " - [" ); |
---|
336 |
for ( i = byte_pos; i < length; i += 1) { |
---|
337 |
printf( "%c", toAscii(*(data_start+i))); |
---|
338 |
} |
---|
339 |
for ( i = 0; i < 8-bytes_left; i += 1) { |
---|
340 |
printf( " " ); |
---|
341 |
} |
---|
342 |
|
---|
343 |
printf( "]" ); |
---|
344 |
printf( "\n" ); |
---|
345 |
} |
---|