5b7a4ca769f636758241c2151b3a0f3e998f2da7
[AGL/meta-agl.git] / meta-agl-core / recipes-graphics / wayland / weston / 0001-libweston-weston-log-Add-a-iterator-helper-for-debug.patch
1 From d5168b8eb0d881a0a6029c0b348a739147317238 Mon Sep 17 00:00:00 2001
2 From: Marius Vlad <marius.vlad@collabora.com>
3 Date: Mon, 29 May 2023 16:30:02 +0300
4 Subject: [PATCH] libweston/weston-log: Add a iterator helper for debug scope
5
6 This adds three new helpers: one to iterate over all debug scopes
7 created/added and other two are for simpler getters for the scope name
8 and the description.
9
10 Included with this change is also a simple test to retrieve them.
11
12 This is an alternative to using the debug scope list advertised when
13 using the weston-debug private extension. libweston users can use this
14 directly to know which scopes they can subscribe to, and there's no need
15 to have a client implementation for the weston-debug protocol.
16
17 Signed-off-by: Marius Vlad <marius.vlad@collabora.com>
18 ---
19  include/libweston/weston-log.h    | 10 ++++
20  libweston/weston-log.c            | 63 +++++++++++++++++++++++
21  tests/iterate-debug-scopes-test.c | 84 +++++++++++++++++++++++++++++++
22  tests/meson.build                 |  6 +++
23  4 files changed, 163 insertions(+)
24  create mode 100644 tests/iterate-debug-scopes-test.c
25
26 diff --git a/include/libweston/weston-log.h b/include/libweston/weston-log.h
27 index aeb7768b..70f41675 100644
28 --- a/include/libweston/weston-log.h
29 +++ b/include/libweston/weston-log.h
30 @@ -134,6 +134,16 @@ weston_log_subscription_iterate(struct weston_log_scope *scope,
31  void
32  weston_log_flight_recorder_display_buffer(FILE *file);
33  
34 +const char *
35 +weston_log_scope_get_description(struct weston_log_scope *scope);
36 +
37 +const char *
38 +weston_log_scope_get_name(struct weston_log_scope *scope);
39 +
40 +struct weston_log_scope *
41 +weston_log_scopes_iterate(struct weston_log_context *log_ctx,
42 +                          struct weston_log_scope *nscope);
43 +
44  #ifdef  __cplusplus
45  }
46  #endif
47 diff --git a/libweston/weston-log.c b/libweston/weston-log.c
48 index 276fde26..8b4c78a5 100644
49 --- a/libweston/weston-log.c
50 +++ b/libweston/weston-log.c
51 @@ -1009,3 +1009,66 @@ weston_log_subscription_iterate(struct weston_log_scope *scope,
52  
53         return container_of(node, struct weston_log_subscription, source_link);
54  }
55 +
56 +/** Iterate over all debug scopes added to a weston_log_context
57 + *
58 + * @param log_ctx the log context
59 + * @param nscope the iterator, use NULL to start from the head of the list
60 + * @returns the next log scope from list added to weston_log_ctx
61 + *
62 + * Note that that \c nscope needs to be NULL-initialized before calling
63 + * this function.
64 + *
65 + * This helper can be used by libweston users to grab all the debug scopes
66 + * created. This would be an alternative to using weston-debug private
67 + * extension.
68 + *
69 + */
70 +WL_EXPORT struct weston_log_scope *
71 +weston_log_scopes_iterate(struct weston_log_context *log_ctx,
72 +                          struct weston_log_scope *nscope)
73 +{
74 +        struct wl_list *list;
75 +        struct wl_list *node;
76 +
77 +        assert(log_ctx);
78 +
79 +        list = &log_ctx->scope_list;
80 +
81 +        if (nscope) {
82 +                node = nscope->compositor_link.next;
83 +        } else {
84 +                node = list->next;
85 +        }
86 +
87 +       assert(node);
88 +       assert(!nscope || node != &nscope->compositor_link);
89 +
90 +        if (node == list)
91 +                return NULL;
92 +
93 +        return container_of(node, struct weston_log_scope, compositor_link);
94 +}
95 +
96 +/** Helper to retrieve, in human readable form,  the name of a log scope
97 + *
98 + * @param scope the scope in question
99 + * @returns the name of the scope as a pointer to a string
100 + */
101 +WL_EXPORT const char *
102 +weston_log_scope_get_name(struct weston_log_scope *scope)
103 +{
104 +        return scope->name;
105 +}
106 +
107 +/** Helper to retreive, in human reable form, the description of a log scope
108 + *
109 + * @param scope the scope in question
110 + * @returns the description of the scope as pointer to a string
111 + *
112 + */
113 +WL_EXPORT const char *
114 +weston_log_scope_get_description(struct weston_log_scope *scope)
115 +{
116 +        return scope->desc;
117 +}
118 diff --git a/tests/iterate-debug-scopes-test.c b/tests/iterate-debug-scopes-test.c
119 new file mode 100644
120 index 00000000..82c6c5c8
121 --- /dev/null
122 +++ b/tests/iterate-debug-scopes-test.c
123 @@ -0,0 +1,84 @@
124 +/*
125 + * Copyright 2023 Collabora, Ltd.
126 + *
127 + * Permission is hereby granted, free of charge, to any person obtaining
128 + * a copy of this software and associated documentation files (the
129 + * "Software"), to deal in the Software without restriction, including
130 + * without limitation the rights to use, copy, modify, merge, publish,
131 + * distribute, sublicense, and/or sell copies of the Software, and to
132 + * permit persons to whom the Software is furnished to do so, subject to
133 + * the following conditions:
134 + *
135 + * The above copyright notice and this permission notice (including the
136 + * next paragraph) shall be included in all copies or substantial
137 + * portions of the Software.
138 + *
139 + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
140 + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
141 + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
142 + * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
143 + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
144 + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
145 + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
146 + * SOFTWARE.
147 + */
148 +#include "config.h"
149 +
150 +#include <unistd.h>
151 +#include <assert.h>
152 +#include <stdlib.h>
153 +#include <stdio.h>
154 +#include <string.h>
155 +
156 +#include <libweston/weston-log.h>
157 +#include "weston-test-client-helper.h"
158 +#include "weston-test-fixture-compositor.h"
159 +
160 +
161 +static enum test_result_code
162 +fixture_setup(struct weston_test_harness *harness)
163 +{
164 +       struct compositor_setup setup;
165 +
166 +       compositor_setup_defaults(&setup);
167 +       setup.shell = SHELL_TEST_DESKTOP;
168 +
169 +       return weston_test_harness_execute_as_plugin(harness, &setup);
170 +}
171 +
172 +DECLARE_FIXTURE_SETUP(fixture_setup);
173 +
174 +static void
175 +iterate_debug_scopes(struct weston_compositor *compositor)
176 +{
177 +       struct weston_log_scope *nscope = NULL;
178 +       const char *test_harness_scope = "test-harness-plugin";
179 +       bool found_test_harness_debug_scope = false;
180 +       struct weston_log_context *log_ctx = compositor->weston_log_ctx;
181 +
182 +       weston_log("Printing available debug scopes:\n");
183 +
184 +       while ((nscope = weston_log_scopes_iterate(log_ctx, nscope))) {
185 +               const char *scope_name;
186 +               const char *desc_name;
187 +
188 +               scope_name = weston_log_scope_get_name(nscope);
189 +               assert(scope_name);
190 +
191 +               desc_name = weston_log_scope_get_description(nscope);
192 +               assert(desc_name);
193 +
194 +               weston_log("\tscope name: %s, desc: %s\n", scope_name, desc_name);
195 +
196 +               if (strcmp(test_harness_scope, scope_name) == 0)
197 +                       found_test_harness_debug_scope = true;
198 +       }
199 +       weston_log("\n");
200 +
201 +       assert(found_test_harness_debug_scope);
202 +}
203 +
204 +PLUGIN_TEST(iterate_default_debug_scopes)
205 +{
206 +       iterate_debug_scopes(compositor);
207 +}
208 diff --git a/tests/meson.build b/tests/meson.build
209 index d8e96e77..e52ff5a6 100644
210 --- a/tests/meson.build
211 +++ b/tests/meson.build
212 @@ -224,6 +224,12 @@ tests = [
213                 ],
214                 'dep_objs': [ dep_lib_desktop ]
215         },
216 +       {       'name': 'iterate-debug-scopes',
217 +               'sources': [
218 +                 'iterate-debug-scopes-test.c',
219 +               ],
220 +               'dep_objs': [ dep_libweston_public ]
221 +       },
222  ]
223  
224  tests_standalone = [
225 -- 
226 2.40.1
227