grub2: bring back build of aros-side grub2 tools
[AROS.git] / workbench / classes / zune / nlist / nlistviews_mcp / Debug.c
blobc15cbd03f8e23680df3608e7bdfd2df9d38451db
1 /***************************************************************************
3 NListviews.mcp - New Listview MUI Custom Class Preferences
4 Registered MUI class, Serial Number: 1d51 (0x9d510001 to 0x9d51001F
5 and 0x9d510101 to 0x9d51013F)
7 Copyright (C) 1996-2001 by Gilles Masson
8 Copyright (C) 2001-2014 NList Open Source Team
10 This library is free software; you can redistribute it and/or
11 modify it under the terms of the GNU Lesser General Public
12 License as published by the Free Software Foundation; either
13 version 2.1 of the License, or (at your option) any later version.
15 This library is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Lesser General Public License for more details.
20 NList classes Support Site: http://www.sf.net/projects/nlist-classes
22 $Id$
24 ***************************************************************************/
26 #ifdef DEBUG
28 #include <stdlib.h>
29 #include <string.h>
30 #include <stdarg.h>
31 #include <stdio.h>
33 #include <proto/intuition.h>
34 #include <proto/utility.h>
35 #include <proto/dos.h>
36 #include <proto/exec.h>
38 #include "SDI_compiler.h"
39 #include "Debug.h"
40 #include "version.h"
42 // special flagging macros
43 #define isFlagSet(v,f) (((v) & (f)) == (f)) // return TRUE if the flag is set
44 #define hasFlag(v,f) (((v) & (f)) != 0) // return TRUE if one of the flags in f is set in v
45 #define isFlagClear(v,f) (((v) & (f)) == 0) // return TRUE if flag f is not set in v
46 #define SET_FLAG(v,f) ((v) |= (f)) // set the flag f in v
47 #define CLEAR_FLAG(v,f) ((v) &= ~(f)) // clear the flag f in v
48 #define MASK_FLAG(v,f) ((v) &= (f)) // mask the variable v with flag f bitwise
50 // our static variables with default values
51 static int indent_level = 0;
52 static BOOL ansi_output = FALSE;
53 static ULONG debug_flags = DBF_ALWAYS | DBF_STARTUP; // default debug flags
54 static ULONG debug_classes = DBC_ERROR | DBC_DEBUG | DBC_WARNING | DBC_ASSERT | DBC_REPORT; // default debug classes
56 /****************************************************************************/
58 void SetupDebug(void)
60 char var[256];
62 kprintf("** NListviews.mcp v" LIB_REV_STRING " startup ***********************\n");
63 kprintf("Initializing runtime debugging:\n");
65 if(GetVar("nlistviews.mcp.debug", var, sizeof(var), 0) > 0)
67 char *tok;
68 char *debug = var;
70 // static list of our debugging classes tokens.
71 // in the yamdebug variable these classes always start with a @
72 static struct { const char *token; unsigned long flag; } dbclasses[] =
74 { "ctrace", DBC_CTRACE },
75 { "report", DBC_REPORT },
76 { "assert", DBC_ASSERT },
77 { "timeval", DBC_TIMEVAL },
78 { "debug", DBC_DEBUG },
79 { "error", DBC_ERROR },
80 { "warning", DBC_WARNING },
81 { "all", DBC_ALL },
82 { NULL, 0 }
85 static struct { const char *token; unsigned long flag; } dbflags[] =
87 { "always", DBF_ALWAYS },
88 { "startup", DBF_STARTUP },
89 { "all", DBF_ALL },
90 { NULL, 0 }
93 // we parse the env variable token-wise
94 while((tok = strtok(debug, ", ;")))
96 ULONG i;
98 // check if the token is class definition or
99 // just a flag definition
100 if(tok[0] == '@')
102 // check if this call is a negation or not
103 if(tok[1] == '!')
105 // search for the token and clear the flag
106 for(i=0; dbclasses[i].token; i++)
108 if(stricmp(tok+2, dbclasses[i].token) == 0)
110 kprintf("clear '%s' debug class flag.\n", dbclasses[i].token);
111 CLEAR_FLAG(debug_classes, dbclasses[i].flag);
115 else
117 // search for the token and set the flag
118 for(i=0; dbclasses[i].token; i++)
120 if(stricmp(tok+1, dbclasses[i].token) == 0)
122 kprintf("set '%s' debug class flag\n", dbclasses[i].token);
123 SET_FLAG(debug_classes, dbclasses[i].flag);
128 else
130 // check if this call is a negation or not
131 if(tok[0] == '!')
133 for(i=0; dbflags[i].token; i++)
135 if(stricmp(tok+1, dbflags[i].token) == 0)
137 kprintf("clear '%s' debug flag\n", dbflags[i].token);
138 CLEAR_FLAG(debug_flags, dbflags[i].flag);
142 else
144 // check if the token was "ansi" and if so enable the ANSI color
145 // output
146 if(stricmp(tok, "ansi") == 0)
148 kprintf("ansi output enabled\n");
149 ansi_output = TRUE;
151 else
153 for(i=0; dbflags[i].token; i++)
155 if(stricmp(tok, dbflags[i].token) == 0)
157 kprintf("set '%s' debug flag\n", dbflags[i].token);
158 SET_FLAG(debug_flags, dbflags[i].flag);
165 debug = NULL;
169 kprintf("set debug classes/flags (env:nlistviews.mcp.debug): %08lx/%08lx\n", debug_classes, debug_flags);
170 kprintf("** Normal processing follows ***************************************\n");
173 /****************************************************************************/
175 void CleanupDebug(void)
177 kprintf("** Cleaned up debugging ********************************************\n");
180 /****************************************************************************/
182 // define variables for using ANSI colors in our debugging scheme
183 #define ANSI_ESC_CLR "\033[0m"
184 #define ANSI_ESC_BOLD "\033[1m"
185 #define ANSI_ESC_UNDERLINE "\033[4m"
186 #define ANSI_ESC_BLINK "\033[5m"
187 #define ANSI_ESC_REVERSE "\033[7m"
188 #define ANSI_ESC_INVISIBLE "\033[8m"
189 #define ANSI_ESC_FG_BLACK "\033[0;30m"
190 #define ANSI_ESC_FG_RED "\033[0;31m"
191 #define ANSI_ESC_FG_GREEN "\033[0;32m"
192 #define ANSI_ESC_FG_BROWN "\033[0;33m"
193 #define ANSI_ESC_FG_BLUE "\033[0;34m"
194 #define ANSI_ESC_FG_PURPLE "\033[0;35m"
195 #define ANSI_ESC_FG_CYAN "\033[0;36m"
196 #define ANSI_ESC_FG_LGRAY "\033[0;37m"
197 #define ANSI_ESC_FG_DGRAY "\033[1;30m"
198 #define ANSI_ESC_FG_LRED "\033[1;31m"
199 #define ANSI_ESC_FG_LGREEN "\033[1;32m"
200 #define ANSI_ESC_FG_YELLOW "\033[1;33m"
201 #define ANSI_ESC_FG_LBLUE "\033[1;34m"
202 #define ANSI_ESC_FG_LPURPLE "\033[1;35m"
203 #define ANSI_ESC_FG_LCYAN "\033[1;36m"
204 #define ANSI_ESC_FG_WHITE "\033[1;37m"
205 #define ANSI_ESC_BG "\033[0;4" // background esc-squ start with 4x
206 #define ANSI_ESC_BG_BLACK "\033[0;40m"
207 #define ANSI_ESC_BG_RED "\033[0;41m"
208 #define ANSI_ESC_BG_GREEN "\033[0;42m"
209 #define ANSI_ESC_BG_BROWN "\033[0;43m"
210 #define ANSI_ESC_BG_BLUE "\033[0;44m"
211 #define ANSI_ESC_BG_PURPLE "\033[0;45m"
212 #define ANSI_ESC_BG_CYAN "\033[0;46m"
213 #define ANSI_ESC_BG_LGRAY "\033[0;47m"
215 /****************************************************************************/
217 INLINE void _INDENT(void)
219 int i;
220 for(i=0; i < indent_level; i++)
221 kprintf(" ");
224 /****************************************************************************/
226 void _ENTER(unsigned long dclass, const char *file, int line, const char *function)
228 if(isFlagSet(debug_classes, dclass))
230 _INDENT();
231 if(ansi_output)
232 kprintf("%s%s:%ld:Entering %s%s\n", ANSI_ESC_FG_BROWN, file, line, function, ANSI_ESC_CLR);
233 else
234 kprintf("%s:%ld:Entering %s\n", file, line, function);
237 indent_level++;
240 void _LEAVE(unsigned long dclass, const char *file, int line, const char *function)
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 indent_level--;
258 if(isFlagSet(debug_classes, dclass))
260 _INDENT();
261 if(ansi_output)
262 kprintf("%s%s:%ld:Leaving %s (result 0x%08lx, %ld)%s\n", ANSI_ESC_FG_BROWN, file, line, function, result, result, ANSI_ESC_CLR);
263 else
264 kprintf("%s:%ld:Leaving %s (result 0x%08lx, %ld)\n", file, line, function, result, result);
268 /****************************************************************************/
270 void _SHOWVALUE(unsigned long dclass, unsigned long dflags, unsigned long value, int size, const char *name, const char *file, int line)
272 if(isFlagSet(debug_classes, dclass) &&
273 isFlagSet(debug_flags, dflags))
275 const char *fmt;
277 switch(size)
279 case 1:
280 fmt = "%s:%ld:%s = %ld, 0x%02lx";
281 break;
283 case 2:
284 fmt = "%s:%ld:%s = %ld, 0x%04lx";
285 break;
287 default:
288 fmt = "%s:%ld:%s = %ld, 0x%08lx";
289 break;
292 _INDENT();
294 if(ansi_output)
295 kprintf(ANSI_ESC_FG_GREEN);
297 kprintf(fmt, file, line, name, value, value);
299 if(size == 1 && value < 256)
301 if(value < ' ' || (value >= 127 && value < 160))
302 kprintf(", '\\x%02lx'", value);
303 else
304 kprintf(", '%lc'", value);
307 if(ansi_output)
308 kprintf("%s\n", ANSI_ESC_CLR);
309 else
310 kprintf("\n");
314 /****************************************************************************/
316 void _SHOWPOINTER(unsigned long dclass, unsigned long dflags, const void *p, const char *name, const char *file, int line)
318 if(isFlagSet(debug_classes, dclass) &&
319 isFlagSet(debug_flags, dflags))
321 const char *fmt;
323 _INDENT();
325 if(p != NULL)
326 fmt = "%s:%ld:%s = 0x%08lx\n";
327 else
328 fmt = "%s:%ld:%s = NULL\n";
330 if(ansi_output)
332 kprintf(ANSI_ESC_FG_GREEN);
333 kprintf(fmt, file, line, name, p);
334 kprintf(ANSI_ESC_CLR);
336 else
337 kprintf(fmt, file, line, name, p);
341 /****************************************************************************/
343 void _SHOWSTRING(unsigned long dclass, unsigned long dflags, const char *string, const char *name, const char *file, int line)
345 if(isFlagSet(debug_classes, dclass) &&
346 isFlagSet(debug_flags, dflags))
348 _INDENT();
350 if(ansi_output)
351 kprintf("%s%s:%ld:%s = 0x%08lx \"%s\"%s\n", ANSI_ESC_FG_GREEN, file, line, name, string, string, ANSI_ESC_CLR);
352 else
353 kprintf("%s:%ld:%s = 0x%08lx \"%s\"\n", file, line, name, string, string);
357 /****************************************************************************/
359 void _SHOWMSG(unsigned long dclass, unsigned long dflags, const char *msg, const char *file, int line)
361 if(isFlagSet(debug_classes, dclass) &&
362 isFlagSet(debug_flags, dflags))
364 _INDENT();
366 if(ansi_output)
367 kprintf("%s%s:%ld:%s%s\n", ANSI_ESC_FG_GREEN, file, line, msg, ANSI_ESC_CLR);
368 else
369 kprintf("%s:%ld:%s\n", file, line, msg);
373 /****************************************************************************/
375 void _DPRINTF(unsigned long dclass, unsigned long dflags, const char *file, int line, const char *format, ...)
377 if((isFlagSet(debug_classes, dclass) && isFlagSet(debug_flags, dflags)) ||
378 (isFlagSet(dclass, DBC_ERROR) || isFlagSet(dclass, DBC_WARNING)))
380 va_list args;
381 static char buf[1024];
383 _INDENT();
385 va_start(args, format);
386 vsnprintf(buf, 1024, format, args);
387 va_end(args);
389 if(ansi_output)
391 const char *highlight = ANSI_ESC_FG_GREEN;
393 switch(dclass)
395 case DBC_CTRACE: highlight = ANSI_ESC_FG_BROWN; break;
396 case DBC_REPORT: highlight = ANSI_ESC_FG_GREEN; break;
397 case DBC_ASSERT: highlight = ANSI_ESC_FG_RED; break;
398 case DBC_TIMEVAL: highlight = ANSI_ESC_FG_GREEN; break;
399 case DBC_DEBUG: highlight = ANSI_ESC_FG_GREEN; break;
400 case DBC_ERROR: highlight = ANSI_ESC_FG_RED; break;
401 case DBC_WARNING: highlight = ANSI_ESC_FG_PURPLE;break;
404 kprintf("%s%s:%ld:%s%s\n", highlight, file, line, buf, ANSI_ESC_CLR);
406 else
407 kprintf("%s:%ld:%s\n", file, line, buf);
411 /****************************************************************************/
413 #endif