From a600d152d21ef398eb72b008ee3fe266696eb638 Mon Sep 17 00:00:00 2001 From: wm4 Date: Sun, 10 May 2020 16:40:26 +0200 Subject: msg: add function to reduce log level Sometimes it's helpful to override this for specific mp_log instances, because in some specific circumstances you just want to suppress log file noise you never want to see. -1 is an allowed value (for suppressing MSGL_FATAL==0). It looks like the libplacebo wrapper still does this wrong, so it will probably trigger UB in some cases. I guess I don't care, though. --- common/msg.c | 19 +++++++++++++++++-- common/msg.h | 2 ++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/common/msg.c b/common/msg.c index 871666add0..0d1c165222 100644 --- a/common/msg.c +++ b/common/msg.c @@ -86,6 +86,7 @@ struct mp_log { struct mp_log_root *root; const char *prefix; const char *verbose_prefix; + int max_level; // minimum log level for this instance int level; // minimum log level for any outputs int terminal_level; // minimum log level for terminal output atomic_ulong reload_counter; @@ -125,8 +126,6 @@ static void update_loglevel(struct mp_log *log) struct mp_log_root *root = log->root; pthread_mutex_lock(&root->lock); log->level = MSGL_STATUS + root->verbose; // default log level - if (root->really_quiet) - log->level -= 10; for (int n = 0; root->msg_levels && root->msg_levels[n * 2 + 0]; n++) { if (match_mod(log->verbose_prefix, root->msg_levels[n * 2 + 0])) log->level = mp_msg_find_level(root->msg_levels[n * 2 + 1]); @@ -143,10 +142,25 @@ static void update_loglevel(struct mp_log *log) log->level = MPMAX(log->level, MSGL_DEBUG); if (log->root->stats_file) log->level = MPMAX(log->level, MSGL_STATS); + log->level = MPMIN(log->level, log->max_level); + if (root->really_quiet) + log->level = -1; atomic_store(&log->reload_counter, atomic_load(&log->root->reload_counter)); pthread_mutex_unlock(&root->lock); } +// Set (numerically) the maximum level that should still be output for this log +// instances. E.g. lev=MSGL_WARN => show only warnings and errors. +void mp_msg_set_max_level(struct mp_log *log, int lev) +{ + if (!log->root) + return; + pthread_mutex_lock(&log->root->lock); + log->max_level = MPCLAMP(lev, -1, MSGL_MAX); + pthread_mutex_unlock(&log->root->lock); + update_loglevel(log); +} + // Get the current effective msg level. // Thread-safety: see mp_msg(). int mp_msg_level(struct mp_log *log) @@ -456,6 +470,7 @@ struct mp_log *mp_log_new(void *talloc_ctx, struct mp_log *parent, talloc_set_destructor(log, destroy_log); log->root = parent->root; log->partial = talloc_strdup(NULL, ""); + log->max_level = MSGL_MAX; if (name) { if (name[0] == '!') { name = &name[1]; diff --git a/common/msg.h b/common/msg.h index 5c580b8b84..b0cec7b88a 100644 --- a/common/msg.h +++ b/common/msg.h @@ -60,6 +60,8 @@ static inline bool mp_msg_test(struct mp_log *log, int lev) return lev <= mp_msg_level(log); } +void mp_msg_set_max_level(struct mp_log *log, int lev); + // Convenience macros. #define mp_fatal(log, ...) mp_msg(log, MSGL_FATAL, __VA_ARGS__) #define mp_err(log, ...) mp_msg(log, MSGL_ERR, __VA_ARGS__) -- cgit v1.2.3