[PATCH wayland 3/3] Add a debug handler and use it to print out protocol debug messages

Jason Ekstrand jason at jlekstrand.net
Wed Dec 18 18:56:20 PST 2013


In order to keep from overloading the debug handler, we first squash the
entire message into a string and call wl_debug once.

Signed-off-by: Jason Ekstrand <jason at jlekstrand.net>
---
 src/connection.c      | 54 ++++++++++++++++++++++++++++-----------------------
 src/wayland-client.c  |  6 ++++++
 src/wayland-client.h  |  1 +
 src/wayland-private.h |  2 ++
 src/wayland-server.c  |  6 ++++++
 src/wayland-server.h  |  1 +
 src/wayland-util.c    | 19 ++++++++++++++++++
 7 files changed, 65 insertions(+), 24 deletions(-)

diff --git a/src/connection.c b/src/connection.c
index 1d8b61b..b7f02b4 100644
--- a/src/connection.c
+++ b/src/connection.c
@@ -1088,67 +1088,73 @@ void
 wl_closure_print(struct wl_closure *closure, struct wl_object *target, int send)
 {
 	int i;
+	struct wl_array msg;
 	struct argument_details arg;
 	const char *signature = closure->message->signature;
 	struct timespec tp;
 	unsigned int time;
 
+	wl_array_init(&msg);
+	wl_array_add(&msg, 128); /* This should be big enough for most cases */
+	msg.size = 0;
+
 	clock_gettime(CLOCK_REALTIME, &tp);
 	time = (tp.tv_sec * 1000000L) + (tp.tv_nsec / 1000);
 
-	fprintf(stderr, "[%10.3f] %s%s@%u.%s(",
-		time / 1000.0,
-		send ? " -> " : "",
-		target->interface->name, target->id,
-		closure->message->name);
+	wl_array_printf(&msg, "[%10.3f] %s%s@%u.%s(",
+			time / 1000.0,
+			send ? " -> " : "",
+			target->interface->name, target->id,
+			closure->message->name);
 
 	for (i = 0; i < closure->count; i++) {
 		signature = get_next_argument(signature, &arg);
 		if (i > 0)
-			fprintf(stderr, ", ");
+			wl_array_printf(&msg, ", ");
 
 		switch (arg.type) {
 		case 'u':
-			fprintf(stderr, "%u", closure->args[i].u);
+			wl_array_printf(&msg, "%u", closure->args[i].u);
 			break;
 		case 'i':
-			fprintf(stderr, "%d", closure->args[i].i);
+			wl_array_printf(&msg, "%d", closure->args[i].i);
 			break;
 		case 'f':
-			fprintf(stderr, "%f",
-				wl_fixed_to_double(closure->args[i].f));
+			wl_array_printf(&msg, "%f",
+					wl_fixed_to_double(closure->args[i].f));
 			break;
 		case 's':
-			fprintf(stderr, "\"%s\"", closure->args[i].s);
+			wl_array_printf(&msg, "\"%s\"", closure->args[i].s);
 			break;
 		case 'o':
 			if (closure->args[i].o)
-				fprintf(stderr, "%s@%u",
-					closure->args[i].o->interface->name,
-					closure->args[i].o->id);
+				wl_array_printf(&msg, "%s@%u",
+						closure->args[i].o->interface->name,
+						closure->args[i].o->id);
 			else
-				fprintf(stderr, "nil");
+				wl_array_printf(&msg, "nil");
 			break;
 		case 'n':
-			fprintf(stderr, "new id %s@",
-				(closure->message->types[i]) ?
-				 closure->message->types[i]->name :
-				  "[unknown]");
+			wl_array_printf(&msg, "new id %s@",
+					(closure->message->types[i]) ?
+					 closure->message->types[i]->name :
+					 "[unknown]");
 			if (closure->args[i].n != 0)
-				fprintf(stderr, "%u", closure->args[i].n);
+				wl_array_printf(&msg, "%u", closure->args[i].n);
 			else
-				fprintf(stderr, "nil");
+				wl_array_printf(&msg, "nil");
 			break;
 		case 'a':
-			fprintf(stderr, "array");
+			wl_array_printf(&msg, "array");
 			break;
 		case 'h':
-			fprintf(stderr, "fd %d", closure->args[i].h);
+			wl_array_printf(&msg, "fd %d", closure->args[i].h);
 			break;
 		}
 	}
 
-	fprintf(stderr, ")\n");
+	wl_debug("%s)\n", msg.data);
+	wl_array_release(&msg);
 }
 
 void
diff --git a/src/wayland-client.c b/src/wayland-client.c
index 799ebab..19d31fc 100644
--- a/src/wayland-client.c
+++ b/src/wayland-client.c
@@ -1587,3 +1587,9 @@ wl_log_set_handler_client(wl_log_func_t handler)
 {
 	wl_log_handler = handler;
 }
+
+WL_EXPORT void
+wl_debug_set_handler_client(wl_log_func_t handler)
+{
+	wl_debug_handler = handler;
+}
diff --git a/src/wayland-client.h b/src/wayland-client.h
index 2a32785..59c06e0 100644
--- a/src/wayland-client.h
+++ b/src/wayland-client.h
@@ -173,6 +173,7 @@ void wl_display_cancel_read(struct wl_display *display);
 int wl_display_read_events(struct wl_display *display);
 
 void wl_log_set_handler_client(wl_log_func_t handler);
+void wl_debug_set_handler_client(wl_log_func_t handler);
 
 #ifdef  __cplusplus
 }
diff --git a/src/wayland-private.h b/src/wayland-private.h
index 67e8783..a74b77c 100644
--- a/src/wayland-private.h
+++ b/src/wayland-private.h
@@ -165,8 +165,10 @@ void
 wl_closure_destroy(struct wl_closure *closure);
 
 extern wl_log_func_t wl_log_handler;
+extern wl_log_func_t wl_debug_handler;
 
 void wl_log(const char *fmt, ...);
+void wl_debug(const char *fmt, ...);
 
 struct wl_display;
 
diff --git a/src/wayland-server.c b/src/wayland-server.c
index e03316c..864d136 100644
--- a/src/wayland-server.c
+++ b/src/wayland-server.c
@@ -1216,6 +1216,12 @@ wl_log_set_handler_server(wl_log_func_t handler)
 	wl_log_handler = handler;
 }
 
+WL_EXPORT void
+wl_debug_set_handler_server(wl_log_func_t handler)
+{
+	wl_debug_handler = handler;
+}
+
 /* Deprecated functions below. */
 
 uint32_t
diff --git a/src/wayland-server.h b/src/wayland-server.h
index f5427fd..6fe2698 100644
--- a/src/wayland-server.h
+++ b/src/wayland-server.h
@@ -448,6 +448,7 @@ wl_shm_buffer_create(struct wl_client *client,
 		     int32_t stride, uint32_t format);
 
 void wl_log_set_handler_server(wl_log_func_t handler);
+void wl_debug_set_handler_server(wl_log_func_t handler);
 
 #ifdef  __cplusplus
 }
diff --git a/src/wayland-util.c b/src/wayland-util.c
index c9d6d0e..fe78a94 100644
--- a/src/wayland-util.c
+++ b/src/wayland-util.c
@@ -23,6 +23,7 @@
 
 #include <stdlib.h>
 #include <stdint.h>
+#include <stdio.h>
 #include <string.h>
 #include <stdarg.h>
 #include <errno.h>
@@ -402,6 +403,12 @@ wl_map_for_each(struct wl_map *map, wl_iterator_func_t func, void *data)
 }
 
 static void
+wl_log_stderr_handler(const char *fmt, va_list arg)
+{
+	vfprintf(stderr, fmt, arg);
+}
+
+static void
 wl_log_noop_handler(const char *fmt, va_list arg)
 {
 }
@@ -417,3 +424,15 @@ wl_log(const char *fmt, ...)
 	wl_log_handler(fmt, argp);
 	va_end(argp);
 }
+
+wl_log_func_t wl_debug_handler = wl_log_stderr_handler;
+
+void
+wl_debug(const char *fmt, ...)
+{
+	va_list argp;
+
+	va_start(argp, fmt);
+	wl_debug_handler(fmt, argp);
+	va_end(argp);
+}
-- 
1.8.4.2



More information about the wayland-devel mailing list