#include #include #include #include #include #include "debug.h" GMutex debug_mutex; #define LEN 2048 void debug_init() { g_mutex_init (&debug_mutex); } void debug_tofile(char *fname, char *fmt, ...) { static struct timeval tv; static int firstrun = 1; struct timeval tv1; va_list args; char buffer[LEN]; int fd, len; long long int ms; std::string s = setlocale(LC_ALL, NULL); setlocale (LC_ALL, "C"); g_mutex_lock(&debug_mutex); if (firstrun) { gettimeofday(&tv,NULL); firstrun = 0; } gettimeofday(&tv1,NULL); ms = ((tv1.tv_sec - tv.tv_sec) * 1000) + ((tv1.tv_usec - tv.tv_usec) / 1000); #ifdef BUILD_WINDOWS fd = open(fname, O_WRONLY | O_APPEND | O_CREAT); #else fd = open(fname, O_WRONLY | O_APPEND | O_CREAT, S_IRUSR | S_IWUSR); #endif if (fd < 0) errorexit((char*)"%s:%d could not open debug '%s' file. Error:%s\n", __FILE__, __LINE__, fname, strerror(errno)); snprintf (buffer, 32, "%lld,", ms); len = strlen (buffer); va_start (args, fmt); vsnprintf (buffer+len, LEN-1-len, fmt, args); va_end (args); buffer[LEN-1] = 0; write (fd, buffer, strlen(buffer)); close (fd); g_mutex_unlock(&debug_mutex); setlocale (LC_ALL, s.c_str()); }