+ +
2da5fe7 OpenAVX v0.2.0 - High-performance open-standard IP audio and video transport suite
saifmukhtar · 2026-11-24
c · 394 lines · 10.9 KB · SPDX: MIT
main
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 #include <stdarg.h>
9  
10 #include <spa/utils/type.h>
11 #include <spa/utils/defs.h>
12 #include <spa/utils/hook.h>
13  
14 #ifdef __cplusplus
15 extern "C" {
16 #endif
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  
26 /** \defgroup spa_log Log
27 * Logging interface
28 */
29  
30 /**
31 * \addtogroup spa_log
32 * \{
33 */
34  
35 /** The default log topic. Redefine this in your code to
36 * allow for the spa_log_* macros to work correctly, e.g:
37 *
38 * \code{.c}
39 * struct spa_log_topic *mylogger;
40 * #undef SPA_LOG_TOPIC_DEFAULT
41 * #define SPA_LOG_TOPIC_DEFAULT mylogger
42 * \endcode
43 */
44 #define SPA_LOG_TOPIC_DEFAULT NULL
45  
46 enum 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 {
62 /** the version of this log. This can be used to expand this
63 * structure in the future */
64 #define SPA_VERSION_LOG 0
65 struct spa_interface iface;
66 /**
67 * Logging level, everything above this level is not logged
68 */
69 enum spa_log_level level;
70 };
71  
72 /**
73 * \struct spa_log_topic
74 *
75 * Identifier for a topic. Topics are string-based filters that logically
76 * group messages together. An implementation may decide to filter different
77 * topics on different levels, for example the "protocol" topic may require
78 * debug level TRACE while the "core" topic defaults to debug level INFO.
79 *
80 * spa_log_topics require a spa_log_methods version of 1 or higher.
81 */
82 struct spa_log_topic {
83 #define SPA_VERSION_LOG_TOPIC 0
84 /** the version of this topic. This can be used to expand this
85 * structure in the future */
86 uint32_t version;
87 /** The string identifier for the topic */
88 const char *topic;
89 /** Logging level set for this topic */
90 enum spa_log_level level;
91 /** False if this topic follows the \ref spa_log level */
92 bool has_custom_level;
93 };
94  
95 /**
96 * Enumeration of log topics in a plugin
97 *
98 * \since 1.1.0
99 */
100 struct spa_log_topic_enum {
101 #define SPA_VERSION_LOG_TOPIC_ENUM 0
102 uint32_t version;
103 /** Array of pointers to log topics */
104 struct spa_log_topic * const * const topics;
105 /** End of topics array */
106 struct spa_log_topic * const * const topics_end;
107 };
108  
109  
110 struct spa_log_methods {
111 #define SPA_VERSION_LOG_METHODS 1
112 uint32_t version;
113 /**
114 * Log a message with the given log level.
115 *
116 * \note If compiled with this header, this function is only called
117 * for implementations of version 0. For versions 1 and above, see
118 * logt() instead.
119 *
120 * \param log a spa_log
121 * \param level a spa_log_level
122 * \param file the file name
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  
135 /**
136 * Log a message with the given log level.
137 *
138 * \note If compiled with this header, this function is only called
139 * for implementations of version 0. For versions 1 and above, see
140 * logtv() instead.
141 *
142 * \param log a spa_log
143 * \param level a spa_log_level
144 * \param file the file name
145 * \param line the line number
146 * \param func the function name
147 * \param fmt printf style format
148 * \param args format arguments
149 */
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);
157 /**
158 * Log a message with the given log level for the given topic.
159 *
160 * \note Callers that do not use topic-based logging (version 0), the \a
161 * topic is NULL
162 *
163 * \param log a spa_log
164 * \param level a spa_log_level
165 * \param topic the topic for this message, may be NULL
166 * \param file the file name
167 * \param line the line number
168 * \param func the function name
169 * \param fmt printf style format
170 * \param ... format arguments
171 *
172 * \since 1
173 */
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  
182 /**
183 * Log a message with the given log level for the given topic.
184 *
185 * \note For callers that do not use topic-based logging (version 0),
186 * the \a topic is NULL
187 *
188 * \param log a spa_log
189 * \param level a spa_log_level
190 * \param topic the topic for this message, may be NULL
191 * \param file the file name
192 * \param line the line number
193 * \param func the function name
194 * \param fmt printf style format
195 * \param args format arguments
196 *
197 * \since 1
198 */
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  
208 /**
209 * Initializes a \ref spa_log_topic to the correct logging level.
210 *
211 * \deprecated
212 * Plugin host should obtain log topics from \ref SPA_LOG_TOPIC_ENUM_NAME
213 * and update them itself.
214 *
215 * \since 1
216 */
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  
314 /**
315 * Name of the symbol indicating a \ref spa_log_topic_enum enumerating
316 * the static log topics in a plugin,
317 *
318 * \since 1.1.0
319 */
320 #define SPA_LOG_TOPIC_ENUM_NAME "spa_log_topic_enum"
321  
322 /**
323 * Define the symbol for \ref SPA_LOG_TOPIC_ENUM_NAME
324 *
325 * \since 1.1.0
326 */
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  
334 /**
335 * Magically register a statically defined \ref spa_log_topic into
336 * the log topic enumeration for a plugin.
337 *
338 * \since 1.1.0
339 */
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  
345 /**
346 * Define and magically register a \ref spa_log_topic
347 *
348 * \since 1.1.0
349 */
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  
354 /**
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  
363 /**
364 * Do \ref SPA_LOG_TOPIC_ENUM_DEFINE for the auto-registered
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  
376 /** keys can be given when initializing the logger handle */
377 #define SPA_KEY_LOG_LEVEL "log.level" /**< the default log level */
378 #define SPA_KEY_LOG_COLORS "log.colors" /**< enable colors in the logger, set to "force" to enable
379 * colors even when not logging to a terminal */
380 #define SPA_KEY_LOG_FILE "log.file" /**< log to the specified file instead of
381 * stderr. */
382 #define SPA_KEY_LOG_TIMESTAMP "log.timestamp" /**< log timestamp type (local, realtime, monotonic, monotonic-raw).
383 * boolean true means local. */
384 #define SPA_KEY_LOG_LINE "log.line" /**< log file and line numbers */
385 #define SPA_KEY_LOG_PATTERNS "log.patterns" /**< Spa:String:JSON array of [ {"pattern" : level}, ... ] */
386  
387 /**
388 * \}
389 */
390  
391 #ifdef __cplusplus
392 } /* extern "C" */
393 #endif
394 #endif /* SPA_LOG_H */