diff options
Diffstat (limited to 'common/msg.c')
-rw-r--r-- | common/msg.c | 72 |
1 files changed, 50 insertions, 22 deletions
diff --git a/common/msg.c b/common/msg.c index 534ba41cb5..6d70f88db3 100644 --- a/common/msg.c +++ b/common/msg.c @@ -27,12 +27,13 @@ #include "mpv_talloc.h" #include "misc/bstr.h" -#include "osdep/atomics.h" +#include "osdep/atomic.h" #include "common/common.h" #include "common/global.h" #include "misc/ring.h" #include "misc/bstr.h" #include "options/options.h" +#include "options/path.h" #include "osdep/terminal.h" #include "osdep/io.h" #include "osdep/timer.h" @@ -59,6 +60,8 @@ struct mp_log_root { int num_buffers; FILE *log_file; FILE *stats_file; + char *log_path; + char *stats_path; // --- must be accessed atomically /* This is incremented every time the msglevels must be reloaded. * (This is perhaps better than maintaining a globally accessible and @@ -286,6 +289,7 @@ static void write_log_file(struct mp_log *log, int lev, char *text) (mp_time_us() - MP_START_TIME) / 1e6, mp_log_levels[lev][0], log->verbose_prefix, text); + fflush(root->log_file); } static void write_msg_to_buffers(struct mp_log *log, int lev, char *text) @@ -457,6 +461,43 @@ void mp_msg_init(struct mpv_global *global) mp_msg_update_msglevels(global); } +// If opt is different from *current_path, reopen *file and update *current_path. +// If there's an error, _append_ it to err_buf. +// *current_path and *file are, rather trickily, only accessible under the +// mp_msg_lock. +static void reopen_file(char *opt, char **current_path, FILE **file, + const char *type, struct mpv_global *global) +{ + void *tmp = talloc_new(NULL); + bool fail = false; + + char *new_path = mp_get_user_path(tmp, global, opt); + if (!new_path) + new_path = ""; + + pthread_mutex_lock(&mp_msg_lock); // for *current_path/*file + + char *old_path = *current_path ? *current_path : ""; + if (strcmp(old_path, new_path) != 0) { + if (*file) + fclose(*file); + *file = NULL; + talloc_free(*current_path); + *current_path = talloc_strdup(NULL, new_path); + if (new_path[0]) { + *file = fopen(new_path, "wb"); + fail = !*file; + } + } + + pthread_mutex_unlock(&mp_msg_lock); + + if (fail) + mp_err(global->log, "Failed to open %s file '%s'\n", type, new_path); + + talloc_free(tmp); +} + void mp_msg_update_msglevels(struct mpv_global *global) { struct mp_log_root *root = global->log->root; @@ -480,11 +521,14 @@ void mp_msg_update_msglevels(struct mpv_global *global) m_option_type_msglevels.copy(NULL, &root->msg_levels, &global->opts->msg_levels); - if (!root->log_file && opts->log_file && opts->log_file[0]) - root->log_file = fopen(opts->log_file, "wb"); - atomic_fetch_add(&root->reload_counter, 1); pthread_mutex_unlock(&mp_msg_lock); + + reopen_file(opts->log_file, &root->log_path, &root->log_file, + "log", global); + + reopen_file(opts->dump_stats, &root->stats_path, &root->stats_file, + "stats", global); } void mp_msg_force_stderr(struct mpv_global *global, bool force_stderr) @@ -499,8 +543,10 @@ void mp_msg_uninit(struct mpv_global *global) struct mp_log_root *root = global->log->root; if (root->stats_file) fclose(root->stats_file); + talloc_free(root->stats_path); if (root->log_file) fclose(root->log_file); + talloc_free(root->log_path); m_option_type_msglevels.free(&root->msg_levels); talloc_free(root); global->log = NULL; @@ -582,24 +628,6 @@ struct mp_log_buffer_entry *mp_msg_log_buffer_read(struct mp_log_buffer *buffer) return ptr; } -int mp_msg_open_stats_file(struct mpv_global *global, const char *path) -{ - struct mp_log_root *root = global->log->root; - int r; - - pthread_mutex_lock(&mp_msg_lock); - - if (root->stats_file) - fclose(root->stats_file); - root->stats_file = fopen(path, "wb"); - r = root->stats_file ? 0 : -1; - - pthread_mutex_unlock(&mp_msg_lock); - - mp_msg_update_msglevels(global); - return r; -} - // Thread-safety: fully thread-safe, but keep in mind that the lifetime of // log must be guaranteed during the call. // Never call this from signal handlers. |