From 2b35c95d6cbd49c8e72a1326452c706e3c9c9f8c Mon Sep 17 00:00:00 2001 From: Willy Tarreau Date: Sun, 15 Oct 2006 15:25:48 +0200 Subject: [PATCH] [MEDIUM] remove useless calls to gettimeofday() send_log(), Alert() and Warning() used gettimeofday() while using should have been preferred. --- src/haproxy.c | 1 + src/log.c | 16 +++++----------- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/src/haproxy.c b/src/haproxy.c index ef246e6eb5..92dc6264bc 100644 --- a/src/haproxy.c +++ b/src/haproxy.c @@ -346,6 +346,7 @@ void init(int argc, char **argv) /* initialize the libc's localtime structures once for all so that we * won't be missing memory if we want to send alerts under OOM conditions. + * Also, the Alert() and Warning() functions need to be initialized. */ tv_now(&now); localtime(&now.tv_sec); diff --git a/src/log.c b/src/log.c index e02a81f82d..6af34644ea 100644 --- a/src/log.c +++ b/src/log.c @@ -67,14 +67,12 @@ void **pool_requri = NULL; void Alert(const char *fmt, ...) { va_list argp; - struct timeval tv; struct tm *tm; if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) { va_start(argp, fmt); - gettimeofday(&tv, NULL); - tm = localtime(&tv.tv_sec); + tm = localtime(&now.tv_sec); fprintf(stderr, "[ALERT] %03d/%02d%02d%02d (%d) : ", tm->tm_yday, tm->tm_hour, tm->tm_min, tm->tm_sec, (int)getpid()); vfprintf(stderr, fmt, argp); @@ -90,14 +88,12 @@ void Alert(const char *fmt, ...) void Warning(const char *fmt, ...) { va_list argp; - struct timeval tv; struct tm *tm; if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) { va_start(argp, fmt); - gettimeofday(&tv, NULL); - tm = localtime(&tv.tv_sec); + tm = localtime(&now.tv_sec); fprintf(stderr, "[WARNING] %03d/%02d%02d%02d (%d) : ", tm->tm_yday, tm->tm_hour, tm->tm_min, tm->tm_sec, (int)getpid()); vfprintf(stderr, fmt, argp); @@ -177,7 +173,6 @@ void send_log(struct proxy *p, int level, const char *message, ...) { static int logfd = -1; /* syslog UDP socket */ static long tvsec = -1; /* to force the string to be initialized */ - struct timeval tv; va_list argp; static char logmsg[MAX_SYSLOG_LEN]; static char *dataptr = NULL; @@ -196,11 +191,10 @@ void send_log(struct proxy *p, int level, const char *message, ...) if (level < 0 || progname == NULL || message == NULL) return; - gettimeofday(&tv, NULL); - if (tv.tv_sec != tvsec || dataptr == NULL) { + if (now.tv_sec != tvsec || dataptr == NULL) { /* this string is rebuild only once a second */ - struct tm *tm = localtime(&tv.tv_sec); - tvsec = tv.tv_sec; + struct tm *tm = localtime(&now.tv_sec); + tvsec = now.tv_sec; hdr_len = snprintf(logmsg, sizeof(logmsg), "<<<<>%s %2d %02d:%02d:%02d %s[%d]: ",