The open source OpenXR runtime
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

u/logging: Copy CHK macros and print helpers from IPC

These are basically direct copies from the IPC versions, why the
Collabora copyright was updated.

Part-of: <https://gitlab.freedesktop.org/monado/monado/-/merge_requests/2483>

+140 -2
+33 -1
src/xrt/auxiliary/util/u_logging.c
··· 1 - // Copyright 2019-2020, Collabora, Ltd. 1 + // Copyright 2019-2025, Collabora, Ltd. 2 2 // SPDX-License-Identifier: BSL-1.0 3 3 /*! 4 4 * @file ··· 12 12 #include "xrt/xrt_config_build.h" 13 13 14 14 #include "util/u_debug.h" 15 + #include "util/u_pretty_print.h" 15 16 #include "u_json.h" 16 17 #include "util/u_truncate_printf.h" 17 18 ··· 421 422 * 'Exported' functions. 422 423 * 423 424 */ 425 + 426 + void 427 + u_log_print_result(enum u_logging_level cond_level, 428 + const char *file, 429 + int line, 430 + const char *calling_fn, 431 + xrt_result_t xret, 432 + const char *called_fn) 433 + { 434 + bool success = xret == XRT_SUCCESS; 435 + enum u_logging_level level = success ? U_LOGGING_INFO : U_LOGGING_ERROR; 436 + 437 + // Should we be logging? 438 + if (level < cond_level) { 439 + return; 440 + } 441 + 442 + struct u_pp_sink_stack_only sink; 443 + u_pp_delegate_t dg = u_pp_sink_stack_only_init(&sink); 444 + 445 + if (success) { 446 + u_pp(dg, "%s: ", called_fn); 447 + } else { 448 + u_pp(dg, "%s failed: ", called_fn); 449 + } 450 + 451 + u_pp_xrt_result(dg, xret); 452 + u_pp(dg, " [%s:%i]", file, line); 453 + 454 + u_log(file, line, calling_fn, level, "%s", sink.buffer); 455 + } 424 456 425 457 void 426 458 u_log(const char *file, int line, const char *func, enum u_logging_level level, const char *format, ...)
+107 -1
src/xrt/auxiliary/util/u_logging.h
··· 1 - // Copyright 2020-2024, Collabora, Ltd. 1 + // Copyright 2020-2025, Collabora, Ltd. 2 2 // SPDX-License-Identifier: BSL-1.0 3 3 /*! 4 4 * @file ··· 12 12 #pragma once 13 13 14 14 #include "xrt/xrt_compiler.h" 15 + #include "xrt/xrt_results.h" 15 16 16 17 #include "util/u_pretty_print.h" 17 18 ··· 176 177 } \ 177 178 } while (false) 178 179 180 + /*! 181 + * This define will error if `XRET` is not `XRT_SUCCESS`, printing out that the 182 + * @p FUNC_STR string has failed, then returns @p XRET. 183 + * 184 + * @param COND_LEVEL Log level used for @p cond_level, in logging helpers. 185 + * @param XRET The @p xrt_result_t to check. 186 + * @param FUNC_STR String literal with the function name, used for logging. 187 + */ 188 + #define U_LOG_CHK_AND_RET(COND_LEVEL, XRET, FUNC_STR) \ 189 + do { \ 190 + xrt_result_t _ret = XRET; \ 191 + if (_ret != XRT_SUCCESS) { \ 192 + u_log_print_result(COND_LEVEL, __FILE__, __LINE__, __func__, _ret, FUNC_STR); \ 193 + return _ret; \ 194 + } \ 195 + } while (false) 196 + 197 + /*! 198 + * This define will error if `XRET` is not `XRT_SUCCESS`, printing out that the 199 + * @p FUNC_STR string has failed, then gotos @p GOTO. 200 + * 201 + * @param COND_LEVEL Log level used for @p cond_level, in logging helpers. 202 + * @param XRET The @p xrt_result_t to check. 203 + * @param FUNC_STR String literal with the function name, used for logging. 204 + * @param GOTO Goto label to jump to on error. 205 + */ 206 + #define U_LOG_CHK_WITH_GOTO(COND_LEVEL, XRET, FUNC_STR, GOTO) \ 207 + do { \ 208 + xrt_result_t _ret = XRET; \ 209 + if (_ret != XRT_SUCCESS) { \ 210 + u_log_print_result(COND_LEVEL, __FILE__, __LINE__, __func__, _ret, FUNC_STR); \ 211 + goto GOTO; \ 212 + } \ 213 + } while (false) 214 + 215 + /*! 216 + * This define will error if `XRET` is not `XRT_SUCCESS`, printing out that the 217 + * @p FUNC_STR string has failed, then returns @p RET. 218 + * 219 + * @param COND_LEVEL Log level used for @p cond_level, in logging helpers. 220 + * @param XRET The @p xrt_result_t to check. 221 + * @param FUNC_STR String literal with the function name, used for logging. 222 + * @param RET The value that is returned on error. 223 + */ 224 + #define U_LOG_CHK_WITH_RET(COND_LEVEL, XRET, FUNC_STR, RET) \ 225 + do { \ 226 + xrt_result_t _ret = XRET; \ 227 + if (_ret != XRT_SUCCESS) { \ 228 + u_log_print_result(COND_LEVEL, __FILE__, __LINE__, __func__, _ret, FUNC_STR); \ 229 + return RET; \ 230 + } \ 231 + } while (false) 232 + 233 + /*! 234 + * This define will error if `XRET` is not `XRT_SUCCESS`, printing out that the 235 + * @p FUNC_STR string has failed, it only prints and does nothing else. 236 + * 237 + * @param COND_LEVEL Log level used for @p cond_level, in logging helpers. 238 + * @param XRET The @p xrt_result_t to check. 239 + * @param FUNC_STR String literal with the function name, used for logging. 240 + */ 241 + #define U_LOG_CHK_ONLY_PRINT(COND_LEVEL, XRET, FUNC_STR) \ 242 + do { \ 243 + xrt_result_t _ret = XRET; \ 244 + if (_ret != XRT_SUCCESS) { \ 245 + u_log_print_result(COND_LEVEL, __FILE__, __LINE__, __func__, _ret, FUNC_STR); \ 246 + } \ 247 + } while (false) 248 + 249 + /*! 250 + * This define will error if `XRET` is not `XRT_SUCCESS`, printing out that the 251 + * @p FUNC_STR string has failed, then it will always return the value. 252 + * 253 + * @param COND_LEVEL Log level used for @p cond_level, in logging helpers. 254 + * @param XRET The @p xrt_result_t to check and always return. 255 + * @param FUNC_STR String literal with the function name, used for logging. 256 + */ 257 + #define U_LOG_CHK_ALWAYS_RET(COND_LEVEL, XRET, FUNC_STR) \ 258 + do { \ 259 + xrt_result_t _ret = XRET; \ 260 + if (_ret != XRT_SUCCESS) { \ 261 + u_log_print_result(COND_LEVEL, __FILE__, __LINE__, __func__, _ret, FUNC_STR); \ 262 + } \ 263 + return _ret; \ 264 + } while (false) 179 265 180 266 /*! 181 267 * Returns the global logging level, subsystems own logging level take precedence. ··· 271 357 */ 272 358 void 273 359 u_log_set_sink(u_log_sink_func_t func, void *data); 360 + 361 + /*! 362 + * Helper to print the results of called functions that return xret results, if 363 + * the result is @p XRT_SUCCESS will log with info, otherwise error. Will also 364 + * check if logging should be done with @p cond_level. 365 + * 366 + * @param cond_level What the current logging level is. 367 + * @param file Callee site (__FILE__). 368 + * @param line Callee site (__LINE__). 369 + * @param calling_fn Callee site (__func__). 370 + * @param xret Result from the called function. 371 + * @param called_fn Which function that this return is from. 372 + */ 373 + void 374 + u_log_print_result(enum u_logging_level cond_level, 375 + const char *file, 376 + int line, 377 + const char *calling_fn, 378 + xrt_result_t xret, 379 + const char *called_fn); 274 380 275 381 /*! 276 382 * @}