This commit is contained in:
gabime 2020-09-26 15:32:44 +03:00
parent 05d5546eb1
commit c7613f3e91
3 changed files with 25 additions and 75 deletions

View File

@ -1,52 +0,0 @@
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
#pragma once
#include <spdlog/logger.h>
#include <string>
#include <unordered_map>
namespace spdlog {
namespace cfg {
class log_levels
{
std::unordered_map<std::string, spdlog::level::level_enum> levels_;
spdlog::level::level_enum global_level_ = level::info;
public:
void set(const std::string &logger_name, level::level_enum lvl)
{
levels_[logger_name] = lvl;
}
void set_global_level(level::level_enum lvl)
{
global_level_ = lvl;
}
level::level_enum default_level()
{
return global_level_;
}
// update log level of given logger if it appears in the config list or if default level is set
// return true if updated
bool update_logger_level(spdlog::logger &logger)
{
// if logger was not configured, check if global log level was configured
auto it = levels_.find(logger.name());
if (it == levels_.end())
{
it = levels_.find("*");
}
if (it != levels_.end())
{
logger.set_level(it->second);
return true;
}
return false;
}
};
} // namespace cfg
} // namespace spdlog

View File

@ -57,6 +57,27 @@ SPDLOG_INLINE void registry::register_logger(std::shared_ptr<logger> new_logger)
register_logger_(std::move(new_logger)); register_logger_(std::move(new_logger));
} }
// set level if this logger was configured if the cfg_levels_
// return true if found and set
SPDLOG_INLINE bool registry::set_level_from_cfg_(logger *logger)
{
if (cfg_levels_.empty())
{
return false;
}
auto cfg_level_it = cfg_levels_.find(logger->name());
if (cfg_level_it == cfg_levels_.end())
{
// if logger name not found, set it anyway if "*" exists (i.e. all loggers)
cfg_level_it = cfg_levels_.find(("*"));
}
if (cfg_level_it != cfg_levels_.end())
{
logger->set_level(cfg_level_it->second);
}
return cfg_level_it != cfg_levels_.end();
}
SPDLOG_INLINE void registry::initialize_logger(std::shared_ptr<logger> new_logger) SPDLOG_INLINE void registry::initialize_logger(std::shared_ptr<logger> new_logger)
{ {
std::lock_guard<std::mutex> lock(logger_map_mutex_); std::lock_guard<std::mutex> lock(logger_map_mutex_);
@ -66,17 +87,8 @@ SPDLOG_INLINE void registry::initialize_logger(std::shared_ptr<logger> new_logge
{ {
new_logger->set_error_handler(err_handler_); new_logger->set_error_handler(err_handler_);
} }
// check if log level for this logger name was already configured
auto cfg_level_it = cfg_levels_.find(new_logger->name()); if (!set_level_from_cfg_(new_logger.get()))
if (cfg_level_it == cfg_levels_.end())
{
cfg_level_it = cfg_levels_.find(("*"));
}
if (cfg_level_it != cfg_levels_.end())
{
new_logger->set_level(cfg_level_it->second);
}
else
{ {
new_logger->set_level(global_log_level_); new_logger->set_level(global_log_level_);
} }
@ -281,20 +293,9 @@ SPDLOG_INLINE void registry::set_levels(std::unordered_map<std::string, spdlog::
std::lock_guard<std::mutex> lock(logger_map_mutex_); std::lock_guard<std::mutex> lock(logger_map_mutex_);
cfg_levels_ = std::move(levels); cfg_levels_ = std::move(levels);
// for each logger update level according to given map
// "*" entry means all loggers
auto global_level_it = cfg_levels_.find("*");
for (auto &logger : loggers_) for (auto &logger : loggers_)
{ {
auto cfg_entry = cfg_levels_.find(logger.first); set_level_from_cfg_(logger.second.get());
if (cfg_entry != cfg_levels_.end())
{
logger.second->set_level(cfg_entry->second);
}
else if (global_level_it != cfg_levels_.end())
{
logger.second->set_level(global_level_it->second);
}
} }
} }

View File

@ -89,6 +89,7 @@ private:
void throw_if_exists_(const std::string &logger_name); void throw_if_exists_(const std::string &logger_name);
void register_logger_(std::shared_ptr<logger> new_logger); void register_logger_(std::shared_ptr<logger> new_logger);
bool set_level_from_cfg_(logger *logger);
std::mutex logger_map_mutex_, flusher_mutex_; std::mutex logger_map_mutex_, flusher_mutex_;
std::recursive_mutex tp_mutex_; std::recursive_mutex tp_mutex_;
std::unordered_map<std::string, std::shared_ptr<logger>> loggers_; std::unordered_map<std::string, std::shared_ptr<logger>> loggers_;