From 551d4aa1be9f4d256df3fadca9a005a0f316adf8 Mon Sep 17 00:00:00 2001 From: bertho Date: Thu, 5 Feb 2009 01:26:26 +0000 Subject: Add a flexible debug interface. Please read the documentation. --- server/src/debug.cc | 173 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 173 insertions(+) create mode 100644 server/src/debug.cc (limited to 'server/src/debug.cc') diff --git a/server/src/debug.cc b/server/src/debug.cc new file mode 100644 index 0000000..9c2f268 --- /dev/null +++ b/server/src/debug.cc @@ -0,0 +1,173 @@ + +#include + +#include +#include +#include +#include +#include "debug.h" + +#define NELEM(x) (sizeof(x)/sizeof((x)[0])) +struct __pracro_debug_channel +{ + char name[16]; + unsigned flags; +}; + +static const char * const debug_class_str[] = { "fixme", "info", "warn", "err", "debug" }; +#define __PRACRO_DEBUG_CHANNEL_MAX 256 +static struct __pracro_debug_channel debug_channel[__PRACRO_DEBUG_CHANNEL_MAX]; +static unsigned n_debug_channel = 0; +static unsigned debug_flags = (1 << __pracro_class_err) | (1 << __pracro_class_fixme); + +static int __pracro_debug_enabled(const enum __pracro_debug_class cl, const char *ch) +{ + unsigned i; + for(i = 0; i < n_debug_channel; i++) { + if(!strcmp(ch, debug_channel[i].name)) { + return debug_channel[i].flags & (1 << cl); + } + } + return debug_flags & (1 << cl); +} + + +#ifdef WITH_DEBUG +int __pracro_debug(const char *func, const int line, const enum __pracro_debug_class cl, const char *ch, const char *fmt, ...) +{ + int ret = 0; + if(__pracro_debug_enabled(cl, ch)) { + if((unsigned)cl < NELEM(debug_class_str)) + ret += fprintf(stderr, "%s:%s:%s:%d ", debug_class_str[(unsigned)cl], ch, func, line); + if(fmt) { + va_list va; + va_start(va, fmt); + ret += vfprintf(stderr, fmt, va); + va_end(va); + } + } + if(ret) + fflush(stderr); + return ret; +} + +int __pracro_debug_va(const char *func, const int line, const enum __pracro_debug_class cl, const char *ch, const char *fmt, va_list va) +{ + int ret = 0; + if(__pracro_debug_enabled(cl, ch)) { + if((unsigned)cl < NELEM(debug_class_str)) + ret += fprintf(stderr, "%s:%s:%s:%d ", debug_class_str[(unsigned)cl], ch, func, line); + if(fmt) + ret += vfprintf(stderr, fmt, va); + } + if(ret) + fflush(stderr); + return ret; +} + +void pracro_debug_init(void) +{ +} + +#else + +/* FIXME: should do syslog here... */ +int __pracro_log(const char *func, const int line, const enum __pracro_debug_class cl, const char *ch, const char *fmt, ...) +{ + int ret = 0; + if(__pracro_debug_enabled(cl, ch)) { + if((unsigned)cl < NELEM(debug_class_str)) + ret += fprintf(stderr, "%s:%s:%s:%d ", debug_class_str[(unsigned)cl], ch, func, line); + if(fmt) { + va_list va; + va_start(va, fmt); + ret += vfprintf(stderr, fmt, va); + va_end(va); + } + } + return ret; +} + +/* FIXME: should do syslog here... */ +int __pracro_log_va(const char *func, const int line, const enum __pracro_debug_class cl, const char *ch, const char *fmt, va_list va) +{ + int ret = 0; + if(__pracro_debug_enabled(cl, ch)) { + if((unsigned)cl < NELEM(debug_class_str)) + ret += fprintf(stderr, "%s:%s:%s:%d ", debug_class_str[(unsigned)cl], ch, func, line); + if(fmt) + ret += vfprintf(stderr, fmt, va); + } + return ret; +} + +void pracro_debug_init(void) +{ +} + +#endif + +/* + * fmt := [set[,set]*]* + * set := [+-]channel + * | class[+-]channel + * | [+-]all + */ +void pracro_debug_parse(const char *fmt) +{ + char *s; + char *next; + char *opt; + + if(!(s = strdup(fmt))) return; + + for(opt = s; opt; opt = next) { + int set = 0; + int clr = 0; + unsigned i; + if((next = strchr(s, ','))) *next++ = '\0'; + char *p = opt + strcspn(opt, "+-"); + if(!*p) p = opt; /* All chars -> a channel name */ + if(p > opt) { + /* we have a class */ + for(i = 0; i < NELEM(debug_class_str); i++) { + int n = strlen(debug_class_str[i]); + if(n != (p - opt)) continue; + if(!memcmp(opt, debug_class_str[i], n)) { + /* Found the class */ + if(*p == '+') + set = 1 << i; + else + clr = 1 << i; + break; + } + } + if(i == NELEM(debug_class_str)) continue; + } else { + if(*p == '-') + clr = ~0; + else + set = ~0; + } + if(*p == '+' || *p == '-') p++; + if(!*p) continue; + if(!strcmp("all", p)) { + debug_flags = (debug_flags & ~clr) | set; + } else { + if(strlen(p) >= sizeof(debug_channel[0].name)) continue; + for(i = 0; i < n_debug_channel; i++) { + if(!strcmp(p, debug_channel[i].name)) { + debug_channel[i].flags = (debug_channel[i].flags & ~clr) | set; + break; + } + } + if(i == n_debug_channel) { + strcpy(debug_channel[i].name, p); + debug_channel[i].flags = (debug_flags & ~clr) | set; + n_debug_channel++; + } + } + } + free(s); +} + -- cgit v1.2.3