From eb587650d25e0d7f0836bc1a9b24f58ed50e870a Mon Sep 17 00:00:00 2001 From: Martin Minarik Date: Fri, 8 Jun 2012 20:59:53 +0200 Subject: [PATCH] log: Fixes Drop the timestamp caching because it doesn't seem useful. Use gettimeofday() to obtain system time. Add null checks to both contstructor and destructor. Drop the flushing and set line buffering instead. Remove weston_log_print(), it only wraps vfprintf anyways. --- src/log.c | 46 +++++++++++++++++----------------------------- src/log.h | 4 ++-- 2 files changed, 19 insertions(+), 31 deletions(-) diff --git a/src/log.c b/src/log.c index 0042619..a111cf8 100644 --- a/src/log.c +++ b/src/log.c @@ -24,6 +24,7 @@ #include #include #include +#include #include #include @@ -33,36 +34,19 @@ static FILE *weston_logfile = NULL; -static char cached_timestamp[21]; -static int cached_tv_sec = 0; - static int weston_log_timestamp(void) { - struct timespec tp; - unsigned int time; - - clock_gettime(CLOCK_REALTIME, &tp); - time = (tp.tv_sec * 1000000L) + (tp.tv_nsec / 1000); + struct timeval tv; + struct tm *brokendown_time; + char string[128]; - if (cached_tv_sec != tp.tv_sec) { - char string[26]; - struct tm *lt = localtime(&tp.tv_sec); + gettimeofday(&tv, NULL); - cached_tv_sec = tp.tv_sec; + brokendown_time = localtime(&tv.tv_sec); - strftime (string,24,"%Y-%m-%d %H:%M:%S", lt); - strncpy (cached_timestamp, string, 20); - } - - return fprintf(weston_logfile, "[%s.%03u] ", cached_timestamp, tp.tv_nsec/1000000); -} + strftime(string, sizeof string, "%Y-%m-%d %H:%M:%S", brokendown_time); -static int weston_log_print(const char *fmt, va_list arg) -{ - int l; - l = vfprintf(weston_logfile, fmt, arg); - fflush(weston_logfile); - return l; + return fprintf(weston_logfile, "[%s.%03li] ", string, tv.tv_usec/1000); } static void @@ -70,7 +54,7 @@ custom_handler(const char *fmt, va_list arg) { weston_log_timestamp(); fprintf(weston_logfile, "libwayland: "); - weston_log_print(fmt, arg); + vfprintf(weston_logfile, fmt, arg); } void @@ -78,15 +62,19 @@ weston_log_file_open(const char *filename) { wl_log_set_handler_server(custom_handler); - weston_logfile = fopen(filename, "a"); + if (filename != NULL) + weston_logfile = fopen(filename, "a"); + if (weston_logfile == NULL) weston_logfile = stderr; + else + setvbuf(weston_logfile, NULL, _IOLBF, 256); } void weston_log_file_close() { - if (weston_logfile != stderr) + if ((weston_logfile != stderr) && (weston_logfile != NULL)) fclose(weston_logfile); weston_logfile = stderr; } @@ -98,7 +86,7 @@ weston_log(const char *fmt, ...) va_list argp; va_start(argp, fmt); l = weston_log_timestamp(); - l += weston_log_print(fmt, argp); + l += vfprintf(weston_logfile, fmt, argp); va_end(argp); return l; } @@ -109,7 +97,7 @@ weston_log_continue(const char *fmt, ...) int l; va_list argp; va_start(argp, fmt); - l = weston_log_print(fmt, argp); + l = vfprintf(weston_logfile, fmt, argp); va_end(argp); return l; } diff --git a/src/log.h b/src/log.h index d050f02..19d36dd 100644 --- a/src/log.h +++ b/src/log.h @@ -20,8 +20,8 @@ * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -#ifndef _WAYLAND_SYSTEM_LOG_H_ -#define _WAYLAND_SYSTEM_LOG_H_ +#ifndef _WESTON_LOG_H_ +#define _WESTON_LOG_H_ void weston_log_file_open(const char *filename); void weston_log_file_close(void); -- 2.7.4