2 * SPDX-License-Identifier: MIT
4 * Copyright 2019 Philippe Proulx <pproulx@efficios.com>
7 #define BT_LOG_TAG "CLI"
10 #include "babeltrace2-log-level.h"
14 #include "common/assert.h"
17 * Known environment variable names for the log levels of the project's
20 static const char* log_level_env_var_names
[] = {
21 "BABELTRACE_PLUGIN_CTF_METADATA_LOG_LEVEL",
22 "BABELTRACE_PYTHON_BT2_LOG_LEVEL",
26 static const int babeltrace2_default_log_level
= BT_LOG_WARNING
;
28 void set_auto_log_levels(int *logging_level
)
30 const char **env_var_name
;
32 /* Setting this is equivalent to passing --debug. */
33 if (getenv("BABELTRACE_DEBUG") &&
34 strcmp(getenv("BABELTRACE_DEBUG"), "1") == 0) {
35 *logging_level
= logging_level_min(*logging_level
, BT_LOG_TRACE
);
38 /* Setting this is equivalent to passing --verbose. */
39 if (getenv("BABELTRACE_VERBOSE") &&
40 strcmp(getenv("BABELTRACE_VERBOSE"), "1") == 0) {
41 *logging_level
= logging_level_min(*logging_level
, BT_LOG_INFO
);
45 * logging_level is BT_LOG_NONE at this point if no log level was
46 * specified at all by the user.
48 if (*logging_level
== -1) {
49 *logging_level
= babeltrace2_default_log_level
;
53 * If the user hasn't requested a specific log level for the lib
54 * (through LIBBABELTRACE2_INIT_LOG_LEVEL), set it.
56 if (!getenv("LIBBABELTRACE2_INIT_LOG_LEVEL")) {
57 bt_logging_set_global_level(*logging_level
);
61 * If the user hasn't requested a specific log level for the CLI,
62 * (through BABELTRACE_CLI_LOG_LEVEL), set it.
64 if (!getenv(ENV_BABELTRACE_CLI_LOG_LEVEL
)) {
65 bt_cli_log_level
= *logging_level
;
68 for (env_var_name
= log_level_env_var_names
; *env_var_name
; env_var_name
++) {
69 if (!getenv(*env_var_name
)) {
73 * Set module's default log level if not
74 * explicitly specified.
76 val
[0] = bt_log_get_letter_from_level(*logging_level
);
77 g_setenv(*env_var_name
, val
, 1);