From 245e5b844177e9ad9a9c07eff5efab7c3fccdebc Mon Sep 17 00:00:00 2001 From: wm4 Date: Sat, 21 Dec 2013 23:11:12 +0100 Subject: msg: remove global state --- common/encode_lavc.c | 2 +- common/msg.c | 106 +++++++++++++++++++++++--------------------- common/msg.h | 9 +--- options/options.c | 13 +++--- options/options.h | 4 ++ options/parse_commandline.c | 13 ++++-- options/parse_commandline.h | 2 +- player/main.c | 7 ++- video/filter/vf_pullup.c | 1 - 9 files changed, 84 insertions(+), 73 deletions(-) diff --git a/common/encode_lavc.c b/common/encode_lavc.c index b25d183477..2cf549e538 100644 --- a/common/encode_lavc.c +++ b/common/encode_lavc.c @@ -130,7 +130,7 @@ struct encode_lavc_context *encode_lavc_init(struct encode_output_conf *options, !strcmp(filename, "/dev/stdout") || !strcmp(filename, "pipe:") || !strcmp(filename, "pipe:1"))) - mp_msg_stdout_in_use = 1; + mp_msg_force_stderr(global, true); ctx = talloc_zero(NULL, struct encode_lavc_context); ctx->log = mp_log_new(ctx, global->log, "encode-lavc"); diff --git a/common/msg.c b/common/msg.c index 5b00de7a76..ff3071a92c 100644 --- a/common/msg.c +++ b/common/msg.c @@ -40,16 +40,23 @@ #define MSGSIZE_MAX 6144 struct mp_log_root { - /* This should, at some point, contain all mp_msg related state, instead - * of having global variables (at least as long as we don't want to - * control the terminal, which is global anyway). But for now, there is - * not much. */ struct mpv_global *global; - char *msglevels; // protected by mp_msg_lock + // --- protected by mp_msg_lock + char *msglevels; + bool smode; // slave mode compatibility glue + bool module; + // --- semi-atomic access + bool color; + int verbose; + bool force_stderr; + bool mute; + // --- must be accessed atomically /* This is incremented every time the msglevels must be reloaded. * (This is perhaps better than maintaining a globally accessible and * synchronized mp_log tree.) */ int64_t reload_counter; + int header; // indicate if last line printed ended with \n or \r + int statusline; // indicates if last line printed was a status line }; struct mp_log { @@ -63,30 +70,9 @@ struct mp_log { // Protects some (not all) state in mp_log_root static pthread_mutex_t mp_msg_lock = PTHREAD_MUTEX_INITIALIZER; -bool mp_msg_stdout_in_use; -int verbose; -bool mp_msg_mute; -int mp_smode; -int mp_msg_color = 1; -int mp_msg_module; -int mp_msg_cancolor; - -// indicate if last line printed ended with \n or \r -static int header = 1; -// indicates if last line printed was a status line -static int statusline; - static const struct mp_log null_log = {0}; struct mp_log *const mp_null_log = (struct mp_log *)&null_log; -static void mp_msg_do_init(void) -{ - char *env = getenv("MPV_VERBOSE"); - if (env) - verbose = atoi(env); - mp_msg_cancolor = isatty(fileno(stdout)); -} - static bool match_mod(const char *name, bstr mod) { if (bstr_equals0(mod, "all")) @@ -99,7 +85,7 @@ static bool match_mod(const char *name, bstr mod) static void update_loglevel(struct mp_log *log) { pthread_mutex_lock(&mp_msg_lock); - log->level = MSGL_STATUS + verbose; // default log level + log->level = MSGL_STATUS + log->root->verbose; // default log level // Stupid exception for the remains of -identify if (match_mod(log->verbose_prefix, bstr0("identify"))) log->level = -1; @@ -117,29 +103,23 @@ static void update_loglevel(struct mp_log *log) // Return whether the message at this verbosity level would be actually printed. bool mp_msg_test(struct mp_log *log, int lev) { - if (mp_msg_mute || !log->root) + mp_memory_barrier(); + if (!log->root || log->root->mute) return false; if (lev == MSGL_STATUS) { // skip status line output if stderr is a tty but in background if (terminal_in_background()) return false; } - mp_memory_barrier(); if (log->reload_counter != log->root->reload_counter) update_loglevel(log); - return lev <= log->level || (mp_smode && lev == MSGL_SMODE); -} - -static int mp_msg_docolor(void) -{ - return mp_msg_cancolor && mp_msg_color; + return lev <= log->level || (log->root->smode && lev == MSGL_SMODE); } static void set_msg_color(FILE* stream, int lev) { static const int v_colors[] = {9, 1, 3, -1, -1, 2, 8, 8, -1}; - if (mp_msg_docolor()) - terminal_set_foreground_color(stream, v_colors[lev]); + terminal_set_foreground_color(stream, v_colors[lev]); } void mp_msg_va(struct mp_log *log, int lev, const char *format, va_list va) @@ -149,7 +129,8 @@ void mp_msg_va(struct mp_log *log, int lev, const char *format, va_list va) pthread_mutex_lock(&mp_msg_lock); - FILE *stream = (mp_msg_stdout_in_use || lev == MSGL_STATUS) ? stderr : stdout; + struct mp_log_root *root = log->root; + FILE *stream = (root->force_stderr || lev == MSGL_STATUS) ? stderr : stdout; char tmp[MSGSIZE_MAX]; if (vsnprintf(tmp, MSGSIZE_MAX, format, va) < 0) @@ -160,13 +141,14 @@ void mp_msg_va(struct mp_log *log, int lev, const char *format, va_list va) /* A status line is normally intended to be overwritten by the next * status line, and does not end with a '\n'. If we're printing a normal * line instead after the status one print '\n' to change line. */ - if (statusline && lev != MSGL_STATUS) + if (root->statusline && lev != MSGL_STATUS) fprintf(stderr, "\n"); - statusline = lev == MSGL_STATUS; + root->statusline = lev == MSGL_STATUS; - set_msg_color(stream, lev); - if (header) { - if ((lev >= MSGL_V && lev != MSGL_SMODE) || verbose || mp_msg_module) { + if (root->color) + set_msg_color(stream, lev); + if (root->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); @@ -174,11 +156,11 @@ void mp_msg_va(struct mp_log *log, int lev, const char *format, va_list va) } size_t len = strlen(tmp); - header = len && (tmp[len - 1] == '\n' || tmp[len - 1] == '\r'); + root->header = len && (tmp[len - 1] == '\n' || tmp[len - 1] == '\r'); fprintf(stream, "%s", tmp); - if (mp_msg_docolor()) + if (root->color) terminal_set_foreground_color(stream, -1); fflush(stream); @@ -226,32 +208,54 @@ void mp_msg_init(struct mpv_global *global) struct mp_log_root *root = talloc_zero(NULL, struct mp_log_root); root->global = global; + root->header = 1; root->reload_counter = 1; struct mp_log dummy = { .root = root }; struct mp_log *log = mp_log_new(root, &dummy, ""); - mp_msg_do_init(); - global->log = log; -} -struct mpv_global *mp_log_get_global(struct mp_log *log) -{ - return log->root->global; + mp_msg_update_msglevels(global); } void mp_msg_update_msglevels(struct mpv_global *global) { struct mp_log_root *root = global->log->root; + struct MPOpts *opts = global->opts; + + if (!opts) + return; + pthread_mutex_lock(&mp_msg_lock); + + root->verbose = opts->verbose; + root->module = opts->msg_module; + root->smode = opts->msg_identify; + root->color = opts->msg_color && isatty(fileno(stdout)); + talloc_free(root->msglevels); root->msglevels = talloc_strdup(root, global->opts->msglevels); + mp_atomic_add_and_fetch(&root->reload_counter, 1); mp_memory_barrier(); pthread_mutex_unlock(&mp_msg_lock); } +void mp_msg_mute(struct mpv_global *global, bool mute) +{ + struct mp_log_root *root = global->log->root; + + root->mute = mute; +} + +void mp_msg_force_stderr(struct mpv_global *global, bool force_stderr) +{ + struct mp_log_root *root = global->log->root; + + root->force_stderr = force_stderr; +} + void mp_msg_uninit(struct mpv_global *global) { talloc_free(global->log->root); diff --git a/common/msg.h b/common/msg.h index 27701ff30b..561efe5e53 100644 --- a/common/msg.h +++ b/common/msg.h @@ -28,11 +28,6 @@ struct mp_log; -extern int verbose; -extern bool mp_msg_mute; -extern bool mp_msg_stdout_in_use; -extern int mp_smode; // slave mode compatibility glue - // A mp_log instance that never outputs anything. extern struct mp_log *const mp_null_log; @@ -85,8 +80,8 @@ struct mpv_global; void mp_msg_init(struct mpv_global *global); void mp_msg_uninit(struct mpv_global *global); void mp_msg_update_msglevels(struct mpv_global *global); - -struct mpv_global *mp_log_get_global(struct mp_log *log); +void mp_msg_mute(struct mpv_global *global, bool mute); +void mp_msg_force_stderr(struct mpv_global *global, bool force_stderr); struct bstr; int mp_msg_split_msglevel(struct bstr *s, struct bstr *out_mod, int *out_level); diff --git a/options/options.c b/options/options.c index bd64669079..85b7a40cbe 100644 --- a/options/options.c +++ b/options/options.c @@ -215,10 +215,8 @@ extern const m_option_t ad_lavc_decode_opts_conf[]; extern const m_option_t mp_input_opts[]; const m_option_t mp_opts[] = { - // handled in command line pre-parser (parser-mpcmd.c) + // handled in command line pre-parser (parse_commandline.c) {"v", NULL, CONF_TYPE_STORE, CONF_GLOBAL | CONF_NOCFG, 0, 0, NULL}, - - // handled in command line parser (parser-mpcmd.c) {"playlist", NULL, CONF_TYPE_STRING, CONF_NOCFG | M_OPT_MIN, 1, 0, NULL}, {"{", NULL, CONF_TYPE_STORE, CONF_NOCFG, 0, 0, NULL}, {"}", NULL, CONF_TYPE_STORE, CONF_NOCFG, 0, 0, NULL}, @@ -236,11 +234,12 @@ const m_option_t mp_opts[] = { // ------------------------- common options -------------------- OPT_FLAG("quiet", quiet, CONF_GLOBAL), - {"really-quiet", &verbose, CONF_TYPE_STORE, CONF_GLOBAL|CONF_PRE_PARSE, 0, -10, NULL}, + OPT_FLAG_STORE("really-quiet", verbose, CONF_GLOBAL | CONF_PRE_PARSE, -10), OPT_GENERAL(char*, "msglevel", msglevels, CONF_GLOBAL|CONF_PRE_PARSE, .type = &m_option_type_msglevels), - {"msgcolor", &mp_msg_color, CONF_TYPE_FLAG, CONF_GLOBAL | CONF_PRE_PARSE, 0, 1, NULL}, - {"msgmodule", &mp_msg_module, CONF_TYPE_FLAG, CONF_GLOBAL, 0, 1, NULL}, + OPT_FLAG("msgcolor", msg_color, CONF_GLOBAL | CONF_PRE_PARSE), + OPT_FLAG("msgmodule", msg_module, CONF_GLOBAL), + OPT_FLAG("identify", msg_identify, CONF_GLOBAL), #if HAVE_PRIORITY {"priority", &proc_priority, CONF_TYPE_STRING, 0, 0, 0, NULL}, #endif @@ -628,7 +627,6 @@ const m_option_t mp_opts[] = { {"", (void *) mp_input_opts, CONF_TYPE_SUBCONFIG}, OPT_FLAG("list-properties", list_properties, CONF_GLOBAL), - {"identify", &mp_smode, CONF_TYPE_FLAG, CONF_GLOBAL, 0, 1, NULL}, {"help", (void *) mp_help_text, CONF_TYPE_PRINT, CONF_NOCFG|CONF_GLOBAL, 0, 0, NULL}, {"h", (void *) mp_help_text, CONF_TYPE_PRINT, CONF_NOCFG|CONF_GLOBAL, 0, 0, NULL}, {"version", (void *)print_version_opt, CONF_TYPE_PRINT_FUNC, CONF_NOCFG|CONF_GLOBAL|M_OPT_PRE_PARSE}, @@ -659,6 +657,7 @@ const m_option_t mp_opts[] = { }; const struct MPOpts mp_default_opts = { + .msg_color = 1, .reset_options = (char **)(const char *[]){"pause", NULL}, .audio_driver_list = NULL, .audio_decoders = "-spdif:*", // never select spdif by default diff --git a/options/options.h b/options/options.h index ae648a87c0..e694216fa8 100644 --- a/options/options.h +++ b/options/options.h @@ -45,6 +45,10 @@ typedef struct mp_vo_opts { typedef struct MPOpts { char *msglevels; + int verbose; + int msg_identify; + int msg_color; + int msg_module; char **reset_options; char **lua_files; diff --git a/options/parse_commandline.c b/options/parse_commandline.c index 7c4eb3cce7..11ef85a871 100644 --- a/options/parse_commandline.c +++ b/options/parse_commandline.c @@ -25,9 +25,11 @@ #include #include +#include "common/global.h" #include "common/msg.h" #include "m_option.h" #include "m_config.h" +#include "options.h" #include "common/playlist.h" #include "common/playlist_parser.h" #include "parse_commandline.h" @@ -263,10 +265,13 @@ err_out: * command line parsing), and --really-quiet suppresses messages printed * during normal options parsing. */ -void m_config_preparse_command_line(m_config_t *config, int argc, char **argv) +void m_config_preparse_command_line(m_config_t *config, struct mpv_global *global, + int argc, char **argv) { + struct MPOpts *opts = global->opts; + // Hack to shut up parser error messages - mp_msg_mute = true; + mp_msg_mute(global, true); struct parse_state p = {config, argc, argv}; while (split_opt_silent(&p) == 0) { @@ -276,9 +281,9 @@ void m_config_preparse_command_line(m_config_t *config, int argc, char **argv) int flags = M_SETOPT_FROM_CMDLINE | M_SETOPT_PRE_PARSE_ONLY; m_config_set_option_ext(config, p.arg, p.param, flags); if (bstrcmp0(p.arg, "v") == 0) - verbose++; + opts->verbose++; } } - mp_msg_mute = false; + mp_msg_mute(global, false); } diff --git a/options/parse_commandline.h b/options/parse_commandline.h index ab048a0af4..39512ca51a 100644 --- a/options/parse_commandline.h +++ b/options/parse_commandline.h @@ -28,7 +28,7 @@ struct mpv_global; int m_config_parse_mp_command_line(m_config_t *config, struct playlist *files, struct mpv_global *global, int argc, char **argv); -void m_config_preparse_command_line(struct m_config *config, +void m_config_preparse_command_line(m_config_t *config, struct mpv_global *global, int argc, char **argv); #endif /* MPLAYER_PARSER_MPCMD_H */ diff --git a/player/main.c b/player/main.c index 1844746650..5183f56ea3 100644 --- a/player/main.c +++ b/player/main.c @@ -312,6 +312,11 @@ static int mpv_main(int argc, char *argv[]) struct MPOpts *opts = mpctx->opts; mpctx->global->opts = opts; + char *verbose_env = getenv("MPV_VERBOSE"); + if (verbose_env) + opts->verbose = atoi(verbose_env); + mp_msg_update_msglevels(mpctx->global); + init_libav(mpctx->global); GetCpuCaps(&gCpuCaps); screenshot_init(mpctx); @@ -319,7 +324,7 @@ static int mpv_main(int argc, char *argv[]) command_init(mpctx); // Preparse the command line - m_config_preparse_command_line(mpctx->mconfig, argc, argv); + m_config_preparse_command_line(mpctx->mconfig, mpctx->global, argc, argv); mp_msg_update_msglevels(mpctx->global); mp_print_version(mpctx->log, false); diff --git a/video/filter/vf_pullup.c b/video/filter/vf_pullup.c index b3bac3e301..b59cde67a1 100644 --- a/video/filter/vf_pullup.c +++ b/video/filter/vf_pullup.c @@ -58,7 +58,6 @@ static void reset(struct vf_instance *vf) struct pullup_context *c; vf->priv->ctx = c = pullup_alloc_context(); vf->priv->fakecount = 1; - c->verbose = verbose>0; c->junk_left = vf->priv->junk_left; c->junk_right = vf->priv->junk_right; c->junk_top = vf->priv->junk_top; -- cgit v1.2.3