Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2017 Pekka Paalanen <pq@iki.fi> |
| 3 | * Copyright © 2018 Zodiac Inflight Innovations |
| 4 | * |
| 5 | * Permission is hereby granted, free of charge, to any person obtaining |
| 6 | * a copy of this software and associated documentation files (the |
| 7 | * "Software"), to deal in the Software without restriction, including |
| 8 | * without limitation the rights to use, copy, modify, merge, publish, |
| 9 | * distribute, sublicense, and/or sell copies of the Software, and to |
| 10 | * permit persons to whom the Software is furnished to do so, subject to |
| 11 | * the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice (including the |
| 14 | * next paragraph) shall be included in all copies or substantial |
| 15 | * portions of the Software. |
| 16 | * |
| 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 21 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 22 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 24 | * SOFTWARE. |
| 25 | */ |
| 26 | |
| 27 | #include "config.h" |
| 28 | |
Marius Vlad | f4f4c2b | 2019-04-29 13:27:47 +0300 | [diff] [blame] | 29 | #include <libweston/weston-debug.h> |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 30 | #include "helpers.h" |
Pekka Paalanen | 3d5d947 | 2019-03-28 16:28:47 +0200 | [diff] [blame] | 31 | #include <libweston/libweston.h> |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 32 | |
Marius Vlad | e0a858a | 2019-06-25 12:48:56 +0300 | [diff] [blame] | 33 | #include "weston-log-internal.h" |
| 34 | |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 35 | #include "weston-debug-server-protocol.h" |
| 36 | |
| 37 | #include <assert.h> |
| 38 | #include <unistd.h> |
| 39 | #include <stdarg.h> |
| 40 | #include <string.h> |
| 41 | #include <errno.h> |
| 42 | #include <sys/time.h> |
| 43 | |
Marius Vlad | 3d7d978 | 2019-04-17 12:35:38 +0300 | [diff] [blame] | 44 | /** Main weston-log context |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 45 | * |
Marius Vlad | e0a858a | 2019-06-25 12:48:56 +0300 | [diff] [blame] | 46 | * One per weston_compositor. Stores list of scopes created and a list pending |
| 47 | * subscriptions. |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 48 | * |
Marius Vlad | e0a858a | 2019-06-25 12:48:56 +0300 | [diff] [blame] | 49 | * A pending subscription is a subscription to a scope which hasn't been |
| 50 | * created. When the scope is finally created the pending subscription will be |
| 51 | * removed from the pending subscription list, but not before was added in the |
| 52 | * scope's subscription list and that of the subscriber list. |
| 53 | * |
| 54 | * Pending subscriptions only make sense for other types of streams, other than |
| 55 | * those created by weston-debug protocol. In the case of the weston-debug |
| 56 | * protocol, the subscription processes is done automatically whenever a client |
| 57 | * connects and subscribes to a scope which was previously advertised by the |
| 58 | * compositor. |
| 59 | * |
| 60 | * @internal |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 61 | */ |
Marius Vlad | 3d7d978 | 2019-04-17 12:35:38 +0300 | [diff] [blame] | 62 | struct weston_log_context { |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 63 | struct wl_listener compositor_destroy_listener; |
| 64 | struct wl_global *global; |
Marius Vlad | 5d5e335 | 2019-04-17 13:05:38 +0300 | [diff] [blame] | 65 | struct wl_list scope_list; /**< weston_log_scope::compositor_link */ |
Marius Vlad | e0a858a | 2019-06-25 12:48:56 +0300 | [diff] [blame] | 66 | struct wl_list pending_subscription_list; /**< weston_log_subscription::source_link */ |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 67 | }; |
| 68 | |
Marius Vlad | 5d5e335 | 2019-04-17 13:05:38 +0300 | [diff] [blame] | 69 | /** weston-log message scope |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 70 | * |
Marius Vlad | 7e4db95 | 2019-04-17 13:47:06 +0300 | [diff] [blame] | 71 | * This is used for scoping logging/debugging messages. Clients can subscribe |
| 72 | * to only the scopes they are interested in. A scope is identified by its name |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 73 | * (also referred to as debug stream name). |
| 74 | */ |
Marius Vlad | 5d5e335 | 2019-04-17 13:05:38 +0300 | [diff] [blame] | 75 | struct weston_log_scope { |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 76 | char *name; |
| 77 | char *desc; |
Marius Vlad | 5d5e335 | 2019-04-17 13:05:38 +0300 | [diff] [blame] | 78 | weston_log_scope_cb begin_cb; |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 79 | void *user_data; |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 80 | struct wl_list compositor_link; |
Marius Vlad | e0a858a | 2019-06-25 12:48:56 +0300 | [diff] [blame] | 81 | struct wl_list subscription_list; /**< weston_log_subscription::source_link */ |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 82 | }; |
| 83 | |
Marius Vlad | e0a858a | 2019-06-25 12:48:56 +0300 | [diff] [blame] | 84 | /** Ties a subscriber to a scope |
| 85 | * |
| 86 | * A subscription is created each time we'd want to subscribe to a scope. From |
| 87 | * the stream type we can retrieve the subscriber and from the subscriber we |
| 88 | * reach each of the streams callbacks. See also weston_log_subscriber object. |
| 89 | * |
| 90 | * When a subscription has been created we store it in the scope's subscription |
| 91 | * list and in the subscriber's subscription list. The subscription might be a |
| 92 | * pending subscription until the scope for which there's was a subscribe has |
| 93 | * been created. The scope creation will take of looking through the pending |
| 94 | * subscription list. |
| 95 | * |
| 96 | * A subscription can reached from a subscriber subscription list by using the |
| 97 | * streams base class. |
| 98 | * |
| 99 | * @internal |
| 100 | */ |
| 101 | struct weston_log_subscription { |
| 102 | struct weston_log_subscriber *owner; |
| 103 | struct wl_list owner_link; /**< weston_log_subscriber::subscription_list */ |
| 104 | |
| 105 | char *scope_name; |
| 106 | struct weston_log_scope *source; |
| 107 | struct wl_list source_link; /**< weston_log_scope::subscription_list or |
| 108 | weston_log_context::pending_subscription_list */ |
| 109 | }; |
| 110 | |
| 111 | |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 112 | /** A debug stream created by a client |
| 113 | * |
| 114 | * A client provides a file descriptor for the server to write debug |
| 115 | * messages into. A weston_debug_stream is associated to one |
Marius Vlad | 5d5e335 | 2019-04-17 13:05:38 +0300 | [diff] [blame] | 116 | * weston_log_scope via the scope name, and the scope provides the messages. |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 117 | * There can be several streams for the same scope, all streams getting the |
| 118 | * same messages. |
| 119 | */ |
| 120 | struct weston_debug_stream { |
Marius Vlad | 7814f30 | 2019-06-21 14:20:15 +0300 | [diff] [blame] | 121 | struct weston_log_subscriber base; |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 122 | int fd; /**< client provided fd */ |
| 123 | struct wl_resource *resource; /**< weston_debug_stream_v1 object */ |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 124 | }; |
| 125 | |
Marius Vlad | 7814f30 | 2019-06-21 14:20:15 +0300 | [diff] [blame] | 126 | static struct weston_debug_stream * |
| 127 | to_weston_debug_stream(struct weston_log_subscriber *sub) |
| 128 | { |
| 129 | return container_of(sub, struct weston_debug_stream, base); |
| 130 | } |
| 131 | |
Marius Vlad | e0a858a | 2019-06-25 12:48:56 +0300 | [diff] [blame] | 132 | /** Creates a new subscription using the subscriber by \c owner. |
| 133 | * |
| 134 | * The subscription created is added to the \c owner subscription list. |
| 135 | * Destroying the subscription using weston_log_subscription_destroy() will |
| 136 | * remove the link from the subscription list and free storage alloc'ed. |
| 137 | * |
| 138 | * @param owner the subscriber owner, must be created before creating a |
| 139 | * subscription |
| 140 | * @param scope_name the scope for which to create this subscription |
| 141 | * @returns a weston_log_subscription object in case of success, or NULL |
| 142 | * otherwise |
| 143 | * |
| 144 | * @internal |
| 145 | * @sa weston_log_subscription_destroy, weston_log_subscription_remove, |
| 146 | * weston_log_subscription_add |
| 147 | */ |
| 148 | struct weston_log_subscription * |
| 149 | weston_log_subscription_create(struct weston_log_subscriber *owner, |
| 150 | const char *scope_name) |
| 151 | { |
| 152 | struct weston_log_subscription *sub; |
| 153 | assert(owner); |
| 154 | assert(scope_name); |
| 155 | |
| 156 | sub = zalloc(sizeof(*sub)); |
| 157 | if (!sub) |
| 158 | return NULL; |
| 159 | |
| 160 | sub->owner = owner; |
| 161 | sub->scope_name = strdup(scope_name); |
| 162 | |
| 163 | wl_list_insert(&sub->owner->subscription_list, &sub->owner_link); |
| 164 | return sub; |
| 165 | } |
| 166 | |
| 167 | /** Destroys the subscription |
| 168 | * |
| 169 | * @param sub |
| 170 | * @internal |
| 171 | */ |
| 172 | void |
| 173 | weston_log_subscription_destroy(struct weston_log_subscription *sub) |
| 174 | { |
| 175 | if (sub->owner) |
| 176 | wl_list_remove(&sub->owner_link); |
| 177 | free(sub->scope_name); |
| 178 | free(sub); |
| 179 | } |
| 180 | |
| 181 | /** Retrieve a subscription by using the subscriber |
| 182 | * |
| 183 | * This is useful when trying to find a subscription from the subscriber by |
| 184 | * having only access to the stream. |
| 185 | * |
| 186 | * @param subscriber the subscriber in question |
| 187 | * @returns a weston_log_subscription object |
| 188 | * |
| 189 | * @internal |
| 190 | */ |
| 191 | struct weston_log_subscription * |
| 192 | weston_log_subscriber_get_only_subscription(struct weston_log_subscriber *subscriber) |
| 193 | { |
| 194 | struct weston_log_subscription *sub; |
| 195 | /* unlikely, but can happen */ |
| 196 | if (wl_list_length(&subscriber->subscription_list) == 0) |
| 197 | return NULL; |
| 198 | |
| 199 | assert(wl_list_length(&subscriber->subscription_list) == 1); |
| 200 | |
| 201 | return wl_container_of(subscriber->subscription_list.prev, |
| 202 | sub, owner_link); |
| 203 | } |
| 204 | |
| 205 | /** Adds the subscription \c sub to the subscription list of the |
| 206 | * scope. |
| 207 | * |
| 208 | * This should used when the scope has been created, and the subscription \c |
| 209 | * sub has be created before calling this function. |
| 210 | * |
| 211 | * @param scope the scope |
| 212 | * @param sub the subscription, it must be created before, see |
| 213 | * weston_log_subscription_create() |
| 214 | * |
| 215 | * @internal |
| 216 | */ |
| 217 | void |
| 218 | weston_log_subscription_add(struct weston_log_scope *scope, |
| 219 | struct weston_log_subscription *sub) |
| 220 | { |
| 221 | assert(scope); |
| 222 | assert(sub); |
| 223 | /* don't allow subscriptions to have a source already! */ |
| 224 | assert(!sub->source); |
| 225 | |
| 226 | sub->source = scope; |
| 227 | wl_list_insert(&scope->subscription_list, &sub->source_link); |
| 228 | } |
| 229 | |
| 230 | /** Removes the subscription from the scope's subscription list |
| 231 | * |
| 232 | * @internal |
| 233 | */ |
| 234 | void |
| 235 | weston_log_subscription_remove(struct weston_log_subscription *sub) |
| 236 | { |
| 237 | assert(sub); |
| 238 | if (sub->source) |
| 239 | wl_list_remove(&sub->source_link); |
| 240 | sub->source = NULL; |
| 241 | } |
| 242 | |
Marius Vlad | 8f329e2 | 2019-06-21 14:57:02 +0300 | [diff] [blame^] | 243 | /** Look-up the scope from the scope list stored in the log context, by |
| 244 | * matching against the \c name. |
| 245 | * |
| 246 | * @param log_ctx |
| 247 | * @param name the scope name, see weston_compositor_add_log_scope() |
| 248 | * @returns NULL if none found, or a pointer to a weston_log_scope |
| 249 | * |
| 250 | * @internal |
| 251 | */ |
| 252 | struct weston_log_scope * |
| 253 | weston_log_get_scope(struct weston_log_context *log_ctx, const char *name) |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 254 | { |
Marius Vlad | 5d5e335 | 2019-04-17 13:05:38 +0300 | [diff] [blame] | 255 | struct weston_log_scope *scope; |
Marius Vlad | 3d7d978 | 2019-04-17 12:35:38 +0300 | [diff] [blame] | 256 | wl_list_for_each(scope, &log_ctx->scope_list, compositor_link) |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 257 | if (strcmp(name, scope->name) == 0) |
| 258 | return scope; |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 259 | return NULL; |
| 260 | } |
| 261 | |
Marius Vlad | 8f329e2 | 2019-06-21 14:57:02 +0300 | [diff] [blame^] | 262 | /** Wrapper to invoke the weston_log_scope_cb. Allows to call the begin_cb of |
| 263 | * a log scope. |
| 264 | * |
| 265 | * @internal |
| 266 | */ |
| 267 | void |
| 268 | weston_log_run_begin_cb(struct weston_log_scope *scope) |
| 269 | { |
| 270 | if (scope->begin_cb) |
| 271 | scope->begin_cb(scope, scope->user_data); |
| 272 | } |
| 273 | |
| 274 | /** Advertise the log scope name and the log scope description |
| 275 | * |
| 276 | * This is only used by the weston-debug protocol! |
| 277 | * |
| 278 | * @internal |
| 279 | */ |
| 280 | void |
| 281 | weston_debug_protocol_advertise_scopes(struct weston_log_context *log_ctx, |
| 282 | struct wl_resource *res) |
| 283 | { |
| 284 | struct weston_log_scope *scope; |
| 285 | wl_list_for_each(scope, &log_ctx->scope_list, compositor_link) |
| 286 | weston_debug_v1_send_available(res, scope->name, scope->desc); |
| 287 | } |
| 288 | |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 289 | static void |
| 290 | stream_close_unlink(struct weston_debug_stream *stream) |
| 291 | { |
| 292 | if (stream->fd != -1) |
| 293 | close(stream->fd); |
| 294 | stream->fd = -1; |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | static void WL_PRINTF(2, 3) |
| 298 | stream_close_on_failure(struct weston_debug_stream *stream, |
| 299 | const char *fmt, ...) |
| 300 | { |
| 301 | char *msg; |
| 302 | va_list ap; |
| 303 | int ret; |
| 304 | |
| 305 | stream_close_unlink(stream); |
| 306 | |
| 307 | va_start(ap, fmt); |
| 308 | ret = vasprintf(&msg, fmt, ap); |
| 309 | va_end(ap); |
| 310 | |
| 311 | if (ret > 0) { |
| 312 | weston_debug_stream_v1_send_failure(stream->resource, msg); |
| 313 | free(msg); |
| 314 | } else { |
| 315 | weston_debug_stream_v1_send_failure(stream->resource, |
| 316 | "MEMFAIL"); |
| 317 | } |
| 318 | } |
| 319 | |
Marius Vlad | 7814f30 | 2019-06-21 14:20:15 +0300 | [diff] [blame] | 320 | /** Write data into a specific debug stream |
| 321 | * |
| 322 | * \param sub The subscriber's stream to write into; must not be NULL. |
| 323 | * \param[in] data Pointer to the data to write. |
| 324 | * \param len Number of bytes to write. |
| 325 | * |
| 326 | * Writes the given data (binary verbatim) into the debug stream. |
| 327 | * If \c len is zero or negative, the write is silently dropped. |
| 328 | * |
| 329 | * Writing is continued until all data has been written or |
| 330 | * a write fails. If the write fails due to a signal, it is re-tried. |
| 331 | * Otherwise on failure, the stream is closed and |
| 332 | * \c weston_debug_stream_v1.failure event is sent to the client. |
| 333 | * |
| 334 | * \memberof weston_debug_stream |
| 335 | */ |
| 336 | static void |
| 337 | weston_debug_stream_write(struct weston_log_subscriber *sub, |
| 338 | const char *data, size_t len) |
| 339 | { |
| 340 | ssize_t len_ = len; |
| 341 | ssize_t ret; |
| 342 | int e; |
| 343 | struct weston_debug_stream *stream = to_weston_debug_stream(sub); |
| 344 | |
| 345 | if (stream->fd == -1) |
| 346 | return; |
| 347 | |
| 348 | while (len_ > 0) { |
| 349 | ret = write(stream->fd, data, len_); |
| 350 | e = errno; |
| 351 | if (ret < 0) { |
| 352 | if (e == EINTR) |
| 353 | continue; |
| 354 | |
| 355 | stream_close_on_failure(stream, |
| 356 | "Error writing %zd bytes: %s (%d)", |
| 357 | len_, strerror(e), e); |
| 358 | break; |
| 359 | } |
| 360 | |
| 361 | len_ -= ret; |
| 362 | data += ret; |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | /** Close the debug stream and send success event |
| 367 | * |
| 368 | * \param sub Subscriber's stream to close. |
| 369 | * |
| 370 | * Closes the debug stream and sends \c weston_debug_stream_v1.complete |
| 371 | * event to the client. This tells the client the debug information dump |
| 372 | * is complete. |
| 373 | * |
| 374 | * \memberof weston_debug_stream |
| 375 | */ |
| 376 | static void |
| 377 | weston_debug_stream_complete(struct weston_log_subscriber *sub) |
| 378 | { |
| 379 | struct weston_debug_stream *stream = to_weston_debug_stream(sub); |
| 380 | |
| 381 | stream_close_unlink(stream); |
| 382 | weston_debug_stream_v1_send_complete(stream->resource); |
| 383 | } |
| 384 | |
| 385 | static void |
| 386 | weston_debug_stream_to_destroy(struct weston_log_subscriber *sub) |
| 387 | { |
| 388 | struct weston_debug_stream *stream = to_weston_debug_stream(sub); |
| 389 | stream_close_on_failure(stream, "debug name removed"); |
| 390 | } |
| 391 | |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 392 | static struct weston_debug_stream * |
Marius Vlad | 3d7d978 | 2019-04-17 12:35:38 +0300 | [diff] [blame] | 393 | stream_create(struct weston_log_context *log_ctx, const char *name, |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 394 | int32_t streamfd, struct wl_resource *stream_resource) |
| 395 | { |
| 396 | struct weston_debug_stream *stream; |
Marius Vlad | 5d5e335 | 2019-04-17 13:05:38 +0300 | [diff] [blame] | 397 | struct weston_log_scope *scope; |
Marius Vlad | 7814f30 | 2019-06-21 14:20:15 +0300 | [diff] [blame] | 398 | struct weston_log_subscription *sub; |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 399 | |
| 400 | stream = zalloc(sizeof *stream); |
| 401 | if (!stream) |
| 402 | return NULL; |
| 403 | |
Marius Vlad | 7814f30 | 2019-06-21 14:20:15 +0300 | [diff] [blame] | 404 | |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 405 | stream->fd = streamfd; |
| 406 | stream->resource = stream_resource; |
| 407 | |
Marius Vlad | 7814f30 | 2019-06-21 14:20:15 +0300 | [diff] [blame] | 408 | stream->base.write = weston_debug_stream_write; |
| 409 | stream->base.destroy = weston_debug_stream_to_destroy; |
| 410 | stream->base.complete = weston_debug_stream_complete; |
| 411 | wl_list_init(&stream->base.subscription_list); |
| 412 | |
| 413 | sub = weston_log_subscription_create(&stream->base, name); |
| 414 | |
Marius Vlad | 8f329e2 | 2019-06-21 14:57:02 +0300 | [diff] [blame^] | 415 | scope = weston_log_get_scope(log_ctx, name); |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 416 | if (scope) { |
Marius Vlad | 7814f30 | 2019-06-21 14:20:15 +0300 | [diff] [blame] | 417 | weston_log_subscription_add(scope, sub); |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 418 | if (scope->begin_cb) |
Marius Vlad | 7814f30 | 2019-06-21 14:20:15 +0300 | [diff] [blame] | 419 | scope->begin_cb(scope, scope->user_data); |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 420 | } else { |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 421 | stream_close_on_failure(stream, |
| 422 | "Debug stream name '%s' is unknown.", |
| 423 | name); |
| 424 | } |
| 425 | |
| 426 | return stream; |
| 427 | } |
| 428 | |
| 429 | static void |
| 430 | stream_destroy(struct wl_resource *stream_resource) |
| 431 | { |
| 432 | struct weston_debug_stream *stream; |
Marius Vlad | 7814f30 | 2019-06-21 14:20:15 +0300 | [diff] [blame] | 433 | struct weston_log_subscription *sub = NULL; |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 434 | |
| 435 | stream = wl_resource_get_user_data(stream_resource); |
| 436 | |
| 437 | if (stream->fd != -1) |
| 438 | close(stream->fd); |
Marius Vlad | 7814f30 | 2019-06-21 14:20:15 +0300 | [diff] [blame] | 439 | |
| 440 | sub = weston_log_subscriber_get_only_subscription(&stream->base); |
| 441 | weston_log_subscription_remove(sub); |
| 442 | weston_log_subscription_destroy(sub); |
| 443 | |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 444 | free(stream); |
| 445 | } |
| 446 | |
| 447 | static void |
| 448 | weston_debug_stream_destroy(struct wl_client *client, |
| 449 | struct wl_resource *stream_resource) |
| 450 | { |
| 451 | wl_resource_destroy(stream_resource); |
| 452 | } |
| 453 | |
| 454 | static const struct weston_debug_stream_v1_interface |
| 455 | weston_debug_stream_impl = { |
| 456 | weston_debug_stream_destroy |
| 457 | }; |
| 458 | |
| 459 | static void |
| 460 | weston_debug_destroy(struct wl_client *client, |
| 461 | struct wl_resource *global_resource) |
| 462 | { |
| 463 | wl_resource_destroy(global_resource); |
| 464 | } |
| 465 | |
| 466 | static void |
| 467 | weston_debug_subscribe(struct wl_client *client, |
| 468 | struct wl_resource *global_resource, |
| 469 | const char *name, |
| 470 | int32_t streamfd, |
| 471 | uint32_t new_stream_id) |
| 472 | { |
Marius Vlad | 3d7d978 | 2019-04-17 12:35:38 +0300 | [diff] [blame] | 473 | struct weston_log_context *log_ctx; |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 474 | struct wl_resource *stream_resource; |
| 475 | uint32_t version; |
| 476 | struct weston_debug_stream *stream; |
| 477 | |
Marius Vlad | 3d7d978 | 2019-04-17 12:35:38 +0300 | [diff] [blame] | 478 | log_ctx = wl_resource_get_user_data(global_resource); |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 479 | version = wl_resource_get_version(global_resource); |
| 480 | |
| 481 | stream_resource = wl_resource_create(client, |
| 482 | &weston_debug_stream_v1_interface, |
| 483 | version, new_stream_id); |
| 484 | if (!stream_resource) |
| 485 | goto fail; |
| 486 | |
Marius Vlad | 3d7d978 | 2019-04-17 12:35:38 +0300 | [diff] [blame] | 487 | stream = stream_create(log_ctx, name, streamfd, stream_resource); |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 488 | if (!stream) |
| 489 | goto fail; |
| 490 | |
| 491 | wl_resource_set_implementation(stream_resource, |
| 492 | &weston_debug_stream_impl, |
| 493 | stream, stream_destroy); |
| 494 | return; |
| 495 | |
| 496 | fail: |
| 497 | close(streamfd); |
| 498 | wl_client_post_no_memory(client); |
| 499 | } |
| 500 | |
| 501 | static const struct weston_debug_v1_interface weston_debug_impl = { |
| 502 | weston_debug_destroy, |
| 503 | weston_debug_subscribe |
| 504 | }; |
| 505 | |
| 506 | static void |
| 507 | bind_weston_debug(struct wl_client *client, |
| 508 | void *data, uint32_t version, uint32_t id) |
| 509 | { |
Marius Vlad | 3d7d978 | 2019-04-17 12:35:38 +0300 | [diff] [blame] | 510 | struct weston_log_context *log_ctx = data; |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 511 | struct wl_resource *resource; |
| 512 | |
| 513 | resource = wl_resource_create(client, |
| 514 | &weston_debug_v1_interface, |
| 515 | version, id); |
| 516 | if (!resource) { |
| 517 | wl_client_post_no_memory(client); |
| 518 | return; |
| 519 | } |
| 520 | wl_resource_set_implementation(resource, &weston_debug_impl, |
Marius Vlad | 3d7d978 | 2019-04-17 12:35:38 +0300 | [diff] [blame] | 521 | log_ctx, NULL); |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 522 | |
Marius Vlad | 8f329e2 | 2019-06-21 14:57:02 +0300 | [diff] [blame^] | 523 | weston_debug_protocol_advertise_scopes(log_ctx, resource); |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 524 | } |
| 525 | |
Marius Vlad | 880b485 | 2019-04-07 17:07:58 +0300 | [diff] [blame] | 526 | /** |
Marius Vlad | 3d7d978 | 2019-04-17 12:35:38 +0300 | [diff] [blame] | 527 | * Connect weston_compositor structure to weston_log_context structure. |
Marius Vlad | 880b485 | 2019-04-07 17:07:58 +0300 | [diff] [blame] | 528 | * |
| 529 | * \param compositor |
Marius Vlad | 3d7d978 | 2019-04-17 12:35:38 +0300 | [diff] [blame] | 530 | * \param log_ctx |
Marius Vlad | 880b485 | 2019-04-07 17:07:58 +0300 | [diff] [blame] | 531 | * \return 0 on success, -1 on failure |
| 532 | * |
Marius Vlad | 3d7d978 | 2019-04-17 12:35:38 +0300 | [diff] [blame] | 533 | * Sets weston_compositor::weston_log_ctx. |
Marius Vlad | 880b485 | 2019-04-07 17:07:58 +0300 | [diff] [blame] | 534 | */ |
| 535 | int |
Marius Vlad | 3d7d978 | 2019-04-17 12:35:38 +0300 | [diff] [blame] | 536 | weston_log_ctx_compositor_setup(struct weston_compositor *compositor, |
| 537 | struct weston_log_context *log_ctx) |
Marius Vlad | 880b485 | 2019-04-07 17:07:58 +0300 | [diff] [blame] | 538 | { |
Marius Vlad | 6f09866 | 2019-04-17 13:50:28 +0300 | [diff] [blame] | 539 | assert(!compositor->weston_log_ctx); |
| 540 | assert(log_ctx); |
Marius Vlad | 880b485 | 2019-04-07 17:07:58 +0300 | [diff] [blame] | 541 | |
Marius Vlad | 3d7d978 | 2019-04-17 12:35:38 +0300 | [diff] [blame] | 542 | compositor->weston_log_ctx = log_ctx; |
Marius Vlad | 880b485 | 2019-04-07 17:07:58 +0300 | [diff] [blame] | 543 | return 0; |
| 544 | } |
| 545 | |
Marius Vlad | a2dace2 | 2019-06-12 16:05:44 +0300 | [diff] [blame] | 546 | /** Creates weston_log_context structure |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 547 | * |
Marius Vlad | a2dace2 | 2019-06-12 16:05:44 +0300 | [diff] [blame] | 548 | * \return NULL in case of failure, or a weston_log_context object in case of |
| 549 | * success |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 550 | * |
Marius Vlad | 3d7d978 | 2019-04-17 12:35:38 +0300 | [diff] [blame] | 551 | * weston_log_context is a singleton for each weston_compositor. |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 552 | * |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 553 | */ |
Marius Vlad | 3d7d978 | 2019-04-17 12:35:38 +0300 | [diff] [blame] | 554 | WL_EXPORT struct weston_log_context * |
| 555 | weston_log_ctx_compositor_create(void) |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 556 | { |
Marius Vlad | 3d7d978 | 2019-04-17 12:35:38 +0300 | [diff] [blame] | 557 | struct weston_log_context *log_ctx; |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 558 | |
Marius Vlad | 3d7d978 | 2019-04-17 12:35:38 +0300 | [diff] [blame] | 559 | log_ctx = zalloc(sizeof *log_ctx); |
| 560 | if (!log_ctx) |
Marius Vlad | 880b485 | 2019-04-07 17:07:58 +0300 | [diff] [blame] | 561 | return NULL; |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 562 | |
Marius Vlad | 3d7d978 | 2019-04-17 12:35:38 +0300 | [diff] [blame] | 563 | wl_list_init(&log_ctx->scope_list); |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 564 | |
Marius Vlad | 3d7d978 | 2019-04-17 12:35:38 +0300 | [diff] [blame] | 565 | return log_ctx; |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 566 | } |
| 567 | |
Marius Vlad | 3d7d978 | 2019-04-17 12:35:38 +0300 | [diff] [blame] | 568 | /** Destroy weston_log_context structure |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 569 | * |
| 570 | * \param compositor The libweston compositor whose weston-debug to tear down. |
| 571 | * |
Marius Vlad | 3d7d978 | 2019-04-17 12:35:38 +0300 | [diff] [blame] | 572 | * Clears weston_compositor::weston_log_ctx. |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 573 | * |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 574 | */ |
Marius Vlad | 3d7d978 | 2019-04-17 12:35:38 +0300 | [diff] [blame] | 575 | WL_EXPORT void |
| 576 | weston_log_ctx_compositor_destroy(struct weston_compositor *compositor) |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 577 | { |
Marius Vlad | 3d7d978 | 2019-04-17 12:35:38 +0300 | [diff] [blame] | 578 | struct weston_log_context *log_ctx = compositor->weston_log_ctx; |
Marius Vlad | 5d5e335 | 2019-04-17 13:05:38 +0300 | [diff] [blame] | 579 | struct weston_log_scope *scope; |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 580 | |
Marius Vlad | 3d7d978 | 2019-04-17 12:35:38 +0300 | [diff] [blame] | 581 | if (log_ctx->global) |
| 582 | wl_global_destroy(log_ctx->global); |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 583 | |
Marius Vlad | 3d7d978 | 2019-04-17 12:35:38 +0300 | [diff] [blame] | 584 | wl_list_for_each(scope, &log_ctx->scope_list, compositor_link) |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 585 | weston_log("Internal warning: debug scope '%s' has not been destroyed.\n", |
| 586 | scope->name); |
| 587 | |
| 588 | /* Remove head to not crash if scope removed later. */ |
Marius Vlad | 3d7d978 | 2019-04-17 12:35:38 +0300 | [diff] [blame] | 589 | wl_list_remove(&log_ctx->scope_list); |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 590 | |
Marius Vlad | 3d7d978 | 2019-04-17 12:35:38 +0300 | [diff] [blame] | 591 | free(log_ctx); |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 592 | |
Marius Vlad | 3d7d978 | 2019-04-17 12:35:38 +0300 | [diff] [blame] | 593 | compositor->weston_log_ctx = NULL; |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | /** Enable weston-debug protocol extension |
| 597 | * |
| 598 | * \param compositor The libweston compositor where to enable. |
| 599 | * |
| 600 | * This enables the weston_debug_v1 Wayland protocol extension which any client |
Emmanuel Gil Peyrot | 426c246 | 2019-02-20 16:33:32 +0100 | [diff] [blame] | 601 | * can use to get debug messages from the compositor. |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 602 | * |
| 603 | * WARNING: This feature should not be used in production. If a client |
| 604 | * provides a file descriptor that blocks writes, it will block the whole |
| 605 | * compositor indefinitely. |
| 606 | * |
| 607 | * There is no control on which client is allowed to subscribe to debug |
| 608 | * messages. Any and all clients are allowed. |
| 609 | * |
| 610 | * The debug extension is disabled by default, and once enabled, cannot be |
| 611 | * disabled again. |
| 612 | */ |
| 613 | WL_EXPORT void |
| 614 | weston_compositor_enable_debug_protocol(struct weston_compositor *compositor) |
| 615 | { |
Marius Vlad | 1e2fda2 | 2019-04-07 19:07:16 +0300 | [diff] [blame] | 616 | struct weston_log_context *log_ctx = compositor->weston_log_ctx; |
| 617 | assert(log_ctx); |
| 618 | if (log_ctx->global) |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 619 | return; |
| 620 | |
Marius Vlad | 1e2fda2 | 2019-04-07 19:07:16 +0300 | [diff] [blame] | 621 | log_ctx->global = wl_global_create(compositor->wl_display, |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 622 | &weston_debug_v1_interface, 1, |
Marius Vlad | 1e2fda2 | 2019-04-07 19:07:16 +0300 | [diff] [blame] | 623 | log_ctx, bind_weston_debug); |
| 624 | if (!log_ctx->global) |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 625 | return; |
| 626 | |
| 627 | weston_log("WARNING: debug protocol has been enabled. " |
| 628 | "This is a potential denial-of-service attack vector and " |
| 629 | "information leak.\n"); |
| 630 | } |
| 631 | |
Marius Vlad | d9bcc0b | 2018-12-13 23:03:30 +0200 | [diff] [blame] | 632 | /** Determine if the debug protocol has been enabled |
| 633 | * |
| 634 | * \param wc The libweston compositor to verify if debug protocol has been enabled |
| 635 | */ |
| 636 | WL_EXPORT bool |
| 637 | weston_compositor_is_debug_protocol_enabled(struct weston_compositor *wc) |
| 638 | { |
Marius Vlad | 3d7d978 | 2019-04-17 12:35:38 +0300 | [diff] [blame] | 639 | return wc->weston_log_ctx->global != NULL; |
Marius Vlad | d9bcc0b | 2018-12-13 23:03:30 +0200 | [diff] [blame] | 640 | } |
| 641 | |
Marius Vlad | 7e4db95 | 2019-04-17 13:47:06 +0300 | [diff] [blame] | 642 | /** Register a new debug stream name, creating a log scope |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 643 | * |
Marius Vlad | 3d7d978 | 2019-04-17 12:35:38 +0300 | [diff] [blame] | 644 | * \param log_ctx The weston_log_context where to add. |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 645 | * \param name The debug stream/scope name; must not be NULL. |
Marius Vlad | a2dace2 | 2019-06-12 16:05:44 +0300 | [diff] [blame] | 646 | * \param description The debug scope description for humans; must not be NULL. |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 647 | * \param begin_cb Optional callback when a client subscribes to this scope. |
| 648 | * \param user_data Optional user data pointer for the callback. |
| 649 | * \return A valid pointer on success, NULL on failure. |
| 650 | * |
| 651 | * This function is used to create a debug scope. All debug message printing |
| 652 | * happens for a scope, which allows clients to subscribe to the kind of |
| 653 | * debug messages they want by \c name. |
| 654 | * |
| 655 | * \c name must be unique in the \c weston_compositor instance. \c name and |
| 656 | * \c description must both be provided. The description is printed when a |
| 657 | * client asks for a list of supported debug scopes. |
| 658 | * |
| 659 | * \c begin_cb, if not NULL, is called when a client subscribes to the |
| 660 | * debug scope creating a debug stream. This is for debug scopes that need |
| 661 | * to print messages as a response to a client appearing, e.g. printing a |
| 662 | * list of windows on demand or a static preamble. The argument \c user_data |
| 663 | * is passed in to the callback and is otherwise unused. |
| 664 | * |
| 665 | * For one-shot debug streams, \c begin_cb should finally call |
| 666 | * weston_debug_stream_complete() to close the stream and tell the client |
| 667 | * the printing is complete. Otherwise the client expects more to be written |
| 668 | * to its file descriptor. |
| 669 | * |
| 670 | * The debug scope must be destroyed before destroying the |
| 671 | * \c weston_compositor. |
| 672 | * |
Marius Vlad | 5d5e335 | 2019-04-17 13:05:38 +0300 | [diff] [blame] | 673 | * \memberof weston_log_scope |
| 674 | * \sa weston_debug_stream, weston_log_scope_cb |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 675 | */ |
Marius Vlad | 5d5e335 | 2019-04-17 13:05:38 +0300 | [diff] [blame] | 676 | WL_EXPORT struct weston_log_scope * |
Marius Vlad | 7e4db95 | 2019-04-17 13:47:06 +0300 | [diff] [blame] | 677 | weston_compositor_add_log_scope(struct weston_log_context *log_ctx, |
| 678 | const char *name, |
| 679 | const char *description, |
| 680 | weston_log_scope_cb begin_cb, |
| 681 | void *user_data) |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 682 | { |
Marius Vlad | 5d5e335 | 2019-04-17 13:05:38 +0300 | [diff] [blame] | 683 | struct weston_log_scope *scope; |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 684 | |
Marius Vlad | 1e2fda2 | 2019-04-07 19:07:16 +0300 | [diff] [blame] | 685 | if (!name || !description) { |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 686 | weston_log("Error: cannot add a debug scope without name or description.\n"); |
| 687 | return NULL; |
| 688 | } |
| 689 | |
Marius Vlad | 3d7d978 | 2019-04-17 12:35:38 +0300 | [diff] [blame] | 690 | if (!log_ctx) { |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 691 | weston_log("Error: cannot add debug scope '%s', infra not initialized.\n", |
| 692 | name); |
| 693 | return NULL; |
| 694 | } |
| 695 | |
Marius Vlad | 8f329e2 | 2019-06-21 14:57:02 +0300 | [diff] [blame^] | 696 | if (weston_log_get_scope(log_ctx, name)){ |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 697 | weston_log("Error: debug scope named '%s' is already registered.\n", |
| 698 | name); |
| 699 | return NULL; |
| 700 | } |
| 701 | |
| 702 | scope = zalloc(sizeof *scope); |
| 703 | if (!scope) { |
| 704 | weston_log("Error adding debug scope '%s': out of memory.\n", |
| 705 | name); |
| 706 | return NULL; |
| 707 | } |
| 708 | |
| 709 | scope->name = strdup(name); |
| 710 | scope->desc = strdup(description); |
| 711 | scope->begin_cb = begin_cb; |
| 712 | scope->user_data = user_data; |
Marius Vlad | 7814f30 | 2019-06-21 14:20:15 +0300 | [diff] [blame] | 713 | wl_list_init(&scope->subscription_list); |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 714 | |
| 715 | if (!scope->name || !scope->desc) { |
| 716 | weston_log("Error adding debug scope '%s': out of memory.\n", |
| 717 | name); |
| 718 | free(scope->name); |
| 719 | free(scope->desc); |
| 720 | free(scope); |
| 721 | return NULL; |
| 722 | } |
| 723 | |
Marius Vlad | 3d7d978 | 2019-04-17 12:35:38 +0300 | [diff] [blame] | 724 | wl_list_insert(log_ctx->scope_list.prev, &scope->compositor_link); |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 725 | |
| 726 | return scope; |
| 727 | } |
| 728 | |
Marius Vlad | 3d7d978 | 2019-04-17 12:35:38 +0300 | [diff] [blame] | 729 | /** Destroy a log scope |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 730 | * |
Marius Vlad | 3d7d978 | 2019-04-17 12:35:38 +0300 | [diff] [blame] | 731 | * \param scope The log scope to destroy; may be NULL. |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 732 | * |
Marius Vlad | 3d7d978 | 2019-04-17 12:35:38 +0300 | [diff] [blame] | 733 | * Destroys the log scope, closing all open streams subscribed to it and |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 734 | * sending them each a \c weston_debug_stream_v1.failure event. |
| 735 | * |
Marius Vlad | 5d5e335 | 2019-04-17 13:05:38 +0300 | [diff] [blame] | 736 | * \memberof weston_log_scope |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 737 | */ |
| 738 | WL_EXPORT void |
Marius Vlad | 7e4db95 | 2019-04-17 13:47:06 +0300 | [diff] [blame] | 739 | weston_compositor_log_scope_destroy(struct weston_log_scope *scope) |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 740 | { |
Marius Vlad | 7814f30 | 2019-06-21 14:20:15 +0300 | [diff] [blame] | 741 | struct weston_log_subscription *sub, *sub_tmp; |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 742 | |
| 743 | if (!scope) |
| 744 | return; |
| 745 | |
Marius Vlad | 7814f30 | 2019-06-21 14:20:15 +0300 | [diff] [blame] | 746 | wl_list_for_each_safe(sub, sub_tmp, &scope->subscription_list, source_link) { |
| 747 | /* destroy each subscription */ |
| 748 | if (sub->owner->destroy) |
| 749 | sub->owner->destroy(sub->owner); |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 750 | |
Marius Vlad | 7814f30 | 2019-06-21 14:20:15 +0300 | [diff] [blame] | 751 | weston_log_subscription_remove(sub); |
| 752 | weston_log_subscription_destroy(sub); |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 753 | } |
| 754 | |
| 755 | wl_list_remove(&scope->compositor_link); |
| 756 | free(scope->name); |
| 757 | free(scope->desc); |
| 758 | free(scope); |
| 759 | } |
| 760 | |
| 761 | /** Are there any active subscriptions to the scope? |
| 762 | * |
Marius Vlad | 7e4db95 | 2019-04-17 13:47:06 +0300 | [diff] [blame] | 763 | * \param scope The log scope to check; may be NULL. |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 764 | * \return True if any streams are open for this scope, false otherwise. |
| 765 | * |
| 766 | * As printing some debugging messages may be relatively expensive, one |
| 767 | * can use this function to determine if there is a need to gather the |
| 768 | * debugging information at all. If this function returns false, all |
| 769 | * printing for this scope is dropped, so gathering the information is |
| 770 | * pointless. |
| 771 | * |
| 772 | * The return value of this function should not be stored, as new clients |
| 773 | * may subscribe to the debug scope later. |
| 774 | * |
| 775 | * If the given scope is NULL, this function will always return false, |
| 776 | * making it safe to use in teardown or destroy code, provided the |
| 777 | * scope is initialized to NULL before creation and set to NULL after |
| 778 | * destruction. |
| 779 | * |
Marius Vlad | 5d5e335 | 2019-04-17 13:05:38 +0300 | [diff] [blame] | 780 | * \memberof weston_log_scope |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 781 | */ |
| 782 | WL_EXPORT bool |
Marius Vlad | 7e4db95 | 2019-04-17 13:47:06 +0300 | [diff] [blame] | 783 | weston_log_scope_is_enabled(struct weston_log_scope *scope) |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 784 | { |
| 785 | if (!scope) |
| 786 | return false; |
| 787 | |
Marius Vlad | 7814f30 | 2019-06-21 14:20:15 +0300 | [diff] [blame] | 788 | return !wl_list_empty(&scope->subscription_list); |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 789 | } |
| 790 | |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 791 | WL_EXPORT void |
Marius Vlad | 7814f30 | 2019-06-21 14:20:15 +0300 | [diff] [blame] | 792 | weston_log_scope_complete(struct weston_log_scope *scope) |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 793 | { |
Marius Vlad | 7814f30 | 2019-06-21 14:20:15 +0300 | [diff] [blame] | 794 | struct weston_log_subscription *sub; |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 795 | |
Marius Vlad | 7814f30 | 2019-06-21 14:20:15 +0300 | [diff] [blame] | 796 | if (!scope) |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 797 | return; |
| 798 | |
Marius Vlad | 7814f30 | 2019-06-21 14:20:15 +0300 | [diff] [blame] | 799 | wl_list_for_each(sub, &scope->subscription_list, source_link) |
| 800 | if (sub->owner && sub->owner->complete) |
| 801 | sub->owner->complete(sub->owner); |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 802 | } |
| 803 | |
Marius Vlad | 7e4db95 | 2019-04-17 13:47:06 +0300 | [diff] [blame] | 804 | /** Write log data for a scope |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 805 | * |
| 806 | * \param scope The debug scope to write for; may be NULL, in which case |
| 807 | * nothing will be written. |
Marius Vlad | a2dace2 | 2019-06-12 16:05:44 +0300 | [diff] [blame] | 808 | * \param[in] data Pointer to the data to write. |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 809 | * \param len Number of bytes to write. |
| 810 | * |
| 811 | * Writes the given data to all subscribed clients' streams. |
| 812 | * |
Marius Vlad | 5d5e335 | 2019-04-17 13:05:38 +0300 | [diff] [blame] | 813 | * \memberof weston_log_scope |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 814 | */ |
| 815 | WL_EXPORT void |
Marius Vlad | 7e4db95 | 2019-04-17 13:47:06 +0300 | [diff] [blame] | 816 | weston_log_scope_write(struct weston_log_scope *scope, |
| 817 | const char *data, size_t len) |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 818 | { |
Marius Vlad | 7814f30 | 2019-06-21 14:20:15 +0300 | [diff] [blame] | 819 | struct weston_log_subscription *sub; |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 820 | |
| 821 | if (!scope) |
| 822 | return; |
| 823 | |
Marius Vlad | 7814f30 | 2019-06-21 14:20:15 +0300 | [diff] [blame] | 824 | wl_list_for_each(sub, &scope->subscription_list, source_link) |
| 825 | if (sub->owner && sub->owner->write) |
| 826 | sub->owner->write(sub->owner, data, len); |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 827 | } |
| 828 | |
| 829 | /** Write a formatted string for a scope (varargs) |
| 830 | * |
Marius Vlad | 7e4db95 | 2019-04-17 13:47:06 +0300 | [diff] [blame] | 831 | * \param scope The log scope to write for; may be NULL, in which case |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 832 | * nothing will be written. |
| 833 | * \param fmt Printf-style format string. |
| 834 | * \param ap Formatting arguments. |
| 835 | * |
| 836 | * Writes to formatted string to all subscribed clients' streams. |
| 837 | * |
| 838 | * The behavioral details for each stream are the same as for |
| 839 | * weston_debug_stream_write(). |
| 840 | * |
Marius Vlad | 5d5e335 | 2019-04-17 13:05:38 +0300 | [diff] [blame] | 841 | * \memberof weston_log_scope |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 842 | */ |
| 843 | WL_EXPORT void |
Marius Vlad | 7e4db95 | 2019-04-17 13:47:06 +0300 | [diff] [blame] | 844 | weston_log_scope_vprintf(struct weston_log_scope *scope, |
| 845 | const char *fmt, va_list ap) |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 846 | { |
| 847 | static const char oom[] = "Out of memory"; |
| 848 | char *str; |
| 849 | int len; |
| 850 | |
Marius Vlad | 7e4db95 | 2019-04-17 13:47:06 +0300 | [diff] [blame] | 851 | if (!weston_log_scope_is_enabled(scope)) |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 852 | return; |
| 853 | |
| 854 | len = vasprintf(&str, fmt, ap); |
| 855 | if (len >= 0) { |
Marius Vlad | 7e4db95 | 2019-04-17 13:47:06 +0300 | [diff] [blame] | 856 | weston_log_scope_write(scope, str, len); |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 857 | free(str); |
| 858 | } else { |
Marius Vlad | 7e4db95 | 2019-04-17 13:47:06 +0300 | [diff] [blame] | 859 | weston_log_scope_write(scope, oom, sizeof oom - 1); |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 860 | } |
| 861 | } |
| 862 | |
| 863 | /** Write a formatted string for a scope |
| 864 | * |
Marius Vlad | 7e4db95 | 2019-04-17 13:47:06 +0300 | [diff] [blame] | 865 | * \param scope The log scope to write for; may be NULL, in which case |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 866 | * nothing will be written. |
| 867 | * \param fmt Printf-style format string and arguments. |
| 868 | * |
| 869 | * Writes to formatted string to all subscribed clients' streams. |
| 870 | * |
| 871 | * The behavioral details for each stream are the same as for |
| 872 | * weston_debug_stream_write(). |
| 873 | * |
Marius Vlad | 5d5e335 | 2019-04-17 13:05:38 +0300 | [diff] [blame] | 874 | * \memberof weston_log_scope |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 875 | */ |
| 876 | WL_EXPORT void |
Marius Vlad | 7e4db95 | 2019-04-17 13:47:06 +0300 | [diff] [blame] | 877 | weston_log_scope_printf(struct weston_log_scope *scope, |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 878 | const char *fmt, ...) |
| 879 | { |
| 880 | va_list ap; |
| 881 | |
| 882 | va_start(ap, fmt); |
Marius Vlad | 7e4db95 | 2019-04-17 13:47:06 +0300 | [diff] [blame] | 883 | weston_log_scope_vprintf(scope, fmt, ap); |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 884 | va_end(ap); |
| 885 | } |
| 886 | |
| 887 | /** Write debug scope name and current time into string |
| 888 | * |
Marius Vlad | a2dace2 | 2019-06-12 16:05:44 +0300 | [diff] [blame] | 889 | * \param[in] scope debug scope; may be NULL |
| 890 | * \param[out] buf Buffer to store the string. |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 891 | * \param len Available size in the buffer in bytes. |
| 892 | * \return \c buf |
| 893 | * |
| 894 | * Reads the current local wall-clock time and formats it into a string. |
| 895 | * and append the debug scope name to it, if a scope is available. |
| 896 | * The string is NUL-terminated, even if truncated. |
| 897 | */ |
| 898 | WL_EXPORT char * |
Marius Vlad | 7e4db95 | 2019-04-17 13:47:06 +0300 | [diff] [blame] | 899 | weston_log_scope_timestamp(struct weston_log_scope *scope, |
| 900 | char *buf, size_t len) |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 901 | { |
| 902 | struct timeval tv; |
| 903 | struct tm *bdt; |
| 904 | char string[128]; |
| 905 | size_t ret = 0; |
| 906 | |
| 907 | gettimeofday(&tv, NULL); |
| 908 | |
| 909 | bdt = localtime(&tv.tv_sec); |
| 910 | if (bdt) |
| 911 | ret = strftime(string, sizeof string, |
| 912 | "%Y-%m-%d %H:%M:%S", bdt); |
| 913 | |
| 914 | if (ret > 0) { |
| 915 | snprintf(buf, len, "[%s.%03ld][%s]", string, |
| 916 | tv.tv_usec / 1000, |
| 917 | (scope) ? scope->name : "no scope"); |
| 918 | } else { |
| 919 | snprintf(buf, len, "[?][%s]", |
| 920 | (scope) ? scope->name : "no scope"); |
| 921 | } |
| 922 | |
| 923 | return buf; |
| 924 | } |