grub2: bring back build of aros-side grub2 tools
[AROS.git] / workbench / classes / zune / nlist / nlistview_mcc / Debug.c
bloba9ce509ed6845f905544016d61324958999acdae
1 /***************************************************************************
3 NListview.mcc - New Listview MUI Custom Class
4 Registered MUI class, Serial Number: 1d51 (0x9d510020 to 0x9d51002F)
6 Copyright (C) 1996-2001 by Gilles Masson
7 Copyright (C) 2001-2014 NList Open Source Team
9 This library is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Lesser General Public
11 License as published by the Free Software Foundation; either
12 version 2.1 of the License, or (at your option) any later version.
14 This library is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
19 NList classes Support Site: http://www.sf.net/projects/nlist-classes
21 $Id$
23 ***************************************************************************/
25 #ifdef DEBUG
27 #include <stdlib.h>
28 #include <string.h>
29 #include <stdarg.h>
30 #include <stdio.h>
32 #include <proto/intuition.h>
33 #include <proto/utility.h>
34 #include <proto/dos.h>
35 #include <proto/exec.h>
37 #include "SDI_compiler.h"
38 #include "Debug.h"
39 #include "version.h"
41 // special flagging macros
42 #define isFlagSet(v,f) (((v) & (f)) == (f)) // return TRUE if the flag is set
43 #define hasFlag(v,f) (((v) & (f)) != 0) // return TRUE if one of the flags in f is set in v
44 #define isFlagClear(v,f) (((v) & (f)) == 0) // return TRUE if flag f is not set in v
45 #define SET_FLAG(v,f) ((v) |= (f)) // set the flag f in v
46 #define CLEAR_FLAG(v,f) ((v) &= ~(f)) // clear the flag f in v
47 #define MASK_FLAG(v,f) ((v) &= (f)) // mask the variable v with flag f bitwise
49 // our static variables with default values
50 static int indent_level = 0;
51 static BOOL ansi_output = FALSE;
52 static ULONG debug_flags = DBF_ALWAYS | DBF_STARTUP; // default debug flags
53 static ULONG debug_classes = DBC_ERROR | DBC_DEBUG | DBC_WARNING | DBC_ASSERT | DBC_REPORT; // default debug classes
55 /****************************************************************************/
57 void SetupDebug(void)
59 char var[256];
61 kprintf("** NListview.mcc v" LIB_REV_STRING " startup ***********************\n");
62 kprintf("Initializing runtime debugging:\n");
64 if(GetVar("nlistview.mcc.debug", var, sizeof(var), 0) > 0)
66 char *tok;
67 char *debug = var;
69 // static list of our debugging classes tokens.
70 // in the yamdebug variable these classes always start with a @
71 static struct { const char *token; unsigned long flag; } dbclasses[] =
73 { "ctrace", DBC_CTRACE },
74 { "report", DBC_REPORT },
75 { "assert", DBC_ASSERT },
76 { "timeval", DBC_TIMEVAL },
77 { "debug", DBC_DEBUG },
78 { "error", DBC_ERROR },
79 { "warning", DBC_WARNING },
80 { "all", DBC_ALL },
81 { NULL, 0 }
84 static struct { const char *token; unsigned long flag; } dbflags[] =
86 { "always", DBF_ALWAYS },
87 { "startup", DBF_STARTUP },
88 { "all", DBF_ALL },
89 { NULL, 0 }
92 // we parse the env variable token-wise
93 while((tok = strtok(debug, ", ;")))
95 ULONG i;
97 // check if the token is class definition or
98 // just a flag definition
99 if(tok[0] == '@')
101 // check if this call is a negation or not
102 if(tok[1] == '!')
104 // search for the token and clear the flag
105 for(i=0; dbclasses[i].token; i++)
107 if(stricmp(tok+2, dbclasses[i].token) == 0)
109 kprintf("clear '%s' debug class flag.\n", dbclasses[i].token);
110 CLEAR_FLAG(debug_classes, dbclasses[i].flag);
114 else
116 // search for the token and set the flag
117 for(i=0; dbclasses[i].token; i++)
119 if(stricmp(tok+1, dbclasses[i].token) == 0)
121 kprintf("set '%s' debug class flag\n", dbclasses[i].token);
122 SET_FLAG(debug_classes, dbclasses[i].flag);
127 else
129 // check if this call is a negation or not
130 if(tok[0] == '!')
132 for(i=0; dbflags[i].token; i++)
134 if(stricmp(tok+1, dbflags[i].token) == 0)
136 kprintf("clear '%s' debug flag\n", dbflags[i].token);
137 CLEAR_FLAG(debug_flags, dbflags[i].flag);
141 else
143 // check if the token was "ansi" and if so enable the ANSI color
144 // output
145 if(stricmp(tok, "ansi") == 0)
147 kprintf("ansi output enabled\n");
148 ansi_output = TRUE;
150 else
152 for(i=0; dbflags[i].token; i++)
154 if(stricmp(tok, dbflags[i].token) == 0)
156 kprintf("set '%s' debug flag\n", dbflags[i].token);
157 SET_FLAG(debug_flags, dbflags[i].flag);
164 debug = NULL;
168 kprintf("set debug classes/flags (env:nlistview.mcc.debug): %08lx/%08lx\n", debug_classes, debug_flags);
169 kprintf("** Normal processing follows ***************************************\n");
172 /****************************************************************************/
174 void CleanupDebug(void)
176 kprintf("** Cleaned up debugging ********************************************\n");
179 /****************************************************************************/
181 // define variables for using ANSI colors in our debugging scheme
182 #define ANSI_ESC_CLR "\033[0m"
183 #define ANSI_ESC_BOLD "\033[1m"
184 #define ANSI_ESC_UNDERLINE "\033[4m"
185 #define ANSI_ESC_BLINK "\033[5m"
186 #define ANSI_ESC_REVERSE "\033[7m"
187 #define ANSI_ESC_INVISIBLE "\033[8m"
188 #define ANSI_ESC_FG_BLACK "\033[0;30m"
189 #define ANSI_ESC_FG_RED "\033[0;31m"
190 #define ANSI_ESC_FG_GREEN "\033[0;32m"
191 #define ANSI_ESC_FG_BROWN "\033[0;33m"
192 #define ANSI_ESC_FG_BLUE "\033[0;34m"
193 #define ANSI_ESC_FG_PURPLE "\033[0;35m"
194 #define ANSI_ESC_FG_CYAN "\033[0;36m"
195 #define ANSI_ESC_FG_LGRAY "\033[0;37m"
196 #define ANSI_ESC_FG_DGRAY "\033[1;30m"
197 #define ANSI_ESC_FG_LRED "\033[1;31m"
198 #define ANSI_ESC_FG_LGREEN "\033[1;32m"
199 #define ANSI_ESC_FG_YELLOW "\033[1;33m"
200 #define ANSI_ESC_FG_LBLUE "\033[1;34m"
201 #define ANSI_ESC_FG_LPURPLE "\033[1;35m"
202 #define ANSI_ESC_FG_LCYAN "\033[1;36m"
203 #define ANSI_ESC_FG_WHITE "\033[1;37m"
204 #define ANSI_ESC_BG "\033[0;4" // background esc-squ start with 4x
205 #define ANSI_ESC_BG_BLACK "\033[0;40m"
206 #define ANSI_ESC_BG_RED "\033[0;41m"
207 #define ANSI_ESC_BG_GREEN "\033[0;42m"
208 #define ANSI_ESC_BG_BROWN "\033[0;43m"
209 #define ANSI_ESC_BG_BLUE "\033[0;44m"
210 #define ANSI_ESC_BG_PURPLE "\033[0;45m"
211 #define ANSI_ESC_BG_CYAN "\033[0;46m"
212 #define ANSI_ESC_BG_LGRAY "\033[0;47m"
214 /****************************************************************************/
216 INLINE void _INDENT(void)
218 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 indent_level--;
243 if(isFlagSet(debug_classes, dclass))
245 _INDENT();
246 if(ansi_output)
247 kprintf("%s%s:%ld:Leaving %s%s\n", ANSI_ESC_FG_BROWN, file, line, function, ANSI_ESC_CLR);
248 else
249 kprintf("%s:%ld:Leaving %s\n", file, line, function);
253 void _RETURN(unsigned long dclass, const char *file, int line, const char *function, unsigned long result)
255 indent_level--;
257 if(isFlagSet(debug_classes, dclass))
259 _INDENT();
260 if(ansi_output)
261 kprintf("%s%s:%ld:Leaving %s (result 0x%08lx, %ld)%s\n", ANSI_ESC_FG_BROWN, file, line, function, result, result, ANSI_ESC_CLR);
262 else
263 kprintf("%s:%ld:Leaving %s (result 0x%08lx, %ld)\n", file, line, function, result, result);
267 /****************************************************************************/
269 void _SHOWVALUE(unsigned long dclass, unsigned long dflags, unsigned long value, int size, const char *name, const char *file, int line)
271 if(isFlagSet(debug_classes, dclass) &&
272 isFlagSet(debug_flags, dflags))
274 const char *fmt;
276 switch(size)
278 case 1:
279 fmt = "%s:%ld:%s = %ld, 0x%02lx";
280 break;
282 case 2:
283 fmt = "%s:%ld:%s = %ld, 0x%04lx";
284 break;
286 default:
287 fmt = "%s:%ld:%s = %ld, 0x%08lx";
288 break;
291 _INDENT();
293 if(ansi_output)
294 kprintf(ANSI_ESC_FG_GREEN);
296 kprintf(fmt, file, line, name, value, value);
298 if(size == 1 && value < 256)
300 if(value < ' ' || (value >= 127 && value < 160))
301 kprintf(", '\\x%02lx'", value);
302 else
303 kprintf(", '%lc'", value);
306 if(ansi_output)
307 kprintf("%s\n", ANSI_ESC_CLR);
308 else
309 kprintf("\n");
313 /****************************************************************************/
315 void _SHOWPOINTER(unsigned long dclass, unsigned long dflags, const void *p, const char *name, const char *file, int line)
317 if(isFlagSet(debug_classes, dclass) &&
318 isFlagSet(debug_flags, dflags))
320 const char *fmt;
322 _INDENT();
324 if(p != NULL)
325 fmt = "%s:%ld:%s = 0x%08lx\n";
326 else
327 fmt = "%s:%ld:%s = NULL\n";
329 if(ansi_output)
331 kprintf(ANSI_ESC_FG_GREEN);
332 kprintf(fmt, file, line, name, p);
333 kprintf(ANSI_ESC_CLR);
335 else
336 kprintf(fmt, file, line, name, p);
340 /****************************************************************************/
342 void _SHOWSTRING(unsigned long dclass, unsigned long dflags, const char *string, const char *name, const char *file, int line)
344 if(isFlagSet(debug_classes, dclass) &&
345 isFlagSet(debug_flags, dflags))
347 _INDENT();
349 if(ansi_output)
350 kprintf("%s%s:%ld:%s = 0x%08lx \"%s\"%s\n", ANSI_ESC_FG_GREEN, file, line, name, string, string, ANSI_ESC_CLR);
351 else
352 kprintf("%s:%ld:%s = 0x%08lx \"%s\"\n", file, line, name, string, string);
356 /****************************************************************************/
358 void _SHOWMSG(unsigned long dclass, unsigned long dflags, const char *msg, const char *file, int line)
360 if(isFlagSet(debug_classes, dclass) &&
361 isFlagSet(debug_flags, dflags))
363 _INDENT();
365 if(ansi_output)
366 kprintf("%s%s:%ld:%s%s\n", ANSI_ESC_FG_GREEN, file, line, msg, ANSI_ESC_CLR);
367 else
368 kprintf("%s:%ld:%s\n", file, line, msg);
372 /****************************************************************************/
374 void _DPRINTF(unsigned long dclass, unsigned long dflags, const char *file, int line, const char *format, ...)
376 if((isFlagSet(debug_classes, dclass) && isFlagSet(debug_flags, dflags)) ||
377 (isFlagSet(dclass, DBC_ERROR) || isFlagSet(dclass, DBC_WARNING)))
379 va_list args;
380 static char buf[1024];
382 _INDENT();
384 va_start(args, format);
385 vsnprintf(buf, 1024, format, args);
386 va_end(args);
388 if(ansi_output)
390 const char *highlight = ANSI_ESC_FG_GREEN;
392 switch(dclass)
394 case DBC_CTRACE: highlight = ANSI_ESC_FG_BROWN; break;
395 case DBC_REPORT: highlight = ANSI_ESC_FG_GREEN; break;
396 case DBC_ASSERT: highlight = ANSI_ESC_FG_RED; break;
397 case DBC_TIMEVAL: highlight = ANSI_ESC_FG_GREEN; break;
398 case DBC_DEBUG: highlight = ANSI_ESC_FG_GREEN; break;
399 case DBC_ERROR: highlight = ANSI_ESC_FG_RED; break;
400 case DBC_WARNING: highlight = ANSI_ESC_FG_PURPLE;break;
403 kprintf("%s%s:%ld:%s%s\n", highlight, file, line, buf, ANSI_ESC_CLR);
405 else
406 kprintf("%s:%ld:%s\n", file, line, buf);
410 /****************************************************************************/
412 #endif