From 1ded661aac2c9d452af9eb31dda142b451e93cea Mon Sep 17 00:00:00 2001 From: Leandro Ribeiro Date: Thu, 6 Feb 2020 16:43:51 -0300 Subject: [PATCH] weston-log: merge functions that destroy different types of subscribers Log subscriber API is not type-safe. File and flight recorder subscribers are created with functions that return weston_log_subscriber objects. But there's a problem: to destroy these objects you have to call the right function for each type of subscriber, and a user calling the wrong destroy function wouldn't get a warning. Merge functions that destroy different types of subscribers, making the log subscriber API type-safe. Signed-off-by: Leandro Ribeiro --- compositor/main.c | 4 ++-- include/libweston/weston-log.h | 9 +++----- libweston/weston-log-file.c | 24 ++++++++----------- libweston/weston-log-flight-rec.c | 38 +++++++++++++------------------ libweston/weston-log-internal.h | 3 ++- libweston/weston-log-wayland.c | 1 + libweston/weston-log.c | 15 ++++++++++++ 7 files changed, 49 insertions(+), 45 deletions(-) diff --git a/compositor/main.c b/compositor/main.c index 2e1a9356..0a8dbe84 100644 --- a/compositor/main.c +++ b/compositor/main.c @@ -3381,8 +3381,8 @@ out: weston_log_scope_destroy(log_scope); log_scope = NULL; weston_log_ctx_destroy(log_ctx); - weston_log_subscriber_destroy_log(logger); - weston_log_subscriber_destroy_flight_rec(flight_rec); + weston_log_subscriber_destroy(logger); + weston_log_subscriber_destroy(flight_rec); out_signals: for (i = ARRAY_LENGTH(signals) - 1; i >= 0; i--) diff --git a/include/libweston/weston-log.h b/include/libweston/weston-log.h index 70f6d347..aeb7768b 100644 --- a/include/libweston/weston-log.h +++ b/include/libweston/weston-log.h @@ -110,6 +110,9 @@ char * weston_log_scope_timestamp(struct weston_log_scope *scope, char *buf, size_t len); +void +weston_log_subscriber_destroy(struct weston_log_subscriber *subscriber); + void weston_log_subscribe(struct weston_log_context *log_ctx, struct weston_log_subscriber *subscriber, @@ -118,15 +121,9 @@ weston_log_subscribe(struct weston_log_context *log_ctx, struct weston_log_subscriber * weston_log_subscriber_create_log(FILE *dump_to); -void -weston_log_subscriber_destroy_log(struct weston_log_subscriber *sub); - struct weston_log_subscriber * weston_log_subscriber_create_flight_rec(size_t size); -void -weston_log_subscriber_destroy_flight_rec(struct weston_log_subscriber *sub); - void weston_log_subscriber_display_flight_rec(struct weston_log_subscriber *sub); diff --git a/libweston/weston-log-file.c b/libweston/weston-log-file.c index 219a15dd..0f8f4f20 100644 --- a/libweston/weston-log-file.c +++ b/libweston/weston-log-file.c @@ -54,14 +54,21 @@ weston_log_file_write(struct weston_log_subscriber *sub, fwrite(data, len, 1, stream->file); } +static void +weston_log_subscriber_destroy_log(struct weston_log_subscriber *subscriber) +{ + struct weston_debug_log_file *file = to_weston_debug_log_file(subscriber); + free(file); +} + /** Creates a file type of subscriber * - * Should be destroyed using weston_log_subscriber_destroy_log() + * Should be destroyed using weston_log_subscriber_destroy() * * @param dump_to if specified, used for writing data to * @returns a weston_log_subscriber object or NULL in case of failure * - * @sa weston_log_subscriber_destroy_log + * @sa weston_log_subscriber_destroy * */ WL_EXPORT struct weston_log_subscriber * @@ -79,6 +86,7 @@ weston_log_subscriber_create_log(FILE *dump_to) file->base.write = weston_log_file_write; + file->base.destroy = weston_log_subscriber_destroy_log; file->base.destroy_subscription = NULL; file->base.complete = NULL; @@ -86,15 +94,3 @@ weston_log_subscriber_create_log(FILE *dump_to) return &file->base; } - -/** Destroy the subscriber created with weston_log_subscriber_create_log - * - * @param subscriber the weston_log_subscriber object to destroy - * - */ -WL_EXPORT void -weston_log_subscriber_destroy_log(struct weston_log_subscriber *subscriber) -{ - struct weston_debug_log_file *file = to_weston_debug_log_file(subscriber); - free(file); -} diff --git a/libweston/weston-log-flight-rec.c b/libweston/weston-log-flight-rec.c index 7243498e..36323f73 100644 --- a/libweston/weston-log-flight-rec.c +++ b/libweston/weston-log-flight-rec.c @@ -218,10 +218,24 @@ weston_log_subscriber_display_flight_rec(struct weston_log_subscriber *sub) weston_log_subscriber_display_flight_rec_data(rb, rb->file); } +static void +weston_log_subscriber_destroy_flight_rec(struct weston_log_subscriber *sub) +{ + struct weston_debug_log_flight_recorder *flight_rec = to_flight_recorder(sub); + + /* Resets weston_primary_flight_recorder_ring_buffer to NULL if it + * is the destroyed subscriber */ + if (weston_primary_flight_recorder_ring_buffer == &flight_rec->rb) + weston_primary_flight_recorder_ring_buffer = NULL; + + free(flight_rec->rb.buf); + free(flight_rec); +} + /** Create a flight recorder type of subscriber * * Allocates both the flight recorder and the underlying ring buffer. Use - * weston_log_subscriber_destroy_flight_rec() to clean-up. + * weston_log_subscriber_destroy() to clean-up. * * @param size specify the maximum size (in bytes) of the backing storage * for the flight recorder @@ -241,6 +255,7 @@ weston_log_subscriber_create_flight_rec(size_t size) return NULL; flight_rec->base.write = weston_log_flight_recorder_write; + flight_rec->base.destroy = weston_log_subscriber_destroy_flight_rec; flight_rec->base.destroy_subscription = NULL; flight_rec->base.complete = NULL; wl_list_init(&flight_rec->base.subscription_list); @@ -260,27 +275,6 @@ weston_log_subscriber_create_flight_rec(size_t size) return &flight_rec->base; } -/** Destroys the weston_log_subscriber object created with - * weston_log_subscriber_create_flight_rec() - * - * @param sub the weston_log_subscriber object - * - * This also resets weston_primary_flight_recorder_ring_buffer to NULL if it - * is the destroyed subscriber. - */ -WL_EXPORT void -weston_log_subscriber_destroy_flight_rec(struct weston_log_subscriber *sub) -{ - struct weston_debug_log_flight_recorder *flight_rec; - - flight_rec = to_flight_recorder(sub); - if (weston_primary_flight_recorder_ring_buffer == &flight_rec->rb) - weston_primary_flight_recorder_ring_buffer = NULL; - - free(flight_rec->rb.buf); - free(flight_rec); -} - /** Retrieve flight recorder ring buffer contents, could be useful when * implementing an assert()-like wrapper. * diff --git a/libweston/weston-log-internal.h b/libweston/weston-log-internal.h index 95e6e653..fbf88e56 100644 --- a/libweston/weston-log-internal.h +++ b/libweston/weston-log-internal.h @@ -52,6 +52,8 @@ struct weston_log_subscription; struct weston_log_subscriber { /** write the data pointed by @param data */ void (*write)(struct weston_log_subscriber *sub, const char *data, size_t len); + /** For destroying the subscriber */ + void (*destroy)(struct weston_log_subscriber *sub); /** For the type of streams that required additional destroy operation * for destroying the stream */ void (*destroy_subscription)(struct weston_log_subscriber *sub); @@ -79,7 +81,6 @@ weston_log_subscription_add(struct weston_log_scope *scope, void weston_log_subscription_remove(struct weston_log_subscription *sub); - void weston_log_bind_weston_debug(struct wl_client *client, void *data, uint32_t version, uint32_t id); diff --git a/libweston/weston-log-wayland.c b/libweston/weston-log-wayland.c index 892b1631..732439f2 100644 --- a/libweston/weston-log-wayland.c +++ b/libweston/weston-log-wayland.c @@ -180,6 +180,7 @@ stream_create(struct weston_log_context *log_ctx, const char *name, stream->resource = stream_resource; stream->base.write = weston_log_debug_wayland_write; + stream->base.destroy = NULL; stream->base.destroy_subscription = weston_log_debug_wayland_to_destroy; stream->base.complete = weston_log_debug_wayland_complete; wl_list_init(&stream->base.subscription_list); diff --git a/libweston/weston-log.c b/libweston/weston-log.c index 6afda2c6..09717c48 100644 --- a/libweston/weston-log.c +++ b/libweston/weston-log.c @@ -939,6 +939,21 @@ weston_log_scope_timestamp(struct weston_log_scope *scope, return buf; } +/** Destroy a file type or a flight-rec type subscriber. + * + * They are created, respectively, with weston_log_subscriber_create_log() + * and weston_log_subscriber_create_flight_rec() + * + * @param subscriber the weston_log_subscriber object to destroy + * + * @ingroup log + */ +WL_EXPORT void +weston_log_subscriber_destroy(struct weston_log_subscriber *subscriber) +{ + subscriber->destroy(subscriber); +} + /** Subscribe to a scope * * Creates a subscription which is used to subscribe the \p subscriber