X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;f=lib%2Flog.c;h=713fa768c0c6acb91806d52319d4802d52d05962;hb=78775c3f5e38e8bee7d04b0f994766c7dd1e6578;hp=88360004ec9ed0b91dff9215ee4ba0677a2fb791;hpb=01a353ec6f528d5320b5f5ac17ee80d37ed901bf;p=libucw.git diff --git a/lib/log.c b/lib/log.c index 88360004..713fa768 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,26 +21,36 @@ 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 tm *tm = localtime(&tim); + struct timeval tv; + struct tm tm; byte *buf, *p; int buflen = 256; int l, l0, r; va_list args2; + gettimeofday(&tv, NULL); + if (!localtime_r(&tv.tv_sec, &tm)) + bzero(&tm, sizeof(tm)); + if (log_switch_hook) - log_switch_hook(tm); + log_switch_hook(&tm); while (1) { p = buf = alloca(buflen); *p++ = cat; - p += strftime(p, buflen, " %Y-%m-%d %H:%M:%S ", tm); + /* We cannot use strftime() here, because it's not re-entrant */ + p += sprintf(p, " %4d-%02d-%02d %02d:%02d:%02d", tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday, + tm.tm_hour, tm.tm_min, tm.tm_sec); + if (log_precise_timings) + p += sprintf(p, ".%06d", (int)tv.tv_usec); + *p++ = ' '; if (log_title) { if (log_pid)