From: Philippe Proulx Date: Sat, 15 Jun 2019 01:09:51 +0000 (-0400) Subject: cli: use log level integer instead of letter in configuration X-Git-Url: https://git.efficios.com/?a=commitdiff_plain;h=83094759480c735f4361ce12e37204b4fb746270;p=babeltrace.git cli: use log level integer instead of letter in configuration This patch makes `struct bt_config` have an integer log level (to be set to one of the `BT_LOG_*` values) instead of a letter. The bt_log_get_letter_from_level() and bt_log_get_level_from_letter() can be used to translate from/to a log level letter/integer, so we use that. This is just cleaner and makes more sense than carrying a letter in the configuration which is just one encoding of a log level value. Signed-off-by: Philippe Proulx Change-Id: I5cbc3f2cb9ac868bc99e9458468a09c58e716884 Reviewed-on: https://review.lttng.org/c/babeltrace/+/1452 Tested-by: jenkins Reviewed-by: Francis Deslauriers --- diff --git a/src/cli/babeltrace2-cfg-cli-args.c b/src/cli/babeltrace2-cfg-cli-args.c index e1470d72..63cfbb43 100644 --- a/src/cli/babeltrace2-cfg-cli-args.c +++ b/src/cli/babeltrace2-cfg-cli-args.c @@ -3715,7 +3715,7 @@ static struct bt_config *bt_config_convert_from_args(int argc, const char *argv[], int *retcode, bool force_omit_system_plugin_path, bool force_omit_home_plugin_path, - const bt_value *initial_plugin_paths, char *log_level) + const bt_value *initial_plugin_paths, int *log_level) { poptContext pc = NULL; char *arg = NULL; @@ -4371,12 +4371,13 @@ struct bt_config *bt_config_convert_from_args(int argc, const char *argv[], stream_intersection_mode = true; break; case OPT_VERBOSE: - if (*log_level != 'V' && *log_level != 'D') { - *log_level = 'I'; + if (*log_level != BT_LOG_VERBOSE && + *log_level != BT_LOG_DEBUG) { + *log_level = BT_LOG_INFO; } break; case OPT_DEBUG: - *log_level = 'V'; + *log_level = BT_LOG_VERBOSE; break; } @@ -4394,9 +4395,10 @@ struct bt_config *bt_config_convert_from_args(int argc, const char *argv[], /* * Legacy behaviour: --verbose used to make the `text` output * format print more information. --verbose is now equivalent to - * the INFO log level, which is why we compare to 'I' here. + * the INFO log level, which is why we compare to `BT_LOG_INFO` + * here. */ - if (*log_level == 'I') { + if (*log_level == BT_LOG_INFO) { append_implicit_component_param(&implicit_text_args, "verbose", "yes"); } @@ -4878,38 +4880,6 @@ void print_gen_usage(FILE *fp) fprintf(fp, "Use `babeltrace2 COMMAND --help` to show the help of COMMAND.\n"); } -static -char log_level_from_arg(const char *arg) -{ - char level = 'U'; - - if (strcmp(arg, "VERBOSE") == 0 || - strcmp(arg, "V") == 0) { - level = 'V'; - } else if (strcmp(arg, "DEBUG") == 0 || - strcmp(arg, "D") == 0) { - level = 'D'; - } else if (strcmp(arg, "INFO") == 0 || - strcmp(arg, "I") == 0) { - level = 'I'; - } else if (strcmp(arg, "WARN") == 0 || - strcmp(arg, "WARNING") == 0 || - strcmp(arg, "W") == 0) { - level = 'W'; - } else if (strcmp(arg, "ERROR") == 0 || - strcmp(arg, "E") == 0) { - level = 'E'; - } else if (strcmp(arg, "FATAL") == 0 || - strcmp(arg, "F") == 0) { - level = 'F'; - } else if (strcmp(arg, "NONE") == 0 || - strcmp(arg, "N") == 0) { - level = 'N'; - } - - return level; -} - struct bt_config *bt_config_cli_args_create(int argc, const char *argv[], int *retcode, bool force_omit_system_plugin_path, bool force_omit_home_plugin_path, @@ -4920,7 +4890,7 @@ struct bt_config *bt_config_cli_args_create(int argc, const char *argv[], const char **command_argv = NULL; int command_argc = -1; const char *command_name = NULL; - char log_level = 'U'; + int log_level = -1; enum command_type { COMMAND_TYPE_NONE = -1, @@ -4956,10 +4926,11 @@ struct bt_config *bt_config_cli_args_create(int argc, const char *argv[], if (strcmp(cur_arg, "-d") == 0 || strcmp(cur_arg, "--debug") == 0) { - log_level = 'V'; + log_level = BT_LOG_VERBOSE; } else if (strcmp(cur_arg, "-v") == 0 || strcmp(cur_arg, "--verbose") == 0) { - if (log_level != 'V' && log_level != 'D') { + if (log_level != BT_LOG_VERBOSE && + log_level != BT_LOG_DEBUG) { /* * Legacy: do not override a previous * --debug because --verbose and --debug @@ -4967,7 +4938,7 @@ struct bt_config *bt_config_cli_args_create(int argc, const char *argv[], * case we want the lowest log level to * apply, VERBOSE). */ - log_level = 'I'; + log_level = BT_LOG_INFO; } } else if (strcmp(cur_arg, "--log-level") == 0 || strcmp(cur_arg, "-l") == 0) { @@ -4977,8 +4948,8 @@ struct bt_config *bt_config_cli_args_create(int argc, const char *argv[], goto end; } - log_level = log_level_from_arg(next_arg); - if (log_level == 'U') { + log_level = bt_log_get_level_from_string(next_arg); + if (log_level < 0) { printf_err("Invalid argument for --log-level option:\n %s\n", next_arg); *retcode = 1; @@ -4989,8 +4960,8 @@ struct bt_config *bt_config_cli_args_create(int argc, const char *argv[], } else if (strncmp(cur_arg, "--log-level=", 12) == 0) { const char *arg = &cur_arg[12]; - log_level = log_level_from_arg(arg); - if (log_level == 'U') { + log_level = bt_log_get_level_from_string(arg); + if (log_level < 0) { printf_err("Invalid argument for --log-level option:\n %s\n", arg); *retcode = 1; @@ -4999,8 +4970,8 @@ struct bt_config *bt_config_cli_args_create(int argc, const char *argv[], } else if (strncmp(cur_arg, "-l", 2) == 0) { const char *arg = &cur_arg[2]; - log_level = log_level_from_arg(arg); - if (log_level == 'U') { + log_level = bt_log_get_level_from_string(arg); + if (log_level < 0) { printf_err("Invalid argument for --log-level option:\n %s\n", arg); *retcode = 1; @@ -5093,8 +5064,9 @@ struct bt_config *bt_config_cli_args_create(int argc, const char *argv[], } if (config) { - if (log_level == 'U') { - log_level = 'W'; + if (log_level < 0) { + /* Default log level */ + log_level = BT_LOG_WARN; } config->log_level = log_level; diff --git a/src/cli/babeltrace2-cfg.h b/src/cli/babeltrace2-cfg.h index a5cc7fcf..41ec3675 100644 --- a/src/cli/babeltrace2-cfg.h +++ b/src/cli/babeltrace2-cfg.h @@ -69,7 +69,7 @@ struct bt_config { bool omit_home_plugin_path; bool command_needs_plugins; const char *command_name; - char log_level; + int log_level; enum bt_config_command command; union { /* BT_CONFIG_COMMAND_RUN */ diff --git a/src/cli/babeltrace2.c b/src/cli/babeltrace2.c index b78eebe0..346938a9 100644 --- a/src/cli/babeltrace2.c +++ b/src/cli/babeltrace2.c @@ -2753,10 +2753,10 @@ void set_auto_log_levels(struct bt_config *cfg) */ if (getenv("BABELTRACE_DEBUG") && strcmp(getenv("BABELTRACE_DEBUG"), "1") == 0) { - cfg->log_level = 'V'; + cfg->log_level = BT_LOG_VERBOSE; } else if (getenv("BABELTRACE_VERBOSE") && strcmp(getenv("BABELTRACE_VERBOSE"), "1") == 0) { - cfg->log_level = 'I'; + cfg->log_level = BT_LOG_INFO; } /* @@ -2771,39 +2771,15 @@ void set_auto_log_levels(struct bt_config *cfg) */ if (!getenv("BABELTRACE_LOGGING_GLOBAL_LEVEL")) { if (cfg->verbose) { - bt_logging_set_global_level(BT_LOGGING_LEVEL_INFO); + bt_logging_set_global_level(BT_LOG_INFO); } else if (cfg->debug) { - bt_logging_set_global_level(BT_LOGGING_LEVEL_VERBOSE); + bt_logging_set_global_level(BT_LOG_VERBOSE); } else { /* * Set library's default log level if not * explicitly specified. */ - switch (cfg->log_level) { - case 'N': - bt_logging_set_global_level(BT_LOGGING_LEVEL_NONE); - break; - case 'V': - bt_logging_set_global_level(BT_LOGGING_LEVEL_VERBOSE); - break; - case 'D': - bt_logging_set_global_level(BT_LOGGING_LEVEL_DEBUG); - break; - case 'I': - bt_logging_set_global_level(BT_LOGGING_LEVEL_INFO); - break; - case 'W': - bt_logging_set_global_level(BT_LOGGING_LEVEL_WARN); - break; - case 'E': - bt_logging_set_global_level(BT_LOGGING_LEVEL_ERROR); - break; - case 'F': - bt_logging_set_global_level(BT_LOGGING_LEVEL_FATAL); - break; - default: - abort(); - } + bt_logging_set_global_level(cfg->log_level); } } @@ -2817,31 +2793,7 @@ void set_auto_log_levels(struct bt_config *cfg) * Set CLI's default log level if not explicitly * specified. */ - switch (cfg->log_level) { - case 'N': - bt_cli_log_level = BT_LOG_NONE; - break; - case 'V': - bt_cli_log_level = BT_LOG_VERBOSE; - break; - case 'D': - bt_cli_log_level = BT_LOG_DEBUG; - break; - case 'I': - bt_cli_log_level = BT_LOG_INFO; - break; - case 'W': - bt_cli_log_level = BT_LOG_WARN; - break; - case 'E': - bt_cli_log_level = BT_LOG_ERROR; - break; - case 'F': - bt_cli_log_level = BT_LOG_FATAL; - break; - default: - abort(); - } + bt_cli_log_level = cfg->log_level; } } @@ -2860,7 +2812,8 @@ void set_auto_log_levels(struct bt_config *cfg) * Set module's default log level if not * explicitly specified. */ - val[0] = cfg->log_level; + val[0] = bt_log_get_letter_from_level( + cfg->log_level); g_setenv(*env_var_name, val, 1); } }