[PATCH weston 07/15] libweston: allow compositors to define the logging behavior

Giulio Camuffo giuliocamuffo at gmail.com
Thu Nov 6 12:41:26 PST 2014


---
 Makefile.am      |  4 +--
 src/compositor.h |  5 ++--
 src/log.c        | 69 ++++++-------------------------------------------
 src/weston.c     | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 90 insertions(+), 66 deletions(-)

diff --git a/Makefile.am b/Makefile.am
index 0e5e88d..1d7ee62 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -46,6 +46,7 @@ libweston_la_LIBADD = $(COMPOSITOR_LIBS) $(LIBUNWIND_LIBS) \
 
 libweston_la_SOURCES =           			\
 	src/git-version.h				\
+	src/log.c					\
 	src/compositor.c				\
 	src/compositor.h				\
 	src/input.c					\
@@ -91,8 +92,7 @@ weston_LDADD = $(COMPOSITOR_LIBS) $(LIBUNWIND_LIBS) \
 	$(DLOPEN_LIBS) -lm libshared.la libweston.la
 
 weston_SOURCES =					\
-	src/weston.c					\
-	src/log.c
+	src/weston.c
 
 # Track this dependency explicitly instead of using BUILT_SOURCES.  We
 # add BUILT_SOURCES to CLEANFILES, but we want to keep git-version.h
diff --git a/src/compositor.h b/src/compositor.h
index 3fc8eaa..e3fcbaf 100644
--- a/src/compositor.h
+++ b/src/compositor.h
@@ -1320,10 +1320,9 @@ weston_compositor_xkb_destroy(struct weston_compositor *ec);
 /* String literal of spaces, the same width as the timestamp. */
 #define STAMP_SPACE "               "
 
+typedef int (*log_func_t)(const char *fmt, va_list ap);
 void
-weston_log_file_open(const char *filename);
-void
-weston_log_file_close(void);
+weston_log_set_handler(log_func_t log, log_func_t cont);
 int
 weston_vlog(const char *fmt, va_list ap);
 int
diff --git a/src/log.c b/src/log.c
index 99bbe18..d74e14b 100644
--- a/src/log.c
+++ b/src/log.c
@@ -33,73 +33,20 @@
 
 #include "compositor.h"
 
-static FILE *weston_logfile = NULL;
+static log_func_t log_handler = 0;
+static log_func_t log_continue_handler = 0;
 
-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);
-}
-
-void
-weston_log_file_open(const char *filename)
+WL_EXPORT void
+weston_log_set_handler(log_func_t log, log_func_t cont)
 {
-	wl_log_set_handler_server(custom_handler);
-
-	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) && (weston_logfile != NULL))
-		fclose(weston_logfile);
-	weston_logfile = stderr;
+	log_handler = log;
+	log_continue_handler = cont;
 }
 
 WL_EXPORT int
 weston_vlog(const char *fmt, va_list ap)
 {
-	int l;
-
-	l = weston_log_timestamp();
-	l += vfprintf(weston_logfile, fmt, ap);
-
-	return l;
+	return log_handler(fmt, ap);
 }
 
 WL_EXPORT int
@@ -118,7 +65,7 @@ weston_log(const char *fmt, ...)
 WL_EXPORT int
 weston_vlog_continue(const char *fmt, va_list argp)
 {
-	return vfprintf(weston_logfile, fmt, argp);
+       return log_continue_handler(fmt, argp);
 }
 
 WL_EXPORT int
diff --git a/src/weston.c b/src/weston.c
index 5d19efd..9a42cac 100644
--- a/src/weston.c
+++ b/src/weston.c
@@ -32,6 +32,7 @@
 #include <sys/utsname.h>
 #include <sys/stat.h>
 #include <sys/wait.h>
+#include <sys/time.h>
 
 #ifdef HAVE_LIBUNWIND
 #define UNW_LOCAL_ONLY
@@ -74,6 +75,82 @@ sigchld_handler(int signal_number, void *data)
 	return 1;
 }
 
+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 == 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);
+}
+
+
 #ifdef HAVE_LIBUNWIND
 
 static void
@@ -549,6 +626,7 @@ int main(int argc, char *argv[])
 		return EXIT_SUCCESS;
 	}
 
+	weston_log_set_handler(vlog, vlog_continue);
 	weston_log_file_open(log);
 
 	weston_log("%s\n"
-- 
2.1.3



More information about the wayland-devel mailing list