Set log level via environment variable

If the BTOP_LOG_LEVEL is found in the environment it is parsed and
overwrites the current configured log level. The --debug switch still
has precedence

The value of BTOP_LOG_LEVEL must match one of `Logger::log_levels`
fields, being case insensitive
This commit is contained in:
nobounce 2023-09-18 18:37:04 +02:00 committed by Steffen Winter
parent 310d96443c
commit 3c92f2b912
No known key found for this signature in database
GPG key ID: D4053C3600EF3B1F
2 changed files with 24 additions and 4 deletions

View file

@ -1053,6 +1053,13 @@ optional arguments:
and screen draw functions and sets loglevel to DEBUG
```
It's also possible to specify a log level using the `BTOP_LOG_LEVEL` environment variable.
It takes the same values as the `log_level` configuation option + "DISABLED", being case insensitive.
```bash
BTOP_LOG_LEVEL=info btop
```
will launch btop with the info logging level.
## LICENSE
[Apache License 2.0](LICENSE)

View file

@ -880,19 +880,32 @@ int main(int argc, char **argv) {
}
//? Config init
{ vector<string> load_warnings;
{
vector<string> load_warnings;
Config::load(Config::conf_file, load_warnings);
if (Config::current_boxes.empty()) Config::check_boxes(Config::getS("shown_boxes"));
Config::set("lowcolor", (Global::arg_low_color ? true : not Config::getB("truecolor")));
std::string log_level_env {};
{
char* log_env = std::getenv("BTOP_LOG_LEVEL");
if (log_env != nullptr) {
log_level_env = Tools::str_to_upper(log_env);
}
}
if (Global::debug) {
Logger::set("DEBUG");
Logger::debug("Starting in DEBUG mode!");
}
else Logger::set(Config::getS("log_level"));
Logger::info("Logger set to {}", (Global::debug ? "DEBUG" : Config::getS("log_level")));
else if (v_contains(Logger::log_levels, log_level_env)) {
Logger::set(log_level_env);
Logger::info("Log level set to {}", log_level_env);
}
else {
Logger::set(Config::getS("log_level"));
Logger::info("Log level set to {}", Config::getS("log_level"));
}
for (const auto& err_str : load_warnings) Logger::warning("{}", err_str);
}