From ffe894ec0a73ab6a16ce1ca62800bf1612542107 Mon Sep 17 00:00:00 2001 From: wm4 Date: Fri, 6 Feb 2015 16:48:52 +0100 Subject: options: change --msg-level option Make it accept "," as separator, instead of only ":". Do this by using the key-value-list parser. Before this, the option was stored as a string, with the option parser verifying that the option value as correct. Now it's stored pre-parsed, although the log levels still require separate verification and parsing-on-use to some degree (which is why the msg-level option type doesn't go away). Because the internal type changes, the client API "native" type also changes. This could be prevented with some more effort, but I don't think it's worth it - if MPV_FORMAT_STRING is used, it still works the same, just with a different separator on read accesses. --- options/m_option.c | 123 +++++++++++++++++++++++++++++------------------------ options/options.c | 2 +- options/options.h | 2 +- 3 files changed, 69 insertions(+), 58 deletions(-) (limited to 'options') diff --git a/options/m_option.c b/options/m_option.c index 74d6509111..ca90c52624 100644 --- a/options/m_option.c +++ b/options/m_option.c @@ -1495,6 +1495,73 @@ const m_option_type_t m_option_type_keyvalue_list = { .set = keyvalue_list_set, }; + +#undef VAL +#define VAL(x) (*(char **)(x)) + +static int check_msg_levels(struct mp_log *log, char **list) +{ + for (int n = 0; list && list[n * 2 + 0]; n++) { + char *level = list[n * 2 + 1]; + if (mp_msg_find_level(level) < 0 && strcmp(level, "no") != 0) { + mp_err(log, "Invalid message level '%s'\n", level); + return M_OPT_INVALID; + } + } + return 1; +} + +static int parse_msglevels(struct mp_log *log, const m_option_t *opt, + struct bstr name, struct bstr param, void *dst) +{ + if (bstr_equals0(param, "help")) { + mp_info(log, "Syntax: --msglevel=module1=level:module2=level:...\n" + "'module' is output prefix as shown with -v, or a prefix\n" + "of it. level is one of:\n\n" + " fatal error warn info status v debug trace\n\n" + "The level specifies the minimum log level a message\n" + "must have to be printed.\n" + "The special module name 'all' affects all modules.\n"); + return M_OPT_EXIT; + } + + char **dst_copy = NULL; + int r = m_option_type_keyvalue_list.parse(log, opt, name, param, &dst_copy); + if (r >= 0) + r = check_msg_levels(log, dst_copy); + + if (r >= 0) + m_option_type_keyvalue_list.copy(opt, dst, &dst_copy); + m_option_type_keyvalue_list.free(&dst_copy); + return r; +} + +static int set_msglevels(const m_option_t *opt, void *dst, + struct mpv_node *src) +{ + char **dst_copy = NULL; + int r = m_option_type_keyvalue_list.set(opt, &dst_copy, src); + if (r >= 0) + r = check_msg_levels(mp_null_log, dst_copy); + + if (r >= 0) + m_option_type_keyvalue_list.copy(opt, dst, &dst_copy); + m_option_type_keyvalue_list.free(&dst_copy); + return r; +} + +const m_option_type_t m_option_type_msglevels = { + .name = "Output verbosity levels", + .size = sizeof(char **), + .flags = M_OPT_TYPE_DYNAMIC, + .parse = parse_msglevels, + .print = print_keyvalue_list, + .copy = copy_str_list, + .free = free_str_list, + .get = keyvalue_list_get, + .set = set_msglevels, +}; + /////////////////// Print static int parse_print(struct mp_log *log, const m_option_t *opt, @@ -1630,62 +1697,6 @@ const m_option_type_t m_option_type_subconfig = { .parse = parse_subconf, }; -#undef VAL -#define VAL(x) (*(char **)(x)) - -static int parse_msglevels(struct mp_log *log, const m_option_t *opt, - struct bstr name, struct bstr param, void *dst) -{ - if (param.start == NULL) - return M_OPT_MISSING_PARAM; - - if (bstr_equals0(param, "help")) { - mp_info(log, "Syntax: --msglevel=module1=level:module2=level:...\n" - "'module' is output prefix as shown with -v, or a prefix\n" - "of it. level is one of:\n\n" - " fatal error warn info status v debug trace\n\n" - "The level specifies the minimum log level a message\n" - "must have to be printed.\n" - "The special module name 'all' affects all modules.\n"); - return M_OPT_EXIT; - } - - bstr s = param; - while (1) { - int res = mp_msg_split_msglevel(&s, &(bstr){0}, &(int){0}); - if (res == 0) - break; - if (res < 0) { - mp_err(log, "Invalid syntax: %.*s\n", BSTR_P(s)); - return M_OPT_INVALID; - } - } - - if (dst && param.len) { - char *prev = VAL(dst); - char *new; - if (prev && prev[0]) { - new = talloc_asprintf(NULL, "%s:%.*s", prev, BSTR_P(param)); - } else { - new = bstrdup0(NULL, param); - } - talloc_free(prev); - VAL(dst) = new; - } - - return 1; -} - -const m_option_type_t m_option_type_msglevels = { - .name = "Output verbosity levels", - .size = sizeof(char *), - .flags = M_OPT_TYPE_DYNAMIC, - .parse = parse_msglevels, - .print = print_str, - .copy = copy_str, - .free = free_str, -}; - #undef VAL // Split the string on the given split character. diff --git a/options/options.c b/options/options.c index acc2c99b9e..a7f6ab193f 100644 --- a/options/options.c +++ b/options/options.c @@ -105,7 +105,7 @@ const m_option_t mp_opts[] = { OPT_FLAG("quiet", quiet, CONF_GLOBAL), OPT_FLAG_STORE("really-quiet", verbose, CONF_GLOBAL | CONF_PRE_PARSE, -10), OPT_FLAG("terminal", use_terminal, CONF_GLOBAL | CONF_PRE_PARSE), - OPT_GENERAL(char*, "msg-level", msglevels, CONF_GLOBAL|CONF_PRE_PARSE, + OPT_GENERAL(char**, "msg-level", msg_levels, CONF_GLOBAL|CONF_PRE_PARSE, .type = &m_option_type_msglevels), OPT_STRING("dump-stats", dump_stats, CONF_GLOBAL | CONF_PRE_PARSE), OPT_FLAG("msg-color", msg_color, CONF_GLOBAL | CONF_PRE_PARSE), diff --git a/options/options.h b/options/options.h index 7d110faf2a..6c74fd3ed7 100644 --- a/options/options.h +++ b/options/options.h @@ -54,9 +54,9 @@ struct mp_cache_opts { typedef struct MPOpts { int use_terminal; - char *msglevels; char *dump_stats; int verbose; + char **msg_levels; int msg_color; int msg_module; int msg_time; -- cgit v1.2.3