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 | c901e89 | 2019-06-21 22:49:18 +0300 | [diff] [blame] | 29 | #include <libweston/weston-log.h> |
Pekka Paalanen | c232f8d | 2019-04-05 16:09:45 +0300 | [diff] [blame] | 30 | #include "shared/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" |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 34 | #include "weston-debug-server-protocol.h" |
| 35 | |
| 36 | #include <assert.h> |
| 37 | #include <unistd.h> |
| 38 | #include <stdarg.h> |
| 39 | #include <string.h> |
| 40 | #include <errno.h> |
| 41 | #include <sys/time.h> |
| 42 | |
Marius Vlad | 8b8b803 | 2019-06-27 18:34:03 +0300 | [diff] [blame] | 43 | /** |
| 44 | * @defgroup log Public Logging/Debugging API |
| 45 | * @defgroup internal-log Private/Internal Logging/Debugging API |
| 46 | * @defgroup debug-protocol weston-debug protocol specific |
| 47 | */ |
| 48 | |
Marius Vlad | 3d7d978 | 2019-04-17 12:35:38 +0300 | [diff] [blame] | 49 | /** Main weston-log context |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 50 | * |
Marius Vlad | e0a858a | 2019-06-25 12:48:56 +0300 | [diff] [blame] | 51 | * One per weston_compositor. Stores list of scopes created and a list pending |
| 52 | * subscriptions. |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 53 | * |
Marius Vlad | e0a858a | 2019-06-25 12:48:56 +0300 | [diff] [blame] | 54 | * A pending subscription is a subscription to a scope which hasn't been |
| 55 | * created. When the scope is finally created the pending subscription will be |
| 56 | * removed from the pending subscription list, but not before was added in the |
| 57 | * scope's subscription list and that of the subscriber list. |
| 58 | * |
| 59 | * Pending subscriptions only make sense for other types of streams, other than |
| 60 | * those created by weston-debug protocol. In the case of the weston-debug |
| 61 | * protocol, the subscription processes is done automatically whenever a client |
| 62 | * connects and subscribes to a scope which was previously advertised by the |
| 63 | * compositor. |
| 64 | * |
Marius Vlad | 8b8b803 | 2019-06-27 18:34:03 +0300 | [diff] [blame] | 65 | * @ingroup internal-log |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 66 | */ |
Marius Vlad | 3d7d978 | 2019-04-17 12:35:38 +0300 | [diff] [blame] | 67 | struct weston_log_context { |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 68 | struct wl_global *global; |
Leandro Ribeiro | 4ec38d1 | 2020-01-24 01:12:59 -0300 | [diff] [blame] | 69 | struct wl_listener compositor_destroy_listener; |
Marius Vlad | 5d5e335 | 2019-04-17 13:05:38 +0300 | [diff] [blame] | 70 | struct wl_list scope_list; /**< weston_log_scope::compositor_link */ |
Marius Vlad | e0a858a | 2019-06-25 12:48:56 +0300 | [diff] [blame] | 71 | struct wl_list pending_subscription_list; /**< weston_log_subscription::source_link */ |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 72 | }; |
| 73 | |
Marius Vlad | 5d5e335 | 2019-04-17 13:05:38 +0300 | [diff] [blame] | 74 | /** weston-log message scope |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 75 | * |
Marius Vlad | 7e4db95 | 2019-04-17 13:47:06 +0300 | [diff] [blame] | 76 | * This is used for scoping logging/debugging messages. Clients can subscribe |
| 77 | * 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] | 78 | * (also referred to as debug stream name). |
Marius Vlad | 8b8b803 | 2019-06-27 18:34:03 +0300 | [diff] [blame] | 79 | * |
| 80 | * @ingroup log |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 81 | */ |
Marius Vlad | 5d5e335 | 2019-04-17 13:05:38 +0300 | [diff] [blame] | 82 | struct weston_log_scope { |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 83 | char *name; |
| 84 | char *desc; |
Marius Vlad | 0c7beb0 | 2019-07-25 11:46:44 +0300 | [diff] [blame] | 85 | weston_log_scope_cb new_subscription; |
Marius Vlad | 9bb1c3a | 2019-07-29 12:02:47 +0300 | [diff] [blame] | 86 | weston_log_scope_cb destroy_subscription; |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 87 | void *user_data; |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 88 | struct wl_list compositor_link; |
Marius Vlad | e0a858a | 2019-06-25 12:48:56 +0300 | [diff] [blame] | 89 | struct wl_list subscription_list; /**< weston_log_subscription::source_link */ |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 90 | }; |
| 91 | |
Marius Vlad | e0a858a | 2019-06-25 12:48:56 +0300 | [diff] [blame] | 92 | /** Ties a subscriber to a scope |
| 93 | * |
| 94 | * A subscription is created each time we'd want to subscribe to a scope. From |
| 95 | * the stream type we can retrieve the subscriber and from the subscriber we |
| 96 | * reach each of the streams callbacks. See also weston_log_subscriber object. |
| 97 | * |
| 98 | * When a subscription has been created we store it in the scope's subscription |
| 99 | * list and in the subscriber's subscription list. The subscription might be a |
| 100 | * pending subscription until the scope for which there's was a subscribe has |
| 101 | * been created. The scope creation will take of looking through the pending |
| 102 | * subscription list. |
| 103 | * |
| 104 | * A subscription can reached from a subscriber subscription list by using the |
| 105 | * streams base class. |
| 106 | * |
Marius Vlad | 8b8b803 | 2019-06-27 18:34:03 +0300 | [diff] [blame] | 107 | * @ingroup internal-log |
Marius Vlad | e0a858a | 2019-06-25 12:48:56 +0300 | [diff] [blame] | 108 | */ |
| 109 | struct weston_log_subscription { |
| 110 | struct weston_log_subscriber *owner; |
| 111 | struct wl_list owner_link; /**< weston_log_subscriber::subscription_list */ |
| 112 | |
| 113 | char *scope_name; |
| 114 | struct weston_log_scope *source; |
| 115 | struct wl_list source_link; /**< weston_log_scope::subscription_list or |
| 116 | weston_log_context::pending_subscription_list */ |
Marius Vlad | 410d0bc | 2019-07-29 12:05:29 +0300 | [diff] [blame] | 117 | |
| 118 | void *data; |
Marius Vlad | e0a858a | 2019-06-25 12:48:56 +0300 | [diff] [blame] | 119 | }; |
| 120 | |
Marius Vlad | 9f71a4a | 2019-06-22 00:29:31 +0300 | [diff] [blame] | 121 | static struct weston_log_subscription * |
| 122 | find_pending_subscription(struct weston_log_context *log_ctx, |
| 123 | const char *scope_name) |
| 124 | { |
| 125 | struct weston_log_subscription *sub; |
| 126 | |
| 127 | wl_list_for_each(sub, &log_ctx->pending_subscription_list, source_link) |
Marius Vlad | f68ee07 | 2019-11-05 17:08:17 +0200 | [diff] [blame] | 128 | if (!strcmp(sub->scope_name, scope_name)) |
Marius Vlad | 9f71a4a | 2019-06-22 00:29:31 +0300 | [diff] [blame] | 129 | return sub; |
| 130 | |
| 131 | return NULL; |
| 132 | } |
| 133 | |
| 134 | /** Create a pending subscription and add it the list of pending subscriptions |
| 135 | * |
| 136 | * @param owner a subscriber represented by weston_log_subscriber object |
| 137 | * @param scope_name the name of the scope (which we don't have in the list of scopes) |
| 138 | * @param log_ctx the logging context used to add the pending subscription |
| 139 | * |
| 140 | * @memberof weston_log_subscription |
| 141 | */ |
| 142 | static void |
| 143 | weston_log_subscription_create_pending(struct weston_log_subscriber *owner, |
| 144 | const char *scope_name, |
| 145 | struct weston_log_context *log_ctx) |
| 146 | { |
| 147 | assert(owner); |
| 148 | assert(scope_name); |
| 149 | struct weston_log_subscription *sub = zalloc(sizeof(*sub)); |
| 150 | |
| 151 | if (!sub) |
| 152 | return; |
| 153 | |
| 154 | sub->scope_name = strdup(scope_name); |
| 155 | sub->owner = owner; |
| 156 | |
| 157 | wl_list_insert(&log_ctx->pending_subscription_list, &sub->source_link); |
| 158 | } |
| 159 | |
| 160 | /** Destroys the pending subscription created previously with |
| 161 | * weston_log_subscription_create_pending() |
| 162 | * |
| 163 | * @param sub the weston_log_subscription object to remove from the list |
| 164 | * of subscriptions and to destroy the subscription |
| 165 | * |
| 166 | * @memberof weston_log_subscription |
| 167 | */ |
| 168 | static void |
| 169 | weston_log_subscription_destroy_pending(struct weston_log_subscription *sub) |
| 170 | { |
| 171 | assert(sub); |
| 172 | /* pending subsriptions do not have a source */ |
| 173 | wl_list_remove(&sub->source_link); |
| 174 | free(sub->scope_name); |
| 175 | free(sub); |
| 176 | } |
| 177 | |
Marius Vlad | 8b8b803 | 2019-06-27 18:34:03 +0300 | [diff] [blame] | 178 | /** Write to the stream's subscription |
| 179 | * |
| 180 | * @memberof weston_log_subscription |
| 181 | */ |
Marius Vlad | dad882a | 2019-07-17 15:43:53 +0300 | [diff] [blame] | 182 | static void |
| 183 | weston_log_subscription_write(struct weston_log_subscription *sub, |
| 184 | const char *data, size_t len) |
| 185 | { |
| 186 | if (sub->owner && sub->owner->write) |
| 187 | sub->owner->write(sub->owner, data, len); |
| 188 | } |
| 189 | |
Marius Vlad | 8b8b803 | 2019-06-27 18:34:03 +0300 | [diff] [blame] | 190 | /** Write a formatted string to the stream's subscription |
| 191 | * |
| 192 | * @memberof weston_log_subscription |
| 193 | */ |
Marius Vlad | dad882a | 2019-07-17 15:43:53 +0300 | [diff] [blame] | 194 | static void |
| 195 | weston_log_subscription_vprintf(struct weston_log_subscription *sub, |
| 196 | const char *fmt, va_list ap) |
| 197 | { |
| 198 | static const char oom[] = "Out of memory"; |
| 199 | char *str; |
| 200 | int len; |
| 201 | |
| 202 | if (!weston_log_scope_is_enabled(sub->source)) |
| 203 | return; |
| 204 | |
| 205 | len = vasprintf(&str, fmt, ap); |
| 206 | if (len >= 0) { |
| 207 | weston_log_subscription_write(sub, str, len); |
| 208 | free(str); |
| 209 | } else { |
| 210 | weston_log_subscription_write(sub, oom, sizeof oom - 1); |
| 211 | } |
| 212 | } |
| 213 | |
Marius Vlad | 410d0bc | 2019-07-29 12:05:29 +0300 | [diff] [blame] | 214 | void |
| 215 | weston_log_subscription_set_data(struct weston_log_subscription *sub, void *data) |
| 216 | { |
| 217 | /* don't allow data to already be set */ |
| 218 | assert(!sub->data); |
| 219 | sub->data = data; |
| 220 | } |
| 221 | |
| 222 | void * |
| 223 | weston_log_subscription_get_data(struct weston_log_subscription *sub) |
| 224 | { |
| 225 | return sub->data; |
| 226 | } |
| 227 | |
Marius Vlad | e0a858a | 2019-06-25 12:48:56 +0300 | [diff] [blame] | 228 | /** Creates a new subscription using the subscriber by \c owner. |
| 229 | * |
| 230 | * The subscription created is added to the \c owner subscription list. |
| 231 | * Destroying the subscription using weston_log_subscription_destroy() will |
| 232 | * remove the link from the subscription list and free storage alloc'ed. |
| 233 | * |
Marius Vlad | 5ae0e62 | 2019-07-11 17:44:50 +0300 | [diff] [blame] | 234 | * Note that this adds the subscription to the scope's subscription list |
| 235 | * hence the \c scope required argument. |
| 236 | * |
Marius Vlad | e0a858a | 2019-06-25 12:48:56 +0300 | [diff] [blame] | 237 | * @param owner the subscriber owner, must be created before creating a |
| 238 | * subscription |
Marius Vlad | 5ae0e62 | 2019-07-11 17:44:50 +0300 | [diff] [blame] | 239 | * @param scope the scope in order to add the subscription to the scope's |
| 240 | * subscription list |
Marius Vlad | e0a858a | 2019-06-25 12:48:56 +0300 | [diff] [blame] | 241 | * @returns a weston_log_subscription object in case of success, or NULL |
| 242 | * otherwise |
| 243 | * |
Marius Vlad | e0a858a | 2019-06-25 12:48:56 +0300 | [diff] [blame] | 244 | * @sa weston_log_subscription_destroy, weston_log_subscription_remove, |
| 245 | * weston_log_subscription_add |
Marius Vlad | 8b8b803 | 2019-06-27 18:34:03 +0300 | [diff] [blame] | 246 | * @memberof weston_log_subscription |
Marius Vlad | e0a858a | 2019-06-25 12:48:56 +0300 | [diff] [blame] | 247 | */ |
Marius Vlad | 5ae0e62 | 2019-07-11 17:44:50 +0300 | [diff] [blame] | 248 | void |
Marius Vlad | e0a858a | 2019-06-25 12:48:56 +0300 | [diff] [blame] | 249 | weston_log_subscription_create(struct weston_log_subscriber *owner, |
Marius Vlad | 5ae0e62 | 2019-07-11 17:44:50 +0300 | [diff] [blame] | 250 | struct weston_log_scope *scope) |
Marius Vlad | e0a858a | 2019-06-25 12:48:56 +0300 | [diff] [blame] | 251 | { |
| 252 | struct weston_log_subscription *sub; |
Marius Vlad | 9f71a4a | 2019-06-22 00:29:31 +0300 | [diff] [blame] | 253 | assert(owner); |
Marius Vlad | 5ae0e62 | 2019-07-11 17:44:50 +0300 | [diff] [blame] | 254 | assert(scope); |
| 255 | assert(scope->name); |
Marius Vlad | e0a858a | 2019-06-25 12:48:56 +0300 | [diff] [blame] | 256 | |
| 257 | sub = zalloc(sizeof(*sub)); |
| 258 | if (!sub) |
Marius Vlad | 5ae0e62 | 2019-07-11 17:44:50 +0300 | [diff] [blame] | 259 | return; |
Marius Vlad | e0a858a | 2019-06-25 12:48:56 +0300 | [diff] [blame] | 260 | |
| 261 | sub->owner = owner; |
Marius Vlad | 5ae0e62 | 2019-07-11 17:44:50 +0300 | [diff] [blame] | 262 | sub->scope_name = strdup(scope->name); |
Marius Vlad | e0a858a | 2019-06-25 12:48:56 +0300 | [diff] [blame] | 263 | |
| 264 | wl_list_insert(&sub->owner->subscription_list, &sub->owner_link); |
Marius Vlad | 5ae0e62 | 2019-07-11 17:44:50 +0300 | [diff] [blame] | 265 | |
| 266 | weston_log_subscription_add(scope, sub); |
Marius Vlad | 0c7beb0 | 2019-07-25 11:46:44 +0300 | [diff] [blame] | 267 | weston_log_run_cb_new_subscription(sub); |
Marius Vlad | e0a858a | 2019-06-25 12:48:56 +0300 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | /** Destroys the subscription |
| 271 | * |
Marius Vlad | 5ae0e62 | 2019-07-11 17:44:50 +0300 | [diff] [blame] | 272 | * Removes the subscription from the scopes subscription list and from |
| 273 | * subscriber's subscription list. It destroys the subscription afterwads. |
| 274 | * |
| 275 | * @memberof weston_log_subscription |
Marius Vlad | e0a858a | 2019-06-25 12:48:56 +0300 | [diff] [blame] | 276 | */ |
| 277 | void |
| 278 | weston_log_subscription_destroy(struct weston_log_subscription *sub) |
| 279 | { |
Marius Vlad | 5ae0e62 | 2019-07-11 17:44:50 +0300 | [diff] [blame] | 280 | assert(sub); |
Marius Vlad | 9bb1c3a | 2019-07-29 12:02:47 +0300 | [diff] [blame] | 281 | |
| 282 | if (sub->source->destroy_subscription) |
| 283 | sub->source->destroy_subscription(sub, sub->source->user_data); |
| 284 | |
Marius Vlad | e0a858a | 2019-06-25 12:48:56 +0300 | [diff] [blame] | 285 | if (sub->owner) |
| 286 | wl_list_remove(&sub->owner_link); |
Marius Vlad | 5ae0e62 | 2019-07-11 17:44:50 +0300 | [diff] [blame] | 287 | |
| 288 | weston_log_subscription_remove(sub); |
Marius Vlad | e0a858a | 2019-06-25 12:48:56 +0300 | [diff] [blame] | 289 | free(sub->scope_name); |
| 290 | free(sub); |
| 291 | } |
| 292 | |
| 293 | /** Retrieve a subscription by using the subscriber |
| 294 | * |
| 295 | * This is useful when trying to find a subscription from the subscriber by |
| 296 | * having only access to the stream. |
| 297 | * |
| 298 | * @param subscriber the subscriber in question |
| 299 | * @returns a weston_log_subscription object |
| 300 | * |
Marius Vlad | 8b8b803 | 2019-06-27 18:34:03 +0300 | [diff] [blame] | 301 | * @memberof weston_log_subscription |
Marius Vlad | e0a858a | 2019-06-25 12:48:56 +0300 | [diff] [blame] | 302 | */ |
| 303 | struct weston_log_subscription * |
| 304 | weston_log_subscriber_get_only_subscription(struct weston_log_subscriber *subscriber) |
| 305 | { |
| 306 | struct weston_log_subscription *sub; |
| 307 | /* unlikely, but can happen */ |
| 308 | if (wl_list_length(&subscriber->subscription_list) == 0) |
| 309 | return NULL; |
| 310 | |
| 311 | assert(wl_list_length(&subscriber->subscription_list) == 1); |
| 312 | |
| 313 | return wl_container_of(subscriber->subscription_list.prev, |
| 314 | sub, owner_link); |
| 315 | } |
| 316 | |
| 317 | /** Adds the subscription \c sub to the subscription list of the |
| 318 | * scope. |
| 319 | * |
| 320 | * This should used when the scope has been created, and the subscription \c |
| 321 | * sub has be created before calling this function. |
| 322 | * |
| 323 | * @param scope the scope |
| 324 | * @param sub the subscription, it must be created before, see |
| 325 | * weston_log_subscription_create() |
| 326 | * |
Marius Vlad | 8b8b803 | 2019-06-27 18:34:03 +0300 | [diff] [blame] | 327 | * @memberof weston_log_subscription |
Marius Vlad | e0a858a | 2019-06-25 12:48:56 +0300 | [diff] [blame] | 328 | */ |
| 329 | void |
| 330 | weston_log_subscription_add(struct weston_log_scope *scope, |
| 331 | struct weston_log_subscription *sub) |
| 332 | { |
| 333 | assert(scope); |
| 334 | assert(sub); |
| 335 | /* don't allow subscriptions to have a source already! */ |
| 336 | assert(!sub->source); |
| 337 | |
| 338 | sub->source = scope; |
| 339 | wl_list_insert(&scope->subscription_list, &sub->source_link); |
| 340 | } |
| 341 | |
| 342 | /** Removes the subscription from the scope's subscription list |
| 343 | * |
Marius Vlad | 8b8b803 | 2019-06-27 18:34:03 +0300 | [diff] [blame] | 344 | * @memberof weston_log_subscription |
Marius Vlad | e0a858a | 2019-06-25 12:48:56 +0300 | [diff] [blame] | 345 | */ |
| 346 | void |
| 347 | weston_log_subscription_remove(struct weston_log_subscription *sub) |
| 348 | { |
| 349 | assert(sub); |
| 350 | if (sub->source) |
| 351 | wl_list_remove(&sub->source_link); |
| 352 | sub->source = NULL; |
| 353 | } |
| 354 | |
Marius Vlad | 8f329e2 | 2019-06-21 14:57:02 +0300 | [diff] [blame] | 355 | /** Look-up the scope from the scope list stored in the log context, by |
| 356 | * matching against the \c name. |
| 357 | * |
| 358 | * @param log_ctx |
Leandro Ribeiro | f66685d | 2019-12-26 16:03:04 -0300 | [diff] [blame] | 359 | * @param name the scope name, see weston_log_ctx_add_log_scope() and |
| 360 | * weston_compositor_add_log_scope() |
Marius Vlad | 8f329e2 | 2019-06-21 14:57:02 +0300 | [diff] [blame] | 361 | * @returns NULL if none found, or a pointer to a weston_log_scope |
| 362 | * |
Marius Vlad | 8b8b803 | 2019-06-27 18:34:03 +0300 | [diff] [blame] | 363 | * @ingroup internal-log |
Marius Vlad | 8f329e2 | 2019-06-21 14:57:02 +0300 | [diff] [blame] | 364 | */ |
| 365 | struct weston_log_scope * |
| 366 | 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] | 367 | { |
Marius Vlad | 5d5e335 | 2019-04-17 13:05:38 +0300 | [diff] [blame] | 368 | struct weston_log_scope *scope; |
Marius Vlad | 3d7d978 | 2019-04-17 12:35:38 +0300 | [diff] [blame] | 369 | wl_list_for_each(scope, &log_ctx->scope_list, compositor_link) |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 370 | if (strcmp(name, scope->name) == 0) |
| 371 | return scope; |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 372 | return NULL; |
| 373 | } |
| 374 | |
Marius Vlad | 0c7beb0 | 2019-07-25 11:46:44 +0300 | [diff] [blame] | 375 | /** Wrapper to invoke the weston_log_scope_cb. Allows to call the cb |
| 376 | * new_subscription of a log scope. |
Marius Vlad | 8f329e2 | 2019-06-21 14:57:02 +0300 | [diff] [blame] | 377 | * |
Marius Vlad | 8b8b803 | 2019-06-27 18:34:03 +0300 | [diff] [blame] | 378 | * @ingroup internal-log |
Marius Vlad | 8f329e2 | 2019-06-21 14:57:02 +0300 | [diff] [blame] | 379 | */ |
| 380 | void |
Marius Vlad | 0c7beb0 | 2019-07-25 11:46:44 +0300 | [diff] [blame] | 381 | weston_log_run_cb_new_subscription(struct weston_log_subscription *sub) |
Marius Vlad | 8f329e2 | 2019-06-21 14:57:02 +0300 | [diff] [blame] | 382 | { |
Marius Vlad | 0c7beb0 | 2019-07-25 11:46:44 +0300 | [diff] [blame] | 383 | if (sub->source->new_subscription) |
| 384 | sub->source->new_subscription(sub, sub->source->user_data); |
Marius Vlad | 8f329e2 | 2019-06-21 14:57:02 +0300 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | /** Advertise the log scope name and the log scope description |
| 388 | * |
| 389 | * This is only used by the weston-debug protocol! |
| 390 | * |
Marius Vlad | 8b8b803 | 2019-06-27 18:34:03 +0300 | [diff] [blame] | 391 | * @ingroup internal-log |
Marius Vlad | 8f329e2 | 2019-06-21 14:57:02 +0300 | [diff] [blame] | 392 | */ |
| 393 | void |
| 394 | weston_debug_protocol_advertise_scopes(struct weston_log_context *log_ctx, |
| 395 | struct wl_resource *res) |
| 396 | { |
| 397 | struct weston_log_scope *scope; |
| 398 | wl_list_for_each(scope, &log_ctx->scope_list, compositor_link) |
| 399 | weston_debug_v1_send_available(res, scope->name, scope->desc); |
| 400 | } |
| 401 | |
Leandro Ribeiro | 4ec38d1 | 2020-01-24 01:12:59 -0300 | [diff] [blame] | 402 | /** Disable debug-protocol |
| 403 | * |
| 404 | * @param log_ctx The log context where the debug-protocol is linked |
| 405 | * |
| 406 | * @ingroup internal-log |
| 407 | */ |
| 408 | static void |
| 409 | weston_log_ctx_disable_debug_protocol(struct weston_log_context *log_ctx) |
| 410 | { |
| 411 | if (!log_ctx->global) |
| 412 | return; |
| 413 | |
| 414 | wl_global_destroy(log_ctx->global); |
| 415 | log_ctx->global = NULL; |
| 416 | } |
| 417 | |
Marius Vlad | a2dace2 | 2019-06-12 16:05:44 +0300 | [diff] [blame] | 418 | /** Creates weston_log_context structure |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 419 | * |
Marius Vlad | a2dace2 | 2019-06-12 16:05:44 +0300 | [diff] [blame] | 420 | * \return NULL in case of failure, or a weston_log_context object in case of |
| 421 | * success |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 422 | * |
Marius Vlad | 3d7d978 | 2019-04-17 12:35:38 +0300 | [diff] [blame] | 423 | * weston_log_context is a singleton for each weston_compositor. |
Marius Vlad | 8b8b803 | 2019-06-27 18:34:03 +0300 | [diff] [blame] | 424 | * @ingroup log |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 425 | * |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 426 | */ |
Marius Vlad | 3d7d978 | 2019-04-17 12:35:38 +0300 | [diff] [blame] | 427 | WL_EXPORT struct weston_log_context * |
Leandro Ribeiro | 4f13595 | 2020-01-17 11:19:59 -0300 | [diff] [blame] | 428 | weston_log_ctx_create(void) |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 429 | { |
Marius Vlad | 3d7d978 | 2019-04-17 12:35:38 +0300 | [diff] [blame] | 430 | struct weston_log_context *log_ctx; |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 431 | |
Marius Vlad | 3d7d978 | 2019-04-17 12:35:38 +0300 | [diff] [blame] | 432 | log_ctx = zalloc(sizeof *log_ctx); |
| 433 | if (!log_ctx) |
Marius Vlad | 880b485 | 2019-04-07 17:07:58 +0300 | [diff] [blame] | 434 | return NULL; |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 435 | |
Marius Vlad | 3d7d978 | 2019-04-17 12:35:38 +0300 | [diff] [blame] | 436 | wl_list_init(&log_ctx->scope_list); |
Marius Vlad | 9f71a4a | 2019-06-22 00:29:31 +0300 | [diff] [blame] | 437 | wl_list_init(&log_ctx->pending_subscription_list); |
Leandro Ribeiro | 4ec38d1 | 2020-01-24 01:12:59 -0300 | [diff] [blame] | 438 | wl_list_init(&log_ctx->compositor_destroy_listener.link); |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 439 | |
Marius Vlad | 3d7d978 | 2019-04-17 12:35:38 +0300 | [diff] [blame] | 440 | return log_ctx; |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 441 | } |
| 442 | |
Marius Vlad | 3d7d978 | 2019-04-17 12:35:38 +0300 | [diff] [blame] | 443 | /** Destroy weston_log_context structure |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 444 | * |
Leandro Ribeiro | 4ec38d1 | 2020-01-24 01:12:59 -0300 | [diff] [blame] | 445 | * \param log_ctx The log context to destroy. |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 446 | * |
Marius Vlad | 8b8b803 | 2019-06-27 18:34:03 +0300 | [diff] [blame] | 447 | * @ingroup log |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 448 | * |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 449 | */ |
Marius Vlad | 3d7d978 | 2019-04-17 12:35:38 +0300 | [diff] [blame] | 450 | WL_EXPORT void |
Leandro Ribeiro | 4ec38d1 | 2020-01-24 01:12:59 -0300 | [diff] [blame] | 451 | weston_log_ctx_destroy(struct weston_log_context *log_ctx) |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 452 | { |
Marius Vlad | 5d5e335 | 2019-04-17 13:05:38 +0300 | [diff] [blame] | 453 | struct weston_log_scope *scope; |
Marius Vlad | 9f71a4a | 2019-06-22 00:29:31 +0300 | [diff] [blame] | 454 | struct weston_log_subscription *pending_sub, *pending_sub_tmp; |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 455 | |
Leandro Ribeiro | 4ec38d1 | 2020-01-24 01:12:59 -0300 | [diff] [blame] | 456 | /* We can't destroy the log context if there's still a compositor |
| 457 | * that depends on it. This is an user error */ |
| 458 | assert(wl_list_empty(&log_ctx->compositor_destroy_listener.link)); |
| 459 | |
| 460 | weston_log_ctx_disable_debug_protocol(log_ctx); |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 461 | |
Marius Vlad | 3d7d978 | 2019-04-17 12:35:38 +0300 | [diff] [blame] | 462 | wl_list_for_each(scope, &log_ctx->scope_list, compositor_link) |
Marius Vlad | ef4c268 | 2019-06-23 15:48:54 +0300 | [diff] [blame] | 463 | fprintf(stderr, "Internal warning: debug scope '%s' has not been destroyed.\n", |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 464 | scope->name); |
| 465 | |
| 466 | /* Remove head to not crash if scope removed later. */ |
Marius Vlad | 3d7d978 | 2019-04-17 12:35:38 +0300 | [diff] [blame] | 467 | wl_list_remove(&log_ctx->scope_list); |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 468 | |
Marius Vlad | 9f71a4a | 2019-06-22 00:29:31 +0300 | [diff] [blame] | 469 | /* Remove any pending subscription(s) which nobody subscribed to */ |
| 470 | wl_list_for_each_safe(pending_sub, pending_sub_tmp, |
| 471 | &log_ctx->pending_subscription_list, source_link) { |
| 472 | weston_log_subscription_destroy_pending(pending_sub); |
| 473 | } |
| 474 | |
| 475 | /* pending_subscription_list should be empty at this point */ |
| 476 | |
Marius Vlad | 3d7d978 | 2019-04-17 12:35:38 +0300 | [diff] [blame] | 477 | free(log_ctx); |
Leandro Ribeiro | 4ec38d1 | 2020-01-24 01:12:59 -0300 | [diff] [blame] | 478 | } |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 479 | |
Leandro Ribeiro | 4ec38d1 | 2020-01-24 01:12:59 -0300 | [diff] [blame] | 480 | static void |
| 481 | compositor_destroy_listener(struct wl_listener *listener, void *data) |
| 482 | { |
| 483 | struct weston_log_context *log_ctx = |
| 484 | wl_container_of(listener, log_ctx, compositor_destroy_listener); |
| 485 | |
| 486 | /* We have to keep this list initalized as weston_log_ctx_destroy() has |
| 487 | * to check if there's any compositor destroy listener registered */ |
| 488 | wl_list_remove(&log_ctx->compositor_destroy_listener.link); |
| 489 | wl_list_init(&log_ctx->compositor_destroy_listener.link); |
| 490 | |
| 491 | weston_log_ctx_disable_debug_protocol(log_ctx); |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | /** Enable weston-debug protocol extension |
| 495 | * |
| 496 | * \param compositor The libweston compositor where to enable. |
| 497 | * |
| 498 | * 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] | 499 | * can use to get debug messages from the compositor. |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 500 | * |
| 501 | * WARNING: This feature should not be used in production. If a client |
| 502 | * provides a file descriptor that blocks writes, it will block the whole |
| 503 | * compositor indefinitely. |
| 504 | * |
| 505 | * There is no control on which client is allowed to subscribe to debug |
| 506 | * messages. Any and all clients are allowed. |
| 507 | * |
| 508 | * The debug extension is disabled by default, and once enabled, cannot be |
| 509 | * disabled again. |
Marius Vlad | 8b8b803 | 2019-06-27 18:34:03 +0300 | [diff] [blame] | 510 | * |
| 511 | * @ingroup debug-protocol |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 512 | */ |
| 513 | WL_EXPORT void |
| 514 | weston_compositor_enable_debug_protocol(struct weston_compositor *compositor) |
| 515 | { |
Marius Vlad | 1e2fda2 | 2019-04-07 19:07:16 +0300 | [diff] [blame] | 516 | struct weston_log_context *log_ctx = compositor->weston_log_ctx; |
| 517 | assert(log_ctx); |
| 518 | if (log_ctx->global) |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 519 | return; |
| 520 | |
Marius Vlad | 1e2fda2 | 2019-04-07 19:07:16 +0300 | [diff] [blame] | 521 | log_ctx->global = wl_global_create(compositor->wl_display, |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 522 | &weston_debug_v1_interface, 1, |
Marius Vlad | 69e7571 | 2019-06-25 13:29:57 +0300 | [diff] [blame] | 523 | log_ctx, weston_log_bind_weston_debug); |
Marius Vlad | 1e2fda2 | 2019-04-07 19:07:16 +0300 | [diff] [blame] | 524 | if (!log_ctx->global) |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 525 | return; |
| 526 | |
Leandro Ribeiro | 4ec38d1 | 2020-01-24 01:12:59 -0300 | [diff] [blame] | 527 | log_ctx->compositor_destroy_listener.notify = compositor_destroy_listener; |
| 528 | wl_signal_add(&compositor->destroy_signal, &log_ctx->compositor_destroy_listener); |
| 529 | |
Marius Vlad | ef4c268 | 2019-06-23 15:48:54 +0300 | [diff] [blame] | 530 | fprintf(stderr, "WARNING: debug protocol has been enabled. " |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 531 | "This is a potential denial-of-service attack vector and " |
| 532 | "information leak.\n"); |
| 533 | } |
| 534 | |
Marius Vlad | d9bcc0b | 2018-12-13 23:03:30 +0200 | [diff] [blame] | 535 | /** Determine if the debug protocol has been enabled |
| 536 | * |
Marius Vlad | 8b8b803 | 2019-06-27 18:34:03 +0300 | [diff] [blame] | 537 | * \param wc The libweston compositor to verify if debug protocol has been |
| 538 | * enabled |
| 539 | * |
| 540 | * @ingroup debug-protocol |
Marius Vlad | d9bcc0b | 2018-12-13 23:03:30 +0200 | [diff] [blame] | 541 | */ |
| 542 | WL_EXPORT bool |
| 543 | weston_compositor_is_debug_protocol_enabled(struct weston_compositor *wc) |
| 544 | { |
Marius Vlad | 3d7d978 | 2019-04-17 12:35:38 +0300 | [diff] [blame] | 545 | return wc->weston_log_ctx->global != NULL; |
Marius Vlad | d9bcc0b | 2018-12-13 23:03:30 +0200 | [diff] [blame] | 546 | } |
| 547 | |
Marius Vlad | 9f71a4a | 2019-06-22 00:29:31 +0300 | [diff] [blame] | 548 | /** Register a new stream name, creating a log scope. |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 549 | * |
Marius Vlad | 9f71a4a | 2019-06-22 00:29:31 +0300 | [diff] [blame] | 550 | * @param log_ctx The weston_log_context where to add. |
| 551 | * @param name The debug stream/scope name; must not be NULL. |
| 552 | * @param description The log scope description for humans; must not be NULL. |
Marius Vlad | 0c7beb0 | 2019-07-25 11:46:44 +0300 | [diff] [blame] | 553 | * @param new_subscription Optional callback when a client subscribes to this |
Marius Vlad | dad882a | 2019-07-17 15:43:53 +0300 | [diff] [blame] | 554 | * scope. |
Marius Vlad | 9bb1c3a | 2019-07-29 12:02:47 +0300 | [diff] [blame] | 555 | * @param destroy_subscription Optional callback when a client destroys the |
| 556 | * subscription. |
Marius Vlad | 9f71a4a | 2019-06-22 00:29:31 +0300 | [diff] [blame] | 557 | * @param user_data Optional user data pointer for the callback. |
| 558 | * @returns A valid pointer on success, NULL on failure. |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 559 | * |
Marius Vlad | 9f71a4a | 2019-06-22 00:29:31 +0300 | [diff] [blame] | 560 | * This function is used to create a log scope. All debug message printing |
| 561 | * happens for a scope, which allows clients to subscribe to the kind of debug |
| 562 | * messages they want by \c name. For the weston-debug protocol, |
| 563 | * subscription for the scope will happen automatically but for other types of |
| 564 | * streams, weston_log_subscribe() should be called as to create a subscription |
| 565 | * and tie it to the scope created by this function. |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 566 | * |
Marius Vlad | 9f71a4a | 2019-06-22 00:29:31 +0300 | [diff] [blame] | 567 | * \p name must be unique in the weston_compositor instance. \p name |
| 568 | * and \p description must both be provided. In case of the weston-debug |
| 569 | * protocol, the description is printed when a client asks for a list of |
| 570 | * supported log scopes. |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 571 | * |
Marius Vlad | 0c7beb0 | 2019-07-25 11:46:44 +0300 | [diff] [blame] | 572 | * \p new_subscription, if not NULL, is called when a client subscribes to the log |
Marius Vlad | 9f71a4a | 2019-06-22 00:29:31 +0300 | [diff] [blame] | 573 | * scope creating a debug stream. This is for log scopes that need to print |
| 574 | * messages as a response to a client appearing, e.g. printing a list of |
| 575 | * windows on demand or a static preamble. The argument \p user_data is |
| 576 | * passed in to the callback and is otherwise unused. |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 577 | * |
Marius Vlad | 0c7beb0 | 2019-07-25 11:46:44 +0300 | [diff] [blame] | 578 | * For one-shot debug streams, \c new_subscription should finally call |
Marius Vlad | dad882a | 2019-07-17 15:43:53 +0300 | [diff] [blame] | 579 | * weston_log_subscription_complete() to close the stream and tell the client |
| 580 | * the printing is complete. Otherwise the client expects more data to be |
| 581 | * written. The complete callback in weston_log_subscriber should be installed |
| 582 | * to trigger it and it is set-up automatically for the weston-debug protocol. |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 583 | * |
Marius Vlad | 9f71a4a | 2019-06-22 00:29:31 +0300 | [diff] [blame] | 584 | * As subscription can take place before creating the scope, any pending |
| 585 | * subscriptions to scope added by weston_log_subscribe(), will be checked |
| 586 | * against the scope being created and if found will be added to the scope's |
| 587 | * subscription list. |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 588 | * |
Leandro Ribeiro | f014964 | 2019-12-18 15:52:18 -0300 | [diff] [blame] | 589 | * The log scope must be destroyed using weston_log_scope_destroy() |
Marius Vlad | 9f71a4a | 2019-06-22 00:29:31 +0300 | [diff] [blame] | 590 | * before destroying the weston_compositor. |
| 591 | * |
| 592 | * @memberof weston_log_scope |
| 593 | * @sa weston_log_scope_cb, weston_log_subscribe |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 594 | */ |
Marius Vlad | 5d5e335 | 2019-04-17 13:05:38 +0300 | [diff] [blame] | 595 | WL_EXPORT struct weston_log_scope * |
Leandro Ribeiro | 5976dbb | 2019-12-18 15:21:03 -0300 | [diff] [blame] | 596 | weston_log_ctx_add_log_scope(struct weston_log_context *log_ctx, |
| 597 | const char *name, |
| 598 | const char *description, |
| 599 | weston_log_scope_cb new_subscription, |
| 600 | weston_log_scope_cb destroy_subscription, |
| 601 | void *user_data) |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 602 | { |
Marius Vlad | 5d5e335 | 2019-04-17 13:05:38 +0300 | [diff] [blame] | 603 | struct weston_log_scope *scope; |
Marius Vlad | 9f71a4a | 2019-06-22 00:29:31 +0300 | [diff] [blame] | 604 | struct weston_log_subscription *pending_sub = NULL; |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 605 | |
Marius Vlad | 1e2fda2 | 2019-04-07 19:07:16 +0300 | [diff] [blame] | 606 | if (!name || !description) { |
Marius Vlad | ef4c268 | 2019-06-23 15:48:54 +0300 | [diff] [blame] | 607 | fprintf(stderr, "Error: cannot add a debug scope without name or description.\n"); |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 608 | return NULL; |
| 609 | } |
| 610 | |
Marius Vlad | 3d7d978 | 2019-04-17 12:35:38 +0300 | [diff] [blame] | 611 | if (!log_ctx) { |
Marius Vlad | ef4c268 | 2019-06-23 15:48:54 +0300 | [diff] [blame] | 612 | fprintf(stderr, "Error: cannot add debug scope '%s', infra not initialized.\n", |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 613 | name); |
| 614 | return NULL; |
| 615 | } |
| 616 | |
Marius Vlad | 8f329e2 | 2019-06-21 14:57:02 +0300 | [diff] [blame] | 617 | if (weston_log_get_scope(log_ctx, name)){ |
Marius Vlad | ef4c268 | 2019-06-23 15:48:54 +0300 | [diff] [blame] | 618 | fprintf(stderr, "Error: debug scope named '%s' is already registered.\n", |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 619 | name); |
| 620 | return NULL; |
| 621 | } |
| 622 | |
| 623 | scope = zalloc(sizeof *scope); |
| 624 | if (!scope) { |
Marius Vlad | ef4c268 | 2019-06-23 15:48:54 +0300 | [diff] [blame] | 625 | fprintf(stderr, "Error adding debug scope '%s': out of memory.\n", |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 626 | name); |
| 627 | return NULL; |
| 628 | } |
| 629 | |
| 630 | scope->name = strdup(name); |
| 631 | scope->desc = strdup(description); |
Marius Vlad | 0c7beb0 | 2019-07-25 11:46:44 +0300 | [diff] [blame] | 632 | scope->new_subscription = new_subscription; |
Marius Vlad | 9bb1c3a | 2019-07-29 12:02:47 +0300 | [diff] [blame] | 633 | scope->destroy_subscription = destroy_subscription; |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 634 | scope->user_data = user_data; |
Marius Vlad | 7814f30 | 2019-06-21 14:20:15 +0300 | [diff] [blame] | 635 | wl_list_init(&scope->subscription_list); |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 636 | |
| 637 | if (!scope->name || !scope->desc) { |
Marius Vlad | ef4c268 | 2019-06-23 15:48:54 +0300 | [diff] [blame] | 638 | fprintf(stderr, "Error adding debug scope '%s': out of memory.\n", |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 639 | name); |
| 640 | free(scope->name); |
| 641 | free(scope->desc); |
| 642 | free(scope); |
| 643 | return NULL; |
| 644 | } |
| 645 | |
Marius Vlad | 3d7d978 | 2019-04-17 12:35:38 +0300 | [diff] [blame] | 646 | wl_list_insert(log_ctx->scope_list.prev, &scope->compositor_link); |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 647 | |
Marius Vlad | 9f71a4a | 2019-06-22 00:29:31 +0300 | [diff] [blame] | 648 | /* check if there are any pending subscriptions to this scope */ |
| 649 | while ((pending_sub = find_pending_subscription(log_ctx, scope->name)) != NULL) { |
Marius Vlad | 5ae0e62 | 2019-07-11 17:44:50 +0300 | [diff] [blame] | 650 | weston_log_subscription_create(pending_sub->owner, scope); |
Marius Vlad | 9f71a4a | 2019-06-22 00:29:31 +0300 | [diff] [blame] | 651 | |
| 652 | /* remove it from pending */ |
| 653 | weston_log_subscription_destroy_pending(pending_sub); |
| 654 | } |
| 655 | |
| 656 | |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 657 | return scope; |
| 658 | } |
| 659 | |
Leandro Ribeiro | f66685d | 2019-12-26 16:03:04 -0300 | [diff] [blame] | 660 | /** Register a new stream name, creating a log scope. |
| 661 | * |
| 662 | * @param compositor The compositor that contains the log context where the log |
| 663 | * scope will be linked. |
| 664 | * @param name The debug stream/scope name; must not be NULL. |
| 665 | * @param description The log scope description for humans; must not be NULL. |
| 666 | * @param new_subscription Optional callback when a client subscribes to this |
| 667 | * scope. |
| 668 | * @param destroy_subscription Optional callback when a client destroys the |
| 669 | * subscription. |
| 670 | * @param user_data Optional user data pointer for the callback. |
| 671 | * @returns A valid pointer on success, NULL on failure. |
| 672 | * |
| 673 | * This function works like weston_log_ctx_add_log_scope(), but the log scope |
| 674 | * created is linked to the log context of \c compositor. |
| 675 | * |
| 676 | * @memberof weston_compositor |
| 677 | * @sa weston_log_ctx_add_log_scope |
| 678 | */ |
| 679 | WL_EXPORT struct weston_log_scope * |
| 680 | weston_compositor_add_log_scope(struct weston_compositor *compositor, |
| 681 | const char *name, |
| 682 | const char *description, |
| 683 | weston_log_scope_cb new_subscription, |
| 684 | weston_log_scope_cb destroy_subscription, |
| 685 | void *user_data) |
| 686 | { |
| 687 | struct weston_log_scope *scope; |
| 688 | scope = weston_log_ctx_add_log_scope(compositor->weston_log_ctx, |
| 689 | name, description, |
| 690 | new_subscription, |
| 691 | destroy_subscription, |
| 692 | user_data); |
| 693 | return scope; |
| 694 | } |
| 695 | |
Marius Vlad | 3d7d978 | 2019-04-17 12:35:38 +0300 | [diff] [blame] | 696 | /** Destroy a log scope |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 697 | * |
Marius Vlad | 9f71a4a | 2019-06-22 00:29:31 +0300 | [diff] [blame] | 698 | * @param scope The log scope to destroy; may be NULL. |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 699 | * |
Marius Vlad | 9f71a4a | 2019-06-22 00:29:31 +0300 | [diff] [blame] | 700 | * Destroys the log scope, calling each stream's destroy callback if one was |
| 701 | * installed/created. |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 702 | * |
Marius Vlad | 9f71a4a | 2019-06-22 00:29:31 +0300 | [diff] [blame] | 703 | * @memberof weston_log_scope |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 704 | */ |
| 705 | WL_EXPORT void |
Leandro Ribeiro | f014964 | 2019-12-18 15:52:18 -0300 | [diff] [blame] | 706 | weston_log_scope_destroy(struct weston_log_scope *scope) |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 707 | { |
Marius Vlad | 7814f30 | 2019-06-21 14:20:15 +0300 | [diff] [blame] | 708 | struct weston_log_subscription *sub, *sub_tmp; |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 709 | |
| 710 | if (!scope) |
| 711 | return; |
| 712 | |
Marius Vlad | 7814f30 | 2019-06-21 14:20:15 +0300 | [diff] [blame] | 713 | wl_list_for_each_safe(sub, sub_tmp, &scope->subscription_list, source_link) { |
| 714 | /* destroy each subscription */ |
Leandro Ribeiro | 8c02ea1 | 2020-02-06 15:41:32 -0300 | [diff] [blame] | 715 | if (sub->owner->destroy_subscription) |
| 716 | sub->owner->destroy_subscription(sub->owner); |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 717 | |
Marius Vlad | 7814f30 | 2019-06-21 14:20:15 +0300 | [diff] [blame] | 718 | weston_log_subscription_destroy(sub); |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | wl_list_remove(&scope->compositor_link); |
| 722 | free(scope->name); |
| 723 | free(scope->desc); |
| 724 | free(scope); |
| 725 | } |
| 726 | |
| 727 | /** Are there any active subscriptions to the scope? |
| 728 | * |
Marius Vlad | 7e4db95 | 2019-04-17 13:47:06 +0300 | [diff] [blame] | 729 | * \param scope The log scope to check; may be NULL. |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 730 | * \return True if any streams are open for this scope, false otherwise. |
| 731 | * |
| 732 | * As printing some debugging messages may be relatively expensive, one |
| 733 | * can use this function to determine if there is a need to gather the |
| 734 | * debugging information at all. If this function returns false, all |
| 735 | * printing for this scope is dropped, so gathering the information is |
| 736 | * pointless. |
| 737 | * |
| 738 | * The return value of this function should not be stored, as new clients |
| 739 | * may subscribe to the debug scope later. |
| 740 | * |
| 741 | * If the given scope is NULL, this function will always return false, |
| 742 | * making it safe to use in teardown or destroy code, provided the |
| 743 | * scope is initialized to NULL before creation and set to NULL after |
| 744 | * destruction. |
| 745 | * |
Marius Vlad | 5d5e335 | 2019-04-17 13:05:38 +0300 | [diff] [blame] | 746 | * \memberof weston_log_scope |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 747 | */ |
| 748 | WL_EXPORT bool |
Marius Vlad | 7e4db95 | 2019-04-17 13:47:06 +0300 | [diff] [blame] | 749 | weston_log_scope_is_enabled(struct weston_log_scope *scope) |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 750 | { |
| 751 | if (!scope) |
| 752 | return false; |
| 753 | |
Marius Vlad | 7814f30 | 2019-06-21 14:20:15 +0300 | [diff] [blame] | 754 | return !wl_list_empty(&scope->subscription_list); |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 755 | } |
| 756 | |
Marius Vlad | dad882a | 2019-07-17 15:43:53 +0300 | [diff] [blame] | 757 | /** Close the stream's complete callback if one was installed/created. |
| 758 | * |
Marius Vlad | 8b8b803 | 2019-06-27 18:34:03 +0300 | [diff] [blame] | 759 | * @ingroup log |
Marius Vlad | dad882a | 2019-07-17 15:43:53 +0300 | [diff] [blame] | 760 | */ |
| 761 | WL_EXPORT void |
| 762 | weston_log_subscription_complete(struct weston_log_subscription *sub) |
| 763 | { |
| 764 | if (sub->owner && sub->owner->complete) |
| 765 | sub->owner->complete(sub->owner); |
| 766 | } |
| 767 | |
Marius Vlad | 9f71a4a | 2019-06-22 00:29:31 +0300 | [diff] [blame] | 768 | /** Close the log scope. |
| 769 | * |
| 770 | * @param scope The log scope to complete; may be NULL. |
| 771 | * |
| 772 | * Complete the log scope, calling each stream's complete callback if one was |
| 773 | * installed/created. This can be useful to signal the reading end that the |
| 774 | * data has been transmited and should no longer expect that written over the |
| 775 | * stream. Particularly useful for the weston-debug protocol. |
| 776 | * |
| 777 | * @memberof weston_log_scope |
Leandro Ribeiro | ac691a8 | 2019-12-18 15:21:03 -0300 | [diff] [blame] | 778 | * @sa weston_log_ctx_add_log_scope, weston_compositor_add_log_scope, |
| 779 | * weston_log_scope_destroy |
Marius Vlad | 9f71a4a | 2019-06-22 00:29:31 +0300 | [diff] [blame] | 780 | */ |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 781 | WL_EXPORT void |
Marius Vlad | 7814f30 | 2019-06-21 14:20:15 +0300 | [diff] [blame] | 782 | weston_log_scope_complete(struct weston_log_scope *scope) |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 783 | { |
Marius Vlad | 7814f30 | 2019-06-21 14:20:15 +0300 | [diff] [blame] | 784 | struct weston_log_subscription *sub; |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 785 | |
Marius Vlad | 7814f30 | 2019-06-21 14:20:15 +0300 | [diff] [blame] | 786 | if (!scope) |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 787 | return; |
| 788 | |
Marius Vlad | 7814f30 | 2019-06-21 14:20:15 +0300 | [diff] [blame] | 789 | wl_list_for_each(sub, &scope->subscription_list, source_link) |
Marius Vlad | dad882a | 2019-07-17 15:43:53 +0300 | [diff] [blame] | 790 | weston_log_subscription_complete(sub); |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 791 | } |
| 792 | |
Marius Vlad | 7e4db95 | 2019-04-17 13:47:06 +0300 | [diff] [blame] | 793 | /** Write log data for a scope |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 794 | * |
| 795 | * \param scope The debug scope to write for; may be NULL, in which case |
| 796 | * nothing will be written. |
Marius Vlad | a2dace2 | 2019-06-12 16:05:44 +0300 | [diff] [blame] | 797 | * \param[in] data Pointer to the data to write. |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 798 | * \param len Number of bytes to write. |
| 799 | * |
| 800 | * Writes the given data to all subscribed clients' streams. |
| 801 | * |
Marius Vlad | 5d5e335 | 2019-04-17 13:05:38 +0300 | [diff] [blame] | 802 | * \memberof weston_log_scope |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 803 | */ |
| 804 | WL_EXPORT void |
Marius Vlad | 7e4db95 | 2019-04-17 13:47:06 +0300 | [diff] [blame] | 805 | weston_log_scope_write(struct weston_log_scope *scope, |
| 806 | const char *data, size_t len) |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 807 | { |
Marius Vlad | 7814f30 | 2019-06-21 14:20:15 +0300 | [diff] [blame] | 808 | struct weston_log_subscription *sub; |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 809 | |
| 810 | if (!scope) |
| 811 | return; |
| 812 | |
Marius Vlad | 7814f30 | 2019-06-21 14:20:15 +0300 | [diff] [blame] | 813 | wl_list_for_each(sub, &scope->subscription_list, source_link) |
Marius Vlad | dad882a | 2019-07-17 15:43:53 +0300 | [diff] [blame] | 814 | weston_log_subscription_write(sub, data, len); |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 815 | } |
| 816 | |
| 817 | /** Write a formatted string for a scope (varargs) |
| 818 | * |
Marius Vlad | 7e4db95 | 2019-04-17 13:47:06 +0300 | [diff] [blame] | 819 | * \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] | 820 | * nothing will be written. |
| 821 | * \param fmt Printf-style format string. |
| 822 | * \param ap Formatting arguments. |
| 823 | * |
| 824 | * Writes to formatted string to all subscribed clients' streams. |
| 825 | * |
| 826 | * The behavioral details for each stream are the same as for |
| 827 | * weston_debug_stream_write(). |
| 828 | * |
Marius Vlad | 5d5e335 | 2019-04-17 13:05:38 +0300 | [diff] [blame] | 829 | * \memberof weston_log_scope |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 830 | */ |
Marius Vlad | 843b238 | 2019-08-06 17:37:51 +0300 | [diff] [blame] | 831 | WL_EXPORT int |
Marius Vlad | 7e4db95 | 2019-04-17 13:47:06 +0300 | [diff] [blame] | 832 | weston_log_scope_vprintf(struct weston_log_scope *scope, |
| 833 | const char *fmt, va_list ap) |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 834 | { |
| 835 | static const char oom[] = "Out of memory"; |
| 836 | char *str; |
Marius Vlad | 843b238 | 2019-08-06 17:37:51 +0300 | [diff] [blame] | 837 | int len = 0; |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 838 | |
Marius Vlad | 7e4db95 | 2019-04-17 13:47:06 +0300 | [diff] [blame] | 839 | if (!weston_log_scope_is_enabled(scope)) |
Marius Vlad | 843b238 | 2019-08-06 17:37:51 +0300 | [diff] [blame] | 840 | return len; |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 841 | |
| 842 | len = vasprintf(&str, fmt, ap); |
| 843 | if (len >= 0) { |
Marius Vlad | 7e4db95 | 2019-04-17 13:47:06 +0300 | [diff] [blame] | 844 | weston_log_scope_write(scope, str, len); |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 845 | free(str); |
| 846 | } else { |
Marius Vlad | 7e4db95 | 2019-04-17 13:47:06 +0300 | [diff] [blame] | 847 | weston_log_scope_write(scope, oom, sizeof oom - 1); |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 848 | } |
Marius Vlad | 843b238 | 2019-08-06 17:37:51 +0300 | [diff] [blame] | 849 | |
| 850 | return len; |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 851 | } |
| 852 | |
| 853 | /** Write a formatted string for a scope |
| 854 | * |
Marius Vlad | 7e4db95 | 2019-04-17 13:47:06 +0300 | [diff] [blame] | 855 | * \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] | 856 | * nothing will be written. |
| 857 | * \param fmt Printf-style format string and arguments. |
| 858 | * |
| 859 | * Writes to formatted string to all subscribed clients' streams. |
| 860 | * |
| 861 | * The behavioral details for each stream are the same as for |
| 862 | * weston_debug_stream_write(). |
| 863 | * |
Marius Vlad | 5d5e335 | 2019-04-17 13:05:38 +0300 | [diff] [blame] | 864 | * \memberof weston_log_scope |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 865 | */ |
Marius Vlad | 843b238 | 2019-08-06 17:37:51 +0300 | [diff] [blame] | 866 | WL_EXPORT int |
Marius Vlad | 7e4db95 | 2019-04-17 13:47:06 +0300 | [diff] [blame] | 867 | weston_log_scope_printf(struct weston_log_scope *scope, |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 868 | const char *fmt, ...) |
| 869 | { |
| 870 | va_list ap; |
Marius Vlad | 843b238 | 2019-08-06 17:37:51 +0300 | [diff] [blame] | 871 | int len; |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 872 | |
| 873 | va_start(ap, fmt); |
Marius Vlad | 843b238 | 2019-08-06 17:37:51 +0300 | [diff] [blame] | 874 | len = weston_log_scope_vprintf(scope, fmt, ap); |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 875 | va_end(ap); |
Marius Vlad | 843b238 | 2019-08-06 17:37:51 +0300 | [diff] [blame] | 876 | |
| 877 | return len; |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 878 | } |
| 879 | |
Marius Vlad | dad882a | 2019-07-17 15:43:53 +0300 | [diff] [blame] | 880 | /** Write a formatted string for a subscription |
| 881 | * |
| 882 | * \param sub The subscription to write for; may be NULL, in which case |
| 883 | * nothing will be written. |
| 884 | * \param fmt Printf-style format string and arguments. |
| 885 | * |
| 886 | * Writes to formatted string to the stream that created the subscription. |
| 887 | * |
Marius Vlad | 8b8b803 | 2019-06-27 18:34:03 +0300 | [diff] [blame] | 888 | * @ingroup log |
Marius Vlad | dad882a | 2019-07-17 15:43:53 +0300 | [diff] [blame] | 889 | */ |
| 890 | WL_EXPORT void |
| 891 | weston_log_subscription_printf(struct weston_log_subscription *sub, |
| 892 | const char *fmt, ...) |
| 893 | { |
| 894 | va_list ap; |
| 895 | |
| 896 | va_start(ap, fmt); |
| 897 | weston_log_subscription_vprintf(sub, fmt, ap); |
| 898 | va_end(ap); |
| 899 | } |
| 900 | |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 901 | /** Write debug scope name and current time into string |
| 902 | * |
Marius Vlad | a2dace2 | 2019-06-12 16:05:44 +0300 | [diff] [blame] | 903 | * \param[in] scope debug scope; may be NULL |
| 904 | * \param[out] buf Buffer to store the string. |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 905 | * \param len Available size in the buffer in bytes. |
| 906 | * \return \c buf |
| 907 | * |
| 908 | * Reads the current local wall-clock time and formats it into a string. |
| 909 | * and append the debug scope name to it, if a scope is available. |
| 910 | * The string is NUL-terminated, even if truncated. |
Marius Vlad | 8b8b803 | 2019-06-27 18:34:03 +0300 | [diff] [blame] | 911 | * |
| 912 | * @memberof weston_log_scope |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 913 | */ |
| 914 | WL_EXPORT char * |
Marius Vlad | 7e4db95 | 2019-04-17 13:47:06 +0300 | [diff] [blame] | 915 | weston_log_scope_timestamp(struct weston_log_scope *scope, |
| 916 | char *buf, size_t len) |
Pekka Paalanen | a5630ea | 2017-10-12 13:13:42 +0200 | [diff] [blame] | 917 | { |
| 918 | struct timeval tv; |
| 919 | struct tm *bdt; |
| 920 | char string[128]; |
| 921 | size_t ret = 0; |
| 922 | |
| 923 | gettimeofday(&tv, NULL); |
| 924 | |
| 925 | bdt = localtime(&tv.tv_sec); |
| 926 | if (bdt) |
| 927 | ret = strftime(string, sizeof string, |
| 928 | "%Y-%m-%d %H:%M:%S", bdt); |
| 929 | |
| 930 | if (ret > 0) { |
| 931 | snprintf(buf, len, "[%s.%03ld][%s]", string, |
| 932 | tv.tv_usec / 1000, |
| 933 | (scope) ? scope->name : "no scope"); |
| 934 | } else { |
| 935 | snprintf(buf, len, "[?][%s]", |
| 936 | (scope) ? scope->name : "no scope"); |
| 937 | } |
| 938 | |
| 939 | return buf; |
| 940 | } |
Marius Vlad | 9f71a4a | 2019-06-22 00:29:31 +0300 | [diff] [blame] | 941 | |
Leandro Ribeiro | 1ded661 | 2020-02-06 16:43:51 -0300 | [diff] [blame^] | 942 | /** Destroy a file type or a flight-rec type subscriber. |
| 943 | * |
| 944 | * They are created, respectively, with weston_log_subscriber_create_log() |
| 945 | * and weston_log_subscriber_create_flight_rec() |
| 946 | * |
| 947 | * @param subscriber the weston_log_subscriber object to destroy |
| 948 | * |
| 949 | * @ingroup log |
| 950 | */ |
| 951 | WL_EXPORT void |
| 952 | weston_log_subscriber_destroy(struct weston_log_subscriber *subscriber) |
| 953 | { |
| 954 | subscriber->destroy(subscriber); |
| 955 | } |
| 956 | |
Marius Vlad | 9f71a4a | 2019-06-22 00:29:31 +0300 | [diff] [blame] | 957 | /** Subscribe to a scope |
| 958 | * |
| 959 | * Creates a subscription which is used to subscribe the \p subscriber |
| 960 | * to the scope \c scope_name. |
| 961 | * |
| 962 | * If \c scope_name has already been created (using |
Leandro Ribeiro | f66685d | 2019-12-26 16:03:04 -0300 | [diff] [blame] | 963 | * weston_log_ctx_add_log_scope or weston_compositor_add_log_scope) the |
| 964 | * subscription will take place immediately, otherwise we store the |
| 965 | * subscription into a pending list. See also weston_log_ctx_add_log_scope() |
| 966 | * and weston_compositor_add_log_scope() |
Marius Vlad | 9f71a4a | 2019-06-22 00:29:31 +0300 | [diff] [blame] | 967 | * |
| 968 | * @param log_ctx the log context, used for accessing pending list |
| 969 | * @param subscriber the subscriber, which has to be created before |
| 970 | * @param scope_name the scope name. In case the scope is not created |
| 971 | * we temporarily store the subscription in the pending list. |
Marius Vlad | 8b8b803 | 2019-06-27 18:34:03 +0300 | [diff] [blame] | 972 | * |
| 973 | * @ingroup log |
Marius Vlad | 9f71a4a | 2019-06-22 00:29:31 +0300 | [diff] [blame] | 974 | */ |
| 975 | WL_EXPORT void |
| 976 | weston_log_subscribe(struct weston_log_context *log_ctx, |
| 977 | struct weston_log_subscriber *subscriber, |
| 978 | const char *scope_name) |
| 979 | { |
| 980 | assert(log_ctx); |
| 981 | assert(subscriber); |
| 982 | assert(scope_name); |
| 983 | |
| 984 | struct weston_log_scope *scope; |
Marius Vlad | 9f71a4a | 2019-06-22 00:29:31 +0300 | [diff] [blame] | 985 | |
| 986 | scope = weston_log_get_scope(log_ctx, scope_name); |
Marius Vlad | 5ae0e62 | 2019-07-11 17:44:50 +0300 | [diff] [blame] | 987 | if (scope) |
| 988 | weston_log_subscription_create(subscriber, scope); |
| 989 | else |
Marius Vlad | 9f71a4a | 2019-06-22 00:29:31 +0300 | [diff] [blame] | 990 | /* |
| 991 | * if we don't have already as scope for it, add it to pending |
| 992 | * subscription list |
| 993 | */ |
| 994 | weston_log_subscription_create_pending(subscriber, scope_name, log_ctx); |
Marius Vlad | 9f71a4a | 2019-06-22 00:29:31 +0300 | [diff] [blame] | 995 | } |
Marius Vlad | d0d89d0 | 2019-08-06 19:22:53 +0300 | [diff] [blame] | 996 | |
| 997 | /** Iterate over all subscriptions in a scope |
| 998 | * |
| 999 | * @param scope the scope for which you want to iterate |
| 1000 | * @param sub_iter the iterator, use NULL to start from the 'head' |
| 1001 | * @returns the next subscription from the log scope |
| 1002 | * |
| 1003 | * This is (quite) useful when 'log_scope' and 'log_subscription' are opaque. Do note |
| 1004 | * that \c sub_iter needs to be NULL-initialized before calling this function. |
| 1005 | * |
| 1006 | */ |
| 1007 | WL_EXPORT struct weston_log_subscription * |
| 1008 | weston_log_subscription_iterate(struct weston_log_scope *scope, |
| 1009 | struct weston_log_subscription *sub_iter) |
| 1010 | { |
| 1011 | struct wl_list *list = &scope->subscription_list; |
| 1012 | struct wl_list *node; |
| 1013 | |
| 1014 | /* go to the next item in the list or if not set starts with the head */ |
| 1015 | if (sub_iter) |
| 1016 | node = sub_iter->source_link.next; |
| 1017 | else |
| 1018 | node = list->next; |
| 1019 | |
| 1020 | assert(node); |
| 1021 | assert(!sub_iter || node != &sub_iter->source_link); |
| 1022 | |
| 1023 | /* if we're at the end */ |
| 1024 | if (node == list) |
| 1025 | return NULL; |
| 1026 | |
| 1027 | return container_of(node, struct weston_log_subscription, source_link); |
| 1028 | } |