From 591e21a2ebd105c33127d4a792d4d0e1a083fcfc Mon Sep 17 00:00:00 2001 From: wm4 Date: Wed, 7 Sep 2016 11:26:25 +0200 Subject: osdep: rename atomics.h to atomic.h The standard header is stdatomic.h, so the extra "s" freaks me out every time I look at it. --- common/msg.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'common/msg.c') diff --git a/common/msg.c b/common/msg.c index 534ba41cb5..b109817c5b 100644 --- a/common/msg.c +++ b/common/msg.c @@ -27,7 +27,7 @@ #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" -- cgit v1.2.3 From ce65ea3345b657b624f28587bd7f31466ac145a2 Mon Sep 17 00:00:00 2001 From: wm4 Date: Mon, 19 Sep 2016 19:56:40 +0200 Subject: player: make --log-file and --dump-stats freely settable at runtime Same deal as with the previous commit. We use the file paths to decide when we should attempt to reopen them. --- common/msg.c | 57 +++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 37 insertions(+), 20 deletions(-) (limited to 'common/msg.c') diff --git a/common/msg.c b/common/msg.c index b109817c5b..0c5cc6f2bf 100644 --- a/common/msg.c +++ b/common/msg.c @@ -59,6 +59,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 @@ -457,10 +459,35 @@ 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. +static void reopen_file(char **opt, char **current_path, FILE **file, + const char *type, char *err_buf, size_t err_buf_size) +{ + char *old_path = *current_path ? *current_path : ""; + char *new_path = *opt ? *opt : ""; + 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"); + if (!*file) { + mp_snprintf_cat(err_buf, err_buf_size, + "Failed to open %s file '%s'\n", type, new_path); + } + } + } + +} + void mp_msg_update_msglevels(struct mpv_global *global) { struct mp_log_root *root = global->log->root; struct MPOpts *opts = global->opts; + char fail_msg[512] = ""; if (!opts) return; @@ -480,11 +507,17 @@ 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"); + reopen_file(&opts->log_file, &root->log_path, &root->log_file, "log", + fail_msg, sizeof(fail_msg)); + + reopen_file(&opts->dump_stats, &root->stats_path, &root->stats_file, "stats", + fail_msg, sizeof(fail_msg)); atomic_fetch_add(&root->reload_counter, 1); pthread_mutex_unlock(&mp_msg_lock); + + if (fail_msg[0]) + mp_err(global->log, "%s", fail_msg); } void mp_msg_force_stderr(struct mpv_global *global, bool force_stderr) @@ -499,8 +532,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 +617,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. -- cgit v1.2.3 From ef2bbd5a7ad2a85c3bb41b5813ad601a88b8c8e2 Mon Sep 17 00:00:00 2001 From: wm4 Date: Wed, 28 Sep 2016 15:04:30 +0200 Subject: msg: make --log-file and --dump-stats accept config path expansion Seems like a valid use-case. Not sure if I like it calling back into the config code. Care has to be taken for not letting the config path resolving code dead-lock (which is why locking details in the msg.c code are changed). Fixes #3591. --- common/msg.c | 44 +++++++++++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 17 deletions(-) (limited to 'common/msg.c') diff --git a/common/msg.c b/common/msg.c index 0c5cc6f2bf..4029698888 100644 --- a/common/msg.c +++ b/common/msg.c @@ -33,6 +33,7 @@ #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" @@ -459,13 +460,23 @@ 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 opt is different from *current_path, reopen *file and update *current_path. // If there's an error, _append_ it to err_buf. -static void reopen_file(char **opt, char **current_path, FILE **file, - const char *type, char *err_buf, size_t err_buf_size) +// *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 : ""; - char *new_path = *opt ? *opt : ""; if (strcmp(old_path, new_path) != 0) { if (*file) fclose(*file); @@ -474,20 +485,22 @@ static void reopen_file(char **opt, char **current_path, FILE **file, *current_path = talloc_strdup(NULL, new_path); if (new_path[0]) { *file = fopen(new_path, "wb"); - if (!*file) { - mp_snprintf_cat(err_buf, err_buf_size, - "Failed to open %s file '%s'\n", type, new_path); - } + 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; struct MPOpts *opts = global->opts; - char fail_msg[512] = ""; if (!opts) return; @@ -507,17 +520,14 @@ void mp_msg_update_msglevels(struct mpv_global *global) m_option_type_msglevels.copy(NULL, &root->msg_levels, &global->opts->msg_levels); - reopen_file(&opts->log_file, &root->log_path, &root->log_file, "log", - fail_msg, sizeof(fail_msg)); - - reopen_file(&opts->dump_stats, &root->stats_path, &root->stats_file, "stats", - fail_msg, sizeof(fail_msg)); - atomic_fetch_add(&root->reload_counter, 1); pthread_mutex_unlock(&mp_msg_lock); - if (fail_msg[0]) - mp_err(global->log, "%s", fail_msg); + 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) -- cgit v1.2.3 From ea50f6fdefc5968cafd68a7db7d49af5aefbd5b6 Mon Sep 17 00:00:00 2001 From: wm4 Date: Fri, 30 Sep 2016 14:55:59 +0200 Subject: msg: flush after every message for --log-file We'd like to get log messages on the output as soon as possible in the output. I also feel like using fflush() is nicer than using setvbuf(). Who knows how the latter behaves on win32. --- common/msg.c | 1 + 1 file changed, 1 insertion(+) (limited to 'common/msg.c') diff --git a/common/msg.c b/common/msg.c index 4029698888..6d70f88db3 100644 --- a/common/msg.c +++ b/common/msg.c @@ -289,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) -- cgit v1.2.3