From 5b14db5ab1be11f905fbc4afe7fc2e9ff5c29a81 Mon Sep 17 00:00:00 2001 From: wm4 Date: Thu, 16 Jan 2014 21:23:27 +0100 Subject: msg: print module prefixes even if message contains newlines This makes mp_msg(x, y, "a\nb\n") behave the same as mp_msg(x, y, "a\n") mp_msg(x, y, "b\n") which is probably what one would expect. Before this commit, the "b" line didn't have a prefix when using ths single mp_msg call. --- common/msg.c | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) (limited to 'common') diff --git a/common/msg.c b/common/msg.c index 76d2f95872..32bbe8e89e 100644 --- a/common/msg.c +++ b/common/msg.c @@ -197,13 +197,18 @@ void mp_msg_va(struct mp_log *log, int lev, const char *format, va_list va) snprintf(tmp, MSGSIZE_MAX, "[fprintf error]\n"); tmp[MSGSIZE_MAX - 2] = '\n'; tmp[MSGSIZE_MAX - 1] = 0; + char *text = tmp; bool header = root->header; - char *terminate = ""; + const char *prefix = log->prefix; + char *terminate = NULL; + + if ((lev >= MSGL_V && lev != MSGL_SMODE) || root->verbose || root->module) + prefix = log->verbose_prefix; if (lev == MSGL_STATUS) { if (root->termosd) { - prepare_status_line(root, tmp); + prepare_status_line(root, text); terminate = "\r"; } else { terminate = "\n"; @@ -211,21 +216,27 @@ void mp_msg_va(struct mp_log *log, int lev, const char *format, va_list va) root->header = true; } else { flush_status_line(root); - size_t len = strlen(tmp); - root->header = len && tmp[len - 1] == '\n'; + size_t len = strlen(text); + root->header = len && text[len - 1] == '\n'; } if (root->color) set_msg_color(stream, lev); - if (header) { - if ((lev >= MSGL_V && lev != MSGL_SMODE) || root->verbose || root->module) { - fprintf(stream, "[%s] ", log->verbose_prefix); - } else if (log->prefix) { - fprintf(stream, "[%s] ", log->prefix); - } - } - fprintf(stream, "%s%s", tmp, terminate); + do { + if (header && prefix) + fprintf(stream, "[%s] ", prefix); + + char *next = strchr(text, '\n'); + int len = next ? next - text + 1 : strlen(text); + fprintf(stream, "%.*s", len, text); + text = text + len; + + header = true; + } while (text[0]); + + if (terminate) + fprintf(stream, "%s", terminate); if (root->color) terminal_set_foreground_color(stream, -1); -- cgit v1.2.3