Monado OpenXR Runtime
Loading...
Searching...
No Matches
oxr_logger.h
Go to the documentation of this file.
1// Copyright 2018-2022, Collabora, Ltd.
2// Copyright 2026, NVIDIA CORPORATION.
3// SPDX-License-Identifier: BSL-1.0
4/*!
5 * @file
6 * @brief Logging functions.
7 * @author Jakob Bornecrantz <jakob@collabora.com>
8 * @ingroup oxr_main
9 */
10
11#pragma once
12
13#include "util/u_pretty_print.h"
14
15// we need no platform-specific defines from OpenXR.
16#include "openxr/openxr.h"
17
18
19#ifdef __cplusplus
20extern "C" {
21#endif
22
23
24/*!
25 * Helper macro to log a warning just once.
26 *
27 * @ingroup oxr_main
28 */
29#define OXR_WARN_ONCE(log, ...) \
30 do { \
31 static bool _once = false; \
32 if (!_once) { \
33 _once = true; \
34 oxr_warn(log, __VA_ARGS__); \
35 } \
36 } while (false)
37
38/*!
39 * Logger struct that lives on the stack, one for each call client call.
40 *
41 * @ingroup oxr_main
42 */
44{
45 struct oxr_instance *inst;
46 const char *api_func_name;
47};
48
49
50/*!
51 * @addtogroup oxr_main
52 * @{
53 */
54
55void
56oxr_log_init(struct oxr_logger *logger, const char *api_func_name);
57void
58oxr_log_set_instance(struct oxr_logger *logger, struct oxr_instance *inst);
59void
60oxr_log(struct oxr_logger *logger, const char *fmt, ...) XRT_PRINTF_FORMAT(2, 3);
61void
62oxr_warn(struct oxr_logger *logger, const char *fmt, ...) XRT_PRINTF_FORMAT(2, 3);
63
64/*!
65 * Output an error and return the result code.
66 *
67 * Intended for use in a return statement, to log error information and return
68 * the result code in a single line.
69 *
70 * Note: The format string is appended to the function name with no spaces,
71 * so it should either start with a parenthesized argument name followed by a
72 * space and the message, or should start with a space then the message.
73 * That is, a format string of `"(arg) info"` becomes `XR_ERROR: xrFunc(arg)
74 * info`, and a format string of `" info msg"` becomes `XR_ERROR: xrFunc info
75 * msg`.
76 */
77XrResult
78oxr_error(struct oxr_logger *logger, XrResult result, const char *fmt, ...) XRT_PRINTF_FORMAT(3, 4);
79
80
81
82/*
83 *
84 * Sink logger.
85 *
86 */
87
88/*!
89 * Allocate on the stack, make sure to zero initialize.
90 */
92{
93 char *store;
94 size_t store_size;
95 size_t length;
96};
97
98/*!
99 * Log string to sink logger.
100 */
101void
102oxr_slog(struct oxr_sink_logger *slog, const char *fmt, ...) XRT_PRINTF_FORMAT(2, 3);
103
104/*!
105 * Add the string to the slog struct.
106 */
107void
108oxr_slog_add_array(struct oxr_sink_logger *slog, const char *str, size_t size);
109
110/*!
111 * Get a pretty print delegate from a @ref oxr_sink_logger.
112 */
113static inline u_pp_delegate_t
115{
117 return dg;
118}
119
120/*!
121 * Cancel logging, frees all internal data.
122 */
123void
124oxr_slog_cancel(struct oxr_sink_logger *slog);
125
126/*!
127 * Flush sink as a log message, frees all internal data.
128 */
129void
130oxr_log_slog(struct oxr_logger *log, struct oxr_sink_logger *slog);
131
132/*!
133 * Flush sink as a warning message, frees all internal data.
134 */
135void
136oxr_warn_slog(struct oxr_logger *log, struct oxr_sink_logger *slog);
137
138/*!
139 * Flush sink as a error message, frees all internal data.
140 */
141XrResult
142oxr_error_slog(struct oxr_logger *log, XrResult res, struct oxr_sink_logger *slog);
143
144
145/*!
146 * @}
147 */
148
149
150#ifdef __cplusplus
151}
152#endif
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 oxr_warn_slog(struct oxr_logger *log, struct oxr_sink_logger *slog)
Flush sink as a warning message, frees all internal data.
Definition oxr_logger.c:316
void void XrResult oxr_error(struct oxr_logger *logger, XrResult result, const char *fmt,...) XRT_PRINTF_FORMAT(3
Output an error and return the result code.
void oxr_log_slog(struct oxr_logger *log, struct oxr_sink_logger *slog)
Flush sink as a log message, frees all internal data.
Definition oxr_logger.c:309
void oxr_slog_cancel(struct oxr_sink_logger *slog)
Cancel logging, frees all internal data.
Definition oxr_logger.c:303
void void oxr_slog_add_array(struct oxr_sink_logger *slog, const char *str, size_t size)
Add the string to the slog struct.
Definition oxr_logger.c:288
static u_pp_delegate_t oxr_slog_dg(struct oxr_sink_logger *slog)
Get a pretty print delegate from a oxr_sink_logger.
Definition oxr_logger.h:114
void oxr_slog(struct oxr_sink_logger *slog, const char *fmt,...) XRT_PRINTF_FORMAT(2
Log string to sink logger.
XrResult oxr_error_slog(struct oxr_logger *log, XrResult res, struct oxr_sink_logger *slog)
Flush sink as a error message, frees all internal data.
Definition oxr_logger.c:323
Main object that ties everything together.
Definition oxr_objects.h:1215
Logger struct that lives on the stack, one for each call client call.
Definition oxr_logger.h:44
Allocate on the stack, make sure to zero initialize.
Definition oxr_logger.h:92
Helper struct to hold a function pointer and data pointer.
Definition u_pretty_print.h:102
Pretty printing various Monado things.