From c938a893593fa7ea2e596ec41e186cdfbaee4e58 Mon Sep 17 00:00:00 2001 From: Jef Driesen Date: Thu, 3 Jul 2008 09:46:18 +0000 Subject: [PATCH] Write timestamps to the logfile. --- src/utils.c | 47 +++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 43 insertions(+), 4 deletions(-) diff --git a/src/utils.c b/src/utils.c index 0c24228..34a1fca 100644 --- a/src/utils.c +++ b/src/utils.c @@ -1,22 +1,52 @@ #include #include +#include static FILE* g_logfile = NULL; +static unsigned char g_lastchar = '\n'; + +#ifdef _WIN32 + #include + static unsigned long g_timestamp; +#else + #include + static struct timeval g_timestamp; +#endif + int message (const char* fmt, ...) { va_list ap; - + if (g_logfile) { + if (g_lastchar == '\n') { +#ifdef _WIN32 + unsigned long timestamp = GetTickCount () - g_timestamp; + unsigned long sec = timestamp / 1000L, msec = timestamp % 1000L; + fprintf (g_logfile, "[%li.%03li] ", sec, msec); +#else + struct timeval now = {0}, timestamp = {0}; + gettimeofday (&now, NULL); + timersub (&now, &g_timestamp, ×tamp); + fprintf (g_logfile, "[%lli.%06lli] ", (long long)timestamp.tv_sec, (long long)timestamp.tv_usec); +#endif + } + + size_t len = strlen (fmt); + if (len > 0) + g_lastchar = fmt[len - 1]; + else + g_lastchar = 0; + va_start (ap, fmt); vfprintf (g_logfile, fmt, ap); va_end (ap); } - + va_start (ap, fmt); int rc = vfprintf (stdout, fmt, ap); va_end (ap); - + return rc; } @@ -26,7 +56,16 @@ void message_set_logfile (const char* filename) fclose (g_logfile); g_logfile = NULL; } - + if (filename) g_logfile = fopen (filename, "w"); + + if (g_logfile) { + g_lastchar = '\n'; +#ifdef _WIN32 + g_timestamp = GetTickCount (); +#else + gettimeofday (&g_timestamp, NULL); +#endif + } }