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.
This commit is contained in:
Martin Minarik 2012-06-08 20:59:53 +02:00 committed by Kristian Høgsberg
parent 6d11836721
commit eb587650d2
2 changed files with 19 additions and 31 deletions

View File

@ -24,6 +24,7 @@
#include <stdarg.h> #include <stdarg.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/time.h>
#include <time.h> #include <time.h>
#include <wayland-server.h> #include <wayland-server.h>
@ -33,36 +34,19 @@
static FILE *weston_logfile = NULL; static FILE *weston_logfile = NULL;
static char cached_timestamp[21];
static int cached_tv_sec = 0;
static int weston_log_timestamp(void) static int weston_log_timestamp(void)
{ {
struct timespec tp; struct timeval tv;
unsigned int time; struct tm *brokendown_time;
char string[128];
clock_gettime(CLOCK_REALTIME, &tp); gettimeofday(&tv, NULL);
time = (tp.tv_sec * 1000000L) + (tp.tv_nsec / 1000);
if (cached_tv_sec != tp.tv_sec) { brokendown_time = localtime(&tv.tv_sec);
char string[26];
struct tm *lt = localtime(&tp.tv_sec);
cached_tv_sec = tp.tv_sec; strftime(string, sizeof string, "%Y-%m-%d %H:%M:%S", brokendown_time);
strftime (string,24,"%Y-%m-%d %H:%M:%S", lt); return fprintf(weston_logfile, "[%s.%03li] ", string, tv.tv_usec/1000);
strncpy (cached_timestamp, string, 20);
}
return fprintf(weston_logfile, "[%s.%03u] ", cached_timestamp, tp.tv_nsec/1000000);
}
static int weston_log_print(const char *fmt, va_list arg)
{
int l;
l = vfprintf(weston_logfile, fmt, arg);
fflush(weston_logfile);
return l;
} }
static void static void
@ -70,7 +54,7 @@ custom_handler(const char *fmt, va_list arg)
{ {
weston_log_timestamp(); weston_log_timestamp();
fprintf(weston_logfile, "libwayland: "); fprintf(weston_logfile, "libwayland: ");
weston_log_print(fmt, arg); vfprintf(weston_logfile, fmt, arg);
} }
void void
@ -78,15 +62,19 @@ weston_log_file_open(const char *filename)
{ {
wl_log_set_handler_server(custom_handler); wl_log_set_handler_server(custom_handler);
weston_logfile = fopen(filename, "a"); if (filename != NULL)
weston_logfile = fopen(filename, "a");
if (weston_logfile == NULL) if (weston_logfile == NULL)
weston_logfile = stderr; weston_logfile = stderr;
else
setvbuf(weston_logfile, NULL, _IOLBF, 256);
} }
void void
weston_log_file_close() weston_log_file_close()
{ {
if (weston_logfile != stderr) if ((weston_logfile != stderr) && (weston_logfile != NULL))
fclose(weston_logfile); fclose(weston_logfile);
weston_logfile = stderr; weston_logfile = stderr;
} }
@ -98,7 +86,7 @@ weston_log(const char *fmt, ...)
va_list argp; va_list argp;
va_start(argp, fmt); va_start(argp, fmt);
l = weston_log_timestamp(); l = weston_log_timestamp();
l += weston_log_print(fmt, argp); l += vfprintf(weston_logfile, fmt, argp);
va_end(argp); va_end(argp);
return l; return l;
} }
@ -109,7 +97,7 @@ weston_log_continue(const char *fmt, ...)
int l; int l;
va_list argp; va_list argp;
va_start(argp, fmt); va_start(argp, fmt);
l = weston_log_print(fmt, argp); l = vfprintf(weston_logfile, fmt, argp);
va_end(argp); va_end(argp);
return l; return l;
} }

View File

@ -20,8 +20,8 @@
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/ */
#ifndef _WAYLAND_SYSTEM_LOG_H_ #ifndef _WESTON_LOG_H_
#define _WAYLAND_SYSTEM_LOG_H_ #define _WESTON_LOG_H_
void weston_log_file_open(const char *filename); void weston_log_file_open(const char *filename);
void weston_log_file_close(void); void weston_log_file_close(void);