Merged difference between 7.12 and 7.15 into trunk.
[AROS.git] / external / openurl / cmd / debug.c
blob05229527fe61a8db058056e1cc82e9346a3c28e0
1 /***************************************************************************
3 openurl.library - universal URL display and browser launcher library
4 Copyright (C) 1998-2005 by Troels Walsted Hansen, et al.
5 Copyright (C) 2005-2013 by openurl.library Open Source Team
7 This library is free software; it has been placed in the public domain
8 and you can freely redistribute it and/or modify it. Please note, however,
9 that some components may be under the LGPL or GPL license.
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.
15 openurl.library project: http://sourceforge.net/projects/openurllib/
17 $Id$
19 ***************************************************************************/
21 #ifdef DEBUG
23 #include <stdlib.h>
24 #include <string.h>
25 #include <stdarg.h>
26 #include <stdio.h>
28 #include <proto/utility.h>
29 #include <proto/dos.h>
30 #include <proto/exec.h>
32 #include "version.h"
34 #include "debug.h"
35 #include "macros.h"
37 // our static variables with default values
38 static int indent_level = 0;
39 static BOOL ansi_output = FALSE;
40 static ULONG debug_flags = DBF_ALWAYS | DBF_STARTUP; // default debug flags
41 static ULONG debug_classes = DBC_ERROR | DBC_DEBUG | DBC_WARNING | DBC_ASSERT | DBC_REPORT; // default debug classes
43 /****************************************************************************/
45 void SetupDebug(void)
47 char var[256];
49 kprintf("** OpenURL (%s) startup ****************************\n", LIB_DATE);
50 kprintf("Initializing runtime debugging:\n");
52 if(GetVar("openurl.debug", var, sizeof(var), 0) > 0)
54 char *s = var;
56 // static list of our debugging classes tokens.
57 // in the yamdebug variable these classes always start with a @
58 static struct { const char *token; unsigned long flag; } dbclasses[] =
60 { "ctrace", DBC_CTRACE },
61 { "report", DBC_REPORT },
62 { "assert", DBC_ASSERT },
63 { "timeval", DBC_TIMEVAL },
64 { "debug", DBC_DEBUG },
65 { "error", DBC_ERROR },
66 { "warning", DBC_WARNING },
67 { "all", DBC_ALL },
68 { NULL, 0 }
71 static struct { const char *token; unsigned long flag; } dbflags[] =
73 { "always", DBF_ALWAYS },
74 { "startup", DBF_STARTUP },
75 { "template", DBF_TEMPLATE },
76 { "all", DBF_ALL },
77 { NULL, 0 }
80 // we parse the env variable token-wise
81 while(*s)
83 ULONG i;
84 char *e;
86 if((e = strpbrk(s, " ,;")) == NULL)
87 e = s+strlen(s);
89 // check if the token is class definition or
90 // just a flag definition
91 if(s[0] == '@')
93 // skip the '@'
94 s++;
95 // check if this call is a negation or not
96 if(s[0] == '!')
98 // skip the '!'
99 s++;
100 // search for the token and clear the flag
101 for(i=0; dbclasses[i].token; i++)
103 if(strnicmp(s, dbclasses[i].token, strlen(dbclasses[i].token)) == 0)
105 kprintf("clear '%s' debug class flag.\n", dbclasses[i].token);
106 CLEAR_FLAG(debug_classes, dbclasses[i].flag);
110 else
112 // search for the token and set the flag
113 for(i=0; dbclasses[i].token; i++)
115 if(strnicmp(s, dbclasses[i].token, strlen(dbclasses[i].token)) == 0)
117 kprintf("set '%s' debug class flag\n", dbclasses[i].token);
118 SET_FLAG(debug_classes, dbclasses[i].flag);
123 else
125 // check if this call is a negation or not
126 if(s[0] == '!')
128 // skip the '!'
129 s++;
130 for(i=0; dbflags[i].token; i++)
132 if(strnicmp(s, dbflags[i].token, strlen(dbflags[i].token)) == 0)
134 kprintf("clear '%s' debug flag\n", dbflags[i].token);
135 CLEAR_FLAG(debug_flags, dbflags[i].flag);
139 else
141 // check if the token was "ansi" and if so enable the ANSI color
142 // output
143 if(strnicmp(s, "ansi", 4) == 0)
145 kprintf("ansi output enabled\n");
146 ansi_output = TRUE;
148 else
150 for(i=0; dbflags[i].token; i++)
152 if(strnicmp(s, dbflags[i].token, strlen(dbflags[i].token)) == 0)
154 kprintf("set '%s' debug flag\n", dbflags[i].token);
155 SET_FLAG(debug_flags, dbflags[i].flag);
162 // set the next start to our last search
163 if(*e)
164 s = ++e;
165 else
166 break;
170 kprintf("set debug classes/flags (env:openurl.prefs.debug): %08lx/%08lx\n", debug_classes, debug_flags);
171 kprintf("** Normal processing follows ***************************************\n");
174 /****************************************************************************/
176 // define variables for using ANSI colors in our debugging scheme
177 #define ANSI_ESC_CLR "\033[0m"
178 #define ANSI_ESC_BOLD "\033[1m"
179 #define ANSI_ESC_UNDERLINE "\033[4m"
180 #define ANSI_ESC_BLINK "\033[5m"
181 #define ANSI_ESC_REVERSE "\033[7m"
182 #define ANSI_ESC_INVISIBLE "\033[8m"
183 #define ANSI_ESC_FG_BLACK "\033[0;30m"
184 #define ANSI_ESC_FG_RED "\033[0;31m"
185 #define ANSI_ESC_FG_GREEN "\033[0;32m"
186 #define ANSI_ESC_FG_BROWN "\033[0;33m"
187 #define ANSI_ESC_FG_BLUE "\033[0;34m"
188 #define ANSI_ESC_FG_PURPLE "\033[0;35m"
189 #define ANSI_ESC_FG_CYAN "\033[0;36m"
190 #define ANSI_ESC_FG_LGRAY "\033[0;37m"
191 #define ANSI_ESC_FG_DGRAY "\033[1;30m"
192 #define ANSI_ESC_FG_LRED "\033[1;31m"
193 #define ANSI_ESC_FG_LGREEN "\033[1;32m"
194 #define ANSI_ESC_FG_YELLOW "\033[1;33m"
195 #define ANSI_ESC_FG_LBLUE "\033[1;34m"
196 #define ANSI_ESC_FG_LPURPLE "\033[1;35m"
197 #define ANSI_ESC_FG_LCYAN "\033[1;36m"
198 #define ANSI_ESC_FG_WHITE "\033[1;37m"
199 #define ANSI_ESC_BG "\033[0;4" // background esc-squ start with 4x
200 #define ANSI_ESC_BG_BLACK "\033[0;40m"
201 #define ANSI_ESC_BG_RED "\033[0;41m"
202 #define ANSI_ESC_BG_GREEN "\033[0;42m"
203 #define ANSI_ESC_BG_BROWN "\033[0;43m"
204 #define ANSI_ESC_BG_BLUE "\033[0;44m"
205 #define ANSI_ESC_BG_PURPLE "\033[0;45m"
206 #define ANSI_ESC_BG_CYAN "\033[0;46m"
207 #define ANSI_ESC_BG_LGRAY "\033[0;47m"
209 /****************************************************************************/
211 INLINE void _INDENT(void)
213 int i;
214 for(i=0; i < indent_level; i++)
215 kprintf(" ");
218 /****************************************************************************/
220 void _ENTER(unsigned long dclass, const char *file, int line, const char *function)
222 if(isFlagSet(debug_classes, dclass))
224 _INDENT();
225 if(ansi_output)
226 kprintf("%s%s:%ld:Entering %s%s\n", ANSI_ESC_FG_BROWN, file, line, function, ANSI_ESC_CLR);
227 else
228 kprintf("%s:%ld:Entering %s\n", file, line, function);
231 indent_level++;
234 void _LEAVE(unsigned long dclass, const char *file, int line, const char *function)
236 indent_level--;
238 if(isFlagSet(debug_classes, dclass))
240 _INDENT();
241 if(ansi_output)
242 kprintf("%s%s:%ld:Leaving %s%s\n", ANSI_ESC_FG_BROWN, file, line, function, ANSI_ESC_CLR);
243 else
244 kprintf("%s:%ld:Leaving %s\n", file, line, function);
248 void _RETURN(unsigned long dclass, const char *file, int line, const char *function, unsigned long result)
250 indent_level--;
252 if(isFlagSet(debug_classes, dclass))
254 _INDENT();
255 if(ansi_output)
256 kprintf("%s%s:%ld:Leaving %s (result 0x%08lx, %ld)%s\n", ANSI_ESC_FG_BROWN, file, line, function, result, result, ANSI_ESC_CLR);
257 else
258 kprintf("%s:%ld:Leaving %s (result 0x%08lx, %ld)\n", file, line, function, result, result);
262 /****************************************************************************/
264 void _SHOWVALUE(unsigned long dclass, unsigned long dflags, unsigned long value, int size, const char *name, const char *file, int line)
266 if(isFlagSet(debug_classes, dclass) &&
267 isFlagSet(debug_flags, dflags))
269 const char *fmt;
271 switch(size)
273 case 1:
274 fmt = "%s:%ld:%s = %ld, 0x%02lx";
275 break;
277 case 2:
278 fmt = "%s:%ld:%s = %ld, 0x%04lx";
279 break;
281 default:
282 fmt = "%s:%ld:%s = %ld, 0x%08lx";
283 break;
286 _INDENT();
288 if(ansi_output)
289 kprintf(ANSI_ESC_FG_GREEN);
291 kprintf(fmt, file, line, name, value, value);
293 if(size == 1 && value < 256)
295 if(value < ' ' || (value >= 127 && value < 160))
296 kprintf(", '\\x%02lx'", value);
297 else
298 kprintf(", '%lc'", value);
301 if(ansi_output)
302 kprintf("%s\n", ANSI_ESC_CLR);
303 else
304 kprintf("\n");
308 /****************************************************************************/
310 void _SHOWPOINTER(unsigned long dclass, unsigned long dflags, const void *p, const char *name, const char *file, int line)
312 if(isFlagSet(debug_classes, dclass) &&
313 isFlagSet(debug_flags, dflags))
315 const char *fmt;
317 _INDENT();
319 if(p != NULL)
320 fmt = "%s:%ld:%s = 0x%08lx\n";
321 else
322 fmt = "%s:%ld:%s = NULL\n";
324 if(ansi_output)
326 kprintf(ANSI_ESC_FG_GREEN);
327 kprintf(fmt, file, line, name, p);
328 kprintf(ANSI_ESC_CLR);
330 else
331 kprintf(fmt, file, line, name, p);
335 /****************************************************************************/
337 void _SHOWSTRING(unsigned long dclass, unsigned long dflags, const char *string, const char *name, const char *file, int line)
339 if(isFlagSet(debug_classes, dclass) &&
340 isFlagSet(debug_flags, dflags))
342 _INDENT();
344 if(ansi_output)
345 kprintf("%s%s:%ld:%s = 0x%08lx \"%s\"%s\n", ANSI_ESC_FG_GREEN, file, line, name, string, string, ANSI_ESC_CLR);
346 else
347 kprintf("%s:%ld:%s = 0x%08lx \"%s\"\n", file, line, name, string, string);
351 /****************************************************************************/
353 void _SHOWMSG(unsigned long dclass, unsigned long dflags, const char *msg, const char *file, int line)
355 if(isFlagSet(debug_classes, dclass) &&
356 isFlagSet(debug_flags, dflags))
358 _INDENT();
360 if(ansi_output)
361 kprintf("%s%s:%ld:%s%s\n", ANSI_ESC_FG_GREEN, file, line, msg, ANSI_ESC_CLR);
362 else
363 kprintf("%s:%ld:%s\n", file, line, msg);
367 /****************************************************************************/
369 void _DPRINTF(unsigned long dclass, unsigned long dflags, const char *file, int line, const char *format, ...)
371 if((isFlagSet(debug_classes, dclass) && isFlagSet(debug_flags, dflags)) ||
372 (isFlagSet(dclass, DBC_ERROR) || isFlagSet(dclass, DBC_WARNING)))
374 va_list args;
375 static char buf[1024];
377 _INDENT();
379 va_start(args, format);
380 vsnprintf(buf, 1024, format, args);
381 va_end(args);
383 if(ansi_output)
385 const char *highlight = ANSI_ESC_FG_GREEN;
387 switch(dclass)
389 case DBC_CTRACE: highlight = ANSI_ESC_FG_BROWN; break;
390 case DBC_REPORT: highlight = ANSI_ESC_FG_GREEN; break;
391 case DBC_ASSERT: highlight = ANSI_ESC_FG_RED; break;
392 case DBC_TIMEVAL: highlight = ANSI_ESC_FG_GREEN; break;
393 case DBC_DEBUG: highlight = ANSI_ESC_FG_GREEN; break;
394 case DBC_ERROR: highlight = ANSI_ESC_FG_RED; break;
395 case DBC_WARNING: highlight = ANSI_ESC_FG_PURPLE;break;
398 kprintf("%s%s:%ld:%s%s\n", highlight, file, line, buf, ANSI_ESC_CLR);
400 else
401 kprintf("%s:%ld:%s\n", file, line, buf);
405 /****************************************************************************/
407 #endif