mirror of
https://github.com/mpv-player/mpv
synced 2025-01-03 05:22:23 +00:00
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.
This commit is contained in:
parent
34fce974c6
commit
23b83c6676
@ -32,6 +32,7 @@ API changes
|
|||||||
|
|
||||||
::
|
::
|
||||||
|
|
||||||
|
1.19 - mpv_request_log_messages() now accepts "terminal-default" as parameter
|
||||||
1.18 - add MPV_END_FILE_REASON_REDIRECT, and change behavior of
|
1.18 - add MPV_END_FILE_REASON_REDIRECT, and change behavior of
|
||||||
MPV_EVENT_END_FILE accordingly
|
MPV_EVENT_END_FILE accordingly
|
||||||
- a bunch of interface-changes.rst changes
|
- a bunch of interface-changes.rst changes
|
||||||
|
21
common/msg.c
21
common/msg.c
@ -106,16 +106,12 @@ static void update_loglevel(struct mp_log *log)
|
|||||||
{
|
{
|
||||||
struct mp_log_root *root = log->root;
|
struct mp_log_root *root = log->root;
|
||||||
pthread_mutex_lock(&mp_msg_lock);
|
pthread_mutex_lock(&mp_msg_lock);
|
||||||
log->level = -1;
|
log->level = MSGL_STATUS + log->root->verbose; // default log level
|
||||||
log->terminal_level = -1;
|
for (int n = 0; root->msg_levels && root->msg_levels[n * 2 + 0]; n++) {
|
||||||
if (log->root->use_terminal) {
|
if (match_mod(log->verbose_prefix, root->msg_levels[n * 2 + 0]))
|
||||||
log->level = MSGL_STATUS + log->root->verbose; // default log level
|
log->level = mp_msg_find_level(root->msg_levels[n * 2 + 1]);
|
||||||
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->terminal_level = log->level;
|
||||||
for (int n = 0; n < log->root->num_buffers; n++)
|
for (int n = 0; n < log->root->num_buffers; n++)
|
||||||
log->level = MPMAX(log->level, log->root->buffers[n]->level);
|
log->level = MPMAX(log->level, log->root->buffers[n]->level);
|
||||||
if (log->root->log_file)
|
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)
|
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());
|
!(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;
|
struct mp_log_root *root = log->root;
|
||||||
for (int n = 0; n < root->num_buffers; n++) {
|
for (int n = 0; n < root->num_buffers; n++) {
|
||||||
struct mp_log_buffer *buffer = root->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)
|
// Assuming a single writer (serialized by msg lock)
|
||||||
int avail = mp_ring_available(buffer->ring) / sizeof(void *);
|
int avail = mp_ring_available(buffer->ring) / sizeof(void *);
|
||||||
if (avail < 1)
|
if (avail < 1)
|
||||||
|
@ -18,6 +18,9 @@ struct mp_log_buffer_entry {
|
|||||||
char *text;
|
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;
|
||||||
struct mp_log_buffer *mp_msg_log_buffer_new(struct mpv_global *global,
|
struct mp_log_buffer *mp_msg_log_buffer_new(struct mpv_global *global,
|
||||||
int size, int level,
|
int size, int level,
|
||||||
|
@ -198,7 +198,7 @@ extern "C" {
|
|||||||
* relational operators (<, >, <=, >=).
|
* relational operators (<, >, <=, >=).
|
||||||
*/
|
*/
|
||||||
#define MPV_MAKE_VERSION(major, minor) (((major) << 16) | (minor) | 0UL)
|
#define MPV_MAKE_VERSION(major, minor) (((major) << 16) | (minor) | 0UL)
|
||||||
#define MPV_CLIENT_API_VERSION MPV_MAKE_VERSION(1, 18)
|
#define MPV_CLIENT_API_VERSION MPV_MAKE_VERSION(1, 19)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the MPV_CLIENT_API_VERSION the mpv source has been compiled with.
|
* Return the MPV_CLIENT_API_VERSION the mpv source has been compiled with.
|
||||||
@ -1418,6 +1418,9 @@ int mpv_request_event(mpv_handle *ctx, mpv_event_id event, int enable);
|
|||||||
* @param min_level Minimal log level as string. Valid log levels:
|
* @param min_level Minimal log level as string. Valid log levels:
|
||||||
* no fatal error warn info status v debug trace
|
* no fatal error warn info status v debug trace
|
||||||
* The value "no" disables all messages. This is the default.
|
* The value "no" disables all messages. This is the default.
|
||||||
|
* An exception is the value "terminal-default", which uses the
|
||||||
|
* log level as set by the "--msg-level" option. This works
|
||||||
|
* even if the terminal is disabled. (Since API version 1.19.)
|
||||||
* Also see mpv_log_level.
|
* Also see mpv_log_level.
|
||||||
*/
|
*/
|
||||||
int mpv_request_log_messages(mpv_handle *ctx, const char *min_level);
|
int mpv_request_log_messages(mpv_handle *ctx, const char *min_level);
|
||||||
|
@ -1523,6 +1523,9 @@ int mpv_request_log_messages(mpv_handle *ctx, const char *min_level)
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (strcmp(min_level, "terminal-default") == 0)
|
||||||
|
level = MP_LOG_BUFFER_MSGL_TERM;
|
||||||
|
|
||||||
if (level < 0 && strcmp(min_level, "no") != 0)
|
if (level < 0 && strcmp(min_level, "no") != 0)
|
||||||
return MPV_ERROR_INVALID_PARAMETER;
|
return MPV_ERROR_INVALID_PARAMETER;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user