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