From 23b83c6676c17963a59febeac72fdc73a4198769 Mon Sep 17 00:00:00 2001 From: wm4 Date: Sat, 20 Jun 2015 21:40:47 +0200 Subject: client API: allow using msg-level option for log messages Client API users can enable log output with mpv_request_log_messages(). But you can enable only a single log level. This is normally enough, but the --msg-level option (which controls the terminal log level) provides more flexibility. Due to internal complexity, it would be hard to provide the same flexibility for each client API handle. But there's a simple way to achieve basically the same thing: add an option that sends log messages to the API handle, which would also be printed to the terminal as by --msg-level. The only change is that we don't disable this logic if the terminal is disabled. Instead we check for this before the message is output, which in theory can lower performance if messages are being spammed. It could be handled with some more effort, but the gain would be negligible. --- common/msg.c | 21 ++++++++++----------- common/msg_control.h | 3 +++ 2 files changed, 13 insertions(+), 11 deletions(-) (limited to 'common') diff --git a/common/msg.c b/common/msg.c index a2b0cf2169..6a7f699b31 100644 --- a/common/msg.c +++ b/common/msg.c @@ -106,16 +106,12 @@ static void update_loglevel(struct mp_log *log) { struct mp_log_root *root = log->root; pthread_mutex_lock(&mp_msg_lock); - log->level = -1; - log->terminal_level = -1; - if (log->root->use_terminal) { - log->level = MSGL_STATUS + log->root->verbose; // default log level - 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]); - } - log->terminal_level = log->root->use_terminal ? log->level : -1; + log->level = MSGL_STATUS + log->root->verbose; // default log level + 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]); } + log->terminal_level = log->level; for (int n = 0; n < log->root->num_buffers; n++) log->level = MPMAX(log->level, log->root->buffers[n]->level); if (log->root->log_file) @@ -239,7 +235,7 @@ static void pretty_print_module(FILE* stream, const char *prefix, bool use_color static bool test_terminal_level(struct mp_log *log, int lev) { - return lev <= log->terminal_level && + return lev <= log->terminal_level && log->root->use_terminal && !(lev == MSGL_STATUS && terminal_in_background()); } @@ -297,7 +293,10 @@ static void write_msg_to_buffers(struct mp_log *log, int lev, char *text) struct mp_log_root *root = log->root; for (int n = 0; n < root->num_buffers; n++) { struct mp_log_buffer *buffer = root->buffers[n]; - if (lev <= buffer->level && lev != MSGL_STATUS) { + int buffer_level = buffer->level; + if (buffer_level == MP_LOG_BUFFER_MSGL_TERM) + buffer_level = log->terminal_level; + if (lev <= buffer_level && lev != MSGL_STATUS) { // Assuming a single writer (serialized by msg lock) int avail = mp_ring_available(buffer->ring) / sizeof(void *); if (avail < 1) diff --git a/common/msg_control.h b/common/msg_control.h index c26a557c9e..d8d9b2e6e9 100644 --- a/common/msg_control.h +++ b/common/msg_control.h @@ -18,6 +18,9 @@ struct mp_log_buffer_entry { char *text; }; +// Use --msg-level option for log level of this log buffer +#define MP_LOG_BUFFER_MSGL_TERM (MSGL_MAX + 1) + struct mp_log_buffer; struct mp_log_buffer *mp_msg_log_buffer_new(struct mpv_global *global, int size, int level, -- cgit v1.2.3