2 * Copyright 2019 Philippe Proulx <pproulx@efficios.com>
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 #define BT_LOG_TAG "CLI"
26 #include "babeltrace2-log-level.h"
30 #include "common/assert.h"
33 * Known environment variable names for the log levels of the project's
36 static const char* log_level_env_var_names
[] = {
37 "BABELTRACE_PLUGIN_CTF_METADATA_LOG_LEVEL",
38 "BABELTRACE_PYTHON_BT2_LOG_LEVEL",
42 static const int babeltrace2_default_log_level
= BT_LOG_WARNING
;
44 void set_auto_log_levels(int *logging_level
)
46 const char **env_var_name
;
48 /* Setting this is equivalent to passing --debug. */
49 if (getenv("BABELTRACE_DEBUG") &&
50 strcmp(getenv("BABELTRACE_DEBUG"), "1") == 0) {
51 *logging_level
= logging_level_min(*logging_level
, BT_LOG_TRACE
);
54 /* Setting this is equivalent to passing --verbose. */
55 if (getenv("BABELTRACE_VERBOSE") &&
56 strcmp(getenv("BABELTRACE_VERBOSE"), "1") == 0) {
57 *logging_level
= logging_level_min(*logging_level
, BT_LOG_INFO
);
61 * logging_level is BT_LOG_NONE at this point if no log level was
62 * specified at all by the user.
64 if (*logging_level
== -1) {
65 *logging_level
= babeltrace2_default_log_level
;
69 * If the user hasn't requested a specific log level for the lib
70 * (through LIBBABELTRACE2_INIT_LOG_LEVEL), set it.
72 if (!getenv("LIBBABELTRACE2_INIT_LOG_LEVEL")) {
73 bt_logging_set_global_level(*logging_level
);
77 * If the user hasn't requested a specific log level for the CLI,
78 * (through BABELTRACE_CLI_LOG_LEVEL), set it.
80 if (!getenv(ENV_BABELTRACE_CLI_LOG_LEVEL
)) {
81 bt_cli_log_level
= *logging_level
;
84 for (env_var_name
= log_level_env_var_names
; *env_var_name
; env_var_name
++) {
85 if (!getenv(*env_var_name
)) {
89 * Set module's default log level if not
90 * explicitly specified.
92 val
[0] = bt_log_get_letter_from_level(*logging_level
);
93 g_setenv(*env_var_name
, val
, 1);