Commit | Line | Data |
---|---|---|
ebddaeb8 | 1 | /* |
0235b0db | 2 | * SPDX-License-Identifier: MIT |
ebddaeb8 | 3 | * |
0235b0db | 4 | * Copyright 2019 Philippe Proulx <pproulx@efficios.com> |
ebddaeb8 SM |
5 | */ |
6 | ||
7 | #define BT_LOG_TAG "CLI" | |
8 | #include "logging.h" | |
9 | ||
10 | #include "babeltrace2-log-level.h" | |
11 | ||
12 | #include <stdlib.h> | |
13 | ||
14 | #include "common/assert.h" | |
15 | ||
16 | /* | |
17 | * Known environment variable names for the log levels of the project's | |
18 | * modules. | |
19 | */ | |
20 | static const char* log_level_env_var_names[] = { | |
21 | "BABELTRACE_PLUGIN_CTF_METADATA_LOG_LEVEL", | |
22 | "BABELTRACE_PYTHON_BT2_LOG_LEVEL", | |
23 | NULL, | |
24 | }; | |
25 | ||
26 | static const int babeltrace2_default_log_level = BT_LOG_WARNING; | |
27 | ||
28 | void set_auto_log_levels(int *logging_level) | |
29 | { | |
30 | const char **env_var_name; | |
31 | ||
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); | |
36 | } | |
37 | ||
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); | |
42 | } | |
43 | ||
44 | /* | |
45 | * logging_level is BT_LOG_NONE at this point if no log level was | |
46 | * specified at all by the user. | |
47 | */ | |
48 | if (*logging_level == -1) { | |
49 | *logging_level = babeltrace2_default_log_level; | |
50 | } | |
51 | ||
52 | /* | |
53 | * If the user hasn't requested a specific log level for the lib | |
54 | * (through LIBBABELTRACE2_INIT_LOG_LEVEL), set it. | |
55 | */ | |
56 | if (!getenv("LIBBABELTRACE2_INIT_LOG_LEVEL")) { | |
57 | bt_logging_set_global_level(*logging_level); | |
58 | } | |
59 | ||
60 | /* | |
61 | * If the user hasn't requested a specific log level for the CLI, | |
62 | * (through BABELTRACE_CLI_LOG_LEVEL), set it. | |
63 | */ | |
64 | if (!getenv(ENV_BABELTRACE_CLI_LOG_LEVEL)) { | |
65 | bt_cli_log_level = *logging_level; | |
66 | } | |
67 | ||
68 | for (env_var_name = log_level_env_var_names; *env_var_name; env_var_name++) { | |
69 | if (!getenv(*env_var_name)) { | |
70 | char val[2] = { 0 }; | |
71 | ||
72 | /* | |
73 | * Set module's default log level if not | |
74 | * explicitly specified. | |
75 | */ | |
76 | val[0] = bt_log_get_letter_from_level(*logging_level); | |
77 | g_setenv(*env_var_name, val, 1); | |
78 | } | |
79 | } | |
80 | } |