summaryrefslogtreecommitdiffstats
path: root/common/msg.c
diff options
context:
space:
mode:
Diffstat (limited to 'common/msg.c')
-rw-r--r--common/msg.c68
1 files changed, 45 insertions, 23 deletions
diff --git a/common/msg.c b/common/msg.c
index 840f2ab30d..2a1f151e0f 100644
--- a/common/msg.c
+++ b/common/msg.c
@@ -22,7 +22,6 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include <unistd.h>
#include "mpv_talloc.h"
@@ -195,8 +194,23 @@ int mp_msg_level(struct mp_log *log)
static inline int term_msg_fileno(struct mp_log_root *root, int lev)
{
- return (root->force_stderr || lev == MSGL_STATUS || lev == MSGL_FATAL ||
- lev == MSGL_ERR || lev == MSGL_WARN) ? STDERR_FILENO : STDOUT_FILENO;
+ return root->force_stderr ? STDERR_FILENO : STDOUT_FILENO;
+}
+
+static inline FILE *term_msg_fp(struct mp_log_root *root, int lev)
+{
+ return term_msg_fileno(root, lev) == STDERR_FILENO ? stderr : stdout;
+}
+
+static inline bool is_status_output(struct mp_log_root *root, int lev)
+{
+ if (lev == MSGL_STATUS)
+ return true;
+ int msg_out = term_msg_fileno(root, lev);
+ int status_out = term_msg_fileno(root, MSGL_STATUS);
+ if (msg_out != status_out && root->isatty[msg_out] != root->isatty[status_out])
+ return false;
+ return true;
}
// Reposition cursor and clear lines for outputting the status line. In certain
@@ -207,6 +221,9 @@ static void prepare_prefix(struct mp_log_root *root, bstr *out, int lev, int ter
int new_lines = lev == MSGL_STATUS ? term_lines : 0;
out->len = 0;
+ if (!is_status_output(root, lev))
+ return;
+
if (!root->isatty[term_msg_fileno(root, lev)]) {
if (root->status_lines)
bstr_xappend(root, out, bstr0("\n"));
@@ -243,33 +260,39 @@ static void prepare_prefix(struct mp_log_root *root, bstr *out, int lev, int ter
root->blank_lines += root->status_lines;
}
-void mp_msg_flush_status_line(struct mp_log *log, bool clear)
+static void msg_flush_status_line(struct mp_log_root *root, bool clear)
{
- if (!log->root)
- return;
-
- mp_mutex_lock(&log->root->lock);
- if (!log->root->status_lines)
+ if (!root->status_lines)
goto done;
+ FILE *fp = term_msg_fp(root, MSGL_STATUS);
if (!clear) {
- if (log->root->isatty[STDERR_FILENO])
- fprintf(stderr, TERM_ESC_RESTORE_CURSOR);
- fprintf(stderr, "\n");
- log->root->blank_lines = 0;
- log->root->status_lines = 0;
+ if (root->isatty[term_msg_fileno(root, MSGL_STATUS)])
+ fprintf(fp, TERM_ESC_RESTORE_CURSOR);
+ fprintf(fp, "\n");
+ root->blank_lines = 0;
+ root->status_lines = 0;
goto done;
}
bstr term_msg = {0};
- prepare_prefix(log->root, &term_msg, MSGL_STATUS, 0);
+ prepare_prefix(root, &term_msg, MSGL_STATUS, 0);
if (term_msg.len) {
- fprintf(stderr, "%.*s", BSTR_P(term_msg));
+ fprintf(fp, "%.*s", BSTR_P(term_msg));
talloc_free(term_msg.start);
}
done:
- log->root->status_line.len = 0;
+ root->status_line.len = 0;
+}
+
+void mp_msg_flush_status_line(struct mp_log *log, bool clear)
+{
+ if (!log->root)
+ return;
+
+ mp_mutex_lock(&log->root->lock);
+ msg_flush_status_line(log->root, clear);
mp_mutex_unlock(&log->root->lock);
}
@@ -278,7 +301,7 @@ void mp_msg_set_term_title(struct mp_log *log, const char *title)
if (log->root && title) {
// Lock because printf to terminal is not necessarily atomic.
mp_mutex_lock(&log->root->lock);
- fprintf(stderr, "\033]0;%s\007", title);
+ fprintf(term_msg_fp(log->root, MSGL_STATUS), "\033]0;%s\007", title);
mp_mutex_unlock(&log->root->lock);
}
}
@@ -554,14 +577,13 @@ void mp_msg_va(struct mp_log *log, int lev, const char *format, va_list va)
root->term_status_msg.len = 0;
if (lev != MSGL_STATUS && root->status_line.len && root->status_log &&
- test_terminal_level(root->status_log, MSGL_STATUS))
+ is_status_output(root, lev) && test_terminal_level(root->status_log, MSGL_STATUS))
{
write_term_msg(root->status_log, MSGL_STATUS, root->status_line,
&root->term_status_msg);
}
- int fileno = term_msg_fileno(root, lev);
- FILE *stream = fileno == STDERR_FILENO ? stderr : stdout;
+ FILE *stream = term_msg_fp(root, lev);
if (root->term_msg.len) {
fwrite(root->term_msg.start, root->term_msg.len, 1, stream);
if (root->term_status_msg.len)
@@ -850,8 +872,8 @@ void mp_msg_uninit(struct mpv_global *global)
{
struct mp_log_root *root = global->log->root;
mp_msg_flush_status_line(global->log, true);
- if (root->really_quiet && root->isatty[STDERR_FILENO])
- fprintf(stderr, TERM_ESC_RESTORE_CURSOR);
+ if (root->really_quiet && root->isatty[term_msg_fileno(root, MSGL_STATUS)])
+ fprintf(term_msg_fp(root, MSGL_STATUS), TERM_ESC_RESTORE_CURSOR);
terminate_log_file_thread(root);
mp_msg_log_buffer_destroy(root->early_buffer);
mp_msg_log_buffer_destroy(root->early_filebuffer);