X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;ds=inline;f=lib%2Flog.c;h=713fa768c0c6acb91806d52319d4802d52d05962;hb=1d8112cb590e458baa899805908a8ac910f58163;hp=0c91bcd15ec109fb49e4631647eb2cafdef99850;hpb=dd727096c813abe54a7eb438d3cde3c0120f9dd3;p=libucw.git diff --git a/lib/log.c b/lib/log.c index 0c91bcd1..713fa768 100644 --- a/lib/log.c +++ b/lib/log.c @@ -1,7 +1,7 @@ /* - * Sherlock Library -- Logging + * UCW Library -- Logging * - * (c) 1997--2004 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. @@ -11,9 +11,9 @@ #include #include -#include #include #include +#include #include #include @@ -21,25 +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) @@ -54,7 +65,9 @@ vlog_msg(unsigned int cat, const char *msg, va_list args) } l0 = p - buf + 1; r = buflen - l0; - l = vsnprintf(p, r, msg, args); + va_copy(args2, args); + l = vsnprintf(p, r, msg, args2); + va_end(args2); if (l < 0) l = r; else if (l < r) @@ -84,7 +97,7 @@ log_msg(unsigned int cat, const char *msg, ...) } void -die(byte *msg, ...) +die(const char *msg, ...) { va_list args; @@ -100,20 +113,18 @@ die(byte *msg, ...) #endif } -#ifdef DEBUG void assert_failed(char *assertion, char *file, int line) { log(L_FATAL, "Assertion `%s' failed at %s:%d", assertion, file, line); abort(); } -#else + void -assert_failed(void) +assert_failed_noinfo(void) { die("Internal error: Assertion failed."); } -#endif static byte * log_basename(byte *n)