Loading...
Searching...
No Matches
log.h
Go to the documentation of this file.
/* Simple Plugin API */
/* SPDX-FileCopyrightText: Copyright © 2018 Wim Taymans */
/* SPDX-License-Identifier: MIT */
#ifndef SPA_LOG_H
#define SPA_LOG_H
#include <stdarg.h>
#include <spa/utils/type.h>
#include <spa/utils/defs.h>
#include <spa/utils/hook.h>
#ifdef __cplusplus
extern "C" {
#endif
#ifndef SPA_API_LOG
#ifdef SPA_API_IMPL
#define SPA_API_LOG SPA_API_IMPL
#else
#define SPA_API_LOG static inline
#endif
#endif
#define SPA_LOG_TOPIC_DEFAULT NULL
enum spa_log_level {
SPA_LOG_LEVEL_NONE = 0,
SPA_LOG_LEVEL_ERROR,
SPA_LOG_LEVEL_WARN,
SPA_LOG_LEVEL_INFO,
SPA_LOG_LEVEL_DEBUG,
SPA_LOG_LEVEL_TRACE,
};
/**
* The Log interface
*/
#define SPA_TYPE_INTERFACE_Log SPA_TYPE_INFO_INTERFACE_BASE "Log"
struct spa_log {
#define SPA_VERSION_LOG 0
struct spa_interface iface;
enum spa_log_level level;
};
struct spa_log_topic {
#define SPA_VERSION_LOG_TOPIC 0
uint32_t version;
const char *topic;
enum spa_log_level level;
bool has_custom_level;
};
struct spa_log_topic_enum {
#define SPA_VERSION_LOG_TOPIC_ENUM 0
uint32_t version;
struct spa_log_topic * const * const topics;
struct spa_log_topic * const * const topics_end;
};
struct spa_log_methods {
#define SPA_VERSION_LOG_METHODS 1
uint32_t version;
* \param line the line number
* \param func the function name
* \param fmt printf style format
* \param ... format arguments
*/
void (*log) (void *object,
enum spa_log_level level,
const char *file,
int line,
const char *func,
const char *fmt, ...) SPA_PRINTF_FUNC(6, 7);
void (*logv) (void *object,
enum spa_log_level level,
const char *file,
int line,
const char *func,
const char *fmt,
va_list args) SPA_PRINTF_FUNC(6, 0);
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, ...) SPA_PRINTF_FUNC(7, 8);
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) SPA_PRINTF_FUNC(7, 0);
void (*topic_init) (void *object, struct spa_log_topic *topic);
};
#define SPA_LOG_TOPIC(v, t) \
(struct spa_log_topic){ .version = (v), .topic = (t)}
SPA_API_LOG void spa_log_topic_init(struct spa_log *log, struct spa_log_topic *topic)
{
if (SPA_UNLIKELY(!log))
return;
spa_interface_call(&log->iface, struct spa_log_methods, topic_init, 1, topic);
}
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)
{
enum spa_log_level max_level;
if (SPA_UNLIKELY(!log))
return false;
if (topic && topic->has_custom_level)
max_level = topic->level;
else
max_level = log->level;
return level <= max_level;
}
/* Transparently calls to version 0 log if v1 is not supported */
#define spa_log_logt(l,lev,topic,...) \
({ \
struct spa_log *_l = l; \
if (SPA_UNLIKELY(spa_log_level_topic_enabled(_l, topic, lev))) { \
struct spa_interface *_if = &_l->iface; \
if (!spa_interface_call(_if, \
struct spa_log_methods, logt, 1, \
lev, topic, \
__VA_ARGS__)) \
spa_interface_call(_if, \
struct spa_log_methods, log, 0, \
lev, __VA_ARGS__); \
} \
})
/* Transparently calls to version 0 logv if v1 is not supported */
SPA_PRINTF_FUNC(7, 0)
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)
{
if (SPA_UNLIKELY(spa_log_level_topic_enabled(l, topic, level))) {
struct spa_interface *i = &l->iface;
if (!spa_interface_call(i,
struct spa_log_methods, logtv, 1,
level, topic,
file, line, func, fmt, args))
spa_interface_call(i,
struct spa_log_methods, logv, 0,
level, file, line, func, fmt, args);
}
}
#define spa_logt_lev(l,lev,t,...) \
spa_log_logt(l,lev,t,__FILE__,__LINE__,__func__,__VA_ARGS__)
#define spa_log_lev(l,lev,...) \
spa_logt_lev(l,lev,SPA_LOG_TOPIC_DEFAULT,__VA_ARGS__)
#define spa_log_log(l,lev,...) \
spa_log_logt(l,lev,SPA_LOG_TOPIC_DEFAULT,__VA_ARGS__)
#define spa_log_logv(l,lev,...) \
spa_log_logtv(l,lev,SPA_LOG_TOPIC_DEFAULT,__VA_ARGS__)
#define spa_log_error(l,...) spa_log_lev(l,SPA_LOG_LEVEL_ERROR,__VA_ARGS__)
#define spa_log_warn(l,...) spa_log_lev(l,SPA_LOG_LEVEL_WARN,__VA_ARGS__)
#define spa_log_info(l,...) spa_log_lev(l,SPA_LOG_LEVEL_INFO,__VA_ARGS__)
#define spa_log_debug(l,...) spa_log_lev(l,SPA_LOG_LEVEL_DEBUG,__VA_ARGS__)
#define spa_log_trace(l,...) spa_log_lev(l,SPA_LOG_LEVEL_TRACE,__VA_ARGS__)
#define spa_logt_error(l,t,...) spa_logt_lev(l,SPA_LOG_LEVEL_ERROR,t,__VA_ARGS__)
#define spa_logt_warn(l,t,...) spa_logt_lev(l,SPA_LOG_LEVEL_WARN,t,__VA_ARGS__)
#define spa_logt_info(l,t,...) spa_logt_lev(l,SPA_LOG_LEVEL_INFO,t,__VA_ARGS__)
#define spa_logt_debug(l,t,...) spa_logt_lev(l,SPA_LOG_LEVEL_DEBUG,t,__VA_ARGS__)
#define spa_logt_trace(l,t,...) spa_logt_lev(l,SPA_LOG_LEVEL_TRACE,t,__VA_ARGS__)
#ifndef FASTPATH
#define spa_log_trace_fp(l,...) spa_log_lev(l,SPA_LOG_LEVEL_TRACE,__VA_ARGS__)
#else
#define spa_log_trace_fp(l,...)
#endif
#define SPA_LOG_TOPIC_ENUM_NAME "spa_log_topic_enum"
#define SPA_LOG_TOPIC_ENUM_DEFINE(s, e) \
SPA_EXPORT struct spa_log_topic_enum spa_log_topic_enum = (struct spa_log_topic_enum) { \
.version = SPA_VERSION_LOG_TOPIC_ENUM, \
.topics = (s), \
.topics_end = (e), \
}
#define SPA_LOG_TOPIC_REGISTER(v) \
__attribute__((used)) __attribute__((retain)) \
__attribute__((section("spa_log_topic"))) __attribute__((aligned(__alignof__(struct spa_log_topic *)))) \
static struct spa_log_topic * const spa_log_topic_export_##v = &v
#define SPA_LOG_TOPIC_DEFINE(var,name) \
struct spa_log_topic var = SPA_LOG_TOPIC(SPA_VERSION_LOG_TOPIC, name); \
SPA_LOG_TOPIC_REGISTER(var)
* Define and magically register a \ref spa_log_topic with static scope
*
* \since 1.1.0
*/
#define SPA_LOG_TOPIC_DEFINE_STATIC(var,name) \
static struct spa_log_topic var = SPA_LOG_TOPIC(SPA_VERSION_LOG_TOPIC, name); \
SPA_LOG_TOPIC_REGISTER(var)
* \ref spa_log_topic in the plugin.
*
* \since 1.1.0
*/
#define SPA_LOG_TOPIC_ENUM_DEFINE_REGISTERED \
extern struct spa_log_topic * const __start_spa_log_topic[]; \
extern struct spa_log_topic * const __stop_spa_log_topic[]; \
SPA_LOG_TOPIC_ENUM_DEFINE(__start_spa_log_topic, __stop_spa_log_topic)
/** \fn spa_log_error */
#define SPA_KEY_LOG_LEVEL "log.level"
#define SPA_KEY_LOG_COLORS "log.colors"
#define SPA_KEY_LOG_FILE "log.file"
#define SPA_KEY_LOG_TIMESTAMP "log.timestamp"
#define SPA_KEY_LOG_LINE "log.line"
#define SPA_KEY_LOG_PATTERNS "log.patterns"
#ifdef __cplusplus
} /* extern "C" */
#endif
#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:297
SPA_UNLIKELY
#define SPA_UNLIKELY(x)
Definition defs.h:398
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