From ccc928020c3a0dbf96f7ed8640e7281550d6276c Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Wed, 4 Jun 2008 19:00:46 +0200 Subject: [PATCH] Replaced safe_msg() with a flag L_SIGHANDLER to msg(). Updated all callers. --- lib/lib.h | 6 ++-- lib/log.c | 91 +++++++++++++------------------------------------------ 2 files changed, 24 insertions(+), 73 deletions(-) diff --git a/lib/lib.h b/lib/lib.h index 6fdefd64..1bd0b354 100644 --- a/lib/lib.h +++ b/lib/lib.h @@ -1,7 +1,7 @@ /* * The UCW Library -- Miscellaneous Functions * - * (c) 1997--2007 Martin Mares + * (c) 1997--2008 Martin Mares * (c) 2005 Tomas Valla * (c) 2006 Robert Spalek * (c) 2007 Pavel Charvat @@ -90,6 +90,8 @@ #define L_ERROR_R 'e' #define L_FATAL '!' /* die() */ +#define L_SIGHANDLER 0x10000 /* Avoid things which are unsafe in signal handlers */ + 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 */ @@ -101,8 +103,6 @@ extern void (*log_switch_hook)(struct tm *tm); void msg(uns cat, const char *fmt, ...) FORMAT_CHECK(printf,2,3); void vmsg(uns cat, const char *fmt, va_list args); -void safe_msg(uns cat, const char *fmt, ...) FORMAT_CHECK(printf,2,3); -void safe_vmsg(uns cat, const char *fmt, va_list args); void die(const char *, ...) NONRET FORMAT_CHECK(printf,1,2); void log_init(const char *argv0); void log_file(const char *name); diff --git a/lib/log.c b/lib/log.c index 4d91b315..f3ab51db 100644 --- a/lib/log.c +++ b/lib/log.c @@ -1,7 +1,7 @@ /* * UCW Library -- Logging * - * (c) 1997--2006 Martin Mares + * (c) 1997--2008 Martin Mares * * This software may be freely distributed and used according to the terms * of the GNU Lesser General Public License. @@ -29,88 +29,39 @@ void vmsg(unsigned int cat, const char *fmt, va_list args) { struct timeval tv; + int have_tm = 0; 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 (!(cat & L_SIGHANDLER)) + { + /* CAVEAT: These calls are not safe in signal handlers. */ + gettimeofday(&tv, NULL); + if (localtime_r(&tv.tv_sec, &tm)) + have_tm = 1; + if (log_switch_hook) + log_switch_hook(&tm); + } - if (log_switch_hook) - log_switch_hook(&tm); while (1) { p = buf = alloca(buflen); - *p++ = cat; - /* 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) + *p++ = cat & 0xff; + if (have_tm) { - if (log_pid) - p += sprintf(p, "[%s (%d)] ", log_title, log_pid); - else - p += sprintf(p, "[%s] ", log_title); + p += strftime(p, buflen, " %Y-%m-%d %H:%M:%S", &tm); + if (log_precise_timings) + p += sprintf(p, ".%06d", (int)tv.tv_usec); } else { - if (log_pid) - p += sprintf(p, "[%d] ", log_pid); + p += sprintf(p, " \?\?\?\?-\?\?-\?\? \?\?:\?\?:\?\?"); + if (log_precise_timings) + p += sprintf(p, ".\?\?\?\?\?\?"); } - l0 = p - buf + 1; - r = buflen - l0; - va_copy(args2, args); - l = vsnprintf(p, r, fmt, args2); - va_end(args2); - if (l < 0) - l = r; - else if (l < r) - { - while (*p) - { - if (*p < 0x20 && *p != '\t') - *p = 0x7f; - p++; - } - *p = '\n'; - write(2, buf, l + l0); - return; - } - buflen = l + l0 + 1; - } -} - -void -msg(unsigned int cat, const char *fmt, ...) -{ - va_list args; - - va_start(args, fmt); - vmsg(cat, fmt, args); - va_end(args); -} - -void -safe_vmsg(unsigned int cat, const char *fmt, va_list args) -{ - byte *buf, *p; - int buflen = 256; - int l, l0, r; - va_list args2; - - while (1) - { - p = buf = alloca(buflen); - *p++ = cat; - p += sprintf(p, " \?\?\?\?-\?\?-\?\? \?\?:\?\?:\?\?"); - if (log_precise_timings) - p += sprintf(p, ".\?\?\?\?\?\?"); *p++ = ' '; if (log_title) { @@ -148,12 +99,12 @@ safe_vmsg(unsigned int cat, const char *fmt, va_list args) } void -safe_msg(unsigned int cat, const char *fmt, ...) +msg(unsigned int cat, const char *fmt, ...) { va_list args; va_start(args, fmt); - safe_vmsg(cat, fmt, args); + vmsg(cat, fmt, args); va_end(args); } -- 2.39.2