From: Martin Mares Date: Fri, 20 Jan 2006 18:34:28 +0000 (+0000) Subject: Added a possibility of logging with microsecond timestamps. X-Git-Tag: holmes-import~689 X-Git-Url: http://mj.ucw.cz/gitweb/?a=commitdiff_plain;h=ce64a28cb066300d964d86f2db9842e1708f9e70;p=libucw.git Added a possibility of logging with microsecond timestamps. --- diff --git a/lib/lib.h b/lib/lib.h index 80090971..a0f6788a 100644 --- a/lib/lib.h +++ b/lib/lib.h @@ -81,6 +81,7 @@ extern char *log_title; /* NULL - print no title, default is log_progname */ extern char *log_filename; /* Expanded name of the current log file */ extern volatile int log_switch_nest; /* log_switch() nesting counter, increment to disable automatic switches */ extern int log_pid; /* 0 if shouldn't be logged */ +extern int log_precise_timings; /* Include microsecond timestamps in log messages */ extern void (*log_die_hook)(void); struct tm; extern void (*log_switch_hook)(struct tm *tm); diff --git a/lib/log.c b/lib/log.c index 084c72d7..e513cbe6 100644 --- a/lib/log.c +++ b/lib/log.c @@ -1,7 +1,7 @@ /* * UCW Library -- Logging * - * (c) 1997--2005 Martin Mares + * (c) 1997--2006 Martin Mares * * This software may be freely distributed and used according to the terms * of the GNU Lesser General Public License. @@ -13,6 +13,7 @@ #include #include #include +#include #include #include @@ -20,20 +21,22 @@ static char log_progname[32]; char *log_filename; char *log_title; int log_pid; +int log_precise_timings; void (*log_die_hook)(void); void (*log_switch_hook)(struct tm *tm); void vlog_msg(unsigned int cat, const char *msg, va_list args) { - time_t tim = time(NULL); + struct timeval tv; struct tm tm; byte *buf, *p; int buflen = 256; int l, l0, r; va_list args2; - if (!localtime_r(&tim, &tm)) + gettimeofday(&tv, NULL); + if (!localtime_r(&tv.tv_sec, &tm)) bzero(&tm, sizeof(tm)); if (log_switch_hook) @@ -42,7 +45,10 @@ vlog_msg(unsigned int cat, const char *msg, va_list args) { p = buf = alloca(buflen); *p++ = cat; - p += strftime(p, buflen, " %Y-%m-%d %H:%M:%S ", &tm); + p += strftime(p, buflen, " %Y-%m-%d %H:%M:%S", &tm); + if (log_precise_timings) + p += sprintf(p, ".%06d", (int)tv.tv_usec); + *p++ = ' '; if (log_title) { if (log_pid)