[MEDIUM] remove useless calls to gettimeofday()
send_log(), Alert() and Warning() used gettimeofday() while using
<now> should have been preferred.
diff --git a/src/haproxy.c b/src/haproxy.c
index ef246e6..92dc626 100644
--- a/src/haproxy.c
+++ b/src/haproxy.c
@@ -346,6 +346,7 @@
/* 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 <now> to be initialized.
*/
tv_now(&now);
localtime(&now.tv_sec);
diff --git a/src/log.c b/src/log.c
index e02a81f..6af3464 100644
--- a/src/log.c
+++ b/src/log.c
@@ -67,14 +67,12 @@
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 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 @@
{
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 @@
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]: ",