You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Modelbahn/server/debug.cc

57 lines
1.2 KiB

// #define _GNU_SOURCE /* See feature_test_macros(7) */
#include <stdarg.h>
#include <execinfo.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/syscall.h>
#include "modelbahn.h"
#define DEBUG_FILE "/tmp/modelbahn-server.log"
int _debuglevel = 0xff;
pid_t gettid();
void debug (int type, char *fmt,...) {
va_list args;
char text1[DEBUG_TEXT1LEN];
char text2[DEBUG_TEXT2LEN];
pid_t pid = gettid();
va_start (args, fmt);
vsnprintf (text1, (DEBUG_TEXT1LEN-1), fmt, args);
va_end (args);
text1[DEBUG_TEXT1LEN-1] = 0;
text2[DEBUG_TEXT2LEN-1] = 0;
if (type > 0) snprintf (text2, DEBUG_TEXT2LEN-1, "(%d)%d: %s", pid, type, text1);
else snprintf (text2, DEBUG_TEXT2LEN-1, "(%d) %s", pid, text1);
if (type == 0 || (type & _debuglevel) != 0) {
FILE *f;
printf ("%s\n", text2);
f= fopen(DEBUG_FILE, "a");
if (f) {
fprintf (f, "%s\n", text2);
fclose (f);
}
}
};
/*******************************************************************************************
* helper stuff
*******************************************************************************************/
//
// return the current thread process id
//
pid_t gettid() {
pid_t tid = 0;
tid = syscall(SYS_gettid);
return tid;
};