From 2f951a372c5330b4e8a8209122905e073d189bda Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Thu, 23 Feb 2012 10:41:46 +0100 Subject: Add thread id to debug output. --- server/src/debug.cc | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) (limited to 'server/src/debug.cc') diff --git a/server/src/debug.cc b/server/src/debug.cc index fa83a80..d3e74e8 100644 --- a/server/src/debug.cc +++ b/server/src/debug.cc @@ -34,6 +34,13 @@ #include "log.h" +#include + +static unsigned int gettid() +{ + return (unsigned int)pthread_self(); +} + static FILE *logfp = stderr; #define NELEM(x) (sizeof(x)/sizeof((x)[0])) @@ -72,8 +79,8 @@ int __debug(const char *func, const int line, int ret = 0; if(__debug_enabled(cl, ch)) { if((unsigned)cl < NELEM(debug_class_str)) - ret += fprintf(logfp, "%s:%s:%s:%d ", - debug_class_str[(unsigned)cl], ch, func, line); + ret += fprintf(logfp, "%u %s:%s:%s:%d ", + gettid(), debug_class_str[(unsigned)cl], ch, func, line); if(fmt) { va_list va; va_start(va, fmt); @@ -94,8 +101,8 @@ int __debug_va(const char *func, const int line, int ret = 0; if(__debug_enabled(cl, ch)) { if((unsigned)cl < NELEM(debug_class_str)) - ret += fprintf(logfp, "%s:%s:%s:%d ", - debug_class_str[(unsigned)cl], ch, func, line); + ret += fprintf(logfp, "%u %s:%s:%s:%d ", + gettid(), debug_class_str[(unsigned)cl], ch, func, line); if(fmt) ret += vfprintf(logfp, fmt, va); } @@ -120,7 +127,8 @@ int __log(const char *func, const int line, const enum __debug_class cl, if((unsigned)cl < NELEM(debug_class_str)) 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); + ret = fprintf(logfp, "%u %s:%s:%s:%d ", + gettid(), debug_class_str[(unsigned)cl], ch, func, line); #endif sprintf(str, "%d", line); logmsg = std::string(debug_class_str[(unsigned)cl]) + ":" + ch + ":" + func + ":" + str; @@ -157,7 +165,8 @@ int __log_va(const char *func, const int line, const enum __debug_class cl, if(__debug_enabled(cl, ch)) { 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); + ret = fprintf(logfp, "%u %s:%s:%s:%d ", + gettid(), debug_class_str[(unsigned)cl], ch, func, line); #endif sprintf(str, "%d", line); logmsg = std::string(debug_class_str[(unsigned)cl]) + ":" + ch + ":" + func + ":" + str; -- cgit v1.2.3 From aac862e2797e9a05c64a872850bb2e9f304d6e13 Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Thu, 12 Apr 2012 10:05:31 +0200 Subject: New mutex wrapped debug system. --- server/src/debug.cc | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'server/src/debug.cc') diff --git a/server/src/debug.cc b/server/src/debug.cc index d3e74e8..e42baae 100644 --- a/server/src/debug.cc +++ b/server/src/debug.cc @@ -36,6 +36,10 @@ #include +#include "mutex.h" + +static Mutex mutex; + static unsigned int gettid() { return (unsigned int)pthread_self(); @@ -76,6 +80,7 @@ int __debug(const char *func, const int line, const enum __debug_class cl, const char *ch, const char *fmt, ...) { + MutexAutolock m(mutex); int ret = 0; if(__debug_enabled(cl, ch)) { if((unsigned)cl < NELEM(debug_class_str)) @@ -98,6 +103,7 @@ int __debug_va(const char *func, const int line, const enum __debug_class cl, const char *ch, const char *fmt, va_list va) { + MutexAutolock m(mutex); int ret = 0; if(__debug_enabled(cl, ch)) { if((unsigned)cl < NELEM(debug_class_str)) @@ -117,6 +123,8 @@ int __debug_va(const char *func, const int line, int __log(const char *func, const int line, const enum __debug_class cl, const char *ch, const char *fmt, ...) { + MutexAutolock m(mutex); + std::string logmsg; char str[8]; @@ -157,6 +165,8 @@ int __log(const char *func, const int line, const enum __debug_class cl, int __log_va(const char *func, const int line, const enum __debug_class cl, const char *ch, const char *fmt, va_list va) { + MutexAutolock m(mutex); + std::string logmsg; char str[8]; #ifdef WITH_DEBUG @@ -190,9 +200,25 @@ int __log_va(const char *func, const int line, const enum __debug_class cl, void debug_init(FILE *fp) { + mutex.name = "debug"; + MutexAutolock m(mutex); logfp = fp; } +void debug_reinit(const char *logfile) +{ + MutexAutolock m(mutex); + + if(logfp != stderr) { + fclose(logfp); + logfp = fopen(logfile, "a"); + if(!logfp) { + fprintf(stderr, "Could not write to logfile: '%s'\n", logfile); + logfp = stderr; + } + } +} + /* * fmt := [set[,set]*]* @@ -202,6 +228,7 @@ void debug_init(FILE *fp) */ void debug_parse(const char *fmt) { + MutexAutolock m(mutex); char *s; char *next; char *opt; -- cgit v1.2.3