PipeWire: spa/support/log.h Source File

PipeWire 1.4.2 |

Loading...
Searching...
No Matches
log.h
Go to the documentation of this file.
1/* Simple Plugin API */
2/* SPDX-FileCopyrightText: Copyright © 2018 Wim Taymans */
3/* SPDX-License-Identifier: MIT */
4
5#ifndef SPA_LOG_H
6#define SPA_LOG_H
7
8#ifdef __cplusplus
9extern "C" {
10#endif
11
12#include <stdarg.h>
13
14#include <spa/utils/type.h>
15#include <spa/utils/defs.h>
16#include <spa/utils/hook.h>
17
18#ifndef SPA_API_LOG
19 #ifdef SPA_API_IMPL
20 #define SPA_API_LOG SPA_API_IMPL
21 #else
22 #define SPA_API_LOG static inline
23 #endif
24#endif
25
29
34
44#define SPA_LOG_TOPIC_DEFAULT NULL
45
46enum spa_log_level {
47 SPA_LOG_LEVEL_NONE = 0,
48 SPA_LOG_LEVEL_ERROR,
49 SPA_LOG_LEVEL_WARN,
50 SPA_LOG_LEVEL_INFO,
51 SPA_LOG_LEVEL_DEBUG,
52 SPA_LOG_LEVEL_TRACE,
53};
54
55/**
56 * The Log interface
57 */
58#define SPA_TYPE_INTERFACE_Log SPA_TYPE_INFO_INTERFACE_BASE "Log"
59
60
61 struct spa_log {
63
64#define SPA_VERSION_LOG 0
65 struct spa_interface iface;
69 enum spa_log_level level;
70};
71
82struct spa_log_topic {
83#define SPA_VERSION_LOG_TOPIC 0
86 uint32_t version;
88 const char *topic;
90 enum spa_log_level level;
92 bool has_custom_level;
93};
94
100 struct spa_log_topic_enum {
101#define SPA_VERSION_LOG_TOPIC_ENUM 0
102 uint32_t version;
104 struct spa_log_topic * const * const topics;
106 struct spa_log_topic * const * const topics_end;
107};
108
109
110struct spa_log_methods {
111#define SPA_VERSION_LOG_METHODS 1
112 uint32_t version;
121
123 * \param line the line number
124 * \param func the function name
125 * \param fmt printf style format
126 * \param ... format arguments
127 */
128 void (*log) (void *object,
129 enum spa_log_level level,
130 const char *file,
131 int line,
132 const char *func,
133 const char *fmt, ...) SPA_PRINTF_FUNC(6, 7);
134
150 void (*logv) (void *object,
151 enum spa_log_level level,
152 const char *file,
153 int line,
154 const char *func,
155 const char *fmt,
156 va_list args) SPA_PRINTF_FUNC(6, 0);
174 void (*logt) (void *object,
175 enum spa_log_level level,
176 const struct spa_log_topic *topic,
177 const char *file,
178 int line,
179 const char *func,
180 const char *fmt, ...) SPA_PRINTF_FUNC(7, 8);
181
199 void (*logtv) (void *object,
200 enum spa_log_level level,
201 const struct spa_log_topic *topic,
202 const char *file,
203 int line,
204 const char *func,
205 const char *fmt,
206 va_list args) SPA_PRINTF_FUNC(7, 0);
207
217 void (*topic_init) (void *object, struct spa_log_topic *topic);
218};
219
220
221#define SPA_LOG_TOPIC(v, t) \
222 (struct spa_log_topic){ .version = (v), .topic = (t)}
223
224 SPA_API_LOG void spa_log_topic_init(struct spa_log *log, struct spa_log_topic *topic)
225{
226 if (SPA_UNLIKELY(!log))
227 return;
228
229 spa_interface_call(&log->iface, struct spa_log_methods, topic_init, 1, topic);
230}
231
232 SPA_API_LOG bool spa_log_level_topic_enabled(const struct spa_log *log,
233 const struct spa_log_topic *topic,
234 enum spa_log_level level)
235{
236 enum spa_log_level max_level;
237
238 if (SPA_UNLIKELY(!log))
239 return false;
240
241 if (topic && topic->has_custom_level)
242 max_level = topic->level;
243 else
244 max_level = log->level;
245
246 return level <= max_level;
247}
248
249/* Transparently calls to version 0 log if v1 is not supported */
250#define spa_log_logt(l,lev,topic,...) \
251({ \
252 struct spa_log *_l = l; \
253 if (SPA_UNLIKELY(spa_log_level_topic_enabled(_l, topic, lev))) { \
254 struct spa_interface *_if = &_l->iface; \
255 if (!spa_interface_call(_if, \
256 struct spa_log_methods, logt, 1, \
257 lev, topic, \
258 __VA_ARGS__)) \
259 spa_interface_call(_if, \
260 struct spa_log_methods, log, 0, \
261 lev, __VA_ARGS__); \
262 } \
263})
264
265/* Transparently calls to version 0 logv if v1 is not supported */
266 SPA_PRINTF_FUNC(7, 0)
267 SPA_API_LOG void spa_log_logtv(struct spa_log *l, enum spa_log_level level,
268 const struct spa_log_topic *topic, const char *file, int line,
269 const char *func, const char *fmt, va_list args)
270{
271 if (SPA_UNLIKELY(spa_log_level_topic_enabled(l, topic, level))) {
272 struct spa_interface *i = &l->iface;
273 if (!spa_interface_call(i,
274 struct spa_log_methods, logtv, 1,
275 level, topic,
276 file, line, func, fmt, args))
277 spa_interface_call(i,
278 struct spa_log_methods, logv, 0,
279 level, file, line, func, fmt, args);
280 }
281}
282
283#define spa_logt_lev(l,lev,t,...) \
284 spa_log_logt(l,lev,t,__FILE__,__LINE__,__func__,__VA_ARGS__)
285
286#define spa_log_lev(l,lev,...) \
287 spa_logt_lev(l,lev,SPA_LOG_TOPIC_DEFAULT,__VA_ARGS__)
288
289#define spa_log_log(l,lev,...) \
290 spa_log_logt(l,lev,SPA_LOG_TOPIC_DEFAULT,__VA_ARGS__)
291
292#define spa_log_logv(l,lev,...) \
293 spa_log_logtv(l,lev,SPA_LOG_TOPIC_DEFAULT,__VA_ARGS__)
294
295#define spa_log_error(l,...) spa_log_lev(l,SPA_LOG_LEVEL_ERROR,__VA_ARGS__)
296#define spa_log_warn(l,...) spa_log_lev(l,SPA_LOG_LEVEL_WARN,__VA_ARGS__)
297#define spa_log_info(l,...) spa_log_lev(l,SPA_LOG_LEVEL_INFO,__VA_ARGS__)
298#define spa_log_debug(l,...) spa_log_lev(l,SPA_LOG_LEVEL_DEBUG,__VA_ARGS__)
299#define spa_log_trace(l,...) spa_log_lev(l,SPA_LOG_LEVEL_TRACE,__VA_ARGS__)
300
301#define spa_logt_error(l,t,...) spa_logt_lev(l,SPA_LOG_LEVEL_ERROR,t,__VA_ARGS__)
302#define spa_logt_warn(l,t,...) spa_logt_lev(l,SPA_LOG_LEVEL_WARN,t,__VA_ARGS__)
303#define spa_logt_info(l,t,...) spa_logt_lev(l,SPA_LOG_LEVEL_INFO,t,__VA_ARGS__)
304#define spa_logt_debug(l,t,...) spa_logt_lev(l,SPA_LOG_LEVEL_DEBUG,t,__VA_ARGS__)
305#define spa_logt_trace(l,t,...) spa_logt_lev(l,SPA_LOG_LEVEL_TRACE,t,__VA_ARGS__)
306
307#ifndef FASTPATH
308#define spa_log_trace_fp(l,...) spa_log_lev(l,SPA_LOG_LEVEL_TRACE,__VA_ARGS__)
309#else
310#define spa_log_trace_fp(l,...)
311#endif
312
313
320#define SPA_LOG_TOPIC_ENUM_NAME "spa_log_topic_enum"
321
327#define SPA_LOG_TOPIC_ENUM_DEFINE(s, e) \
328 SPA_EXPORT struct spa_log_topic_enum spa_log_topic_enum = (struct spa_log_topic_enum) { \
329 .version = SPA_VERSION_LOG_TOPIC_ENUM, \
330 .topics = (s), \
331 .topics_end = (e), \
332 }
333
340#define SPA_LOG_TOPIC_REGISTER(v) \
341 __attribute__((used)) __attribute__((retain)) \
342 __attribute__((section("spa_log_topic"))) __attribute__((aligned(__alignof__(struct spa_log_topic *)))) \
343 static struct spa_log_topic * const spa_log_topic_export_##v = &v
344
348
350#define SPA_LOG_TOPIC_DEFINE(var,name) \
351 struct spa_log_topic var = SPA_LOG_TOPIC(SPA_VERSION_LOG_TOPIC, name); \
352 SPA_LOG_TOPIC_REGISTER(var)
353
355 * Define and magically register a \ref spa_log_topic with static scope
356 *
357 * \since 1.1.0
358 */
359#define SPA_LOG_TOPIC_DEFINE_STATIC(var,name) \
360 static struct spa_log_topic var = SPA_LOG_TOPIC(SPA_VERSION_LOG_TOPIC, name); \
361 SPA_LOG_TOPIC_REGISTER(var)
362
365 * \ref spa_log_topic in the plugin.
366 *
367 * \since 1.1.0
368 */
369#define SPA_LOG_TOPIC_ENUM_DEFINE_REGISTERED \
370 extern struct spa_log_topic * const __start_spa_log_topic[]; \
371 extern struct spa_log_topic * const __stop_spa_log_topic[]; \
372 SPA_LOG_TOPIC_ENUM_DEFINE(__start_spa_log_topic, __stop_spa_log_topic)
373
374/** \fn spa_log_error */
375
377#define SPA_KEY_LOG_LEVEL "log.level"
378#define SPA_KEY_LOG_COLORS "log.colors"
380#define SPA_KEY_LOG_FILE "log.file"
382#define SPA_KEY_LOG_TIMESTAMP "log.timestamp"
384#define SPA_KEY_LOG_LINE "log.line"
385#define SPA_KEY_LOG_PATTERNS "log.patterns"
386
388
390
391#ifdef __cplusplus
392} /* extern "C" */
393#endif
394#endif /* SPA_LOG_H */
defs.h
spa/utils/defs.h
args
uint32_t int int const char va_list args
Definition core.h:434
spa_interface_call
#define spa_interface_call(iface, method_type, method, vers,...)
Invoke method named method in the callbacks on the given interface object.
Definition hook.h:248
spa_log_level
spa_log_level
Definition log.h:55
spa_log_level_topic_enabled
SPA_API_LOG bool spa_log_level_topic_enabled(const struct spa_log *log, const struct spa_log_topic *topic, enum spa_log_level level)
Definition log.h:246
spa_log_topic_init
SPA_API_LOG void spa_log_topic_init(struct spa_log *log, struct spa_log_topic *topic)
Definition log.h:238
spa_log_logtv
SPA_API_LOG void spa_log_logtv(struct spa_log *l, enum spa_log_level level, const struct spa_log_topic *topic, const char *file, int line, const char *func, const char *fmt, va_list args)
Definition log.h:281
SPA_LOG_LEVEL_INFO
@ SPA_LOG_LEVEL_INFO
Definition log.h:59
SPA_LOG_LEVEL_NONE
@ SPA_LOG_LEVEL_NONE
Definition log.h:56
SPA_LOG_LEVEL_TRACE
@ SPA_LOG_LEVEL_TRACE
Definition log.h:61
SPA_LOG_LEVEL_DEBUG
@ SPA_LOG_LEVEL_DEBUG
Definition log.h:60
SPA_LOG_LEVEL_ERROR
@ SPA_LOG_LEVEL_ERROR
Definition log.h:57
SPA_LOG_LEVEL_WARN
@ SPA_LOG_LEVEL_WARN
Definition log.h:58
SPA_PRINTF_FUNC
#define SPA_PRINTF_FUNC(fmt, arg1)
Definition defs.h:295
SPA_UNLIKELY
#define SPA_UNLIKELY(x)
Definition defs.h:394
hook.h
spa/utils/hook.h
SPA_API_LOG
#define SPA_API_LOG
Definition log.h:29
type.h
spa/utils/type.h
spa_interface
Definition hook.h:148
spa_log_methods
Definition log.h:123
spa_log_methods::logv
void(* logv)(void *object, enum spa_log_level level, const char *file, int line, const char *func, const char *fmt, va_list args)
Log a message with the given log level.
Definition log.h:164
spa_log_methods::logtv
void(* logtv)(void *object, enum spa_log_level level, const struct spa_log_topic *topic, const char *file, int line, const char *func, const char *fmt, va_list args)
Log a message with the given log level for the given topic.
Definition log.h:213
spa_log_methods::logt
void(* logt)(void *object, enum spa_log_level level, const struct spa_log_topic *topic, const char *file, int line, const char *func, const char *fmt,...)
Log a message with the given log level for the given topic.
Definition log.h:188
spa_log_methods::topic_init
void(* topic_init)(void *object, struct spa_log_topic *topic)
Initializes a spa_log_topic to the correct logging level.
Definition log.h:231
spa_log_methods::log
void(* log)(void *object, enum spa_log_level level, const char *file, int line, const char *func, const char *fmt,...)
Log a message with the given log level.
Definition log.h:142
spa_log_topic_enum
Enumeration of log topics in a plugin.
Definition log.h:112
spa_log_topic_enum::topics_end
struct spa_log_topic *const *const topics_end
End of topics array.
Definition log.h:119
spa_log_topic_enum::version
uint32_t version
Definition log.h:115
spa_log_topic
Identifier for a topic.
Definition log.h:93
spa_log_topic::version
uint32_t version
the version of this topic.
Definition log.h:98
spa_log_topic::has_custom_level
bool has_custom_level
False if this topic follows the Log level.
Definition log.h:104
spa_log_topic::level
enum spa_log_level level
Logging level set for this topic.
Definition log.h:102
spa_log
Definition log.h:71
spa_log::iface
struct spa_interface iface
Definition log.h:76
spa_log::level
enum spa_log_level level
Logging level, everything above this level is not logged.
Definition log.h:80