[MEDIUM] remove useless calls to gettimeofday()

send_log(), Alert() and Warning() used gettimeofday() while using
<now> should have been preferred.
This commit is contained in:
Willy Tarreau 2006-10-15 15:25:48 +02:00
parent b17916e89b
commit 2b35c95d6c
2 changed files with 6 additions and 11 deletions

View File

@ -346,6 +346,7 @@ void init(int argc, char **argv)
/* initialize the libc's localtime structures once for all so that we /* 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. * won't be missing memory if we want to send alerts under OOM conditions.
* Also, the Alert() and Warning() functions need <now> to be initialized.
*/ */
tv_now(&now); tv_now(&now);
localtime(&now.tv_sec); localtime(&now.tv_sec);

View File

@ -67,14 +67,12 @@ void **pool_requri = NULL;
void Alert(const char *fmt, ...) void Alert(const char *fmt, ...)
{ {
va_list argp; va_list argp;
struct timeval tv;
struct tm *tm; struct tm *tm;
if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) { if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
va_start(argp, fmt); va_start(argp, fmt);
gettimeofday(&tv, NULL); tm = localtime(&now.tv_sec);
tm = localtime(&tv.tv_sec);
fprintf(stderr, "[ALERT] %03d/%02d%02d%02d (%d) : ", fprintf(stderr, "[ALERT] %03d/%02d%02d%02d (%d) : ",
tm->tm_yday, tm->tm_hour, tm->tm_min, tm->tm_sec, (int)getpid()); tm->tm_yday, tm->tm_hour, tm->tm_min, tm->tm_sec, (int)getpid());
vfprintf(stderr, fmt, argp); vfprintf(stderr, fmt, argp);
@ -90,14 +88,12 @@ void Alert(const char *fmt, ...)
void Warning(const char *fmt, ...) void Warning(const char *fmt, ...)
{ {
va_list argp; va_list argp;
struct timeval tv;
struct tm *tm; struct tm *tm;
if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) { if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
va_start(argp, fmt); va_start(argp, fmt);
gettimeofday(&tv, NULL); tm = localtime(&now.tv_sec);
tm = localtime(&tv.tv_sec);
fprintf(stderr, "[WARNING] %03d/%02d%02d%02d (%d) : ", fprintf(stderr, "[WARNING] %03d/%02d%02d%02d (%d) : ",
tm->tm_yday, tm->tm_hour, tm->tm_min, tm->tm_sec, (int)getpid()); tm->tm_yday, tm->tm_hour, tm->tm_min, tm->tm_sec, (int)getpid());
vfprintf(stderr, fmt, argp); 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 int logfd = -1; /* syslog UDP socket */
static long tvsec = -1; /* to force the string to be initialized */ static long tvsec = -1; /* to force the string to be initialized */
struct timeval tv;
va_list argp; va_list argp;
static char logmsg[MAX_SYSLOG_LEN]; static char logmsg[MAX_SYSLOG_LEN];
static char *dataptr = NULL; 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) if (level < 0 || progname == NULL || message == NULL)
return; return;
gettimeofday(&tv, NULL); if (now.tv_sec != tvsec || dataptr == NULL) {
if (tv.tv_sec != tvsec || dataptr == NULL) {
/* this string is rebuild only once a second */ /* this string is rebuild only once a second */
struct tm *tm = localtime(&tv.tv_sec); struct tm *tm = localtime(&now.tv_sec);
tvsec = tv.tv_sec; tvsec = now.tv_sec;
hdr_len = snprintf(logmsg, sizeof(logmsg), hdr_len = snprintf(logmsg, sizeof(logmsg),
"<<<<>%s %2d %02d:%02d:%02d %s[%d]: ", "<<<<>%s %2d %02d:%02d:%02d %s[%d]: ",