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 <leandrohr@riseup.net>
dev
Leandro Ribeiro 5 years ago committed by Pekka Paalanen
parent 8c02ea1069
commit 1ded661aac
  1. 4
      compositor/main.c
  2. 9
      include/libweston/weston-log.h
  3. 24
      libweston/weston-log-file.c
  4. 38
      libweston/weston-log-flight-rec.c
  5. 3
      libweston/weston-log-internal.h
  6. 1
      libweston/weston-log-wayland.c
  7. 15
      libweston/weston-log.c

@ -3381,8 +3381,8 @@ out:
weston_log_scope_destroy(log_scope); weston_log_scope_destroy(log_scope);
log_scope = NULL; log_scope = NULL;
weston_log_ctx_destroy(log_ctx); weston_log_ctx_destroy(log_ctx);
weston_log_subscriber_destroy_log(logger); weston_log_subscriber_destroy(logger);
weston_log_subscriber_destroy_flight_rec(flight_rec); weston_log_subscriber_destroy(flight_rec);
out_signals: out_signals:
for (i = ARRAY_LENGTH(signals) - 1; i >= 0; i--) for (i = ARRAY_LENGTH(signals) - 1; i >= 0; i--)

@ -110,6 +110,9 @@ char *
weston_log_scope_timestamp(struct weston_log_scope *scope, weston_log_scope_timestamp(struct weston_log_scope *scope,
char *buf, size_t len); char *buf, size_t len);
void
weston_log_subscriber_destroy(struct weston_log_subscriber *subscriber);
void void
weston_log_subscribe(struct weston_log_context *log_ctx, weston_log_subscribe(struct weston_log_context *log_ctx,
struct weston_log_subscriber *subscriber, struct weston_log_subscriber *subscriber,
@ -118,15 +121,9 @@ weston_log_subscribe(struct weston_log_context *log_ctx,
struct weston_log_subscriber * struct weston_log_subscriber *
weston_log_subscriber_create_log(FILE *dump_to); weston_log_subscriber_create_log(FILE *dump_to);
void
weston_log_subscriber_destroy_log(struct weston_log_subscriber *sub);
struct weston_log_subscriber * struct weston_log_subscriber *
weston_log_subscriber_create_flight_rec(size_t size); weston_log_subscriber_create_flight_rec(size_t size);
void
weston_log_subscriber_destroy_flight_rec(struct weston_log_subscriber *sub);
void void
weston_log_subscriber_display_flight_rec(struct weston_log_subscriber *sub); weston_log_subscriber_display_flight_rec(struct weston_log_subscriber *sub);

@ -54,14 +54,21 @@ weston_log_file_write(struct weston_log_subscriber *sub,
fwrite(data, len, 1, stream->file); 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 /** 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 * @param dump_to if specified, used for writing data to
* @returns a weston_log_subscriber object or NULL in case of failure * @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 * 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.write = weston_log_file_write;
file->base.destroy = weston_log_subscriber_destroy_log;
file->base.destroy_subscription = NULL; file->base.destroy_subscription = NULL;
file->base.complete = NULL; file->base.complete = NULL;
@ -86,15 +94,3 @@ weston_log_subscriber_create_log(FILE *dump_to)
return &file->base; 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);
}

@ -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); 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 /** Create a flight recorder type of subscriber
* *
* Allocates both the flight recorder and the underlying ring buffer. Use * 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 * @param size specify the maximum size (in bytes) of the backing storage
* for the flight recorder * for the flight recorder
@ -241,6 +255,7 @@ weston_log_subscriber_create_flight_rec(size_t size)
return NULL; return NULL;
flight_rec->base.write = weston_log_flight_recorder_write; 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.destroy_subscription = NULL;
flight_rec->base.complete = NULL; flight_rec->base.complete = NULL;
wl_list_init(&flight_rec->base.subscription_list); 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; 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 /** Retrieve flight recorder ring buffer contents, could be useful when
* implementing an assert()-like wrapper. * implementing an assert()-like wrapper.
* *

@ -52,6 +52,8 @@ struct weston_log_subscription;
struct weston_log_subscriber { struct weston_log_subscriber {
/** write the data pointed by @param data */ /** write the data pointed by @param data */
void (*write)(struct weston_log_subscriber *sub, const char *data, size_t len); 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 the type of streams that required additional destroy operation
* for destroying the stream */ * for destroying the stream */
void (*destroy_subscription)(struct weston_log_subscriber *sub); void (*destroy_subscription)(struct weston_log_subscriber *sub);
@ -79,7 +81,6 @@ weston_log_subscription_add(struct weston_log_scope *scope,
void void
weston_log_subscription_remove(struct weston_log_subscription *sub); weston_log_subscription_remove(struct weston_log_subscription *sub);
void void
weston_log_bind_weston_debug(struct wl_client *client, weston_log_bind_weston_debug(struct wl_client *client,
void *data, uint32_t version, uint32_t id); void *data, uint32_t version, uint32_t id);

@ -180,6 +180,7 @@ stream_create(struct weston_log_context *log_ctx, const char *name,
stream->resource = stream_resource; stream->resource = stream_resource;
stream->base.write = weston_log_debug_wayland_write; stream->base.write = weston_log_debug_wayland_write;
stream->base.destroy = NULL;
stream->base.destroy_subscription = weston_log_debug_wayland_to_destroy; stream->base.destroy_subscription = weston_log_debug_wayland_to_destroy;
stream->base.complete = weston_log_debug_wayland_complete; stream->base.complete = weston_log_debug_wayland_complete;
wl_list_init(&stream->base.subscription_list); wl_list_init(&stream->base.subscription_list);

@ -939,6 +939,21 @@ weston_log_scope_timestamp(struct weston_log_scope *scope,
return buf; 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 /** Subscribe to a scope
* *
* Creates a subscription which is used to subscribe the \p subscriber * Creates a subscription which is used to subscribe the \p subscriber

Loading…
Cancel
Save