From: Philippe Proulx Date: Fri, 9 Jun 2017 21:48:27 +0000 (-0400) Subject: cli: use WARN as the default log level when not explicitly set by user X-Git-Tag: v2.0.0-pre1~5 X-Git-Url: https://git.efficios.com/?a=commitdiff_plain;h=c6d4d1ae9c00b7cabf44777a1ec5e4da8a68000b;p=babeltrace.git cli: use WARN as the default log level when not explicitly set by user For all known log levels (library, CLI, plugins, etc.), when the dedicated log level environment variable is unset, make the CLI set it to WARN. This means that you can always override the default, -v, or -d log level with a specific environment variable. For example, if you need VERBOSE logging for all the loggers but the library's: BABELTRACE_LOGGING_GLOBAL_LEVEL=N babeltrace -d ... Using WARN as the default log level will help spot bugs quickly without a specific option to use this log level. Signed-off-by: Philippe Proulx Signed-off-by: Jérémie Galarneau --- diff --git a/cli/babeltrace.c b/cli/babeltrace.c index f91bad77..d7eeec61 100644 --- a/cli/babeltrace.c +++ b/cli/babeltrace.c @@ -58,6 +58,7 @@ #include "babeltrace-cfg-cli-args-default.h" #define ENV_BABELTRACE_WARN_COMMAND_NAME_DIRECTORY_CLASH "BABELTRACE_CLI_WARN_COMMAND_NAME_DIRECTORY_CLASH" +#define ENV_BABELTRACE_CLI_LOG_LEVEL "BABELTRACE_CLI_LOG_LEVEL" /* * Known environment variable names for the log levels of the project's @@ -1909,9 +1910,77 @@ void warn_command_name_and_directory_clash(struct bt_config *cfg) static void init_log_level(void) { - bt_cli_log_level = bt_log_get_level_from_env("BABELTRACE_CLI_LOG_LEVEL"); + bt_cli_log_level = bt_log_get_level_from_env(ENV_BABELTRACE_CLI_LOG_LEVEL); } +static +void set_auto_log_levels(struct bt_config *cfg) +{ + const char **env_var_name; + + /* + * Set log levels according to --debug or --verbose. For + * backward compatibility, --debug is more verbose than + * --verbose. So: + * + * --verbose: INFO log level + * --debug: VERBOSE log level (includes DEBUG, which is + * is less verbose than VERBOSE in the internal + * logging framework) + */ + if (!getenv("BABELTRACE_LOGGING_GLOBAL_LEVEL")) { + if (cfg->verbose) { + bt_logging_set_global_level(BT_LOGGING_LEVEL_INFO); + } else if (cfg->debug) { + bt_logging_set_global_level(BT_LOGGING_LEVEL_VERBOSE); + } else { + /* + * Set library's default log level if not + * explicitly specified. + */ + bt_logging_set_global_level(BT_LOGGING_LEVEL_WARN); + } + } + + if (!getenv(ENV_BABELTRACE_CLI_LOG_LEVEL)) { + if (cfg->verbose) { + bt_cli_log_level = BT_LOG_INFO; + } else if (cfg->debug) { + bt_cli_log_level = BT_LOG_VERBOSE; + } else { + /* + * Set CLI's default log level if not explicitly + * specified. + */ + bt_cli_log_level = BT_LOG_WARN; + } + } + + env_var_name = log_level_env_var_names; + + while (*env_var_name) { + if (!getenv(*env_var_name)) { + if (cfg->verbose) { + setenv(*env_var_name, "I", 1); + } else if (cfg->debug) { + setenv(*env_var_name, "V", 1); + } else { + /* + * Set module's default log level if not + * explicitly specified. + */ + setenv(*env_var_name, "W", 1); + } + } + + env_var_name++; + } + + babeltrace_debug = cfg->debug; + babeltrace_verbose = cfg->verbose; +} + +static void set_sigint_handler(void) { struct sigaction new_action, old_action; @@ -1931,7 +2000,6 @@ int main(int argc, const char **argv) int ret; int retcode; struct bt_config *cfg; - const char **env_var_name; init_log_level(); set_sigint_handler(); @@ -1957,38 +2025,7 @@ int main(int argc, const char **argv) goto end; } - /* - * Set log levels according to --debug or --verbose. For - * backward compatibility, --debug is more verbose than - * --verbose. So: - * - * --verbose: INFO log level - * --debug: VERBOSE log level (includes DEBUG, which is - * is less verbose than VERBOSE in the internal - * logging framework) - */ - if (cfg->verbose) { - bt_cli_log_level = BT_LOGGING_LEVEL_INFO; - bt_logging_set_global_level(BT_LOGGING_LEVEL_INFO); - } else if (cfg->debug) { - bt_cli_log_level = BT_LOGGING_LEVEL_VERBOSE; - bt_logging_set_global_level(BT_LOGGING_LEVEL_VERBOSE); - } - - env_var_name = log_level_env_var_names; - - while (*env_var_name) { - if (cfg->verbose) { - setenv(*env_var_name, "I", 1); - } else if (cfg->debug) { - setenv(*env_var_name, "V", 1); - } - - env_var_name++; - } - - babeltrace_debug = cfg->debug; - babeltrace_verbose = cfg->verbose; + set_auto_log_levels(cfg); print_cfg(cfg); if (cfg->command_needs_plugins) {