[PATCH weston v2 6/6] Add API to install protocol loggers on the server wl_display
Giulio Camuffo
giuliocamuffo at gmail.com
Tue Jul 5 07:51:11 UTC 2016
The new wl_display_add_protocol_logger allows to set a function as
a logger, which will get called when a new request is received or an
event is sent.
This is akin to setting WAYLAND_DEBUG=1, but more powerful because it
can be enabled at run time and allows to show the log e.g. in a UI view.
A test is added for the new functionality.
Signed-off-by: Giulio Camuffo <giulio.camuffo at kdab.com>
---
v2: - return wl_protocol_logger* when adding a logger, and use that to remove it
- add a test
- rename the enum to _REQUEST and _EVENT
Makefile.am | 3 ++
src/wayland-server-core.h | 24 ++++++++++++
src/wayland-server.c | 99 ++++++++++++++++++++++++++++++++++++++++++++---
3 files changed, 120 insertions(+), 6 deletions(-)
diff --git a/Makefile.am b/Makefile.am
index e684a87..07042f6 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -162,6 +162,7 @@ TESTS = \
message-test \
headers-test \
compositor-introspection-test
+ protocol-logger-test
if ENABLE_CPP_TEST
TESTS += cpp-compile-test
@@ -220,6 +221,8 @@ message_test_SOURCES = tests/message-test.c
message_test_LDADD = libtest-runner.la
compositor_introspection_test_SOURCES = tests/compositor-introspection-test.c
compositor_introspection_test_LDADD = libtest-runner.la
+protocol_logger_test_SOURCES = tests/protocol-logger-test.c
+protocol_logger_test_LDADD = libtest-runner.la
headers_test_SOURCES = tests/headers-test.c \
tests/headers-protocol-test.c \
tests/headers-protocol-core-test.c
diff --git a/src/wayland-server-core.h b/src/wayland-server-core.h
index 647d97c..1a4ed4b 100644
--- a/src/wayland-server-core.h
+++ b/src/wayland-server-core.h
@@ -521,6 +521,30 @@ wl_shm_buffer_create(struct wl_client *client,
void
wl_log_set_handler_server(wl_log_func_t handler);
+enum wl_protocol_logger_direction {
+ WL_PROTOCOL_LOGGER_REQUEST,
+ WL_PROTOCOL_LOGGER_EVENT,
+};
+
+struct wl_log_message {
+ struct wl_resource *resource;
+ int message_opcode;
+ const struct wl_message *message;
+ int arguments_count;
+ const union wl_argument *arguments;
+};
+
+typedef void (*wl_protocol_logger_func_t)(void *user_data,
+ enum wl_protocol_logger_direction direction,
+ const struct wl_log_message *message);
+struct wl_protocol_logger;
+struct wl_protocol_logger *
+wl_display_add_protocol_logger(struct wl_display *display,
+ wl_protocol_logger_func_t, void *user_data);
+
+void
+wl_display_remove_protocol_logger(struct wl_protocol_logger *logger);
+
#ifdef __cplusplus
}
#endif
diff --git a/src/wayland-server.c b/src/wayland-server.c
index 98e3bda..c26ab05 100644
--- a/src/wayland-server.c
+++ b/src/wayland-server.c
@@ -95,6 +95,7 @@ struct wl_display {
struct wl_list global_list;
struct wl_list socket_list;
struct wl_list client_list;
+ struct wl_list protocol_loggers;
struct wl_signal destroy_signal;
struct wl_signal create_client_signal;
@@ -123,8 +124,42 @@ struct wl_resource {
wl_dispatcher_func_t dispatcher;
};
+struct wl_protocol_logger {
+ struct wl_list link;
+ wl_protocol_logger_func_t func;
+ void *user_data;
+};
+
static int debug_server = 0;
+static void
+closure_log(struct wl_resource *resource,
+ struct wl_closure *closure, int send)
+{
+ struct wl_object *object = &resource->object;
+ struct wl_display *display = resource->client->display;
+ struct wl_protocol_logger *protocol_logger;
+ struct wl_log_message message;
+
+ if (debug_server)
+ wl_closure_print(closure, object, send);
+
+ if (!wl_list_empty(&display->protocol_loggers)) {
+ message.resource = resource;
+ message.message_opcode = closure->opcode;
+ message.message = closure->message;
+ message.arguments_count = closure->count;
+ message.arguments = closure->args;
+ wl_list_for_each(protocol_logger,
+ &display->protocol_loggers, link) {
+ protocol_logger->func(protocol_logger->user_data,
+ send ? WL_PROTOCOL_LOGGER_EVENT :
+ WL_PROTOCOL_LOGGER_REQUEST,
+ &message);
+ }
+ }
+}
+
WL_EXPORT void
wl_resource_post_event_array(struct wl_resource *resource, uint32_t opcode,
union wl_argument *args)
@@ -143,8 +178,7 @@ wl_resource_post_event_array(struct wl_resource *resource, uint32_t opcode,
if (wl_closure_send(closure, resource->client->connection))
resource->client->error = 1;
- if (debug_server)
- wl_closure_print(closure, object, true);
+ closure_log(resource, closure, true);
wl_closure_destroy(closure);
}
@@ -183,8 +217,7 @@ wl_resource_queue_event_array(struct wl_resource *resource, uint32_t opcode,
if (wl_closure_queue(closure, resource->client->connection))
resource->client->error = 1;
- if (debug_server)
- wl_closure_print(closure, object, true);
+ closure_log(resource, closure, true);
wl_closure_destroy(closure);
}
@@ -331,8 +364,7 @@ wl_client_connection_data(int fd, uint32_t mask, void *data)
break;
}
- if (debug_server)
- wl_closure_print(closure, object, false);
+ closure_log(resource, closure, false);
if ((resource_flags & WL_MAP_ENTRY_LEGACY) ||
resource->dispatcher == NULL) {
@@ -884,6 +916,7 @@ wl_display_create(void)
wl_list_init(&display->socket_list);
wl_list_init(&display->client_list);
wl_list_init(&display->registry_resource_list);
+ wl_list_init(&display->protocol_loggers);
wl_signal_init(&display->destroy_signal);
wl_signal_init(&display->create_client_signal);
@@ -960,6 +993,8 @@ wl_display_destroy(struct wl_display *display)
wl_array_release(&display->additional_shm_formats);
+ wl_list_remove(&display->protocol_loggers);
+
free(display);
}
@@ -1480,6 +1515,58 @@ wl_log_set_handler_server(wl_log_func_t handler)
wl_log_handler = handler;
}
+/** Adds a new protocol logger.
+ *
+ * When a new protocol message arrives or is sent from the server
+ * all the protocol logger functions will be called, carrying the
+ * \a user_data pointer, the direction of the message (incoming or
+ * outgoing) and the actual message.
+ *
+ * \param func The function to call to log a new protocol message
+ * \param user_data The user data pointer to pass to \a func
+ *
+ * \return The protol logger object on success, NULL on failure.
+ *
+ * \sa wl_display_remove_protocol_logger
+ *
+ * \memberof wl_display
+ */
+WL_EXPORT struct wl_protocol_logger *
+wl_display_add_protocol_logger(struct wl_display *display,
+ wl_protocol_logger_func_t func, void *user_data)
+{
+ struct wl_protocol_logger *logger;
+
+ logger = malloc(sizeof *logger);
+ if (!logger)
+ return NULL;
+
+ logger->func = func;
+ logger->user_data = user_data;
+ wl_list_init(&logger->link);
+ wl_list_insert(&display->protocol_loggers, &logger->link);
+
+ return logger;
+}
+
+/** Removes a protocol logger.
+ *
+ * If a protocol logger was previously added with the same \a func and
+ * \a user_data, it will be removed from the display's list so that it
+ * will not be invoked anymore.
+ * The \a logger object becomes invalid after calling this function.
+ *
+ * \sa wl_display_add_protocol_logger
+ *
+ * \memberof wl_display
+ */
+WL_EXPORT void
+wl_display_remove_protocol_logger(struct wl_protocol_logger *logger)
+{
+ wl_list_remove(&logger->link);
+ free(logger);
+}
+
/** Add support for a wl_shm pixel format
*
* \param display The display object
--
2.9.0
More information about the wayland-devel
mailing list