PipeWire: spa/support/log.h Source File

PipeWire 1.2.1

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
36#define SPA_LOG_TOPIC_DEFAULT NULL
37
38enum spa_log_level {
39 SPA_LOG_LEVEL_NONE = 0,
40 SPA_LOG_LEVEL_ERROR,
41 SPA_LOG_LEVEL_WARN,
42 SPA_LOG_LEVEL_INFO,
43 SPA_LOG_LEVEL_DEBUG,
44 SPA_LOG_LEVEL_TRACE,
45};
46
50#define SPA_TYPE_INTERFACE_Log SPA_TYPE_INFO_INTERFACE_BASE "Log"
51
52
53struct spa_log {
56#define SPA_VERSION_LOG 0
57 struct spa_interface iface;
61 enum spa_log_level level;
62};
63
74struct spa_log_topic {
75#define SPA_VERSION_LOG_TOPIC 0
78 uint32_t version;
80 const char *topic;
82 enum spa_log_level level;
84 bool has_custom_level;
85};
86
92 struct spa_log_topic_enum {
93#define SPA_VERSION_LOG_TOPIC_ENUM 0
94 uint32_t version;
96 struct spa_log_topic * const * const topics;
98 struct spa_log_topic * const * const topics_end;
99};
100
101
102 struct spa_log_methods {
103#define SPA_VERSION_LOG_METHODS 1
104 uint32_t version;
120 void (*log) (void *object,
121 enum spa_log_level level,
122 const char *file,
123 int line,
124 const char *func,
125 const char *fmt, ...) SPA_PRINTF_FUNC(6, 7);
126
142 void (*logv) (void *object,
143 enum spa_log_level level,
144 const char *file,
145 int line,
146 const char *func,
147 const char *fmt,
148 va_list args) SPA_PRINTF_FUNC(6, 0);
166 void (*logt) (void *object,
167 enum spa_log_level level,
168 const struct spa_log_topic *topic,
169 const char *file,
170 int line,
171 const char *func,
172 const char *fmt, ...) SPA_PRINTF_FUNC(7, 8);
173
191 void (*logtv) (void *object,
192 enum spa_log_level level,
193 const struct spa_log_topic *topic,
194 const char *file,
195 int line,
196 const char *func,
197 const char *fmt,
198 va_list args) SPA_PRINTF_FUNC(7, 0);
199
209 void (*topic_init) (void *object, struct spa_log_topic *topic);
210};
211
212
213#define SPA_LOG_TOPIC(v, t) \
214 (struct spa_log_topic){ .version = (v), .topic = (t)}
215
216static inline void spa_log_topic_init(struct spa_log *log, struct spa_log_topic *topic)
217{
218 if (SPA_UNLIKELY(!log))
219 return;
220
221 spa_interface_call(&log->iface, struct spa_log_methods, topic_init, 1, topic);
222}
223
224static inline bool spa_log_level_topic_enabled(const struct spa_log *log,
225 const struct spa_log_topic *topic,
226 enum spa_log_level level)
227{
228 enum spa_log_level max_level;
229
230 if (SPA_UNLIKELY(!log))
231 return false;
232
233 if (topic && topic->has_custom_level)
234 max_level = topic->level;
235 else
236 max_level = log->level;
237
238 return level <= max_level;
239}
240
241/* Transparently calls to version 0 log if v1 is not supported */
242#define spa_log_logt(l,lev,topic,...) \
243({ \
244 struct spa_log *_l = l; \
245 if (SPA_UNLIKELY(spa_log_level_topic_enabled(_l, topic, lev))) { \
246 struct spa_interface *_if = &_l->iface; \
247 if (!spa_interface_call(_if, \
248 struct spa_log_methods, logt, 1, \
249 lev, topic, \
250 __VA_ARGS__)) \
251 spa_interface_call(_if, \
252 struct spa_log_methods, log, 0, \
253 lev, __VA_ARGS__); \
254 } \
255})
256
257/* Transparently calls to version 0 logv if v1 is not supported */
258#define spa_log_logtv(l,lev,topic,...) \
259({ \
260 struct spa_log *_l = l; \
261 if (SPA_UNLIKELY(spa_log_level_topic_enabled(_l, topic, lev))) { \
262 struct spa_interface *_if = &_l->iface; \
263 if (!spa_interface_call(_if, \
264 struct spa_log_methods, logtv, 1, \
265 lev, topic, \
266 __VA_ARGS__)) \
267 spa_interface_call(_if, \
268 struct spa_log_methods, logv, 0, \
269 lev, __VA_ARGS__); \
270 } \
271})
272
273#define spa_logt_lev(l,lev,t,...) \
274 spa_log_logt(l,lev,t,__FILE__,__LINE__,__func__,__VA_ARGS__)
275
276#define spa_log_lev(l,lev,...) \
277 spa_logt_lev(l,lev,SPA_LOG_TOPIC_DEFAULT,__VA_ARGS__)
278
279#define spa_log_log(l,lev,...) \
280 spa_log_logt(l,lev,SPA_LOG_TOPIC_DEFAULT,__VA_ARGS__)
281
282#define spa_log_logv(l,lev,...) \
283 spa_log_logtv(l,lev,SPA_LOG_TOPIC_DEFAULT,__VA_ARGS__)
284
285#define spa_log_error(l,...) spa_log_lev(l,SPA_LOG_LEVEL_ERROR,__VA_ARGS__)
286#define spa_log_warn(l,...) spa_log_lev(l,SPA_LOG_LEVEL_WARN,__VA_ARGS__)
287#define spa_log_info(l,...) spa_log_lev(l,SPA_LOG_LEVEL_INFO,__VA_ARGS__)
288#define spa_log_debug(l,...) spa_log_lev(l,SPA_LOG_LEVEL_DEBUG,__VA_ARGS__)
289#define spa_log_trace(l,...) spa_log_lev(l,SPA_LOG_LEVEL_TRACE,__VA_ARGS__)
290
291#define spa_logt_error(l,t,...) spa_logt_lev(l,SPA_LOG_LEVEL_ERROR,t,__VA_ARGS__)
292#define spa_logt_warn(l,t,...) spa_logt_lev(l,SPA_LOG_LEVEL_WARN,t,__VA_ARGS__)
293#define spa_logt_info(l,t,...) spa_logt_lev(l,SPA_LOG_LEVEL_INFO,t,__VA_ARGS__)
294#define spa_logt_debug(l,t,...) spa_logt_lev(l,SPA_LOG_LEVEL_DEBUG,t,__VA_ARGS__)
295#define spa_logt_trace(l,t,...) spa_logt_lev(l,SPA_LOG_LEVEL_TRACE,t,__VA_ARGS__)
296
297#ifndef FASTPATH
298#define spa_log_trace_fp(l,...) spa_log_lev(l,SPA_LOG_LEVEL_TRACE,__VA_ARGS__)
299#else
300#define spa_log_trace_fp(l,...)
301#endif
302
303
310#define SPA_LOG_TOPIC_ENUM_NAME "spa_log_topic_enum"
311
317#define SPA_LOG_TOPIC_ENUM_DEFINE(s, e) \
318 SPA_EXPORT struct spa_log_topic_enum spa_log_topic_enum = (struct spa_log_topic_enum) { \
319 .version = SPA_VERSION_LOG_TOPIC_ENUM, \
320 .topics = (s), \
321 .topics_end = (e), \
322 }
323
330#define SPA_LOG_TOPIC_REGISTER(v) \
331 __attribute__((used)) __attribute__((retain)) \
332 __attribute__((section("spa_log_topic"))) __attribute__((aligned(__alignof__(struct spa_log_topic *)))) \
333 static struct spa_log_topic * const spa_log_topic_export_##v = &v
334
340#define SPA_LOG_TOPIC_DEFINE(var,name) \
341 struct spa_log_topic var = SPA_LOG_TOPIC(SPA_VERSION_LOG_TOPIC, name); \
342 SPA_LOG_TOPIC_REGISTER(var)
343
349#define SPA_LOG_TOPIC_DEFINE_STATIC(var,name) \
350 static struct spa_log_topic var = SPA_LOG_TOPIC(SPA_VERSION_LOG_TOPIC, name); \
351 SPA_LOG_TOPIC_REGISTER(var)
352
359#define SPA_LOG_TOPIC_ENUM_DEFINE_REGISTERED \
360 extern struct spa_log_topic * const __start_spa_log_topic[]; \
361 extern struct spa_log_topic * const __stop_spa_log_topic[]; \
362 SPA_LOG_TOPIC_ENUM_DEFINE(__start_spa_log_topic, __stop_spa_log_topic)
363
367#define SPA_KEY_LOG_LEVEL "log.level"
368#define SPA_KEY_LOG_COLORS "log.colors"
370#define SPA_KEY_LOG_FILE "log.file"
372#define SPA_KEY_LOG_TIMESTAMP "log.timestamp"
373#define SPA_KEY_LOG_LINE "log.line"
374#define SPA_KEY_LOG_PATTERNS "log.patterns"
380#ifdef __cplusplus
381} /* extern "C" */
382#endif
383#endif /* SPA_LOG_H */
defs.h
spa/utils/defs.h
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:238
spa_log_level
spa_log_level
Definition log.h:45
spa_log_topic_init
static void spa_log_topic_init(struct spa_log *log, struct spa_log_topic *topic)
Definition log.h:228
spa_log_level_topic_enabled
static 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:236
SPA_LOG_LEVEL_INFO
@ SPA_LOG_LEVEL_INFO
Definition log.h:49
SPA_LOG_LEVEL_NONE
@ SPA_LOG_LEVEL_NONE
Definition log.h:46
SPA_LOG_LEVEL_TRACE
@ SPA_LOG_LEVEL_TRACE
Definition log.h:51
SPA_LOG_LEVEL_DEBUG
@ SPA_LOG_LEVEL_DEBUG
Definition log.h:50
SPA_LOG_LEVEL_ERROR
@ SPA_LOG_LEVEL_ERROR
Definition log.h:47
SPA_LOG_LEVEL_WARN
@ SPA_LOG_LEVEL_WARN
Definition log.h:48
SPA_PRINTF_FUNC
#define SPA_PRINTF_FUNC(fmt, arg1)
Definition defs.h:295
SPA_UNLIKELY
#define SPA_UNLIKELY(x)
Definition defs.h:369
hook.h
spa/utils/hook.h
type.h
spa/utils/type.h
spa_interface
Definition hook.h:138
spa_log_methods
Definition log.h:113
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:154
spa_log_methods::version
uint32_t version
Definition log.h:116
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:203
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:178
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:221
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:132
spa_log_topic_enum
Enumeration of log topics in a plugin.
Definition log.h:102
spa_log_topic_enum::topics_end
struct spa_log_topic *const *const topics_end
End of topics array.
Definition log.h:109
spa_log_topic_enum::topics
struct spa_log_topic *const *const topics
Array of pointers to log topics.
Definition log.h:107
spa_log_topic_enum::version
uint32_t version
Definition log.h:105
spa_log_topic
Identifier for a topic.
Definition log.h:83
spa_log_topic::version
uint32_t version
the version of this topic.
Definition log.h:88
spa_log_topic::has_custom_level
bool has_custom_level
False if this topic follows the Log level.
Definition log.h:94
spa_log_topic::level
enum spa_log_level level
Logging level set for this topic.
Definition log.h:92
spa_log_topic::topic
const char * topic
The string identifier for the topic.
Definition log.h:90
spa_log
Definition log.h:61
spa_log::iface
struct spa_interface iface
Definition log.h:66
spa_log::level
enum spa_log_level level
Logging level, everything above this level is not logged.
Definition log.h:70