spdlog/include/spdlog/cfg/env.h

37 lines
1022 B
C
Raw Normal View History

2019-12-08 23:07:52 +08:00
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
#pragma once
2019-12-23 02:40:19 +08:00
#include <spdlog/cfg/helpers.h>
2019-12-21 23:47:02 +08:00
#include <spdlog/details/registry.h>
#include <spdlog/details/os.h>
2019-12-08 23:07:52 +08:00
2019-12-11 07:24:51 +08:00
//
// Init levels and patterns from env variables SPDLOG_LEVEL
2019-12-11 07:24:51 +08:00
// Inspired from Rust's "env_logger" crate (https://crates.io/crates/env_logger).
// Note - fallback to "info" level on unrecognized levels
2019-12-11 07:24:51 +08:00
//
2019-12-08 23:08:20 +08:00
// Examples:
2019-12-11 07:24:51 +08:00
//
// set global level to debug:
2019-12-08 23:08:20 +08:00
// export SPDLOG_LEVEL=debug
//
2019-12-11 07:24:51 +08:00
// turn off all logging except for logger1:
// export SPDLOG_LEVEL="off,logger1=debug"
//
2019-12-22 00:08:42 +08:00
2019-12-11 07:24:51 +08:00
// turn off all logging except for logger1 and logger2:
// export SPDLOG_LEVEL="off,logger1=debug,logger2=info"
namespace spdlog {
2019-12-23 02:40:19 +08:00
namespace cfg {
2020-03-06 21:09:46 +08:00
void load_env_levels()
{
2019-12-21 23:47:02 +08:00
auto env_val = details::os::getenv("SPDLOG_LEVEL");
auto levels = helpers::extract_levels(env_val);
details::registry::instance().update_levels(std::move(levels));
}
2019-12-21 23:47:02 +08:00
2019-12-23 02:40:19 +08:00
} // namespace cfg
2019-12-08 23:08:20 +08:00
} // namespace spdlog