log: support topic patterns also in config file log.level
Handle log topic patterns also in wp_log_set_global_level, so that they can be specified everywhere.
This commit is contained in:

committed by
George Kiagiadakis

parent
7c28c226b8
commit
c746e18350
114
lib/wp/log.c
114
lib/wp/log.c
@@ -176,6 +176,9 @@ static GString *spa_dbg_str = NULL;
|
|||||||
|
|
||||||
#include <spa/debug/pod.h>
|
#include <spa/debug/pod.h>
|
||||||
|
|
||||||
|
#define DEFAULT_LOG_LEVEL 4 /* MESSAGE */
|
||||||
|
#define DEFAULT_LOG_LEVEL_FLAGS (G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_ERROR)
|
||||||
|
|
||||||
struct log_topic_pattern
|
struct log_topic_pattern
|
||||||
{
|
{
|
||||||
GPatternSpec *spec;
|
GPatternSpec *spec;
|
||||||
@@ -185,6 +188,7 @@ struct log_topic_pattern
|
|||||||
static struct {
|
static struct {
|
||||||
gboolean use_color;
|
gboolean use_color;
|
||||||
gboolean output_is_journal;
|
gboolean output_is_journal;
|
||||||
|
gboolean set_pw_log;
|
||||||
gint global_log_level;
|
gint global_log_level;
|
||||||
GLogLevelFlags global_log_level_flags;
|
GLogLevelFlags global_log_level_flags;
|
||||||
struct log_topic_pattern *patterns;
|
struct log_topic_pattern *patterns;
|
||||||
@@ -193,8 +197,9 @@ static struct {
|
|||||||
} log_state = {
|
} log_state = {
|
||||||
.use_color = FALSE,
|
.use_color = FALSE,
|
||||||
.output_is_journal = FALSE,
|
.output_is_journal = FALSE,
|
||||||
.global_log_level = 4 /* MESSAGE */,
|
.set_pw_log = FALSE,
|
||||||
.global_log_level_flags = G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_ERROR,
|
.global_log_level = DEFAULT_LOG_LEVEL,
|
||||||
|
.global_log_level_flags = DEFAULT_LOG_LEVEL_FLAGS,
|
||||||
.patterns = NULL,
|
.patterns = NULL,
|
||||||
.log_topics = NULL,
|
.log_topics = NULL,
|
||||||
};
|
};
|
||||||
@@ -384,36 +389,30 @@ update_log_topic_levels (void)
|
|||||||
g_mutex_unlock (&log_state.log_topics_lock);
|
g_mutex_unlock (&log_state.log_topics_lock);
|
||||||
}
|
}
|
||||||
|
|
||||||
gboolean
|
static void
|
||||||
wp_log_set_global_level (const gchar *log_level)
|
free_patterns (struct log_topic_pattern *patterns)
|
||||||
{
|
{
|
||||||
gint level;
|
struct log_topic_pattern *p = patterns;
|
||||||
if (level_index_from_string (log_level, &level)) {
|
|
||||||
log_state.global_log_level = level;
|
while (p && p->spec) {
|
||||||
log_state.global_log_level_flags = level_index_to_full_flags (level);
|
g_clear_pointer (&p->spec, g_pattern_spec_free);
|
||||||
wp_spa_log_get_instance()->level = level_index_to_spa (level);
|
++p;
|
||||||
pw_log_set_level (level_index_to_spa (level));
|
|
||||||
update_log_topic_levels ();
|
|
||||||
return TRUE;
|
|
||||||
} else {
|
|
||||||
return FALSE;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
g_free (patterns);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* private, called from wp_init() */
|
/* Parse value to log level and patterns. If no global level in string,
|
||||||
void
|
global_log_level is not modified. */
|
||||||
wp_log_init (gint flags)
|
static gboolean
|
||||||
|
parse_log_level (const gchar *level_str, struct log_topic_pattern **global_patterns, gint *global_log_level)
|
||||||
{
|
{
|
||||||
const gchar *level_str;
|
|
||||||
gint global_log_level = log_state.global_log_level;
|
|
||||||
struct log_topic_pattern *patterns = NULL, *pttrn;
|
struct log_topic_pattern *patterns = NULL, *pttrn;
|
||||||
gint n_tokens = 0;
|
gint n_tokens = 0;
|
||||||
gchar **tokens = NULL;
|
gchar **tokens = NULL;
|
||||||
|
int level = *global_log_level;
|
||||||
|
|
||||||
level_str = g_getenv ("WIREPLUMBER_DEBUG");
|
*global_patterns = NULL;
|
||||||
|
|
||||||
log_state.use_color = g_log_writer_supports_color (fileno (stderr));
|
|
||||||
log_state.output_is_journal = g_log_writer_is_journald (fileno (stderr));
|
|
||||||
|
|
||||||
if (level_str && level_str[0] != '\0') {
|
if (level_str && level_str[0] != '\0') {
|
||||||
/* [<glob>:]<level>,..., */
|
/* [<glob>:]<level>,..., */
|
||||||
@@ -437,11 +436,12 @@ wp_log_init (gint flags)
|
|||||||
pttrn->log_level = lvl;
|
pttrn->log_level = lvl;
|
||||||
pttrn++;
|
pttrn++;
|
||||||
} else if (n_tok == 1 && level_index_from_string (tok[0], &lvl)) {
|
} else if (n_tok == 1 && level_index_from_string (tok[0], &lvl)) {
|
||||||
global_log_level = lvl;
|
level = lvl;
|
||||||
} else {
|
} else {
|
||||||
/* note that this is going to initialize the wp-log topic here */
|
pttrn->spec = NULL;
|
||||||
wp_warning ("Ignoring invalid format in WIREPLUMBER_DEBUG: '%s'",
|
pw_free_strv (tok);
|
||||||
tokens[i]);
|
free_patterns (patterns);
|
||||||
|
return FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
pw_free_strv (tok);
|
pw_free_strv (tok);
|
||||||
@@ -458,21 +458,63 @@ wp_log_init (gint flags)
|
|||||||
|
|
||||||
pw_free_strv (tokens);
|
pw_free_strv (tokens);
|
||||||
|
|
||||||
|
*global_patterns = patterns;
|
||||||
|
*global_log_level = level;
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
gboolean
|
||||||
|
wp_log_set_global_level (const gchar *level_str)
|
||||||
|
{
|
||||||
|
gint level;
|
||||||
|
GLogLevelFlags flags;
|
||||||
|
struct log_topic_pattern *patterns;
|
||||||
|
|
||||||
|
level = DEFAULT_LOG_LEVEL;
|
||||||
|
if (!parse_log_level (level_str, &patterns, &level))
|
||||||
|
return FALSE;
|
||||||
|
|
||||||
|
flags = level_index_to_full_flags (level);
|
||||||
|
|
||||||
g_mutex_lock (&log_state.log_topics_lock);
|
g_mutex_lock (&log_state.log_topics_lock);
|
||||||
log_state.patterns = patterns;
|
log_state.global_log_level = level;
|
||||||
log_state.global_log_level = global_log_level;
|
log_state.global_log_level_flags = flags;
|
||||||
log_state.global_log_level_flags =
|
SPA_SWAP (log_state.patterns, patterns);
|
||||||
level_index_to_full_flags (global_log_level);
|
|
||||||
g_mutex_unlock (&log_state.log_topics_lock);
|
g_mutex_unlock (&log_state.log_topics_lock);
|
||||||
|
|
||||||
/* set the log level also on the spa_log */
|
free_patterns (patterns);
|
||||||
wp_spa_log_get_instance()->level = level_index_to_spa (global_log_level);
|
|
||||||
|
update_log_topic_levels ();
|
||||||
|
|
||||||
|
wp_spa_log_get_instance()->level = level_index_to_spa (level);
|
||||||
|
|
||||||
|
if (log_state.set_pw_log)
|
||||||
|
pw_log_set_level (level_index_to_spa (level));
|
||||||
|
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* private, called from wp_init() */
|
||||||
|
void
|
||||||
|
wp_log_init (gint flags)
|
||||||
|
{
|
||||||
|
log_state.use_color = g_log_writer_supports_color (fileno (stderr));
|
||||||
|
log_state.output_is_journal = g_log_writer_is_journald (fileno (stderr));
|
||||||
|
log_state.set_pw_log = flags & WP_INIT_SET_PW_LOG && !g_getenv ("WIREPLUMBER_NO_PW_LOG");
|
||||||
|
|
||||||
if (flags & WP_INIT_SET_GLIB_LOG)
|
if (flags & WP_INIT_SET_GLIB_LOG)
|
||||||
g_log_set_writer_func (wp_log_writer_default, NULL, NULL);
|
g_log_set_writer_func (wp_log_writer_default, NULL, NULL);
|
||||||
|
|
||||||
/* set PIPEWIRE_DEBUG and the spa_log interface that pipewire will use */
|
/* set the spa_log interface that pipewire will use */
|
||||||
if (flags & WP_INIT_SET_PW_LOG && !g_getenv ("WIREPLUMBER_NO_PW_LOG")) {
|
if (log_state.set_pw_log)
|
||||||
|
pw_log_set (wp_spa_log_get_instance ());
|
||||||
|
|
||||||
|
if (!wp_log_set_global_level (g_getenv ("WIREPLUMBER_DEBUG"))) {
|
||||||
|
wp_warning ("Ignoring invalid value in WIREPLUMBER_DEBUG");
|
||||||
|
wp_log_set_global_level (NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (log_state.set_pw_log) {
|
||||||
/* always set PIPEWIRE_DEBUG for 2 reasons:
|
/* always set PIPEWIRE_DEBUG for 2 reasons:
|
||||||
* 1. to overwrite it from the environment, in case the user has set it
|
* 1. to overwrite it from the environment, in case the user has set it
|
||||||
* 2. to prevent pw_context from parsing "log.level" from the config file;
|
* 2. to prevent pw_context from parsing "log.level" from the config file;
|
||||||
@@ -482,8 +524,6 @@ wp_log_init (gint flags)
|
|||||||
gchar lvl_str[2];
|
gchar lvl_str[2];
|
||||||
g_snprintf (lvl_str, 2, "%d", wp_spa_log_get_instance ()->level);
|
g_snprintf (lvl_str, 2, "%d", wp_spa_log_get_instance ()->level);
|
||||||
g_warn_if_fail (g_setenv ("PIPEWIRE_DEBUG", lvl_str, TRUE));
|
g_warn_if_fail (g_setenv ("PIPEWIRE_DEBUG", lvl_str, TRUE));
|
||||||
pw_log_set_level (wp_spa_log_get_instance ()->level);
|
|
||||||
pw_log_set (wp_spa_log_get_instance ());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user