Monado OpenXR Runtime
u_pretty_print.h
Go to the documentation of this file.
1// Copyright 2022, Collabora, Ltd.
2// Copyright 2024-2025, 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#define U_STR_NO_NULL(NAME, TYPE) \
64 static inline const char *NAME(TYPE enumerate) \
65 { \
66 const char *str = NAME##_or_null(enumerate); \
67 return str != NULL ? str : "UNKNOWN"; \
68 }
69
70U_STR_NO_NULL(u_str_xrt_input_name, enum xrt_input_name)
71U_STR_NO_NULL(u_str_xrt_output_name, enum xrt_output_name)
72U_STR_NO_NULL(u_str_xrt_device_name, enum xrt_device_name)
73
74#undef U_STR_NO_NULL
75
76
77/*!
78 * Function prototype for receiving pretty printed strings.
79 *
80 * @note Do not keep a reference to the pointer as it's often allocated on the
81 * stack for speed.
82 *
83 * @ingroup aux_pretty
84 */
85typedef void (*u_pp_delegate_func_t)(void *ptr, const char *str, size_t length);
86
87/*!
88 * Helper struct to hold a function pointer and data pointer.
89 *
90 * @ingroup aux_pretty
91 */
93{
94 //! Userdata pointer, placed first to match D/Volt delegates.
95 void *ptr;
96
97 //! String receiving function.
99};
100
101/*!
102 * Helper typedef for delegate struct, less typing.
103 *
104 * @ingroup aux_pretty
105 */
107
108/*!
109 * Formats a string and sends to the delegate.
110 *
111 * @ingroup aux_pretty
112 */
113void
114u_pp(struct u_pp_delegate dg, const char *fmt, ...) XRT_PRINTF_FORMAT(2, 3);
115
116/*!
117 * Pretty prints the @ref xrt_input_name.
118 *
119 * @ingroup aux_pretty
120 */
121void
123
124/*!
125 * Pretty prints the @ref xrt_output_name.
126 *
127 * @ingroup aux_pretty
128 */
129void
131
132/*!
133 * Pretty prints the @ref xrt_result_t.
134 *
135 * @ingroup aux_pretty
136 */
137void
139
140/*!
141 * Pretty prints the @ref xrt_reference_space_type.
142 *
143 * @ingroup aux_pretty
144 */
145void
147
148
149/*!
150 * Pretty prints a milliseconds padded to be at least 16 characters, the
151 * formatting is meant to be human readable, does not use locale.
152 *
153 * Formatted as: " M'TTT'###.FFFms"
154 * Zero: " 0.000ms"
155 *
156 * If the value is 10 seconds or larger (MM) then it will be longer then 16
157 * characters.
158 */
159void
160u_pp_padded_pretty_ms(u_pp_delegate_t dg, uint64_t value_ns);
161
162
163/*
164 *
165 * Math struct printers.
166 *
167 */
168
169/*!
170 * Printers for math structs. None of these functions inserts trailing newlines
171 * because it's hard to remove a trailing newline but easy to add one if one
172 * should be needed. The small functions do not insert a starting newline while
173 * the other functions does. This is so that you can easily chain print
174 * functions to print a struct.
175 *
176 * @note xrt_matrix_* parameters assumed to be column major.
177 *
178 * @ingroup aux_pretty
179 * @{
180 */
181void
182u_pp_small_vec3(u_pp_delegate_t dg, const struct xrt_vec3 *vec);
183
184void
185u_pp_small_pose(u_pp_delegate_t dg, const struct xrt_pose *pose);
186
187void
188u_pp_small_matrix_3x3(u_pp_delegate_t dg, const struct xrt_matrix_3x3 *m);
189
190void
191u_pp_small_matrix_4x4(u_pp_delegate_t dg, const struct xrt_matrix_4x4 *m);
192
193void
194u_pp_small_matrix_4x4_f64(u_pp_delegate_t dg, const struct xrt_matrix_4x4_f64 *m);
195
196void
197u_pp_small_array_f64(struct u_pp_delegate dg, const double *arr, size_t n);
198
199void
200u_pp_small_array2d_f64(struct u_pp_delegate dg, const double *arr, size_t n, size_t m);
201
202void
203u_pp_vec3(u_pp_delegate_t dg, const struct xrt_vec3 *vec, const char *name, const char *indent);
204
205void
206u_pp_pose(u_pp_delegate_t dg, const struct xrt_pose *pose, const char *name, const char *indent);
207
208void
209u_pp_matrix_3x3(u_pp_delegate_t dg, const struct xrt_matrix_3x3 *m, const char *name, const char *indent);
210
211void
212u_pp_matrix_4x4(u_pp_delegate_t dg, const struct xrt_matrix_4x4 *m, const char *name, const char *indent);
213
214void
215u_pp_matrix_4x4_f64(u_pp_delegate_t dg, const struct xrt_matrix_4x4_f64 *m, const char *name, const char *indent);
216
217//! Pretty prints `double arr[n]`
218void
219u_pp_array_f64(u_pp_delegate_t dg, const double *arr, size_t n, const char *name, const char *indent);
220
221//! Pretty prints `double arr[n][m]`
222void
223u_pp_array2d_f64(u_pp_delegate_t dg, const double *arr, size_t n, size_t m, const char *name, const char *indent);
224
225/*!
226 * @}
227 */
228
229
230/*
231 *
232 * Sinks.
233 *
234 */
235
236/*!
237 * Stack only pretty printer sink, no need to free, must be inited before use.
238 *
239 * @ingroup aux_pretty
240 */
242{
243 //! How much of the buffer is used.
244 size_t used;
245
246 //! Storage for the sink.
247 char buffer[1024 * 8];
248};
249
251u_pp_sink_stack_only_init(struct u_pp_sink_stack_only *sink);
252
253
254#ifdef __cplusplus
255}
256#endif
void u_pp_small_vec3(u_pp_delegate_t dg, const struct xrt_vec3 *vec)
Printers for math structs.
Definition: u_pretty_print.c:362
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:510
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:517
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:107
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:123
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:201
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:85
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:221
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:91
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:181
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:287
xrt_input_name
Every internal input source known to monado with a baked in type.
Definition: xrt_defines.h:1356
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:612
xrt_output_name
Name of a output with a baked in type.
Definition: xrt_defines.h:1591
Helper struct to hold a function pointer and data pointer.
Definition: u_pretty_print.h:93
u_pp_delegate_func_t func
String receiving function.
Definition: u_pretty_print.h:98
void * ptr
Userdata pointer, placed first to match D/Volt delegates.
Definition: u_pretty_print.h:95
Stack only pretty printer sink, no need to free, must be inited before use.
Definition: u_pretty_print.h:242
size_t used
How much of the buffer is used.
Definition: u_pretty_print.h:244
A tightly packed 3x3 matrix of floats.
Definition: xrt_defines.h:546
A tightly packed 4x4 matrix of double.
Definition: xrt_defines.h:583
A tightly packed 4x4 matrix of floats.
Definition: xrt_defines.h:573
A pose composed of a position and orientation.
Definition: xrt_defines.h:479
A 3 element vector with single floats.
Definition: xrt_defines.h:289
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:312
Common defines and enums for XRT.
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:801