revert 213 commits (to 56092) from the last month. 10 still need work to resolve...
[AROS.git] / workbench / classes / zune / nlist / nbalance_mcc / Debug.c
blobc26adb7fb34f36c562be7993409cc80aee06dc34
1 /***************************************************************************
3 NBalance.mcc - New Balance MUI Custom Class
4 Copyright (C) 2008-2013 by NList Open Source Team
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 NList classes Support Site: http://www.sf.net/projects/nlist-classes
18 $Id$
20 ***************************************************************************/
22 #ifdef DEBUG
24 #include <stdlib.h>
25 #include <string.h>
26 #include <stdarg.h>
27 #include <stdio.h>
29 #include <proto/intuition.h>
30 #include <proto/utility.h>
31 #include <proto/dos.h>
32 #include <proto/exec.h>
34 #include "SDI_compiler.h"
35 #include "Debug.h"
36 #include "version.h"
38 // special flagging macros
39 #define isFlagSet(v,f) (((v) & (f)) == (f)) // return TRUE if the flag is set
40 #define hasFlag(v,f) (((v) & (f)) != 0) // return TRUE if one of the flags in f is set in v
41 #define isFlagClear(v,f) (((v) & (f)) == 0) // return TRUE if flag f is not set in v
42 #define SET_FLAG(v,f) ((v) |= (f)) // set the flag f in v
43 #define CLEAR_FLAG(v,f) ((v) &= ~(f)) // clear the flag f in v
44 #define MASK_FLAG(v,f) ((v) &= (f)) // mask the variable v with flag f bitwise
46 // our static variables with default values
47 static int indent_level = 0;
48 static BOOL ansi_output = FALSE;
49 static ULONG debug_flags = DBF_ALWAYS | DBF_STARTUP; // default debug flags
50 static ULONG debug_classes = DBC_ERROR | DBC_DEBUG | DBC_WARNING | DBC_ASSERT | DBC_REPORT; // default debug classes
52 /****************************************************************************/
54 void SetupDebug(void)
56 char var[256];
58 kprintf("** NBalance.mcc v" LIB_REV_STRING " startup ***********************\n");
59 kprintf("Initializing runtime debugging:\n");
61 if(GetVar("nbalance.mcc.debug", var, sizeof(var), 0) > 0)
63 char *tok;
64 char *debug = var;
66 // static list of our debugging classes tokens.
67 // in the yamdebug variable these classes always start with a @
68 static struct { const char *token; unsigned long flag; } dbclasses[] =
70 { "ctrace", DBC_CTRACE },
71 { "report", DBC_REPORT },
72 { "assert", DBC_ASSERT },
73 { "timeval", DBC_TIMEVAL },
74 { "debug", DBC_DEBUG },
75 { "error", DBC_ERROR },
76 { "warning", DBC_WARNING },
77 { "all", DBC_ALL },
78 { NULL, 0 }
81 static struct { const char *token; unsigned long flag; } dbflags[] =
83 { "always", DBF_ALWAYS },
84 { "startup", DBF_STARTUP },
85 { "input", DBF_INPUT },
86 { "gui", DBF_GUI },
87 { "all", DBF_ALL },
88 { NULL, 0 }
91 // we parse the env variable token-wise
92 while((tok = strtok(debug, ", ;")) != NULL)
94 ULONG i;
96 // check if the token is class definition or
97 // just a flag definition
98 if(tok[0] == '@')
100 // check if this call is a negation or not
101 if(tok[1] == '!')
103 // search for the token and clear the flag
104 for(i = 0; dbclasses[i].token != NULL; i++)
106 if(stricmp(tok + 2, dbclasses[i].token) == 0)
108 kprintf("clear '%s' debug class flag.\n", dbclasses[i].token);
109 CLEAR_FLAG(debug_classes, dbclasses[i].flag);
113 else
115 // search for the token and set the flag
116 for(i = 0; dbclasses[i].token != NULL; i++)
118 if(stricmp(tok + 1, dbclasses[i].token) == 0)
120 kprintf("set '%s' debug class flag\n", dbclasses[i].token);
121 SET_FLAG(debug_classes, dbclasses[i].flag);
126 else
128 // check if this call is a negation or not
129 if(tok[0] == '!')
131 for(i = 0; dbflags[i].token != NULL; i++)
133 if(stricmp(tok + 1, dbflags[i].token) == 0)
135 kprintf("clear '%s' debug flag\n", dbflags[i].token);
136 CLEAR_FLAG(debug_flags, dbflags[i].flag);
140 else
142 // check if the token was "ansi" and if so enable the ANSI color
143 // output
144 if(stricmp(tok, "ansi") == 0)
146 kprintf("ansi output enabled\n");
147 ansi_output = TRUE;
149 else
151 for(i = 0; dbflags[i].token != NULL; i++)
153 if(stricmp(tok, dbflags[i].token) == 0)
155 kprintf("set '%s' debug flag\n", dbflags[i].token);
156 SET_FLAG(debug_flags, dbflags[i].flag);
163 debug = NULL;
167 kprintf("set debug classes/flags (env:nbalance.mcc.debug): %08lx/%08lx\n", debug_classes, debug_flags);
168 kprintf("** Normal processing follows ***************************************\n");
171 /****************************************************************************/
173 void CleanupDebug(void)
175 kprintf("** Cleaned up debugging ********************************************\n");
178 /****************************************************************************/
180 // define variables for using ANSI colors in our debugging scheme
181 #define ANSI_ESC_CLR "\033[0m"
182 #define ANSI_ESC_BOLD "\033[1m"
183 #define ANSI_ESC_UNDERLINE "\033[4m"
184 #define ANSI_ESC_BLINK "\033[5m"
185 #define ANSI_ESC_REVERSE "\033[7m"
186 #define ANSI_ESC_INVISIBLE "\033[8m"
187 #define ANSI_ESC_FG_BLACK "\033[0;30m"
188 #define ANSI_ESC_FG_RED "\033[0;31m"
189 #define ANSI_ESC_FG_GREEN "\033[0;32m"
190 #define ANSI_ESC_FG_BROWN "\033[0;33m"
191 #define ANSI_ESC_FG_BLUE "\033[0;34m"
192 #define ANSI_ESC_FG_PURPLE "\033[0;35m"
193 #define ANSI_ESC_FG_CYAN "\033[0;36m"
194 #define ANSI_ESC_FG_LGRAY "\033[0;37m"
195 #define ANSI_ESC_FG_DGRAY "\033[1;30m"
196 #define ANSI_ESC_FG_LRED "\033[1;31m"
197 #define ANSI_ESC_FG_LGREEN "\033[1;32m"
198 #define ANSI_ESC_FG_YELLOW "\033[1;33m"
199 #define ANSI_ESC_FG_LBLUE "\033[1;34m"
200 #define ANSI_ESC_FG_LPURPLE "\033[1;35m"
201 #define ANSI_ESC_FG_LCYAN "\033[1;36m"
202 #define ANSI_ESC_FG_WHITE "\033[1;37m"
203 #define ANSI_ESC_BG "\033[0;4" // background esc-squ start with 4x
204 #define ANSI_ESC_BG_BLACK "\033[0;40m"
205 #define ANSI_ESC_BG_RED "\033[0;41m"
206 #define ANSI_ESC_BG_GREEN "\033[0;42m"
207 #define ANSI_ESC_BG_BROWN "\033[0;43m"
208 #define ANSI_ESC_BG_BLUE "\033[0;44m"
209 #define ANSI_ESC_BG_PURPLE "\033[0;45m"
210 #define ANSI_ESC_BG_CYAN "\033[0;46m"
211 #define ANSI_ESC_BG_LGRAY "\033[0;47m"
213 /****************************************************************************/
215 INLINE void _INDENT(void)
217 int i;
219 for(i = 0; i < indent_level; i++)
220 kprintf(" ");
223 /****************************************************************************/
225 void _ENTER(unsigned long dclass, const char *file, int line, const char *function)
227 if(isFlagSet(debug_classes, dclass))
229 _INDENT();
230 if(ansi_output)
231 kprintf("%s%s:%ld:Entering %s%s\n", ANSI_ESC_FG_BROWN, file, line, function, ANSI_ESC_CLR);
232 else
233 kprintf("%s:%ld:Entering %s\n", file, line, function);
236 indent_level++;
239 void _LEAVE(unsigned long dclass, const char *file, int line, const char *function)
241 if(indent_level > 0)
242 indent_level--;
244 if(isFlagSet(debug_classes, dclass))
246 _INDENT();
247 if(ansi_output)
248 kprintf("%s%s:%ld:Leaving %s%s\n", ANSI_ESC_FG_BROWN, file, line, function, ANSI_ESC_CLR);
249 else
250 kprintf("%s:%ld:Leaving %s\n", file, line, function);
254 void _RETURN(unsigned long dclass, const char *file, int line, const char *function, unsigned long result)
256 if(indent_level > 0)
257 indent_level--;
259 if(isFlagSet(debug_classes, dclass))
261 _INDENT();
262 if(ansi_output)
263 kprintf("%s%s:%ld:Leaving %s (result 0x%08lx, %ld)%s\n", ANSI_ESC_FG_BROWN, file, line, function, result, result, ANSI_ESC_CLR);
264 else
265 kprintf("%s:%ld:Leaving %s (result 0x%08lx, %ld)\n", file, line, function, result, result);
269 /****************************************************************************/
271 void _SHOWVALUE(unsigned long dclass, unsigned long dflags, unsigned long value, int size, const char *name, const char *file, int line)
273 if(isFlagSet(debug_classes, dclass) &&
274 isFlagSet(debug_flags, dflags))
276 const char *fmt;
278 switch(size)
280 case 1:
281 fmt = "%s:%ld:%s = %ld, 0x%02lx";
282 break;
284 case 2:
285 fmt = "%s:%ld:%s = %ld, 0x%04lx";
286 break;
288 default:
289 fmt = "%s:%ld:%s = %ld, 0x%08lx";
290 break;
293 _INDENT();
295 if(ansi_output)
296 kprintf(ANSI_ESC_FG_GREEN);
298 kprintf(fmt, file, line, name, value, value);
300 if(size == 1 && value < 256)
302 if(value < ' ' || (value >= 127 && value < 160))
303 kprintf(", '\\x%02lx'", value);
304 else
305 kprintf(", '%lc'", value);
308 if(ansi_output)
309 kprintf("%s\n", ANSI_ESC_CLR);
310 else
311 kprintf("\n");
315 /****************************************************************************/
317 void _SHOWPOINTER(unsigned long dclass, unsigned long dflags, const void *p, const char *name, const char *file, int line)
319 if(isFlagSet(debug_classes, dclass) &&
320 isFlagSet(debug_flags, dflags))
322 const char *fmt;
324 _INDENT();
326 if(p != NULL)
327 fmt = "%s:%ld:%s = 0x%08lx\n";
328 else
329 fmt = "%s:%ld:%s = NULL\n";
331 if(ansi_output)
333 kprintf(ANSI_ESC_FG_GREEN);
334 kprintf(fmt, file, line, name, p);
335 kprintf(ANSI_ESC_CLR);
337 else
338 kprintf(fmt, file, line, name, p);
342 /****************************************************************************/
344 void _SHOWSTRING(unsigned long dclass, unsigned long dflags, const char *string, const char *name, const char *file, int line)
346 if(isFlagSet(debug_classes, dclass) &&
347 isFlagSet(debug_flags, dflags))
349 _INDENT();
351 if(ansi_output)
352 kprintf("%s%s:%ld:%s = 0x%08lx \"%s\"%s\n", ANSI_ESC_FG_GREEN, file, line, name, string, string, ANSI_ESC_CLR);
353 else
354 kprintf("%s:%ld:%s = 0x%08lx \"%s\"\n", file, line, name, string, string);
358 /****************************************************************************/
360 void _SHOWMSG(unsigned long dclass, unsigned long dflags, const char *msg, const char *file, int line)
362 if(isFlagSet(debug_classes, dclass) &&
363 isFlagSet(debug_flags, dflags))
365 _INDENT();
367 if(ansi_output)
368 kprintf("%s%s:%ld:%s%s\n", ANSI_ESC_FG_GREEN, file, line, msg, ANSI_ESC_CLR);
369 else
370 kprintf("%s:%ld:%s\n", file, line, msg);
374 /****************************************************************************/
376 void _DPRINTF(unsigned long dclass, unsigned long dflags, const char *file, int line, const char *format, ...)
378 if((isFlagSet(debug_classes, dclass) && isFlagSet(debug_flags, dflags)) ||
379 (isFlagSet(dclass, DBC_ERROR) || isFlagSet(dclass, DBC_WARNING)))
381 va_list args;
382 static char buf[1024];
384 _INDENT();
386 va_start(args, format);
387 vsnprintf(buf, 1024, format, args);
388 va_end(args);
390 if(ansi_output)
392 const char *highlight = ANSI_ESC_FG_GREEN;
394 switch(dclass)
396 case DBC_CTRACE: highlight = ANSI_ESC_FG_BROWN; break;
397 case DBC_REPORT: highlight = ANSI_ESC_FG_GREEN; break;
398 case DBC_ASSERT: highlight = ANSI_ESC_FG_RED; break;
399 case DBC_TIMEVAL: highlight = ANSI_ESC_FG_GREEN; break;
400 case DBC_DEBUG: highlight = ANSI_ESC_FG_GREEN; break;
401 case DBC_ERROR: highlight = ANSI_ESC_FG_RED; break;
402 case DBC_WARNING: highlight = ANSI_ESC_FG_PURPLE;break;
405 kprintf("%s%s:%ld:%s%s\n", highlight, file, line, buf, ANSI_ESC_CLR);
407 else
408 kprintf("%s:%ld:%s\n", file, line, buf);
412 /****************************************************************************/
414 #endif