[PATCH] Wayland logging functionality

Martin Minarik minarik11 at student.fiit.stuba.sk
Tue May 22 04:20:12 PDT 2012


The core libwayland library should not handle logging itself. Instead,
it returns the error in the API for anybody to subscribe to. The logging
itself happens in weston or wayland demo applications.

The weston and other users of libwayland API can also create their own
custom message handlers and send their own messages to them.
---
 src/wayland-server.c |    2 ++
 src/wayland-util.c   |   39 +++++++++++++++++++++++++++++++++++++++
 src/wayland-util.h   |   16 ++++++++++++++++
 3 files changed, 57 insertions(+), 0 deletions(-)

diff --git a/src/wayland-server.c b/src/wayland-server.c
index c1be56a..c846464 100644
--- a/src/wayland-server.c
+++ b/src/wayland-server.c
@@ -880,6 +880,8 @@ wl_display_create(void)
 	if (debug)
 		wl_debug = 1;
 
+	wl_log_set_default(wl_log_printf_handler);
+
 	display = malloc(sizeof *display);
 	if (display == NULL)
 		return NULL;
diff --git a/src/wayland-util.c b/src/wayland-util.c
index 7bf8924..afba3d4 100644
--- a/src/wayland-util.c
+++ b/src/wayland-util.c
@@ -24,6 +24,7 @@
 #include <stdlib.h>
 #include <stdint.h>
 #include <string.h>
+#include <stdarg.h>
 
 #include "wayland-util.h"
 #include "wayland-private.h"
@@ -270,3 +271,41 @@ wl_map_for_each(struct wl_map *map, wl_iterator_func_t func, void *data)
 	for_each_helper(&map->client_entries, func, data);
 	for_each_helper(&map->server_entries, func, data);
 }
+
+/* The pointer to default log handler */
+static wl_log_func_t wl_log_default_handler;
+
+WL_EXPORT void
+wl_log_printf_handler(const char *message, void * args)
+{
+	va_list *argp = args;
+	vprintf(message, *argp);
+}
+
+WL_EXPORT void
+wl_log_null_handler(const char *message, void * args){}
+
+WL_EXPORT void
+wl_log_set_default(wl_log_func_t handler)
+{
+	wl_log_default_handler = handler;
+}
+
+WL_EXPORT
+void wl_log_to(wl_log_func_t * handler, const char *message, ...)
+{
+	va_list argp;
+	va_start(argp, message);
+	(*handler)(message, &argp);
+	va_end(argp);
+}
+
+WL_EXPORT void
+wl_log(const char *message, ...)
+{
+	va_list argp;
+	va_start(argp, message);
+	wl_log_default_handler(message, &argp);
+	va_end(argp);
+}
+
diff --git a/src/wayland-util.h b/src/wayland-util.h
index fa5c6c5..1cb4bfd 100644
--- a/src/wayland-util.h
+++ b/src/wayland-util.h
@@ -203,6 +203,22 @@ static inline wl_fixed_t wl_fixed_from_int(int i)
 	return i * 256;
 }
 
+typedef void (*wl_log_func_t)(const char *, void * args);
+
+/* Log to default handler */
+void wl_log(const char *message, ...);
+#define WL_PRINTF(...) wl_log(__VA_ARGS__)
+
+/* Log to handler */
+void wl_log_to(wl_log_func_t * handler, const char *message, ...);
+#define WL_FPRINTF(...) wl_log_to(__VA_ARGS__)
+
+/* Implementations of log message handling */
+void wl_log_printf_handler(const char *message, void * args);
+void wl_log_null_handler(const char *message, void * args);
+
+void wl_log_set_default(wl_log_func_t handler);
+
 #ifdef  __cplusplus
 }
 #endif
-- 
1.7.5.4



More information about the wayland-devel mailing list