refactor and and support for init from argv

This commit is contained in:
gabime
2019-12-21 13:29:03 +02:00
parent 9f539d7028
commit 65ada37399
6 changed files with 114 additions and 243 deletions

View File

@@ -0,0 +1,47 @@
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
#pragma once
#include <spdlog/cfg/text_loader.h>
#include <spdlog/details/os.h>
//
// Init levels and patterns from env variables SPDLOG_LEVEL
// Inspired from Rust's "env_logger" crate (https://crates.io/crates/env_logger).
// Note - fallback to "info" level on unrecognized levels
//
// Examples:
//
// set global level to debug:
// export SPDLOG_LEVEL=debug
//
// turn off all logging except for logger1:
// export SPDLOG_LEVEL="off,logger1=debug"
//
// turn off all logging except for logger1 and logger2:
// export SPDLOG_LEVEL="off,logger1=debug,logger2=info"
namespace spdlog {
namespace cfg {
// search for SPDLOG_LEVEL= in the args and use it to init the levels
void init_from_argv(int args, char *argv[])
{
if (args < 2)
{
return;
}
const std::string spdlog_level_prefix = "SPDLOG_LEVEL=";
for (int i = 0; i < args; i++)
{
std::string arg = argv[i];
if (arg.find(spdlog_level_prefix) == 0)
{
auto cfg_string = arg.substr(spdlog_level_prefix.size());
auto levels = text_loader::load_levels(cfg_string);
spdlog::details::registry::instance().set_levels(levels);
return;
}
}
}
} // namespace cfg
} // namespace spdlog

View File

@@ -2,10 +2,8 @@
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
#pragma once
#include <spdlog/common.h>
#include <string>
#include <unordered_map>
#include <spdlog/cfg/text_loader.h>
#include <spdlog/details/os.h>
//
// Init levels and patterns from env variables SPDLOG_LEVEL
@@ -24,11 +22,12 @@
// export SPDLOG_LEVEL="off,logger1=debug,logger2=info"
namespace spdlog {
namespace env {
void load_levels();
} // namespace env
namespace cfg {
void init_from_env()
{
auto cfg = details::os::getenv("SPDLOG_LEVEL");
auto levels = text_loader::load_levels(cfg);
spdlog::details::registry::instance().set_levels(levels);
}
} // namespace cfg
} // namespace spdlog
#ifdef SPDLOG_HEADER_ONLY
#include "env-inl.h"
#endif // SPDLOG_HEADER_ONLY

View File

@@ -4,7 +4,7 @@
#pragma once
#ifndef SPDLOG_HEADER_ONLY
#include <spdlog/cfg/env.h>
#include <spdlog/cfg/text_loader.h>
#endif
#include <spdlog/spdlog.h>
@@ -16,7 +16,8 @@
#include <sstream>
namespace spdlog {
namespace env {
namespace cfg {
namespace text_loader {
using name_val_pair = std::pair<std::string, std::string>;
@@ -106,11 +107,11 @@ inline details::registry::logger_levels extract_levels_(const std::string &input
return rv;
}
SPDLOG_INLINE void load_levels()
SPDLOG_INLINE details::registry::logger_levels load_levels(const std::string &cfg)
{
auto levels = extract_levels_(details::os::getenv("SPDLOG_LEVEL"));
spdlog::details::registry::instance().set_levels(levels);
return extract_levels_(cfg);
}
} // namespace env
} // namespace text_loader
} // namespace cfg
} // namespace spdlog

View File

@@ -0,0 +1,37 @@
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
#pragma once
#include <spdlog/common.h>
#include <string>
#include <unordered_map>
//
// Init levels and patterns from env variables SPDLOG_LEVEL
// Inspired from Rust's "env_logger" crate (https://crates.io/crates/env_logger).
// Note - fallback to "info" level on unrecognized levels
//
// Examples:
//
// set global level to debug:
// export SPDLOG_LEVEL=debug
//
// turn off all logging except for logger1:
// export SPDLOG_LEVEL="off,logger1=debug"
//
// turn off all logging except for logger1 and logger2:
// export SPDLOG_LEVEL="off,logger1=debug,logger2=info"
namespace spdlog {
namespace cfg {
namespace text_loader {
details::registry::logger_levels load_levels(const std::string &cfg);
}
} // namespace cfg
} // namespace spdlog
#ifdef SPDLOG_HEADER_ONLY
#include "text_loader-inl.h"
#endif // SPDLOG_HEADER_ONLY