Commit | Line | Data |
---|---|---|
ebddaeb8 SM |
1 | /* |
2 | * Copyright 2019 Philippe Proulx <pproulx@efficios.com> | |
3 | * | |
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: | |
10 | * | |
11 | * The above copyright notice and this permission notice shall be included in | |
12 | * all copies or substantial portions of the Software. | |
13 | * | |
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 | |
20 | * SOFTWARE. | |
21 | */ | |
22 | ||
23 | #define BT_LOG_TAG "CLI" | |
24 | #include "logging.h" | |
25 | ||
26 | #include "babeltrace2-log-level.h" | |
27 | ||
28 | #include <stdlib.h> | |
29 | ||
30 | #include "common/assert.h" | |
31 | ||
32 | /* | |
33 | * Known environment variable names for the log levels of the project's | |
34 | * modules. | |
35 | */ | |
36 | static const char* log_level_env_var_names[] = { | |
37 | "BABELTRACE_PLUGIN_CTF_METADATA_LOG_LEVEL", | |
38 | "BABELTRACE_PYTHON_BT2_LOG_LEVEL", | |
39 | NULL, | |
40 | }; | |
41 | ||
42 | static const int babeltrace2_default_log_level = BT_LOG_WARNING; | |
43 | ||
44 | void set_auto_log_levels(int *logging_level) | |
45 | { | |
46 | const char **env_var_name; | |
47 | ||
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); | |
52 | } | |
53 | ||
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); | |
58 | } | |
59 | ||
60 | /* | |
61 | * logging_level is BT_LOG_NONE at this point if no log level was | |
62 | * specified at all by the user. | |
63 | */ | |
64 | if (*logging_level == -1) { | |
65 | *logging_level = babeltrace2_default_log_level; | |
66 | } | |
67 | ||
68 | /* | |
69 | * If the user hasn't requested a specific log level for the lib | |
70 | * (through LIBBABELTRACE2_INIT_LOG_LEVEL), set it. | |
71 | */ | |
72 | if (!getenv("LIBBABELTRACE2_INIT_LOG_LEVEL")) { | |
73 | bt_logging_set_global_level(*logging_level); | |
74 | } | |
75 | ||
76 | /* | |
77 | * If the user hasn't requested a specific log level for the CLI, | |
78 | * (through BABELTRACE_CLI_LOG_LEVEL), set it. | |
79 | */ | |
80 | if (!getenv(ENV_BABELTRACE_CLI_LOG_LEVEL)) { | |
81 | bt_cli_log_level = *logging_level; | |
82 | } | |
83 | ||
84 | for (env_var_name = log_level_env_var_names; *env_var_name; env_var_name++) { | |
85 | if (!getenv(*env_var_name)) { | |
86 | char val[2] = { 0 }; | |
87 | ||
88 | /* | |
89 | * Set module's default log level if not | |
90 | * explicitly specified. | |
91 | */ | |
92 | val[0] = bt_log_get_letter_from_level(*logging_level); | |
93 | g_setenv(*env_var_name, val, 1); | |
94 | } | |
95 | } | |
96 | } |