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:
wm4 2016-03-01 22:03:45 +01:00
parent e094499197
commit 4e53272376
1 changed files with 7 additions and 2 deletions

View File

@ -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);
if (mp_msg_test(log, mp_level)) {
char buffer[4096] = "";
int pos = 0;
const char *prefix = avc ? avc->item_name(ptr) : NULL;
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';
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);