revert between 56095 -> 55830 in arch
[AROS.git] / workbench / classes / zune / betterstring / mcc / Debug.c
blob68a8090c82f42f01c2ed442774bb083de6fc295d
1 /***************************************************************************
3 BetterString.mcc - A better String gadget MUI Custom Class
4 Copyright (C) 1997-2000 Allan Odgaard
5 Copyright (C) 2005-2013 by BetterString.mcc Open Source Team
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 BetterString class Support Site: http://www.sf.net/projects/bstring-mcc/
19 $Id$
21 ***************************************************************************/
23 #ifdef DEBUG
25 #include <stdlib.h>
26 #include <string.h>
27 #include <stdarg.h>
28 #include <stdio.h>
30 #include <proto/intuition.h>
31 #include <proto/utility.h>
32 #include <proto/dos.h>
33 #include <proto/exec.h>
35 #include "SDI_compiler.h"
37 #include "private.h"
39 #include "Debug.h"
40 #include "version.h"
42 // our static variables with default values
43 static int indent_level = 0;
44 static BOOL ansi_output = FALSE;
45 static ULONG debug_flags = DBF_ALWAYS | DBF_STARTUP; // default debug flags
46 static ULONG debug_classes = DBC_ERROR | DBC_DEBUG | DBC_WARNING | DBC_ASSERT | DBC_REPORT; // default debug classes
48 /****************************************************************************/
50 void SetupDebug(void)
52 char var[256];
54 kprintf("** BetterString.mcc v" LIB_REV_STRING " startup ********************\n");
55 kprintf("Initializing runtime debugging:\n");
57 if(GetVar("betterstring.mcc.debug", var, sizeof(var), 0) > 0)
59 char *s = var;
61 // static list of our debugging classes tokens.
62 // in the yamdebug variable these classes always start with a @
63 static struct { const char *token; const unsigned long flag; } dbclasses[] =
65 { "ctrace", DBC_CTRACE },
66 { "report", DBC_REPORT },
67 { "assert", DBC_ASSERT },
68 { "timeval", DBC_TIMEVAL },
69 { "debug", DBC_DEBUG },
70 { "error", DBC_ERROR },
71 { "warning", DBC_WARNING },
72 { "all", DBC_ALL },
73 { NULL, 0 }
76 static struct { const char *token; const unsigned long flag; } dbflags[] =
78 { "always", DBF_ALWAYS },
79 { "startup", DBF_STARTUP },
80 { "input", DBF_INPUT },
81 { "clipboard", DBF_CLIPBOARD },
82 { "all", DBF_ALL },
83 { NULL, 0 }
86 kprintf("parsing ENV:betterstring.mcc.debug content '%s'\n", s);
88 // we parse the env variable token-wise
89 while(*s)
91 ULONG i;
92 char *e;
94 if((e = strpbrk(s, " ,;")) == NULL)
95 e = s+strlen(s);
97 // check if the token is class definition or
98 // just a flag definition
99 if(s[0] == '@')
101 // check if this call is a negation or not
102 if(s[1] == '!')
104 // search for the token and clear the flag
105 for(i=0; dbclasses[i].token; i++)
107 if(strnicmp(&s[2], dbclasses[i].token, strlen(dbclasses[i].token)) == 0)
109 kprintf("clear '%s' debug class flag.\n", dbclasses[i].token);
110 clearFlag(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(strnicmp(&s[1], dbclasses[i].token, strlen(dbclasses[i].token)) == 0)
121 kprintf("set '%s' debug class flag\n", dbclasses[i].token);
122 setFlag(debug_classes, dbclasses[i].flag);
127 else
129 // check if this call is a negation or not
130 if(s[0] == '!')
132 for(i=0; dbflags[i].token; i++)
134 if(strnicmp(&s[1], dbflags[i].token, strlen(dbflags[i].token)) == 0)
136 kprintf("clear '%s' debug flag\n", dbflags[i].token);
137 clearFlag(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(strnicmp(s, "ansi", 4) == 0)
147 kprintf("ansi output enabled\n");
148 ansi_output = TRUE;
150 else
152 for(i=0; dbflags[i].token; i++)
154 if(strnicmp(s, dbflags[i].token, strlen(dbflags[i].token)) == 0)
156 kprintf("set '%s' debug flag\n", dbflags[i].token);
157 setFlag(debug_flags, dbflags[i].flag);
164 // set the next start to our last search
165 if(*e)
166 s = ++e;
167 else
168 break;
171 else
173 kprintf("GetVar(\"betterstring.mcc.debug\") failed, error %ld\n", IoErr());
176 kprintf("set debug classes/flags (env:betterstring.mcc.debug): %08lx/%08lx\n", debug_classes, debug_flags);
177 kprintf("** Normal processing follows ***************************************\n");
180 /****************************************************************************/
182 void CleanupDebug(void)
184 kprintf("** Cleaned up debugging ********************************************\n");
187 /****************************************************************************/
189 // define variables for using ANSI colors in our debugging scheme
190 #define ANSI_ESC_CLR "\033[0m"
191 #define ANSI_ESC_BOLD "\033[1m"
192 #define ANSI_ESC_UNDERLINE "\033[4m"
193 #define ANSI_ESC_BLINK "\033[5m"
194 #define ANSI_ESC_REVERSE "\033[7m"
195 #define ANSI_ESC_INVISIBLE "\033[8m"
196 #define ANSI_ESC_FG_BLACK "\033[0;30m"
197 #define ANSI_ESC_FG_RED "\033[0;31m"
198 #define ANSI_ESC_FG_GREEN "\033[0;32m"
199 #define ANSI_ESC_FG_BROWN "\033[0;33m"
200 #define ANSI_ESC_FG_BLUE "\033[0;34m"
201 #define ANSI_ESC_FG_PURPLE "\033[0;35m"
202 #define ANSI_ESC_FG_CYAN "\033[0;36m"
203 #define ANSI_ESC_FG_LGRAY "\033[0;37m"
204 #define ANSI_ESC_FG_DGRAY "\033[1;30m"
205 #define ANSI_ESC_FG_LRED "\033[1;31m"
206 #define ANSI_ESC_FG_LGREEN "\033[1;32m"
207 #define ANSI_ESC_FG_YELLOW "\033[1;33m"
208 #define ANSI_ESC_FG_LBLUE "\033[1;34m"
209 #define ANSI_ESC_FG_LPURPLE "\033[1;35m"
210 #define ANSI_ESC_FG_LCYAN "\033[1;36m"
211 #define ANSI_ESC_FG_WHITE "\033[1;37m"
212 #define ANSI_ESC_BG "\033[0;4" // background esc-squ start with 4x
213 #define ANSI_ESC_BG_BLACK "\033[0;40m"
214 #define ANSI_ESC_BG_RED "\033[0;41m"
215 #define ANSI_ESC_BG_GREEN "\033[0;42m"
216 #define ANSI_ESC_BG_BROWN "\033[0;43m"
217 #define ANSI_ESC_BG_BLUE "\033[0;44m"
218 #define ANSI_ESC_BG_PURPLE "\033[0;45m"
219 #define ANSI_ESC_BG_CYAN "\033[0;46m"
220 #define ANSI_ESC_BG_LGRAY "\033[0;47m"
222 /****************************************************************************/
224 INLINE void _INDENT(void)
226 int i;
227 for(i=0; i < indent_level; i++)
228 kprintf(" ");
231 /****************************************************************************/
233 void _ENTER(unsigned long dclass, const char *file, int line, const char *function)
235 if(isFlagSet(debug_classes, dclass))
237 _INDENT();
238 if(ansi_output)
239 kprintf("%s%s:%ld:Entering %s%s\n", ANSI_ESC_FG_BROWN, file, line, function, ANSI_ESC_CLR);
240 else
241 kprintf("%s:%ld:Entering %s\n", file, line, function);
244 indent_level++;
247 void _LEAVE(unsigned long dclass, const char *file, int line, const char *function)
249 indent_level--;
251 if(isFlagSet(debug_classes, dclass))
253 _INDENT();
254 if(ansi_output)
255 kprintf("%s%s:%ld:Leaving %s%s\n", ANSI_ESC_FG_BROWN, file, line, function, ANSI_ESC_CLR);
256 else
257 kprintf("%s:%ld:Leaving %s\n", file, line, function);
261 void _RETURN(unsigned long dclass, const char *file, int line, const char *function, unsigned long result)
263 indent_level--;
265 if(isFlagSet(debug_classes, dclass))
267 _INDENT();
268 if(ansi_output)
269 kprintf("%s%s:%ld:Leaving %s (result 0x%08lx, %ld)%s\n", ANSI_ESC_FG_BROWN, file, line, function, result, result, ANSI_ESC_CLR);
270 else
271 kprintf("%s:%ld:Leaving %s (result 0x%08lx, %ld)\n", file, line, function, result, result);
275 /****************************************************************************/
277 void _SHOWVALUE(unsigned long dclass, unsigned long dflags, unsigned long value, int size, const char *name, const char *file, int line)
279 if(isFlagSet(debug_classes, dclass) &&
280 isFlagSet(debug_flags, dflags))
282 const char *fmt;
284 switch(size)
286 case 1:
287 fmt = "%s:%ld:%s = %ld, 0x%02lx";
288 break;
290 case 2:
291 fmt = "%s:%ld:%s = %ld, 0x%04lx";
292 break;
294 default:
295 fmt = "%s:%ld:%s = %ld, 0x%08lx";
296 break;
299 _INDENT();
301 if(ansi_output)
302 kprintf(ANSI_ESC_FG_GREEN);
304 kprintf(fmt, file, line, name, value, value);
306 if(size == 1 && value < 256)
308 if(value < ' ' || (value >= 127 && value < 160))
309 kprintf(", '\\x%02lx'", value);
310 else
311 kprintf(", '%lc'", value);
314 if(ansi_output)
315 kprintf("%s\n", ANSI_ESC_CLR);
316 else
317 kprintf("\n");
321 /****************************************************************************/
323 void _SHOWPOINTER(unsigned long dclass, unsigned long dflags, const void *p, const char *name, const char *file, int line)
325 if(isFlagSet(debug_classes, dclass) &&
326 isFlagSet(debug_flags, dflags))
328 const char *fmt;
330 _INDENT();
332 if(p != NULL)
333 fmt = "%s:%ld:%s = 0x%08lx\n";
334 else
335 fmt = "%s:%ld:%s = NULL\n";
337 if(ansi_output)
339 kprintf(ANSI_ESC_FG_GREEN);
340 kprintf(fmt, file, line, name, p);
341 kprintf(ANSI_ESC_CLR);
343 else
344 kprintf(fmt, file, line, name, p);
348 /****************************************************************************/
350 void _SHOWSTRING(unsigned long dclass, unsigned long dflags, const char *string, const char *name, const char *file, int line)
352 if(isFlagSet(debug_classes, dclass) &&
353 isFlagSet(debug_flags, dflags))
355 _INDENT();
357 if(ansi_output)
358 kprintf("%s%s:%ld:%s = 0x%08lx \"%s\"%s\n", ANSI_ESC_FG_GREEN, file, line, name, string, string, ANSI_ESC_CLR);
359 else
360 kprintf("%s:%ld:%s = 0x%08lx \"%s\"\n", file, line, name, string, string);
364 /****************************************************************************/
366 void _SHOWMSG(unsigned long dclass, unsigned long dflags, const char *msg, const char *file, int line)
368 if(isFlagSet(debug_classes, dclass) &&
369 isFlagSet(debug_flags, dflags))
371 _INDENT();
373 if(ansi_output)
374 kprintf("%s%s:%ld:%s%s\n", ANSI_ESC_FG_GREEN, file, line, msg, ANSI_ESC_CLR);
375 else
376 kprintf("%s:%ld:%s\n", file, line, msg);
380 /****************************************************************************/
382 void _DPRINTF(unsigned long dclass, unsigned long dflags, const char *file, int line, const char *format, ...)
384 if((isFlagSet(debug_classes, dclass) && isFlagSet(debug_flags, dflags)) ||
385 (isFlagSet(dclass, DBC_ERROR) || isFlagSet(dclass, DBC_WARNING)))
387 va_list args;
388 static char buf[1024];
390 _INDENT();
392 va_start(args, format);
393 vsnprintf(buf, 1024, format, args);
394 va_end(args);
396 if(ansi_output)
398 const char *highlight = ANSI_ESC_FG_GREEN;
400 switch(dclass)
402 case DBC_CTRACE: highlight = ANSI_ESC_FG_BROWN; break;
403 case DBC_REPORT: highlight = ANSI_ESC_FG_GREEN; break;
404 case DBC_ASSERT: highlight = ANSI_ESC_FG_RED; break;
405 case DBC_TIMEVAL: highlight = ANSI_ESC_FG_GREEN; break;
406 case DBC_DEBUG: highlight = ANSI_ESC_FG_GREEN; break;
407 case DBC_ERROR: highlight = ANSI_ESC_FG_RED; break;
408 case DBC_WARNING: highlight = ANSI_ESC_FG_PURPLE;break;
411 kprintf("%s%s:%ld:%s%s\n", highlight, file, line, buf, ANSI_ESC_CLR);
413 else
414 kprintf("%s:%ld:%s\n", file, line, buf);
418 /****************************************************************************/
420 #endif