diff options
Diffstat (limited to 'server/src/debug.cc')
-rw-r--r-- | server/src/debug.cc | 145 |
1 files changed, 98 insertions, 47 deletions
diff --git a/server/src/debug.cc b/server/src/debug.cc index 8bb45db..1affa23 100644 --- a/server/src/debug.cc +++ b/server/src/debug.cc @@ -27,27 +27,32 @@ */ #include "debug.h" -#include <config.h> - #include <stdio.h> #include <stdlib.h> #include <stdarg.h> #include <string.h> +#include "log.h" + +static FILE *logfp = stderr; + #define NELEM(x) (sizeof(x)/sizeof((x)[0])) -struct __pracro_debug_channel +struct __debug_channel { - char name[64]; - unsigned flags; + 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 const char * const debug_class_str[] = + { "fixme", "err", "warn", "info", "debug" }; + +#define __DEBUG_CHANNEL_MAX 256 + +static struct __debug_channel debug_channel[__DEBUG_CHANNEL_MAX]; static unsigned n_debug_channel = 0; -static unsigned debug_flags = (1 << __pracro_class_err) | (1 << __pracro_class_fixme); +static unsigned debug_flags = (1 << __class_err) | (1 << __class_fixme); -static int __pracro_debug_enabled(const enum __pracro_debug_class cl, const char *ch) +static int __debug_enabled(const enum __debug_class cl, const char *ch) { unsigned i; for(i = 0; i < n_debug_channel; i++) { @@ -60,79 +65,125 @@ static int __pracro_debug_enabled(const enum __pracro_debug_class cl, const char #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 __debug(const char *func, const int line, + const enum __debug_class cl, + const char *ch, const char *fmt, ...) { int ret = 0; - if(__pracro_debug_enabled(cl, ch)) { + if(__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); + ret += fprintf(logfp, "%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); + ret += vfprintf(logfp, fmt, va); va_end(va); } } - if(ret) - fflush(stderr); + if(ret){ + fflush(logfp); + } 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 __debug_va(const char *func, const int line, + const enum __debug_class cl, + const char *ch, const char *fmt, va_list va) { int ret = 0; - if(__pracro_debug_enabled(cl, ch)) { + if(__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); + ret += fprintf(logfp, "%s:%s:%s:%d ", + debug_class_str[(unsigned)cl], ch, func, line); if(fmt) - ret += vfprintf(stderr, fmt, va); + ret += vfprintf(logfp, fmt, va); } - if(ret) - fflush(stderr); + if(ret) { + fflush(logfp); + } return ret; } -void pracro_debug_init(void) -{ -} - -#else +#endif -/* 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 __log(const char *func, const int line, const enum __debug_class cl, + const char *ch, const char *fmt, ...) { + std::string logmsg; + char str[8]; + +#ifdef WITH_DEBUG int ret = 0; - if(__pracro_debug_enabled(cl, ch)) { +#endif + if(__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((unsigned)cl < NELEM(debug_class_str)) +#ifdef WITH_DEBUG + ret = fprintf(logfp, "%s:%s:%s:%d ", debug_class_str[(unsigned)cl], ch, func, line); +#endif + sprintf(str, "%d", line); + logmsg = std::string(debug_class_str[(unsigned)cl]) + ":" + ch + ":" + func + ":" + str; if(fmt) { va_list va; va_start(va, fmt); - ret += vfprintf(stderr, fmt, va); +#ifdef WITH_DEBUG + ret += vfprintf(logfp, fmt, va); +#endif + char* ptr; + if(vasprintf(&ptr, fmt, va) == -1) {} + logmsg += ptr; va_end(va); } } - return ret; +#ifdef WITH_DEBUG + if(ret) { + fprintf(logfp, "\n"); + fflush(logfp); + } +#endif + log(logmsg); + return logmsg.size(); } -/* 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 __log_va(const char *func, const int line, const enum __debug_class cl, + const char *ch, const char *fmt, va_list va) { + std::string logmsg; + char str[8]; +#ifdef WITH_DEBUG int ret = 0; - if(__pracro_debug_enabled(cl, ch)) { +#endif + if(__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); +#ifdef WITH_DEBUG + ret = fprintf(logfp, "%s:%s:%s:%d ", debug_class_str[(unsigned)cl], ch, func, line); +#endif + sprintf(str, "%d", line); + logmsg = std::string(debug_class_str[(unsigned)cl]) + ":" + ch + ":" + func + ":" + str; + if(fmt) { +#ifdef WITH_DEBUG + ret += vfprintf(logfp, fmt, va); +#endif + char* ptr; + if(vasprintf(&ptr, fmt, va) == -1) {} + logmsg += ptr; + } } - return ret; +#ifdef WITH_DEBUG + if(ret) { + fprintf(logfp, "\n"); + fflush(logfp); + } +#endif + return logmsg.size(); } -void pracro_debug_init(void) +void debug_init(FILE *fp) { + logfp = fp; } -#endif /* * fmt := [set[,set]*]* @@ -140,7 +191,7 @@ void pracro_debug_init(void) * | class[+-]channel * | [+-]all */ -void pracro_debug_parse(const char *fmt) +void debug_parse(const char *fmt) { char *s; char *next; @@ -154,14 +205,14 @@ void pracro_debug_parse(const char *fmt) unsigned i; if((next = strchr(opt, ','))) *next++ = '\0'; char *p = opt + strcspn(opt, "+-"); - if(!*p) p = opt; /* All chars -> a channel name */ + if(!*p) p = opt; // All chars -> a channel name if(p > opt) { - /* we have a class */ + // 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 */ + // Found the class if(*p == '+') set = 1 << i; else @@ -188,7 +239,7 @@ void pracro_debug_parse(const char *fmt) break; } } - if(i == n_debug_channel && n_debug_channel < __PRACRO_DEBUG_CHANNEL_MAX) { + if(i == n_debug_channel && n_debug_channel < __DEBUG_CHANNEL_MAX) { strcpy(debug_channel[i].name, p); debug_channel[i].flags = (debug_flags & ~clr) | set; n_debug_channel++; |