refectored file names

This commit is contained in:
gabime 2019-12-21 17:47:02 +02:00
parent fc3d18ed64
commit 773b8c5a54
10 changed files with 57 additions and 66 deletions

View File

@ -21,12 +21,14 @@ void syslog_example();
#include <spdlog/spdlog.h> #include <spdlog/spdlog.h>
#include <spdlog/sinks/stdout_color_sinks.h> #include <spdlog/sinks/stdout_color_sinks.h>
#include <spdlog/cfg/env.h> #include <spdlog/cfg/env.h>
#include <spdlog/cfg/argv_loader.h> #include <spdlog/cfg/argv.h>
int main(int args, char *argv[]) int main(int args, char *argv[])
{ {
// spdlog::cfg::init_from_env();
spdlog::cfg::init_from_argv(args, argv);
//spdlog::cfg::env::load_levels();
spdlog::cfg::argv::load_levels(args, argv);
spdlog::info("HELLO INFO"); spdlog::info("HELLO INFO");
auto l1 = spdlog::stderr_color_st("l1"); auto l1 = spdlog::stderr_color_st("l1");

View File

@ -2,7 +2,7 @@
// Distributed under the MIT License (http://opensource.org/licenses/MIT) // Distributed under the MIT License (http://opensource.org/licenses/MIT)
#pragma once #pragma once
#include <spdlog/cfg/text_loader.h> #include <spdlog/cfg/helpers.h>
#include <spdlog/details/os.h> #include <spdlog/details/os.h>
// //
@ -11,8 +11,9 @@
namespace spdlog { namespace spdlog {
namespace cfg { namespace cfg {
namespace argv {
// search for SPDLOG_LEVEL= in the args and use it to init the levels // search for SPDLOG_LEVEL= in the args and use it to init the levels
void init_from_argv(int args, char *argv[]) void load_levels(int args, char *argv[])
{ {
const std::string spdlog_level_prefix = "SPDLOG_LEVEL="; const std::string spdlog_level_prefix = "SPDLOG_LEVEL=";
for (int i = 1; i < args; i++) for (int i = 1; i < args; i++)
@ -20,11 +21,12 @@ void init_from_argv(int args, char *argv[])
std::string arg = argv[i]; std::string arg = argv[i];
if (arg.find(spdlog_level_prefix) == 0) if (arg.find(spdlog_level_prefix) == 0)
{ {
auto cfg_string = arg.substr(spdlog_level_prefix.size()); auto levels_string = arg.substr(spdlog_level_prefix.size());
auto levels = text_loader::load_levels(cfg_string); auto levels = helpers::extract_levels(levels_string);
spdlog::details::registry::instance().set_levels(levels); details::registry::instance().update_levels(std::move(levels));
} }
} }
} }
} // namespace argv
} // namespace cfg } // namespace cfg
} // namespace spdlog } // namespace spdlog

View File

@ -2,7 +2,8 @@
// Distributed under the MIT License (http://opensource.org/licenses/MIT) // Distributed under the MIT License (http://opensource.org/licenses/MIT)
#pragma once #pragma once
#include <spdlog/cfg/text_loader.h> #include <spdlog/cfg/helpers.h>
#include <spdlog/details/registry.h>
#include <spdlog/details/os.h> #include <spdlog/details/os.h>
// //
@ -23,11 +24,14 @@
namespace spdlog { namespace spdlog {
namespace cfg { namespace cfg {
void init_from_env() namespace env {
void load_levels()
{ {
auto cfg = details::os::getenv("SPDLOG_LEVEL"); auto env_val = details::os::getenv("SPDLOG_LEVEL");
auto levels = text_loader::load_levels(cfg); auto levels = helpers::extract_levels(env_val);
spdlog::details::registry::instance().set_levels(levels); details::registry::instance().update_levels(std::move(levels));
} }
} // namespace env
} // namespace cfg } // namespace cfg
} // namespace spdlog } // namespace spdlog

View File

@ -4,7 +4,7 @@
#pragma once #pragma once
#ifndef SPDLOG_HEADER_ONLY #ifndef SPDLOG_HEADER_ONLY
#include <spdlog/cfg/text_loader.h> #include <spdlog/cfg/helpers.h>
#endif #endif
#include <spdlog/spdlog.h> #include <spdlog/spdlog.h>
@ -17,7 +17,7 @@
namespace spdlog { namespace spdlog {
namespace cfg { namespace cfg {
namespace text_loader { namespace helpers {
using name_val_pair = std::pair<std::string, std::string>; using name_val_pair = std::pair<std::string, std::string>;
@ -62,7 +62,7 @@ inline name_val_pair extract_kv_(char sep, const std::string &str)
// return vector of key/value pairs from sequence of "K1=V1,K2=V2,.." // return vector of key/value pairs from sequence of "K1=V1,K2=V2,.."
// "a=AAA,b=BBB,c=CCC,.." => {("a","AAA"),("b","BBB"),("c", "CCC"),...} // "a=AAA,b=BBB,c=CCC,.." => {("a","AAA"),("b","BBB"),("c", "CCC"),...}
SPDLOG_INLINE std::unordered_map<std::string, std::string> extract_key_vals_(const std::string &str) inline std::unordered_map<std::string, std::string> extract_key_vals_(const std::string &str)
{ {
std::string token; std::string token;
std::istringstream token_stream(str); std::istringstream token_stream(str);
@ -79,7 +79,7 @@ SPDLOG_INLINE std::unordered_map<std::string, std::string> extract_key_vals_(con
return rv; return rv;
} }
inline log_levels extract_levels_(const std::string &input) SPDLOG_INLINE log_levels extract_levels(const std::string &input)
{ {
auto key_vals = extract_key_vals_(input); auto key_vals = extract_key_vals_(input);
log_levels rv; log_levels rv;
@ -99,11 +99,6 @@ inline log_levels extract_levels_(const std::string &input)
return rv; return rv;
} }
SPDLOG_INLINE log_levels load_levels(const std::string &txt)
{
return extract_levels_(txt);
}
} // namespace text_loader } // namespace text_loader
} // namespace cfg } // namespace cfg
} // namespace spdlog } // namespace spdlog

View File

@ -0,0 +1,26 @@
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
#pragma once
#include <spdlog/cfg/log_levels.h>
#include <spdlog/common.h>
#include <string>
#include <unordered_map>
//
// Init levels from given string
//
namespace spdlog {
namespace cfg {
namespace helpers {
log_levels extract_levels(const std::string &txt);
}
} // namespace cfg
} // namespace spdlog
#ifdef SPDLOG_HEADER_ONLY
#include "helpers-inl.h"
#endif // SPDLOG_HEADER_ONLY

View File

@ -39,7 +39,7 @@ public:
} }
private: private:
std::unordered_map<std::string, spdlog::level::level_enum> levels_; levels_map levels_;
spdlog::level::level_enum default_level_ = level::info; spdlog::level::level_enum default_level_ = level::info;
}; };
} // namespace cfg } // namespace cfg

View File

@ -1,38 +0,0 @@
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
#pragma once
#include <spdlog/cfg/cfg.h>
#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 {
log_levels load_levels(const std::string &txt);
}
} // namespace cfg
} // namespace spdlog
#ifdef SPDLOG_HEADER_ONLY
#include "text_loader-inl.h"
#endif // SPDLOG_HEADER_ONLY

View File

@ -260,7 +260,7 @@ SPDLOG_INLINE void registry::set_automatic_registration(bool automatic_registrat
automatic_registration_ = automatic_registration; automatic_registration_ = automatic_registration;
} }
SPDLOG_INLINE void registry::set_levels(cfg::log_levels levels) SPDLOG_INLINE void registry::update_levels(cfg::log_levels levels)
{ {
std::lock_guard<std::mutex> lock(logger_map_mutex_); std::lock_guard<std::mutex> lock(logger_map_mutex_);
levels_ = std::move(levels); levels_ = std::move(levels);

View File

@ -9,7 +9,7 @@
// This class is thread safe // This class is thread safe
#include <spdlog/common.h> #include <spdlog/common.h>
#include <spdlog/cfg/cfg.h> #include <spdlog/cfg/log_levels.h>
#include <chrono> #include <chrono>
#include <functional> #include <functional>
@ -80,7 +80,7 @@ public:
void set_automatic_registration(bool automatic_registration); void set_automatic_registration(bool automatic_registration);
void set_levels(cfg::log_levels levels); void update_levels(cfg::log_levels levels);
static registry &instance(); static registry &instance();

View File

@ -17,7 +17,7 @@
#include "spdlog/sinks/sink-inl.h" #include "spdlog/sinks/sink-inl.h"
#include "spdlog/sinks/base_sink-inl.h" #include "spdlog/sinks/base_sink-inl.h"
#include "spdlog/details/null_mutex.h" #include "spdlog/details/null_mutex.h"
#include "spdlog/cfg/text_loader-inl.h" #include "spdlog/cfg/helpers-inl.h"
#include <mutex> #include <mutex>