X-Git-Url: http://git.wpitchoune.net/gitweb/?a=blobdiff_plain;f=src%2Flib%2Flog.c;h=7b4331f5db5146d94f09b883dfff82c0f0cf9667;hb=f3a27b1b5148cd84badffac85ac89ece2fe1a7b0;hp=4317338ac9a32e2430d43b3ac7e0813212c46b26;hpb=954dbf04870a4f721b101a3475671093c986968a;p=psensor.git diff --git a/src/lib/log.c b/src/lib/log.c index 4317338..7b4331f 100644 --- a/src/lib/log.c +++ b/src/lib/log.c @@ -25,9 +25,12 @@ #include #include +#include #include +#include #include "log.h" +#include "ptime.h" static FILE *file; int log_level = LOG_WARN; @@ -54,9 +57,8 @@ void log_close() #define LOG_BUFFER 4096 static void vlogf(int lvl, const char *fmt, va_list ap) { - struct timeval tv; char buffer[1 + LOG_BUFFER]; - char *lvl_str; + char *lvl_str, *t; FILE *stdf; if (lvl > LOG_INFO && (!file || lvl > log_level)) @@ -65,9 +67,6 @@ static void vlogf(int lvl, const char *fmt, va_list ap) vsnprintf(buffer, LOG_BUFFER, fmt, ap); buffer[LOG_BUFFER] = '\0'; - if (gettimeofday(&tv, NULL) != 0) - timerclear(&tv); - switch (lvl) { case LOG_WARN: lvl_str = "[WARN]"; @@ -85,9 +84,15 @@ static void vlogf(int lvl, const char *fmt, va_list ap) lvl_str = "[??]"; } + t = get_time_str(); + if (!t) + return ; + if (file && lvl <= log_level) { - fprintf(file, "[%ld] %s %s\n", tv.tv_sec, lvl_str, buffer); + fprintf(file, "[%s] %s %s\n", t, lvl_str, buffer); fflush(file); + } else { + t = NULL; } if (lvl <= LOG_INFO) { @@ -96,8 +101,11 @@ static void vlogf(int lvl, const char *fmt, va_list ap) else stdf = stdout; - fprintf(stdf, "[%ld] %s %s\n", tv.tv_sec, lvl_str, buffer); + + fprintf(stdf, "[%s] %s %s\n", t, lvl_str, buffer); } + + free(t); } void log_printf(int lvl, const char *fmt, ...)