Monado OpenXR Runtime
oxr_logger.h
Go to the documentation of this file.
1 // Copyright 2018-2019, 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 #ifdef __cplusplus
13 extern "C" {
14 #endif
15 
16 
17 /*!
18  * Helper macro to log a warning just once.
19  *
20  * @ingroup oxr_main
21  */
22 #define OXR_WARN_ONCE(log, ...) \
23  do { \
24  static bool _once = false; \
25  if (!_once) { \
26  _once = true; \
27  oxr_warn(log, __VA_ARGS__); \
28  } \
29  } while (false)
30 
31 /*!
32  * Logger struct that lives on the stack, one for each call client call.
33  *
34  * @ingroup oxr_main
35  */
36 struct oxr_logger
37 {
38  struct oxr_instance *inst;
39  const char *api_func_name;
40 };
41 
42 
43 /*!
44  * @ingroup oxr_main
45  * @{
46  */
47 
48 void
49 oxr_log_init(struct oxr_logger *logger, const char *api_func_name);
50 void
51 oxr_log_set_instance(struct oxr_logger *logger, struct oxr_instance *inst);
52 void
53 oxr_log(struct oxr_logger *logger, const char *fmt, ...)
54  XRT_PRINTF_FORMAT(2, 3);
55 void
56 oxr_warn(struct oxr_logger *logger, const char *fmt, ...)
57  XRT_PRINTF_FORMAT(2, 3);
58 
59 /*!
60  * Output an error and return the result code.
61  *
62  * Intended for use in a return statement, to log error information and return
63  * the result code in a single line.
64  *
65  * Note: The format string is appended to the function name with no spaces,
66  * so it should either start with a parenthesized argument name followed by a
67  * space and the message, or should start with a space then the message.
68  * That is, a format string of `"(arg) info"` becomes `XR_ERROR: xrFunc(arg)
69  * info`, and a format string of `" info msg"` becomes `XR_ERROR: xrFunc info
70  * msg`.
71  */
72 XrResult
73 oxr_error(struct oxr_logger *logger, XrResult result, const char *fmt, ...)
74  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, ...);
99 
100 /*!
101  * Abort logging, frees all internal data.
102  */
103 void
104 oxr_slog_abort(struct oxr_sink_logger *slog);
105 
106 /*!
107  * Flush sink as a log message, frees all internal data.
108  */
109 void
110 oxr_log_slog(struct oxr_logger *log, struct oxr_sink_logger *slog);
111 
112 /*!
113  * Flush sink as a warning message, frees all internal data.
114  */
115 void
116 oxr_warn_slog(struct oxr_logger *log, struct oxr_sink_logger *slog);
117 
118 /*!
119  * Flush sink as a error message, frees all internal data.
120  */
121 XrResult
122 oxr_error_slog(struct oxr_logger *log,
123  XrResult res,
124  struct oxr_sink_logger *slog);
125 
126 
127 /*!
128  * @}
129  */
130 
131 
132 #ifdef __cplusplus
133 }
134 #endif
void oxr_slog(struct oxr_sink_logger *slog, const char *fmt,...)
Log string to sink logger.
Definition: oxr_logger.c:149
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:187
const char * api_func_name
Definition: oxr_logger.h:39
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_slog_abort(struct oxr_sink_logger *slog)
Abort logging, frees all internal data.
Definition: oxr_logger.c:174
Logger struct that lives on the stack, one for each call client call.
Definition: oxr_logger.h:36
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:194
Allocate on the stack, make sure to zero initialize.
Definition: oxr_logger.h:87
size_t store_size
Definition: oxr_logger.h:90
void oxr_log(struct oxr_logger *logger, const char *fmt,...) XRT_PRINTF_FORMAT(2
size_t length
Definition: oxr_logger.h:91
void oxr_log_set_instance(struct oxr_logger *logger, struct oxr_instance *inst)
Definition: oxr_logger.c:41
struct oxr_instance * inst
Definition: oxr_logger.h:38
void void oxr_warn(struct oxr_logger *logger, const char *fmt,...) XRT_PRINTF_FORMAT(2
char * store
Definition: oxr_logger.h:89
Main object that ties everything together.
Definition: oxr_objects.h:899
void oxr_log_init(struct oxr_logger *logger, const char *api_func_name)
Definition: oxr_logger.c:30
#define XRT_PRINTF_FORMAT(fmt, list)
Definition: xrt_compiler.h:40
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:180