mirror of
https://github.com/mpv-player/mpv.git
synced 2025-12-25 04:10:20 +00:00
av_log: avoid partial lines
We want to add a prefix to the ffmpeg log message, so we called mp_msg multiple times until now. But logging such partial lines is a race condition, because there's only one internal mp_msg buffer, and no external mp_msg locks. Avoid this by building the message on a stack buffer. I might make a mp_log-local partial line buffer, but even then av_log() can be called from multiple threads, while targetting the same mp_log. (Really, ffmpeg's log API needs to be fixed.)
This commit is contained in:
@@ -131,12 +131,17 @@ static void mp_msg_av_log_callback(void *ptr, int level, const char *fmt,
|
|||||||
struct mp_log *log = get_av_log(ptr);
|
struct mp_log *log = get_av_log(ptr);
|
||||||
|
|
||||||
if (mp_msg_test(log, mp_level)) {
|
if (mp_msg_test(log, mp_level)) {
|
||||||
|
char buffer[4096] = "";
|
||||||
|
int pos = 0;
|
||||||
const char *prefix = avc ? avc->item_name(ptr) : NULL;
|
const char *prefix = avc ? avc->item_name(ptr) : NULL;
|
||||||
if (log_print_prefix && prefix)
|
if (log_print_prefix && prefix)
|
||||||
mp_msg(log, mp_level, "%s: ", prefix);
|
pos = snprintf(buffer, sizeof(buffer), "%s: ", prefix);
|
||||||
log_print_prefix = fmt[strlen(fmt) - 1] == '\n';
|
log_print_prefix = fmt[strlen(fmt) - 1] == '\n';
|
||||||
|
|
||||||
mp_msg_va(log, mp_level, fmt, vl);
|
pos = MPMIN(MPMAX(pos, 0), sizeof(buffer));
|
||||||
|
vsnprintf(buffer + pos, sizeof(buffer) - pos, fmt, vl);
|
||||||
|
|
||||||
|
mp_msg(log, mp_level, "%s", buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
pthread_mutex_unlock(&log_lock);
|
pthread_mutex_unlock(&log_lock);
|
||||||
|
|||||||
Reference in New Issue
Block a user