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/
21 ***************************************************************************/
30 #include <proto/utility.h>
31 #include <proto/dos.h>
32 #include <proto/exec.h>
33 #include <exec/semaphores.h>
40 #if defined(__MORPHOS__) || defined(__AROS__)
41 #include <exec/rawfmt.h>
43 #include <clib/debug_protos.h>
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
, ...)
58 #if defined(__amigaos4__)
59 static char buf
[1024];
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
);
72 KPutFmt(format
, args
);
75 ReleaseSemaphore(&debug_sema
);
80 /****************************************************************************/
84 memset(&debug_sema
, 0, sizeof(debug_sema
));
85 InitSemaphore(&debug_sema
);
88 /****************************************************************************/
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)
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
},
116 static struct { const char *token
; unsigned long flag
; } dbflags
[] =
118 { "always", DBF_ALWAYS
},
119 { "startup", DBF_STARTUP
},
125 // we parse the env variable token-wise
131 if((e
= strpbrk(s
, " ,;")) == NULL
)
134 // check if the token is class definition or
135 // just a flag definition
140 // check if this call is a negation or not
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
);
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
);
170 // check if this call is a negation or not
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
);
186 // check if the token was "ansi" and if so enable the ANSI color
188 if(strnicmp(s
, "ansi", 4) == 0)
190 _DBPRINTF("ansi output enabled\n");
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
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)
259 for(i
=0; i
< indent_level
; i
++)
263 /****************************************************************************/
265 void _ENTER(unsigned long dclass
, const char *file
, int line
, const char *function
)
267 if(isFlagSet(debug_classes
, dclass
))
271 _DBPRINTF("%s%s:%ld:Entering %s%s\n", ANSI_ESC_FG_BROWN
, file
, line
, function
, ANSI_ESC_CLR
);
273 _DBPRINTF("%s:%ld:Entering %s\n", file
, line
, function
);
279 void _LEAVE(unsigned long dclass
, const char *file
, int line
, const char *function
)
283 if(isFlagSet(debug_classes
, dclass
))
287 _DBPRINTF("%s%s:%ld:Leaving %s%s\n", ANSI_ESC_FG_BROWN
, file
, line
, function
, ANSI_ESC_CLR
);
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
)
297 if(isFlagSet(debug_classes
, dclass
))
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
);
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
))
319 fmt
= "%s:%ld:%s = %ld, 0x%02lx";
323 fmt
= "%s:%ld:%s = %ld, 0x%04lx";
327 fmt
= "%s:%ld:%s = %ld, 0x%08lx";
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
);
343 _DBPRINTF(", '%lc'", value
);
347 _DBPRINTF("%s\n", ANSI_ESC_CLR
);
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
))
365 fmt
= "%s:%ld:%s = 0x%08lx\n";
367 fmt
= "%s:%ld:%s = NULL\n";
371 _DBPRINTF(ANSI_ESC_FG_GREEN
);
372 _DBPRINTF(fmt
, file
, line
, name
, p
);
373 _DBPRINTF(ANSI_ESC_CLR
);
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
))
390 _DBPRINTF("%s%s:%ld:%s = 0x%08lx \"%s\"%s\n", ANSI_ESC_FG_GREEN
, file
, line
, name
, string
, string
, ANSI_ESC_CLR
);
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
))
406 _DBPRINTF("%s%s:%ld:%s%s\n", ANSI_ESC_FG_GREEN
, file
, line
, msg
, ANSI_ESC_CLR
);
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
)))
420 static char buf
[1024];
424 va_start(args
, format
);
425 vsnprintf(buf
, 1024, format
, args
);
430 const char *highlight
= ANSI_ESC_FG_GREEN
;
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
);
446 _DBPRINTF("%s:%ld:%s\n", file
, line
, buf
);
450 /****************************************************************************/