Resurrected native codesetslib from r27301
[cake.git] / workbench / libs / codesetslib / src / debug.c
blob16fa04f02e5786c72b9212878a302db59c53897a
1 /***************************************************************************
3 codesets.library - Amiga shared library for handling different codesets
4 Copyright (C) 2001-2005 by Alfonso [alfie] Ranieri <alforan@tin.it>.
5 Copyright (C) 2005-2007 by codesets.library 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 codesets.library project: http://sourceforge.net/projects/codesetslib/
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/utility.h>
31 #include <proto/dos.h>
32 #include <proto/exec.h>
34 #include "version.h"
36 #include "debug.h"
37 #include "macros.h"
39 // our static variables with default values
40 static int indent_level = 0;
41 static BOOL ansi_output = FALSE;
42 static ULONG debug_flags = DBF_ALWAYS | DBF_STARTUP; // default debug flags
43 static ULONG debug_classes = DBC_ERROR | DBC_DEBUG | DBC_WARNING | DBC_ASSERT | DBC_REPORT; // default debug classes
45 /****************************************************************************/
47 void SetupDebug(void)
49 char var[256];
51 kprintf("** codesets.library %s (%s) startup ****************************\n", LIB_REV_STRING, LIB_DATE);
52 kprintf("Initializing runtime debugging:\n");
54 if(GetVar("codesets.library.debug", var, sizeof(var), 0) > 0)
56 char *s = var;
58 // static list of our debugging classes tokens.
59 // in the yamdebug variable these classes always start with a @
60 static struct { const char *token; unsigned long flag; } dbclasses[] =
62 { "ctrace", DBC_CTRACE },
63 { "report", DBC_REPORT },
64 { "assert", DBC_ASSERT },
65 { "timeval", DBC_TIMEVAL },
66 { "debug", DBC_DEBUG },
67 { "error", DBC_ERROR },
68 { "warning", DBC_WARNING },
69 { "all", DBC_ALL },
70 { NULL, 0 }
73 static struct { const char *token; unsigned long flag; } dbflags[] =
75 { "always", DBF_ALWAYS },
76 { "startup", DBF_STARTUP },
77 { "utf", DBF_UTF },
78 { "all", DBF_ALL },
79 { NULL, 0 }
82 // we parse the env variable token-wise
83 while(*s)
85 ULONG i;
86 char *e;
88 if((e = strpbrk(s, " ,;")) == NULL)
89 e = s+strlen(s);
91 // check if the token is class definition or
92 // just a flag definition
93 if(s[0] == '@')
95 // skip the '@'
96 s++;
97 // check if this call is a negation or not
98 if(s[0] == '!')
100 // skip the '!'
101 s++;
102 // search for the token and clear the flag
103 for(i=0; dbclasses[i].token; i++)
105 if(strnicmp(s, dbclasses[i].token, strlen(dbclasses[i].token)) == 0)
107 kprintf("clear '%s' debug class flag.\n", dbclasses[i].token);
108 CLEAR_FLAG(debug_classes, dbclasses[i].flag);
112 else
114 // search for the token and set the flag
115 for(i=0; dbclasses[i].token; i++)
117 if(strnicmp(s, dbclasses[i].token, strlen(dbclasses[i].token)) == 0)
119 kprintf("set '%s' debug class flag\n", dbclasses[i].token);
120 SET_FLAG(debug_classes, dbclasses[i].flag);
125 else
127 // check if this call is a negation or not
128 if(s[0] == '!')
130 // skip the '!'
131 s++;
132 for(i=0; dbflags[i].token; i++)
134 if(strnicmp(s, dbflags[i].token, strlen(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(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 SET_FLAG(debug_flags, dbflags[i].flag);
164 // set the next start to our last search
165 if(*e)
166 s = ++e;
167 else
168 break;
172 kprintf("set debug classes/flags (env:codesets.library.debug): %08lx/%08lx\n", debug_classes, debug_flags);
173 kprintf("** Normal processing follows ***************************************\n");
176 /****************************************************************************/
178 // define variables for using ANSI colors in our debugging scheme
179 #define ANSI_ESC_CLR "\033[0m"
180 #define ANSI_ESC_BOLD "\033[1m"
181 #define ANSI_ESC_UNDERLINE "\033[4m"
182 #define ANSI_ESC_BLINK "\033[5m"
183 #define ANSI_ESC_REVERSE "\033[7m"
184 #define ANSI_ESC_INVISIBLE "\033[8m"
185 #define ANSI_ESC_FG_BLACK "\033[0;30m"
186 #define ANSI_ESC_FG_RED "\033[0;31m"
187 #define ANSI_ESC_FG_GREEN "\033[0;32m"
188 #define ANSI_ESC_FG_BROWN "\033[0;33m"
189 #define ANSI_ESC_FG_BLUE "\033[0;34m"
190 #define ANSI_ESC_FG_PURPLE "\033[0;35m"
191 #define ANSI_ESC_FG_CYAN "\033[0;36m"
192 #define ANSI_ESC_FG_LGRAY "\033[0;37m"
193 #define ANSI_ESC_FG_DGRAY "\033[1;30m"
194 #define ANSI_ESC_FG_LRED "\033[1;31m"
195 #define ANSI_ESC_FG_LGREEN "\033[1;32m"
196 #define ANSI_ESC_FG_YELLOW "\033[1;33m"
197 #define ANSI_ESC_FG_LBLUE "\033[1;34m"
198 #define ANSI_ESC_FG_LPURPLE "\033[1;35m"
199 #define ANSI_ESC_FG_LCYAN "\033[1;36m"
200 #define ANSI_ESC_FG_WHITE "\033[1;37m"
201 #define ANSI_ESC_BG "\033[0;4" // background esc-squ start with 4x
202 #define ANSI_ESC_BG_BLACK "\033[0;40m"
203 #define ANSI_ESC_BG_RED "\033[0;41m"
204 #define ANSI_ESC_BG_GREEN "\033[0;42m"
205 #define ANSI_ESC_BG_BROWN "\033[0;43m"
206 #define ANSI_ESC_BG_BLUE "\033[0;44m"
207 #define ANSI_ESC_BG_PURPLE "\033[0;45m"
208 #define ANSI_ESC_BG_CYAN "\033[0;46m"
209 #define ANSI_ESC_BG_LGRAY "\033[0;47m"
211 /****************************************************************************/
213 INLINE void _INDENT(void)
215 int i;
216 for(i=0; i < indent_level; i++)
217 kprintf(" ");
220 /****************************************************************************/
222 void _ENTER(unsigned long dclass, const char *file, int line, const char *function)
224 if(isFlagSet(debug_classes, dclass))
226 _INDENT();
227 if(ansi_output)
228 kprintf("%s%s:%ld:Entering %s%s\n", ANSI_ESC_FG_BROWN, file, line, function, ANSI_ESC_CLR);
229 else
230 kprintf("%s:%ld:Entering %s\n", file, line, function);
233 indent_level++;
236 void _LEAVE(unsigned long dclass, const char *file, int line, const char *function)
238 indent_level--;
240 if(isFlagSet(debug_classes, dclass))
242 _INDENT();
243 if(ansi_output)
244 kprintf("%s%s:%ld:Leaving %s%s\n", ANSI_ESC_FG_BROWN, file, line, function, ANSI_ESC_CLR);
245 else
246 kprintf("%s:%ld:Leaving %s\n", file, line, function);
250 void _RETURN(unsigned long dclass, const char *file, int line, const char *function, unsigned long result)
252 indent_level--;
254 if(isFlagSet(debug_classes, dclass))
256 _INDENT();
257 if(ansi_output)
258 kprintf("%s%s:%ld:Leaving %s (result 0x%08lx, %ld)%s\n", ANSI_ESC_FG_BROWN, file, line, function, result, result, ANSI_ESC_CLR);
259 else
260 kprintf("%s:%ld:Leaving %s (result 0x%08lx, %ld)\n", file, line, function, result, result);
264 /****************************************************************************/
266 void _SHOWVALUE(unsigned long dclass, unsigned long dflags, unsigned long value, int size, const char *name, const char *file, int line)
268 if(isFlagSet(debug_classes, dclass) &&
269 isFlagSet(debug_flags, dflags))
271 const char *fmt;
273 switch(size)
275 case 1:
276 fmt = "%s:%ld:%s = %ld, 0x%02lx";
277 break;
279 case 2:
280 fmt = "%s:%ld:%s = %ld, 0x%04lx";
281 break;
283 default:
284 fmt = "%s:%ld:%s = %ld, 0x%08lx";
285 break;
288 _INDENT();
290 if(ansi_output)
291 kprintf(ANSI_ESC_FG_GREEN);
293 kprintf(fmt, file, line, name, value, value);
295 if(size == 1 && value < 256)
297 if(value < ' ' || (value >= 127 && value < 160))
298 kprintf(", '\\x%02lx'", value);
299 else
300 kprintf(", '%lc'", value);
303 if(ansi_output)
304 kprintf("%s\n", ANSI_ESC_CLR);
305 else
306 kprintf("\n");
310 /****************************************************************************/
312 void _SHOWPOINTER(unsigned long dclass, unsigned long dflags, const void *p, const char *name, const char *file, int line)
314 if(isFlagSet(debug_classes, dclass) &&
315 isFlagSet(debug_flags, dflags))
317 const char *fmt;
319 _INDENT();
321 if(p != NULL)
322 fmt = "%s:%ld:%s = 0x%08lx\n";
323 else
324 fmt = "%s:%ld:%s = NULL\n";
326 if(ansi_output)
328 kprintf(ANSI_ESC_FG_GREEN);
329 kprintf(fmt, file, line, name, p);
330 kprintf(ANSI_ESC_CLR);
332 else
333 kprintf(fmt, file, line, name, p);
337 /****************************************************************************/
339 void _SHOWSTRING(unsigned long dclass, unsigned long dflags, const char *string, const char *name, const char *file, int line)
341 if(isFlagSet(debug_classes, dclass) &&
342 isFlagSet(debug_flags, dflags))
344 _INDENT();
346 if(ansi_output)
347 kprintf("%s%s:%ld:%s = 0x%08lx \"%s\"%s\n", ANSI_ESC_FG_GREEN, file, line, name, string, string, ANSI_ESC_CLR);
348 else
349 kprintf("%s:%ld:%s = 0x%08lx \"%s\"\n", file, line, name, string, string);
353 /****************************************************************************/
355 void _SHOWMSG(unsigned long dclass, unsigned long dflags, const char *msg, const char *file, int line)
357 if(isFlagSet(debug_classes, dclass) &&
358 isFlagSet(debug_flags, dflags))
360 _INDENT();
362 if(ansi_output)
363 kprintf("%s%s:%ld:%s%s\n", ANSI_ESC_FG_GREEN, file, line, msg, ANSI_ESC_CLR);
364 else
365 kprintf("%s:%ld:%s\n", file, line, msg);
369 /****************************************************************************/
371 void _DPRINTF(unsigned long dclass, unsigned long dflags, const char *file, int line, const char *format, ...)
373 if((isFlagSet(debug_classes, dclass) && isFlagSet(debug_flags, dflags)) ||
374 (isFlagSet(dclass, DBC_ERROR) || isFlagSet(dclass, DBC_WARNING)))
376 va_list args;
377 static char buf[1024];
379 _INDENT();
381 va_start(args, format);
382 vsnprintf(buf, 1024, format, args);
383 va_end(args);
385 if(ansi_output)
387 const char *highlight = ANSI_ESC_FG_GREEN;
389 switch(dclass)
391 case DBC_CTRACE: highlight = ANSI_ESC_FG_BROWN; break;
392 case DBC_REPORT: highlight = ANSI_ESC_FG_GREEN; break;
393 case DBC_ASSERT: highlight = ANSI_ESC_FG_RED; break;
394 case DBC_TIMEVAL: highlight = ANSI_ESC_FG_GREEN; break;
395 case DBC_DEBUG: highlight = ANSI_ESC_FG_GREEN; break;
396 case DBC_ERROR: highlight = ANSI_ESC_FG_RED; break;
397 case DBC_WARNING: highlight = ANSI_ESC_FG_PURPLE;break;
400 kprintf("%s%s:%ld:%s%s\n", highlight, file, line, buf, ANSI_ESC_CLR);
402 else
403 kprintf("%s:%ld:%s\n", file, line, buf);
407 /****************************************************************************/
409 #endif