summaryrefslogtreecommitdiff
path: root/server/src/debug.cc
diff options
context:
space:
mode:
authorBent Bisballe Nyeng <deva@aasimon.org>2012-08-23 13:31:57 +0200
committerBent Bisballe Nyeng <deva@aasimon.org>2012-08-23 13:31:57 +0200
commit07694570b09524881d01df7c857cc8f471f1ad04 (patch)
tree8cae77df7bb081c1b7d37a587d11c765769d3643 /server/src/debug.cc
parent05732251c115b3538879ca523c461572115c6526 (diff)
parent909c48a297d7f68b107fce7ad444c2165f749f42 (diff)
Merge branch 'master' of http://git.aasimon.org/public/pracro
Diffstat (limited to 'server/src/debug.cc')
-rw-r--r--server/src/debug.cc48
1 files changed, 42 insertions, 6 deletions
diff --git a/server/src/debug.cc b/server/src/debug.cc
index fa83a80..e42baae 100644
--- a/server/src/debug.cc
+++ b/server/src/debug.cc
@@ -34,6 +34,17 @@
#include "log.h"
+#include <pthread.h>
+
+#include "mutex.h"
+
+static Mutex mutex;
+
+static unsigned int gettid()
+{
+ return (unsigned int)pthread_self();
+}
+
static FILE *logfp = stderr;
#define NELEM(x) (sizeof(x)/sizeof((x)[0]))
@@ -69,11 +80,12 @@ 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))
- 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);
@@ -91,11 +103,12 @@ 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))
- 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);
}
@@ -110,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];
@@ -120,7 +135,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;
@@ -149,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
@@ -157,7 +175,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;
@@ -181,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]*]*
@@ -193,6 +228,7 @@ void debug_init(FILE *fp)
*/
void debug_parse(const char *fmt)
{
+ MutexAutolock m(mutex);
char *s;
char *next;
char *opt;