allow compositors to define the logging behavior

Signed-off-by: Giulio Camuffo <giuliocamuffo@gmail.com>
Reviewed-by: Quentin Glidic <sardemff7+git@sardemff7.net>
Reviewed-by: Pekka Paalanen <pekka.paalanen@collabora.co.uk>
dev
Giulio Camuffo 9 years ago committed by Pekka Paalanen
parent 8aedf7b11e
commit be2b11a7c0
  1. 5
      src/compositor.h
  2. 87
      src/log.c
  3. 80
      src/main.c

@ -1545,10 +1545,9 @@ weston_compositor_xkb_destroy(struct weston_compositor *ec);
/* String literal of spaces, the same width as the timestamp. */ /* String literal of spaces, the same width as the timestamp. */
#define STAMP_SPACE " " #define STAMP_SPACE " "
typedef int (*log_func_t)(const char *fmt, va_list ap);
void void
weston_log_file_open(const char *filename); weston_log_set_handler(log_func_t log, log_func_t cont);
void
weston_log_file_close(void);
int int
weston_vlog(const char *fmt, va_list ap); weston_vlog(const char *fmt, va_list ap);
int int

@ -36,78 +36,33 @@
#include "compositor.h" #include "compositor.h"
#include "os-compatibility.h" static log_func_t log_handler = 0;
static log_func_t log_continue_handler = 0;
static FILE *weston_logfile = NULL; /** Install the log handler
*
static int cached_tm_mday = -1; * The given functions will be called to output text as passed to the
* \a weston_log and \a weston_log_continue functions.
static int weston_log_timestamp(void) *
{ * \param log The log function. This function will be called when
struct timeval tv; * \a weston_log is called, and should begin a new line,
struct tm *brokendown_time; * with user defined line headers, if any.
char string[128]; * \param cont The continue log function. This function will be called
* when \a weston_log_continue is called, and should append
gettimeofday(&tv, NULL); * its output to the current line, without any header or
* other content in between.
brokendown_time = localtime(&tv.tv_sec); */
if (brokendown_time == NULL) WL_EXPORT void
return fprintf(weston_logfile, "[(NULL)localtime] "); weston_log_set_handler(log_func_t log, log_func_t cont)
if (brokendown_time->tm_mday != cached_tm_mday) {
strftime(string, sizeof string, "%Y-%m-%d %Z", brokendown_time);
fprintf(weston_logfile, "Date: %s\n", string);
cached_tm_mday = brokendown_time->tm_mday;
}
strftime(string, sizeof string, "%H:%M:%S", brokendown_time);
return fprintf(weston_logfile, "[%s.%03li] ", string, tv.tv_usec/1000);
}
static void
custom_handler(const char *fmt, va_list arg)
{
weston_log_timestamp();
fprintf(weston_logfile, "libwayland: ");
vfprintf(weston_logfile, fmt, arg);
}
void
weston_log_file_open(const char *filename)
{
wl_log_set_handler_server(custom_handler);
if (filename != NULL) {
weston_logfile = fopen(filename, "a");
if (weston_logfile)
os_fd_set_cloexec(fileno(weston_logfile));
}
if (weston_logfile == NULL)
weston_logfile = stderr;
else
setvbuf(weston_logfile, NULL, _IOLBF, 256);
}
void
weston_log_file_close()
{ {
if ((weston_logfile != stderr) && (weston_logfile != NULL)) log_handler = log;
fclose(weston_logfile); log_continue_handler = cont;
weston_logfile = stderr;
} }
WL_EXPORT int WL_EXPORT int
weston_vlog(const char *fmt, va_list ap) weston_vlog(const char *fmt, va_list ap)
{ {
int l; return log_handler(fmt, ap);
l = weston_log_timestamp();
l += vfprintf(weston_logfile, fmt, ap);
return l;
} }
WL_EXPORT int WL_EXPORT int
@ -126,7 +81,7 @@ weston_log(const char *fmt, ...)
WL_EXPORT int WL_EXPORT int
weston_vlog_continue(const char *fmt, va_list argp) weston_vlog_continue(const char *fmt, va_list argp)
{ {
return vfprintf(weston_logfile, fmt, argp); return log_continue_handler(fmt, argp);
} }
WL_EXPORT int WL_EXPORT int

@ -39,6 +39,7 @@
#include <sys/wait.h> #include <sys/wait.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <libinput.h> #include <libinput.h>
#include <sys/time.h>
#ifdef HAVE_LIBUNWIND #ifdef HAVE_LIBUNWIND
#define UNW_LOCAL_ONLY #define UNW_LOCAL_ONLY
@ -61,6 +62,84 @@
#define WINDOW_TITLE "Weston Compositor" #define WINDOW_TITLE "Weston Compositor"
static FILE *weston_logfile = NULL;
static int cached_tm_mday = -1;
static int weston_log_timestamp(void)
{
struct timeval tv;
struct tm *brokendown_time;
char string[128];
gettimeofday(&tv, NULL);
brokendown_time = localtime(&tv.tv_sec);
if (brokendown_time == NULL)
return fprintf(weston_logfile, "[(NULL)localtime] ");
if (brokendown_time->tm_mday != cached_tm_mday) {
strftime(string, sizeof string, "%Y-%m-%d %Z", brokendown_time);
fprintf(weston_logfile, "Date: %s\n", string);
cached_tm_mday = brokendown_time->tm_mday;
}
strftime(string, sizeof string, "%H:%M:%S", brokendown_time);
return fprintf(weston_logfile, "[%s.%03li] ", string, tv.tv_usec/1000);
}
static void
custom_handler(const char *fmt, va_list arg)
{
weston_log_timestamp();
fprintf(weston_logfile, "libwayland: ");
vfprintf(weston_logfile, fmt, arg);
}
static void
weston_log_file_open(const char *filename)
{
wl_log_set_handler_server(custom_handler);
if (filename != NULL) {
weston_logfile = fopen(filename, "a");
if (weston_logfile)
os_fd_set_cloexec(fileno(weston_logfile));
}
if (weston_logfile == NULL)
weston_logfile = stderr;
else
setvbuf(weston_logfile, NULL, _IOLBF, 256);
}
static void
weston_log_file_close(void)
{
if ((weston_logfile != stderr) && (weston_logfile != NULL))
fclose(weston_logfile);
weston_logfile = stderr;
}
static int
vlog(const char *fmt, va_list ap)
{
int l;
l = weston_log_timestamp();
l += vfprintf(weston_logfile, fmt, ap);
return l;
}
static int
vlog_continue(const char *fmt, va_list argp)
{
return vfprintf(weston_logfile, fmt, argp);
}
static struct wl_list child_process_list; static struct wl_list child_process_list;
static struct weston_compositor *segv_compositor; static struct weston_compositor *segv_compositor;
@ -1482,6 +1561,7 @@ int main(int argc, char *argv[])
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
weston_log_set_handler(vlog, vlog_continue);
weston_log_file_open(log); weston_log_file_open(log);
weston_log("%s\n" weston_log("%s\n"

Loading…
Cancel
Save