Monado OpenXR Runtime
Loading...
Searching...
No Matches
u_pretty_print.h
Go to the documentation of this file.
1// Copyright 2022, Collabora, Ltd.
2// Copyright 2024-2026, NVIDIA CORPORATION.
3// SPDX-License-Identifier: BSL-1.0
4/*!
5 * @file
6 * @brief Pretty printing various Monado things.
7 * @author Jakob Bornecrantz <jakob@collabora.com>
8 * @ingroup aux_pretty
9 */
10#pragma once
11
12#include "xrt/xrt_defines.h"
13
14
15#ifdef __cplusplus
16extern "C" {
17#endif
18
19
20/*!
21 * @defgroup aux_pretty Pretty printing functions and helpers
22 * @ingroup aux_util
23 *
24 * This is common functionality used directly and shared by additional pretty
25 * printing functions implemented in multiple modules, such as @ref oxr_api.
26 *
27 * Some functions have a `_indented` suffix added to them, this means that what
28 * they print starts indented, but also they start with a newline. This is so
29 * they can easily be chained together to form a debug message printing out
30 * various information. Most of the final logging functions in Monado inserts a
31 * newline at the end of the message and we don't want two to be inserted.
32 *
33 * There are also helpers that goes from an enum to a string that that doesn't
34 * use the delegate to do the printing, these returns string that are compiled
35 * into the binary. They will return 'UNKNOWN' if they don't know the value.
36 * But can be made to return NULL on unknown.
37 */
38
39/*!
40 * Returns a string of the input name, or NULL if invalid.
41 *
42 * @ingroup aux_pretty
43 */
44const char *
46
47/*!
48 * Returns a string of the output name, or NULL if invalid.
49 *
50 * @ingroup aux_pretty
51 */
52const char *
54
55/*!
56 * Returns a string of the device name, or NULL if invalid.
57 *
58 * @ingroup aux_pretty
59 */
60const char *
62
63/*!
64 * Returns a string of the result, or NULL if invalid.
65 *
66 * @ingroup aux_pretty
67 */
68const char *
70
71#define U_STR_NO_NULL(NAME, TYPE) \
72 static inline const char *NAME(TYPE enumerate) \
73 { \
74 const char *str = NAME##_or_null(enumerate); \
75 return str != NULL ? str : "UNKNOWN"; \
76 }
77
78U_STR_NO_NULL(u_str_xrt_input_name, enum xrt_input_name)
79U_STR_NO_NULL(u_str_xrt_output_name, enum xrt_output_name)
80U_STR_NO_NULL(u_str_xrt_device_name, enum xrt_device_name)
81U_STR_NO_NULL(u_str_xrt_result, xrt_result_t)
82
83#undef U_STR_NO_NULL
84
85
86/*!
87 * Function prototype for receiving pretty printed strings.
88 *
89 * @note Do not keep a reference to the pointer as it's often allocated on the
90 * stack for speed.
91 *
92 * @ingroup aux_pretty
93 */
94typedef void (*u_pp_delegate_func_t)(void *ptr, const char *str, size_t length);
95
96/*!
97 * Helper struct to hold a function pointer and data pointer.
98 *
99 * @ingroup aux_pretty
100 */
102{
103 //! Userdata pointer, placed first to match D/Volt delegates.
104 void *ptr;
105
106 //! String receiving function.
108};
109
110/*!
111 * Helper typedef for delegate struct, less typing.
112 *
113 * @ingroup aux_pretty
114 */
116
117/*!
118 * Formats a string and sends to the delegate.
119 *
120 * @ingroup aux_pretty
121 */
122void
123u_pp(struct u_pp_delegate dg, const char *fmt, ...) XRT_PRINTF_FORMAT(2, 3);
124
125/*!
126 * Pretty prints the @ref xrt_input_name.
127 *
128 * @ingroup aux_pretty
129 */
130void
132
133/*!
134 * Pretty prints the @ref xrt_output_name.
135 *
136 * @ingroup aux_pretty
137 */
138void
140
141/*!
142 * Pretty prints the @ref xrt_result_t.
143 *
144 * @ingroup aux_pretty
145 */
146void
148
149/*!
150 * Pretty prints the @ref xrt_reference_space_type.
151 *
152 * @ingroup aux_pretty
153 */
154void
156
157/*!
158 * Pretty prints a single @ref xrt_swapchain_usage_bits bit, if no, multiple or
159 * an unknown bit is set it says so and prints the hex value.
160 *
161 * @ingroup aux_pretty
162 */
163void
165
166
167/*!
168 * Pretty prints a milliseconds padded to be at least 16 characters, the
169 * formatting is meant to be human readable, does not use locale.
170 *
171 * Formatted as: " M'TTT'###.FFFms"
172 * Zero: " 0.000ms"
173 *
174 * If the value is 10 seconds or larger (MM) then it will be longer then 16
175 * characters.
176 */
177void
178u_pp_padded_pretty_ms(u_pp_delegate_t dg, uint64_t value_ns);
179
180
181/*
182 *
183 * Math struct printers.
184 *
185 */
186
187/*!
188 * Printers for math structs. None of these functions inserts trailing newlines
189 * because it's hard to remove a trailing newline but easy to add one if one
190 * should be needed. The small functions do not insert a starting newline while
191 * the other functions does. This is so that you can easily chain print
192 * functions to print a struct.
193 *
194 * @note xrt_matrix_* parameters assumed to be column major.
195 *
196 * @ingroup aux_pretty
197 * @{
198 */
199void
200u_pp_small_vec3(u_pp_delegate_t dg, const struct xrt_vec3 *vec);
201
202void
203u_pp_small_pose(u_pp_delegate_t dg, const struct xrt_pose *pose);
204
205void
206u_pp_small_matrix_3x3(u_pp_delegate_t dg, const struct xrt_matrix_3x3 *m);
207
208void
209u_pp_small_matrix_4x4(u_pp_delegate_t dg, const struct xrt_matrix_4x4 *m);
210
211void
212u_pp_small_matrix_4x4_f64(u_pp_delegate_t dg, const struct xrt_matrix_4x4_f64 *m);
213
214void
215u_pp_small_array_f64(struct u_pp_delegate dg, const double *arr, size_t n);
216
217void
218u_pp_small_array2d_f64(struct u_pp_delegate dg, const double *arr, size_t n, size_t m);
219
220void
221u_pp_vec3(u_pp_delegate_t dg, const struct xrt_vec3 *vec, const char *name, const char *indent);
222
223void
224u_pp_pose(u_pp_delegate_t dg, const struct xrt_pose *pose, const char *name, const char *indent);
225
226void
227u_pp_matrix_3x3(u_pp_delegate_t dg, const struct xrt_matrix_3x3 *m, const char *name, const char *indent);
228
229void
230u_pp_matrix_4x4(u_pp_delegate_t dg, const struct xrt_matrix_4x4 *m, const char *name, const char *indent);
231
232void
233u_pp_matrix_4x4_f64(u_pp_delegate_t dg, const struct xrt_matrix_4x4_f64 *m, const char *name, const char *indent);
234
235//! Pretty prints `double arr[n]`
236void
237u_pp_array_f64(u_pp_delegate_t dg, const double *arr, size_t n, const char *name, const char *indent);
238
239//! Pretty prints `double arr[n][m]`
240void
241u_pp_array2d_f64(u_pp_delegate_t dg, const double *arr, size_t n, size_t m, const char *name, const char *indent);
242
243/*!
244 * @}
245 */
246
247
248/*
249 *
250 * Extension list printers.
251 *
252 */
253
254struct u_extension_list;
255
256/*!
257 * @brief Print all the strings in the list with the given prefix.
258 *
259 * @param dg delegate to use for printing
260 * @param usl list of strings to print (must not be NULL)
261 * @param prefix prefix to add to each string to be printed (must not be NULL)
262 * @ingroup aux_pretty
263 */
264XRT_NONNULL_ALL void
265u_pp_string_list(struct u_pp_delegate dg, struct u_extension_list *usl, const char *prefix);
266
267/*!
268 * @brief Pretty print the extension list with extension information.
269 *
270 * It will start on a new line with the enabled extensions, showing which are
271 * required vs optional. Then if there are optional extensions not enabled it
272 * will list those separately, distinguishing between unsupported and skipped.
273 *
274 * @param dg delegate to use for printing
275 * @param enabled_list list of extensions that were enabled (must not be NULL)
276 * @param optional_list optional list to compare against (must not be NULL)
277 * @param skipped_list list of extensions that were skipped (must not be NULL)
278 * @ingroup aux_pretty
279 */
280XRT_NONNULL_ALL void
282 struct u_extension_list *enabled_list,
283 struct u_extension_list *optional_list,
284 struct u_extension_list *skipped_list);
285
286
287/*
288 *
289 * Sinks.
290 *
291 */
292
293/*!
294 * Stack only pretty printer sink, no need to free, must be inited before use.
295 *
296 * @ingroup aux_pretty
297 */
299{
300 //! How much of the buffer is used.
301 size_t used;
302
303 //! Storage for the sink.
304 char buffer[1024 * 8];
305};
306
308u_pp_sink_stack_only_init(struct u_pp_sink_stack_only *sink);
309
310
311#ifdef __cplusplus
312}
313#endif
XRT_NONNULL_ALL void u_pp_string_list(struct u_pp_delegate dg, struct u_extension_list *usl, const char *prefix)
Print all the strings in the list with the given prefix.
Definition u_pretty_print.c:541
XRT_NONNULL_ALL void u_pp_string_list_extensions(struct u_pp_delegate dg, struct u_extension_list *enabled_list, struct u_extension_list *optional_list, struct u_extension_list *skipped_list)
Pretty print the extension list with extension information.
Definition u_pretty_print.c:552
void u_pp_small_vec3(u_pp_delegate_t dg, const struct xrt_vec3 *vec)
Printers for math structs.
Definition u_pretty_print.c:372
void u_pp_array_f64(u_pp_delegate_t dg, const double *arr, size_t n, const char *name, const char *indent)
Pretty prints double arr[n]
Definition u_pretty_print.c:520
void u_pp_array2d_f64(u_pp_delegate_t dg, const double *arr, size_t n, size_t m, const char *name, const char *indent)
Pretty prints double arr[n][m]
Definition u_pretty_print.c:527
const char * u_str_xrt_output_name_or_null(enum xrt_output_name name)
Returns a string of the output name, or NULL if invalid.
Definition u_pretty_print.c:127
const char * u_str_xrt_device_name_or_null(enum xrt_device_name name)
Returns a string of the device name, or NULL if invalid.
Definition u_pretty_print.c:138
const char * u_str_xrt_result_or_null(xrt_result_t xret)
Returns a string of the result, or NULL if invalid.
Definition u_pretty_print.c:149
void u_pp_xrt_output_name(struct u_pp_delegate dg, enum xrt_output_name name)
Pretty prints the xrt_output_name.
Definition u_pretty_print.c:227
void(* u_pp_delegate_func_t)(void *ptr, const char *str, size_t length)
Function prototype for receiving pretty printed strings.
Definition u_pretty_print.h:94
void u_pp(struct u_pp_delegate dg, const char *fmt,...) XRT_PRINTF_FORMAT(2
Formats a string and sends to the delegate.
void u_pp_xrt_result(struct u_pp_delegate dg, xrt_result_t xret)
Pretty prints the xrt_result_t.
Definition u_pretty_print.c:247
void u_pp_xrt_swapchain_usage_bits(struct u_pp_delegate dg, enum xrt_swapchain_usage_bits bits)
Pretty prints a single xrt_swapchain_usage_bits bit, if no, multiple or an unknown bit is set it says...
Definition u_pretty_print.c:292
const char * u_str_xrt_input_name_or_null(enum xrt_input_name name)
Returns a string of the input name, or NULL if invalid.
Definition u_pretty_print.c:116
void void u_pp_xrt_input_name(struct u_pp_delegate dg, enum xrt_input_name name)
Pretty prints the xrt_input_name.
Definition u_pretty_print.c:207
void u_pp_xrt_reference_space_type(struct u_pp_delegate dg, enum xrt_reference_space_type type)
Pretty prints the xrt_reference_space_type.
Definition u_pretty_print.c:267
xrt_input_name
Every internal input source known to monado with a baked in type.
Definition xrt_defines.h:1018
enum xrt_result xrt_result_t
Result type used across Monado.
xrt_reference_space_type
Type of a OpenXR mapped reference space, maps to the semantic spaces on the xrt_space_overseer struct...
Definition xrt_defines.h:655
xrt_output_name
Name of a output with a baked in type.
Definition xrt_defines.h:1702
Definition u_extension_list.cpp:33
Helper struct to hold a function pointer and data pointer.
Definition u_pretty_print.h:102
u_pp_delegate_func_t func
String receiving function.
Definition u_pretty_print.h:107
void * ptr
Userdata pointer, placed first to match D/Volt delegates.
Definition u_pretty_print.h:104
Stack only pretty printer sink, no need to free, must be inited before use.
Definition u_pretty_print.h:299
size_t used
How much of the buffer is used.
Definition u_pretty_print.h:301
A tightly packed 3x3 matrix of floats.
Definition xrt_defines.h:589
A tightly packed 4x4 matrix of double.
Definition xrt_defines.h:626
A tightly packed 4x4 matrix of floats.
Definition xrt_defines.h:616
A pose composed of a position and orientation.
Definition xrt_defines.h:522
A 3 element vector with single floats.
Definition xrt_defines.h:310
void u_pp_padded_pretty_ms(u_pp_delegate_t dg, uint64_t value_ns)
Pretty prints a milliseconds padded to be at least 16 characters, the formatting is meant to be human...
Definition u_pretty_print.c:322
Common defines and enums for XRT.
xrt_swapchain_usage_bits
Usage of the swapchain images.
Definition xrt_defines.h:792
xrt_device_name
A enum that is used to name devices so that the state trackers can reason about the devices easier.
Definition xrt_defines.h:824