diff options
author | wm4 <wm4@nowhere> | 2016-03-01 22:03:45 +0100 |
---|---|---|
committer | wm4 <wm4@nowhere> | 2016-03-01 22:03:45 +0100 |
commit | 4e53272376c546bac2e5c73f7ebfe456162d3663 (patch) | |
tree | ef4c240651e99bdd850ef3e027b39a9af24e95ed | |
parent | e0944991973c04701c830647a3f943b43d546b20 (diff) | |
download | mpv-4e53272376c546bac2e5c73f7ebfe456162d3663.tar.bz2 mpv-4e53272376c546bac2e5c73f7ebfe456162d3663.tar.xz |
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.)
-rw-r--r-- | common/av_log.c | 9 |
1 files changed, 7 insertions, 2 deletions
diff --git a/common/av_log.c b/common/av_log.c index 7e0b271c81..bfd9a836e8 100644 --- a/common/av_log.c +++ b/common/av_log.c @@ -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); |