revert between 56095 -> 55830 in arch
[AROS.git] / workbench / libs / codesets / src / debug.c
blob95edc35dc3d844d7f84f7642f30b1b82197a5ef5
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-2014 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>
33 #include <exec/semaphores.h>
35 #include "version.h"
37 #include "debug.h"
38 #include "macros.h"
40 #if defined(__MORPHOS__) || defined(__AROS__)
41 #include <exec/rawfmt.h>
42 #else
43 #include <clib/debug_protos.h>
44 #endif
46 // our static variables with default values
47 static int indent_level = 0;
48 static BOOL ansi_output = FALSE;
49 static ULONG debug_flags = DBF_ALWAYS | DBF_STARTUP; // default debug flags
50 static ULONG debug_classes = DBC_ERROR | DBC_DEBUG | DBC_WARNING | DBC_ASSERT | DBC_REPORT; // default debug classes
51 static struct SignalSemaphore debug_sema;
53 /****************************************************************************/
55 void _DBPRINTF(const char *format, ...)
57 va_list args;
58 #if defined(__amigaos4__)
59 static char buf[1024];
60 #endif
62 va_start(args, format);
64 ObtainSemaphore(&debug_sema);
66 #if defined(__MORPHOS__) || defined(__AROS__)
67 VNewRawDoFmt(format, (APTR)RAWFMTFUNC_SERIAL, NULL, args);
68 #elif defined(__amigaos4__)
69 vsnprintf(buf, sizeof(buf), format, args);
70 DebugPrintF("%s", buf);
71 #else
72 KPutFmt(format, args);
73 #endif
75 ReleaseSemaphore(&debug_sema);
77 va_end(args);
80 /****************************************************************************/
82 void InitDebug(void)
84 memset(&debug_sema, 0, sizeof(debug_sema));
85 InitSemaphore(&debug_sema);
88 /****************************************************************************/
90 void SetupDebug(void)
92 char var[256];
94 _DBPRINTF("** codesets.library %s (%s) startup ****************************\n", LIB_REV_STRING, LIB_DATE);
95 _DBPRINTF("Initializing runtime debugging:\n");
97 if(GetVar("codesets.library.debug", var, sizeof(var), 0) > 0)
99 char *s = var;
101 // static list of our debugging classes tokens.
102 // in the yamdebug variable these classes always start with a @
103 static struct { const char *token; unsigned long flag; } dbclasses[] =
105 { "ctrace", DBC_CTRACE },
106 { "report", DBC_REPORT },
107 { "assert", DBC_ASSERT },
108 { "timeval", DBC_TIMEVAL },
109 { "debug", DBC_DEBUG },
110 { "error", DBC_ERROR },
111 { "warning", DBC_WARNING },
112 { "all", DBC_ALL },
113 { NULL, 0 }
116 static struct { const char *token; unsigned long flag; } dbflags[] =
118 { "always", DBF_ALWAYS },
119 { "startup", DBF_STARTUP },
120 { "utf", DBF_UTF },
121 { "all", DBF_ALL },
122 { NULL, 0 }
125 // we parse the env variable token-wise
126 while(*s)
128 ULONG i;
129 char *e;
131 if((e = strpbrk(s, " ,;")) == NULL)
132 e = s+strlen(s);
134 // check if the token is class definition or
135 // just a flag definition
136 if(s[0] == '@')
138 // skip the '@'
139 s++;
140 // check if this call is a negation or not
141 if(s[0] == '!')
143 // skip the '!'
144 s++;
145 // search for the token and clear the flag
146 for(i=0; dbclasses[i].token; i++)
148 if(strnicmp(s, dbclasses[i].token, strlen(dbclasses[i].token)) == 0)
150 _DBPRINTF("clear '%s' debug class flag.\n", dbclasses[i].token);
151 CLEAR_FLAG(debug_classes, dbclasses[i].flag);
155 else
157 // search for the token and set the flag
158 for(i=0; dbclasses[i].token; i++)
160 if(strnicmp(s, dbclasses[i].token, strlen(dbclasses[i].token)) == 0)
162 _DBPRINTF("set '%s' debug class flag\n", dbclasses[i].token);
163 SET_FLAG(debug_classes, dbclasses[i].flag);
168 else
170 // check if this call is a negation or not
171 if(s[0] == '!')
173 // skip the '!'
174 s++;
175 for(i=0; dbflags[i].token; i++)
177 if(strnicmp(s, dbflags[i].token, strlen(dbflags[i].token)) == 0)
179 _DBPRINTF("clear '%s' debug flag\n", dbflags[i].token);
180 CLEAR_FLAG(debug_flags, dbflags[i].flag);
184 else
186 // check if the token was "ansi" and if so enable the ANSI color
187 // output
188 if(strnicmp(s, "ansi", 4) == 0)
190 _DBPRINTF("ansi output enabled\n");
191 ansi_output = TRUE;
193 else
195 for(i=0; dbflags[i].token; i++)
197 if(strnicmp(s, dbflags[i].token, strlen(dbflags[i].token)) == 0)
199 _DBPRINTF("set '%s' debug flag\n", dbflags[i].token);
200 SET_FLAG(debug_flags, dbflags[i].flag);
207 // set the next start to our last search
208 if(*e)
209 s = ++e;
210 else
211 break;
215 _DBPRINTF("set debug classes/flags (env:codesets.library.debug): %08lx/%08lx\n", debug_classes, debug_flags);
216 _DBPRINTF("** Normal processing follows ***************************************\n");
219 /****************************************************************************/
221 // define variables for using ANSI colors in our debugging scheme
222 #define ANSI_ESC_CLR "\033[0m"
223 #define ANSI_ESC_BOLD "\033[1m"
224 #define ANSI_ESC_UNDERLINE "\033[4m"
225 #define ANSI_ESC_BLINK "\033[5m"
226 #define ANSI_ESC_REVERSE "\033[7m"
227 #define ANSI_ESC_INVISIBLE "\033[8m"
228 #define ANSI_ESC_FG_BLACK "\033[0;30m"
229 #define ANSI_ESC_FG_RED "\033[0;31m"
230 #define ANSI_ESC_FG_GREEN "\033[0;32m"
231 #define ANSI_ESC_FG_BROWN "\033[0;33m"
232 #define ANSI_ESC_FG_BLUE "\033[0;34m"
233 #define ANSI_ESC_FG_PURPLE "\033[0;35m"
234 #define ANSI_ESC_FG_CYAN "\033[0;36m"
235 #define ANSI_ESC_FG_LGRAY "\033[0;37m"
236 #define ANSI_ESC_FG_DGRAY "\033[1;30m"
237 #define ANSI_ESC_FG_LRED "\033[1;31m"
238 #define ANSI_ESC_FG_LGREEN "\033[1;32m"
239 #define ANSI_ESC_FG_YELLOW "\033[1;33m"
240 #define ANSI_ESC_FG_LBLUE "\033[1;34m"
241 #define ANSI_ESC_FG_LPURPLE "\033[1;35m"
242 #define ANSI_ESC_FG_LCYAN "\033[1;36m"
243 #define ANSI_ESC_FG_WHITE "\033[1;37m"
244 #define ANSI_ESC_BG "\033[0;4" // background esc-squ start with 4x
245 #define ANSI_ESC_BG_BLACK "\033[0;40m"
246 #define ANSI_ESC_BG_RED "\033[0;41m"
247 #define ANSI_ESC_BG_GREEN "\033[0;42m"
248 #define ANSI_ESC_BG_BROWN "\033[0;43m"
249 #define ANSI_ESC_BG_BLUE "\033[0;44m"
250 #define ANSI_ESC_BG_PURPLE "\033[0;45m"
251 #define ANSI_ESC_BG_CYAN "\033[0;46m"
252 #define ANSI_ESC_BG_LGRAY "\033[0;47m"
254 /****************************************************************************/
256 INLINE void _INDENT(void)
258 int i;
259 for(i=0; i < indent_level; i++)
260 _DBPRINTF(" ");
263 /****************************************************************************/
265 void _ENTER(unsigned long dclass, const char *file, int line, const char *function)
267 if(isFlagSet(debug_classes, dclass))
269 _INDENT();
270 if(ansi_output)
271 _DBPRINTF("%s%s:%ld:Entering %s%s\n", ANSI_ESC_FG_BROWN, file, line, function, ANSI_ESC_CLR);
272 else
273 _DBPRINTF("%s:%ld:Entering %s\n", file, line, function);
276 indent_level++;
279 void _LEAVE(unsigned long dclass, const char *file, int line, const char *function)
281 indent_level--;
283 if(isFlagSet(debug_classes, dclass))
285 _INDENT();
286 if(ansi_output)
287 _DBPRINTF("%s%s:%ld:Leaving %s%s\n", ANSI_ESC_FG_BROWN, file, line, function, ANSI_ESC_CLR);
288 else
289 _DBPRINTF("%s:%ld:Leaving %s\n", file, line, function);
293 void _RETURN(unsigned long dclass, const char *file, int line, const char *function, unsigned long result)
295 indent_level--;
297 if(isFlagSet(debug_classes, dclass))
299 _INDENT();
300 if(ansi_output)
301 _DBPRINTF("%s%s:%ld:Leaving %s (result 0x%08lx, %ld)%s\n", ANSI_ESC_FG_BROWN, file, line, function, result, result, ANSI_ESC_CLR);
302 else
303 _DBPRINTF("%s:%ld:Leaving %s (result 0x%08lx, %ld)\n", file, line, function, result, result);
307 /****************************************************************************/
309 void _SHOWVALUE(unsigned long dclass, unsigned long dflags, unsigned long value, int size, const char *name, const char *file, int line)
311 if(isFlagSet(debug_classes, dclass) &&
312 isFlagSet(debug_flags, dflags))
314 const char *fmt;
316 switch(size)
318 case 1:
319 fmt = "%s:%ld:%s = %ld, 0x%02lx";
320 break;
322 case 2:
323 fmt = "%s:%ld:%s = %ld, 0x%04lx";
324 break;
326 default:
327 fmt = "%s:%ld:%s = %ld, 0x%08lx";
328 break;
331 _INDENT();
333 if(ansi_output)
334 _DBPRINTF(ANSI_ESC_FG_GREEN);
336 _DBPRINTF(fmt, file, line, name, value, value);
338 if(size == 1 && value < 256)
340 if(value < ' ' || (value >= 127 && value < 160))
341 _DBPRINTF(", '\\x%02lx'", value);
342 else
343 _DBPRINTF(", '%lc'", value);
346 if(ansi_output)
347 _DBPRINTF("%s\n", ANSI_ESC_CLR);
348 else
349 _DBPRINTF("\n");
353 /****************************************************************************/
355 void _SHOWPOINTER(unsigned long dclass, unsigned long dflags, const void *p, const char *name, const char *file, int line)
357 if(isFlagSet(debug_classes, dclass) &&
358 isFlagSet(debug_flags, dflags))
360 const char *fmt;
362 _INDENT();
364 if(p != NULL)
365 fmt = "%s:%ld:%s = 0x%08lx\n";
366 else
367 fmt = "%s:%ld:%s = NULL\n";
369 if(ansi_output)
371 _DBPRINTF(ANSI_ESC_FG_GREEN);
372 _DBPRINTF(fmt, file, line, name, p);
373 _DBPRINTF(ANSI_ESC_CLR);
375 else
376 _DBPRINTF(fmt, file, line, name, p);
380 /****************************************************************************/
382 void _SHOWSTRING(unsigned long dclass, unsigned long dflags, const char *string, const char *name, const char *file, int line)
384 if(isFlagSet(debug_classes, dclass) &&
385 isFlagSet(debug_flags, dflags))
387 _INDENT();
389 if(ansi_output)
390 _DBPRINTF("%s%s:%ld:%s = 0x%08lx \"%s\"%s\n", ANSI_ESC_FG_GREEN, file, line, name, string, string, ANSI_ESC_CLR);
391 else
392 _DBPRINTF("%s:%ld:%s = 0x%08lx \"%s\"\n", file, line, name, string, string);
396 /****************************************************************************/
398 void _SHOWMSG(unsigned long dclass, unsigned long dflags, const char *msg, const char *file, int line)
400 if(isFlagSet(debug_classes, dclass) &&
401 isFlagSet(debug_flags, dflags))
403 _INDENT();
405 if(ansi_output)
406 _DBPRINTF("%s%s:%ld:%s%s\n", ANSI_ESC_FG_GREEN, file, line, msg, ANSI_ESC_CLR);
407 else
408 _DBPRINTF("%s:%ld:%s\n", file, line, msg);
412 /****************************************************************************/
414 void _DPRINTF(unsigned long dclass, unsigned long dflags, const char *file, int line, const char *format, ...)
416 if((isFlagSet(debug_classes, dclass) && isFlagSet(debug_flags, dflags)) ||
417 (isFlagSet(dclass, DBC_ERROR) || isFlagSet(dclass, DBC_WARNING)))
419 va_list args;
420 static char buf[1024];
422 _INDENT();
424 va_start(args, format);
425 vsnprintf(buf, 1024, format, args);
426 va_end(args);
428 if(ansi_output)
430 const char *highlight = ANSI_ESC_FG_GREEN;
432 switch(dclass)
434 case DBC_CTRACE: highlight = ANSI_ESC_FG_BROWN; break;
435 case DBC_REPORT: highlight = ANSI_ESC_FG_GREEN; break;
436 case DBC_ASSERT: highlight = ANSI_ESC_FG_RED; break;
437 case DBC_TIMEVAL: highlight = ANSI_ESC_FG_GREEN; break;
438 case DBC_DEBUG: highlight = ANSI_ESC_FG_GREEN; break;
439 case DBC_ERROR: highlight = ANSI_ESC_FG_RED; break;
440 case DBC_WARNING: highlight = ANSI_ESC_FG_PURPLE;break;
443 _DBPRINTF("%s%s:%ld:%s%s\n", highlight, file, line, buf, ANSI_ESC_CLR);
445 else
446 _DBPRINTF("%s:%ld:%s\n", file, line, buf);
450 /****************************************************************************/
452 #endif