log: add topic loggers and a default topic

pw_log_log/logv now go through the topic-based logger using the
"default" topic. Log topics themselves can be allocated by the call
sites. The simplest way to use a topic from e.g. a module:

    PW_LOG_TOPIC_STATIC(mod_topic, "mod.foo");
    #define PW_LOG_TOPIC_DEFAULT mod_topic
    ...
    void pipewire__module_init() {
  	  PW_LOG_TOPIC_INIT(mod_topic);
  	  ...
    }

With the #define all pw_log_foo() are now routed through the custom
topic. For the cases where the log topic must be specified, a
pw_logt_foo() set of macros is available.

Log topics are enabled through the PIPEWIRE_DEBUG environment variable
which now supports globs, e.g. PIPEWIRE_DEBUG="*:I;mod.access:D"
to enable global INFO but DEBUG for the access module.

Namespaces documented are "pw", "mod" and "conn", for pipewire-internal
stuff, modules and connection dumping. The latter is special-cased to
avoid spamming the log files, it requires an expcit "conn.<glob>"
pattern to enable.

The "default" topic always exists and is the fallback for any
pw_log_foo() invocation that does not use a topic.
This commit is contained in:
Peter Hutterer 2021-09-16 13:43:48 +10:00 committed by Wim Taymans
parent b58282353b
commit 52bd80aaa4
6 changed files with 308 additions and 50 deletions

View file

@ -22,11 +22,15 @@
* DEALINGS IN THE SOFTWARE.
*/
#include <limits.h>
#include <fnmatch.h>
#include <spa/support/log-impl.h>
#include <spa/pod/pod.h>
#include <spa/debug/types.h>
#include <spa/pod/iter.h>
#include <spa/utils/list.h>
#include <pipewire/log.h>
#include <pipewire/private.h>
@ -40,6 +44,18 @@ enum spa_log_level pw_log_level = DEFAULT_LOG_LEVEL;
static struct spa_log *global_log = &default_log.log;
SPA_EXPORT
struct spa_log_topic *PW_LOG_TOPIC_DEFAULT;
PW_LOG_TOPIC_STATIC(log_topic, "pw.log"); /* log topic for this file here */
PW_LOG_TOPIC(log_conf, "pw.conf");
PW_LOG_TOPIC(log_context, "pw.context");
PW_LOG_TOPIC(log_metadata, "pw.metadata");
PW_LOG_TOPIC(log_node, "pw.node");
PW_LOG_TOPIC(log_port, "pw.port");
PW_LOG_TOPIC(PW_LOG_TOPIC_DEFAULT, "default");
/** Set the global log interface
* \param log the global log to set
*/
@ -74,8 +90,9 @@ void pw_log_set_level(enum spa_log_level level)
global_log->level = level;
}
/** Log a message
/** Log a message for the given topic
* \param level the log level
* \param topic the topic
* \param file the file this message originated from
* \param line the line number
* \param func the function
@ -85,21 +102,46 @@ void pw_log_set_level(enum spa_log_level level)
*/
SPA_EXPORT
void
pw_log_log(enum spa_log_level level,
const char *file,
int line,
const char *func,
const char *fmt, ...)
pw_log_logt(enum spa_log_level level,
const struct spa_log_topic *topic,
const char *file,
int line,
const char *func,
const char *fmt, ...)
{
if (SPA_UNLIKELY(pw_log_level_enabled(level))) {
if (SPA_UNLIKELY(pw_log_topic_enabled(level, topic))) {
va_list args;
va_start(args, fmt);
spa_log_logv(global_log, level, file, line, func, fmt, args);
spa_log_logtv(global_log, level, topic, file, line, func, fmt, args);
va_end(args);
}
}
/** Log a message with va_list
/** Log a message for the given topic with va_list
* \param level the log level
* \param topic the topic
* \param file the file this message originated from
* \param line the line number
* \param func the function
* \param fmt the printf style format
* \param args a va_list of arguments
*
*/
SPA_EXPORT
void
pw_log_logtv(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_log_logtv(global_log, level, topic, file, line, func, fmt, args);
}
/** Log a message for the default topic with va_list
* \param level the log level
* \param file the file this message originated from
* \param line the line number
@ -117,9 +159,30 @@ pw_log_logv(enum spa_log_level level,
const char *fmt,
va_list args)
{
if (SPA_UNLIKELY(pw_log_level_enabled(level))) {
spa_log_logv(global_log, level, file, line, func, fmt, args);
}
pw_log_logtv(level, PW_LOG_TOPIC_DEFAULT, file, line, func, fmt, args);
}
/** Log a message for the default topic
* \param level the log level
* \param file the file this message originated from
* \param line the line number
* \param func the function
* \param fmt the printf style format
* \param ... printf style arguments to log
*
*/
SPA_EXPORT
void
pw_log_log(enum spa_log_level level,
const char *file,
int line,
const char *func,
const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
pw_log_logtv(level, PW_LOG_TOPIC_DEFAULT, file, line, func, fmt, args);
va_end(args);
}
/** \fn void pw_log_error (const char *format, ...)
@ -333,3 +396,21 @@ void pw_log_log_object(enum spa_log_level level,
}
}
}
SPA_EXPORT
void
_pw_log_topic_new(struct spa_log_topic *topic)
{
spa_log_topic_init(global_log, topic);
}
void
pw_log_init(void)
{
PW_LOG_TOPIC_INIT(PW_LOG_TOPIC_DEFAULT);
PW_LOG_TOPIC_INIT(log_topic);
PW_LOG_TOPIC_INIT(log_context);
PW_LOG_TOPIC_INIT(log_metadata);
PW_LOG_TOPIC_INIT(log_node);
PW_LOG_TOPIC_INIT(log_port);
}

View file

@ -47,6 +47,8 @@ extern "C" {
/** The global log level */
extern enum spa_log_level pw_log_level;
extern struct spa_log_topic *PW_LOG_TOPIC_DEFAULT;
/** Configure a logging module. This is usually done automatically
* in pw_init() but you can install a custom logger before calling
* pw_init(). */
@ -58,37 +60,107 @@ struct spa_log *pw_log_get(void);
/** Configure the logging level */
void pw_log_set_level(enum spa_log_level level);
/** Log a message for a topic */
void
pw_log_logt(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(6, 7);
/** Log a message */
/** Log a message for a topic */
void
pw_log_logtv(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(6, 0);
/** Log a message for the default topic */
void
pw_log_log(enum spa_log_level level,
const char *file,
int line, const char *func,
const char *fmt, ...) SPA_PRINTF_FUNC(5, 6);
/** Log a message */
/** Log a message for the default topic */
void
pw_log_logv(enum spa_log_level level,
const char *file,
int line, const char *func,
const char *fmt, va_list args) SPA_PRINTF_FUNC(5, 0);
/** Initialize the log topic. The returned topic is owned by the pipewire
* context and the topic must not be modified or freed.
* Do not use this function directly, use one of PW_LOG_TOPIC_* instead.
*
* \see PW_LOG_TOPIC_STATIC
* \see PW_LOG_TOPIC_EXTERN
* \see PW_LOG_TOPIC
*/
void
_pw_log_topic_new(struct spa_log_topic *topic);
/**
* Declare a static log topic named \a var. The usual usage is:
* \code
* PW_LOG_TOPIC_STATIC(my_topic);
* #define PW_LOG_TOPIC_DEFAULT my_topic
*
* void foo() {
* pw_log_debug("bar");
* }
* \endcode
*/
#define PW_LOG_TOPIC_STATIC(var, topic) \
static struct spa_log_topic var##__LINE__ = SPA_LOG_TOPIC(0, topic); \
static struct spa_log_topic *var = &(var##__LINE__)
/**
* Declare a static log topic named \a var.
* See \ref PW_LOG_TOPIC_STATIC for an example usage.
*/
#define PW_LOG_TOPIC_EXTERN(var) \
extern struct spa_log_topic *var
/**
* Declare a static log topic named \a var.
* See \ref PW_LOG_TOPIC_STATIC for an example usage.
*/
#define PW_LOG_TOPIC(var, topic) \
struct spa_log_topic var##__LINE__ = SPA_LOG_TOPIC(0, topic); \
struct spa_log_topic *var = &(var##__LINE__)
#define PW_LOG_TOPIC_INIT(var) \
spa_log_topic_init(pw_log_get(), var);
/** Check if a loglevel is enabled */
#define pw_log_level_enabled(lev) (pw_log_level >= (lev))
#define pw_log_topic_enabled(lev,t) ((t) && (t)->has_custom_level ? (t)->level >= (lev) : pw_log_level_enabled((lev)))
#define pw_log(lev,...) \
({ \
if (SPA_UNLIKELY(pw_log_level_enabled (lev))) \
pw_log_log(lev,__FILE__,__LINE__,__func__,__VA_ARGS__); \
#define pw_logt(lev,topic,...) \
({ \
if (SPA_UNLIKELY(pw_log_topic_enabled(lev,topic))) \
pw_log_logt(lev,topic,__FILE__,__LINE__,__func__,__VA_ARGS__); \
})
#define pw_log(lev,...) pw_logt(lev,PW_LOG_TOPIC_DEFAULT,__VA_ARGS__)
#define pw_log_error(...) pw_log(SPA_LOG_LEVEL_ERROR,__VA_ARGS__)
#define pw_log_warn(...) pw_log(SPA_LOG_LEVEL_WARN,__VA_ARGS__)
#define pw_log_info(...) pw_log(SPA_LOG_LEVEL_INFO,__VA_ARGS__)
#define pw_log_debug(...) pw_log(SPA_LOG_LEVEL_DEBUG,__VA_ARGS__)
#define pw_log_trace(...) pw_log(SPA_LOG_LEVEL_TRACE,__VA_ARGS__)
#define pw_logt_error(t,...) pw_logt(SPA_LOG_LEVEL_ERROR,t,__VA_ARGS__)
#define pw_logt_warn(t,...) pw_logt(SPA_LOG_LEVEL_WARN,t,__VA_ARGS__)
#define pw_logt_info(t,...) pw_logt(SPA_LOG_LEVEL_INFO,t,__VA_ARGS__)
#define pw_logt_debug(t,...) pw_logt(SPA_LOG_LEVEL_DEBUG,t,__VA_ARGS__)
#define pw_logt_trace(t,...) pw_logt(SPA_LOG_LEVEL_TRACE,t,__VA_ARGS__)
#ifndef FASTPATH
#define pw_log_trace_fp(...) pw_log(SPA_LOG_LEVEL_TRACE,__VA_ARGS__)
#else

View file

@ -213,30 +213,6 @@ static void unref_handle(struct handle *handle)
}
}
static void configure_debug(struct support *support, const char *str)
{
char **level;
int n_tokens;
level = pw_split_strv(str, ":", INT_MAX, &n_tokens);
if (n_tokens > 0) {
switch (level[0][0]) {
case 'X': pw_log_set_level(SPA_LOG_LEVEL_NONE); break;
case 'E': pw_log_set_level(SPA_LOG_LEVEL_ERROR); break;
case 'W': pw_log_set_level(SPA_LOG_LEVEL_WARN); break;
case 'I': pw_log_set_level(SPA_LOG_LEVEL_INFO); break;
case 'D': pw_log_set_level(SPA_LOG_LEVEL_DEBUG); break;
case 'T': pw_log_set_level(SPA_LOG_LEVEL_TRACE); break;
default: pw_log_set_level(atoi(level[0])); break;
}
}
if (n_tokens > 1)
support->categories = pw_split_strv(level[1], ",", INT_MAX, &n_tokens);
pw_free_strv(level);
}
SPA_EXPORT
uint32_t pw_get_support(struct spa_support *support, uint32_t max_support)
{
@ -623,9 +599,6 @@ void pw_init(int *argc, char **argv[])
if ((str = getenv("PIPEWIRE_NO_CONFIG")) != NULL)
support->no_config = pw_properties_parse_bool(str);
if ((str = getenv("PIPEWIRE_DEBUG")) != NULL)
configure_debug(support, str);
init_i18n(support);
if ((str = getenv("SPA_PLUGIN_DIR")) == NULL)
@ -672,6 +645,8 @@ void pw_init(int *argc, char **argv[])
SPA_SUPPORT_INIT(SPA_TYPE_INTERFACE_Log, pw_log_get());
}
pw_log_init();
n_items = 0;
if ((str = getenv("PIPEWIRE_CPU")))
items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_CPU_FORCE, str);

View file

@ -1257,6 +1257,9 @@ void pw_log_log_object(enum spa_log_level level, const char *file, int line,
bool pw_log_is_default(void);
void pw_log_init(void);
void pw_log_deinit(void);
int pw_settings_init(struct pw_context *context);
void pw_settings_clean(struct pw_context *context);