mirror of https://github.com/gabime/spdlog.git
parent
ed6919bf8e
commit
166843ff3a
@ -1,37 +0,0 @@
|
|||||||
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
|
|
||||||
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
|
||||||
|
|
||||||
#pragma once
|
|
||||||
#include "./helpers.h"
|
|
||||||
|
|
||||||
//
|
|
||||||
// Init log levels using each argv entry that starts with "SPDLOG_LEVEL="
|
|
||||||
//
|
|
||||||
// set all loggers to debug level:
|
|
||||||
// example.exe "SPDLOG_LEVEL=debug"
|
|
||||||
|
|
||||||
// set logger1 to trace level
|
|
||||||
// example.exe "SPDLOG_LEVEL=logger1=trace"
|
|
||||||
|
|
||||||
// turn off all logging except for logger1 and logger2:
|
|
||||||
// example.exe "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
|
|
||||||
inline void load_argv_levels(int argc, const char **argv) {
|
|
||||||
const std::string spdlog_level_prefix = "SPDLOG_LEVEL=";
|
|
||||||
for (int i = 1; i < argc; i++) {
|
|
||||||
std::string arg = argv[i];
|
|
||||||
if (arg.find(spdlog_level_prefix) == 0) {
|
|
||||||
auto levels_string = arg.substr(spdlog_level_prefix.size());
|
|
||||||
helpers::load_levels(levels_string);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
inline void load_argv_levels(int argc, char **argv) { load_argv_levels(argc, const_cast<const char **>(argv)); }
|
|
||||||
|
|
||||||
} // namespace cfg
|
|
||||||
} // namespace spdlog
|
|
@ -1,36 +0,0 @@
|
|||||||
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
|
|
||||||
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
|
||||||
|
|
||||||
#pragma once
|
|
||||||
#include "../details/os.h"
|
|
||||||
#include "../details/registry.h"
|
|
||||||
#include "./helpers.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 {
|
|
||||||
inline void load_env_levels() {
|
|
||||||
auto env_val = details::os::getenv("SPDLOG_LEVEL");
|
|
||||||
if (!env_val.empty()) {
|
|
||||||
helpers::load_levels(env_val);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace cfg
|
|
||||||
} // namespace spdlog
|
|
@ -1,25 +0,0 @@
|
|||||||
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
|
|
||||||
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
|
||||||
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include <unordered_map>
|
|
||||||
|
|
||||||
#include "../common.h"
|
|
||||||
|
|
||||||
namespace spdlog {
|
|
||||||
namespace cfg {
|
|
||||||
namespace helpers {
|
|
||||||
//
|
|
||||||
// Init levels from given string
|
|
||||||
//
|
|
||||||
// Examples:
|
|
||||||
//
|
|
||||||
// set global level to debug: "debug"
|
|
||||||
// turn off all logging except for logger1: "off,logger1=debug"
|
|
||||||
// turn off all logging except for logger1 and logger2: "off,logger1=debug,logger2=info"
|
|
||||||
//
|
|
||||||
SPDLOG_API void load_levels(const std::string &input);
|
|
||||||
} // namespace helpers
|
|
||||||
} // namespace cfg
|
|
||||||
} // namespace spdlog
|
|
@ -0,0 +1,59 @@
|
|||||||
|
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
|
||||||
|
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
// Loggers registry of unique name->logger pointer
|
||||||
|
// An attempt to create a logger with an already existing name will result with spdlog_ex exception.
|
||||||
|
// If user requests a non-existing logger, nullptr will be returned
|
||||||
|
// This class is thread safe
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
#include <mutex>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include "../common.h"
|
||||||
|
#include "./periodic_worker.h"
|
||||||
|
|
||||||
|
namespace spdlog {
|
||||||
|
class logger;
|
||||||
|
|
||||||
|
namespace details {
|
||||||
|
class thread_pool;
|
||||||
|
|
||||||
|
class SPDLOG_API context {
|
||||||
|
public:
|
||||||
|
context() = default;
|
||||||
|
explicit context(std::unique_ptr<logger> global_logger);
|
||||||
|
~context() = default;
|
||||||
|
context(const context &) = delete;
|
||||||
|
context &operator=(const context &) = delete;
|
||||||
|
|
||||||
|
[[nodiscard]] std::shared_ptr<logger> global_logger();
|
||||||
|
|
||||||
|
// Return raw ptr to the global logger.
|
||||||
|
// To be used directly by the spdlog global api (e.g. spdlog::info)
|
||||||
|
// This make the global API faster, but cannot be used concurrently with set_global_logger().
|
||||||
|
// e.g do not call set_global_logger() from one thread while calling spdlog::info() from
|
||||||
|
// another.
|
||||||
|
[[nodiscard]] logger *global_logger_raw() const noexcept;
|
||||||
|
|
||||||
|
// set logger instance.
|
||||||
|
void set_logger(std::shared_ptr<logger> new_logger);
|
||||||
|
|
||||||
|
void set_tp(std::shared_ptr<thread_pool> tp);
|
||||||
|
|
||||||
|
[[nodiscard]] std::shared_ptr<thread_pool> get_tp();
|
||||||
|
|
||||||
|
// clean all resources
|
||||||
|
void shutdown();
|
||||||
|
[[nodiscard]] std::recursive_mutex &tp_mutex();
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::recursive_mutex tp_mutex_;
|
||||||
|
std::shared_ptr<thread_pool> tp_;
|
||||||
|
std::shared_ptr<logger> global_logger_;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace details
|
||||||
|
} // namespace spdlog
|
@ -1,120 +0,0 @@
|
|||||||
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
|
|
||||||
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
|
||||||
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
// Loggers registry of unique name->logger pointer
|
|
||||||
// An attempt to create a logger with an already existing name will result with spdlog_ex exception.
|
|
||||||
// If user requests a non-existing logger, nullptr will be returned
|
|
||||||
// This class is thread safe
|
|
||||||
|
|
||||||
#include <chrono>
|
|
||||||
#include <functional>
|
|
||||||
#include <memory>
|
|
||||||
#include <mutex>
|
|
||||||
#include <string>
|
|
||||||
#include <string_view>
|
|
||||||
#include <unordered_map>
|
|
||||||
|
|
||||||
#include "../common.h"
|
|
||||||
#include "./periodic_worker.h"
|
|
||||||
|
|
||||||
namespace spdlog {
|
|
||||||
class logger;
|
|
||||||
|
|
||||||
namespace details {
|
|
||||||
class thread_pool;
|
|
||||||
|
|
||||||
class SPDLOG_API registry {
|
|
||||||
public:
|
|
||||||
using log_levels = std::unordered_map<std::string, level>;
|
|
||||||
|
|
||||||
static registry &instance();
|
|
||||||
registry(const registry &) = delete;
|
|
||||||
registry &operator=(const registry &) = delete;
|
|
||||||
|
|
||||||
void register_logger(std::shared_ptr<logger> new_logger);
|
|
||||||
void initialize_logger(std::shared_ptr<logger> new_logger);
|
|
||||||
std::shared_ptr<logger> get(const std::string &logger_name);
|
|
||||||
std::shared_ptr<logger> get(std::string_view logger_name);
|
|
||||||
std::shared_ptr<logger> get(const char *logger_name);
|
|
||||||
std::shared_ptr<logger> default_logger();
|
|
||||||
|
|
||||||
// Return raw ptr to the default logger.
|
|
||||||
// To be used directly by the spdlog default api (e.g. spdlog::info)
|
|
||||||
// This make the default API faster, but cannot be used concurrently with set_default_logger().
|
|
||||||
// e.g do not call set_default_logger() from one thread while calling spdlog::info() from
|
|
||||||
// another.
|
|
||||||
logger *get_default_raw() const;
|
|
||||||
|
|
||||||
// set default logger.
|
|
||||||
// default logger is stored in default_logger_ (for faster retrieval) and in the loggers_ map.
|
|
||||||
void set_default_logger(std::shared_ptr<logger> new_default_logger);
|
|
||||||
|
|
||||||
void set_tp(std::shared_ptr<thread_pool> tp);
|
|
||||||
|
|
||||||
std::shared_ptr<thread_pool> get_tp();
|
|
||||||
|
|
||||||
// Set global formatter. Each sink in each logger will get a clone of this object
|
|
||||||
void set_formatter(std::unique_ptr<formatter> formatter);
|
|
||||||
|
|
||||||
void set_level(level level);
|
|
||||||
|
|
||||||
void flush_on(level level);
|
|
||||||
|
|
||||||
template <typename Rep, typename Period>
|
|
||||||
void flush_every(std::chrono::duration<Rep, Period> interval) {
|
|
||||||
std::lock_guard<std::mutex> lock(flusher_mutex_);
|
|
||||||
auto clbk = [this]() { this->flush_all(); };
|
|
||||||
periodic_flusher_ = std::make_unique<periodic_worker>(clbk, interval);
|
|
||||||
}
|
|
||||||
|
|
||||||
std::unique_ptr<periodic_worker> &get_flusher() {
|
|
||||||
std::lock_guard<std::mutex> lock(flusher_mutex_);
|
|
||||||
return periodic_flusher_;
|
|
||||||
}
|
|
||||||
|
|
||||||
void set_error_handler(err_handler handler);
|
|
||||||
|
|
||||||
void apply_all(const std::function<void(const std::shared_ptr<logger>)> &fun);
|
|
||||||
|
|
||||||
void flush_all();
|
|
||||||
|
|
||||||
void drop(const std::string &logger_name);
|
|
||||||
|
|
||||||
void drop_all();
|
|
||||||
|
|
||||||
// clean all resources and threads started by the registry
|
|
||||||
void shutdown();
|
|
||||||
|
|
||||||
std::recursive_mutex &tp_mutex();
|
|
||||||
|
|
||||||
void set_automatic_registration(bool automatic_registration);
|
|
||||||
|
|
||||||
// set levels for all existing/future loggers. global_level can be null if should not set.
|
|
||||||
void set_levels(log_levels levels, level *global_level);
|
|
||||||
|
|
||||||
void apply_logger_env_levels(std::shared_ptr<logger> new_logger);
|
|
||||||
|
|
||||||
private:
|
|
||||||
registry();
|
|
||||||
~registry();
|
|
||||||
|
|
||||||
void throw_if_exists_(const std::string &logger_name);
|
|
||||||
void register_logger_(std::shared_ptr<logger> new_logger);
|
|
||||||
std::mutex logger_map_mutex_, flusher_mutex_;
|
|
||||||
std::recursive_mutex tp_mutex_;
|
|
||||||
std::unordered_map<std::string, std::shared_ptr<logger>> loggers_;
|
|
||||||
log_levels log_levels_;
|
|
||||||
std::unique_ptr<formatter> formatter_;
|
|
||||||
spdlog::level global_log_level_ = level::info;
|
|
||||||
level flush_level_ = level::off;
|
|
||||||
err_handler err_handler_;
|
|
||||||
std::shared_ptr<thread_pool> tp_;
|
|
||||||
std::unique_ptr<periodic_worker> periodic_flusher_;
|
|
||||||
std::shared_ptr<logger> default_logger_;
|
|
||||||
bool automatic_registration_ = true;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace details
|
|
||||||
} // namespace spdlog
|
|
Before Width: | Height: | Size: 4.6 KiB After Width: | Height: | Size: 4.6 KiB |
@ -1,99 +0,0 @@
|
|||||||
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
|
|
||||||
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
|
||||||
|
|
||||||
#include "spdlog/cfg/helpers.h"
|
|
||||||
|
|
||||||
#include <algorithm>
|
|
||||||
#include <sstream>
|
|
||||||
#include <string>
|
|
||||||
#include <utility>
|
|
||||||
|
|
||||||
#include "spdlog/details/registry.h"
|
|
||||||
|
|
||||||
namespace spdlog {
|
|
||||||
namespace cfg {
|
|
||||||
namespace helpers {
|
|
||||||
|
|
||||||
// inplace convert to lowercase
|
|
||||||
inline std::string &to_lower_(std::string &str) {
|
|
||||||
std::transform(str.begin(), str.end(), str.begin(),
|
|
||||||
[](char ch) { return static_cast<char>((ch >= 'A' && ch <= 'Z') ? ch + ('a' - 'A') : ch); });
|
|
||||||
return str;
|
|
||||||
}
|
|
||||||
|
|
||||||
// inplace trim spaces
|
|
||||||
inline std::string &trim_(std::string &str) {
|
|
||||||
const char *spaces = " \n\r\t";
|
|
||||||
str.erase(str.find_last_not_of(spaces) + 1);
|
|
||||||
str.erase(0, str.find_first_not_of(spaces));
|
|
||||||
return str;
|
|
||||||
}
|
|
||||||
|
|
||||||
// return (name,value) trimmed pair from given "name=value" string.
|
|
||||||
// return empty string on missing parts
|
|
||||||
// "key=val" => ("key", "val")
|
|
||||||
// " key = val " => ("key", "val")
|
|
||||||
// "key=" => ("key", "")
|
|
||||||
// "val" => ("", "val")
|
|
||||||
|
|
||||||
inline std::pair<std::string, std::string> extract_kv_(char sep, const std::string &str) {
|
|
||||||
auto n = str.find(sep);
|
|
||||||
std::string k, v;
|
|
||||||
if (n == std::string::npos) {
|
|
||||||
v = str;
|
|
||||||
} else {
|
|
||||||
k = str.substr(0, n);
|
|
||||||
v = str.substr(n + 1);
|
|
||||||
}
|
|
||||||
return std::make_pair(trim_(k), trim_(v));
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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"),...}
|
|
||||||
inline std::unordered_map<std::string, std::string> extract_key_vals_(const std::string &str) {
|
|
||||||
std::string token;
|
|
||||||
std::istringstream token_stream(str);
|
|
||||||
std::unordered_map<std::string, std::string> rv{};
|
|
||||||
while (std::getline(token_stream, token, ',')) {
|
|
||||||
if (token.empty()) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
auto kv = extract_kv_('=', token);
|
|
||||||
rv[kv.first] = kv.second;
|
|
||||||
}
|
|
||||||
return rv;
|
|
||||||
}
|
|
||||||
|
|
||||||
void load_levels(const std::string &input) {
|
|
||||||
if (input.empty() || input.size() > 512) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto key_vals = extract_key_vals_(input);
|
|
||||||
std::unordered_map<std::string, level> levels;
|
|
||||||
level global_level = level::info;
|
|
||||||
bool global_level_found = false;
|
|
||||||
|
|
||||||
for (auto &name_level : key_vals) {
|
|
||||||
const auto &logger_name = name_level.first;
|
|
||||||
auto level_name = to_lower_(name_level.second);
|
|
||||||
auto level = level_from_str(level_name);
|
|
||||||
// ignore unrecognized level names
|
|
||||||
if (level == level::off && level_name != "off") {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (logger_name.empty()) // no logger name indicate global level
|
|
||||||
{
|
|
||||||
global_level_found = true;
|
|
||||||
global_level = level;
|
|
||||||
} else {
|
|
||||||
levels[logger_name] = level;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
details::registry::instance().set_levels(std::move(levels), global_level_found ? &global_level : nullptr);
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace helpers
|
|
||||||
} // namespace cfg
|
|
||||||
} // namespace spdlog
|
|
@ -0,0 +1,49 @@
|
|||||||
|
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
|
||||||
|
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
||||||
|
|
||||||
|
#include "spdlog/details/context.h"
|
||||||
|
|
||||||
|
#include "spdlog/logger.h"
|
||||||
|
|
||||||
|
#ifndef SPDLOG_DISABLE_GLOBAL_LOGGER
|
||||||
|
#include "spdlog/sinks/stdout_color_sinks.h"
|
||||||
|
#endif // SPDLOG_DISABLE_GLOBAL_LOGGER
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
namespace spdlog {
|
||||||
|
namespace details {
|
||||||
|
|
||||||
|
context::context(std::unique_ptr<logger> global_logger) { global_logger_ = std::move(global_logger); }
|
||||||
|
|
||||||
|
std::shared_ptr<logger> context::global_logger() { return global_logger_; }
|
||||||
|
|
||||||
|
// Return raw ptr to the global logger.
|
||||||
|
// To be used directly by the spdlog default api (e.g. spdlog::info)
|
||||||
|
// This make the default API faster, but cannot be used concurrently with set_global_logger().
|
||||||
|
// e.g do not call set_global_logger() from one thread while calling spdlog::info() from another.
|
||||||
|
logger *context::global_logger_raw() const noexcept { return global_logger_.get(); }
|
||||||
|
|
||||||
|
// set global logger
|
||||||
|
void context::set_logger(std::shared_ptr<logger> new_global_logger) { global_logger_ = std::move(new_global_logger); }
|
||||||
|
|
||||||
|
void context::set_tp(std::shared_ptr<thread_pool> tp) {
|
||||||
|
std::lock_guard lock(tp_mutex_);
|
||||||
|
tp_ = std::move(tp);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::shared_ptr<thread_pool> context::get_tp() {
|
||||||
|
std::lock_guard lock(tp_mutex_);
|
||||||
|
return tp_;
|
||||||
|
}
|
||||||
|
|
||||||
|
// clean all resources and threads started by the registry
|
||||||
|
void context::shutdown() {
|
||||||
|
std::lock_guard lock(tp_mutex_);
|
||||||
|
tp_.reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::recursive_mutex &context::tp_mutex() { return tp_mutex_; }
|
||||||
|
|
||||||
|
} // namespace details
|
||||||
|
} // namespace spdlog
|
@ -1,22 +0,0 @@
|
|||||||
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
|
|
||||||
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
|
||||||
|
|
||||||
#include "spdlog/details/periodic_worker.h"
|
|
||||||
|
|
||||||
namespace spdlog {
|
|
||||||
namespace details {
|
|
||||||
|
|
||||||
// stop the worker thread and join it
|
|
||||||
periodic_worker::~periodic_worker() {
|
|
||||||
if (worker_thread_.joinable()) {
|
|
||||||
{
|
|
||||||
std::lock_guard<std::mutex> lock(mutex_);
|
|
||||||
active_ = false;
|
|
||||||
}
|
|
||||||
cv_.notify_one();
|
|
||||||
worker_thread_.join();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace details
|
|
||||||
} // namespace spdlog
|
|
@ -1,263 +0,0 @@
|
|||||||
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
|
|
||||||
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
|
||||||
|
|
||||||
#include "spdlog/details/registry.h"
|
|
||||||
|
|
||||||
#include "spdlog/common.h"
|
|
||||||
#include "spdlog/logger.h"
|
|
||||||
#include "spdlog/pattern_formatter.h"
|
|
||||||
|
|
||||||
#ifndef SPDLOG_DISABLE_DEFAULT_LOGGER
|
|
||||||
// support for the default stdout color logger
|
|
||||||
#ifdef _WIN32
|
|
||||||
#include "spdlog/sinks/wincolor_sink.h"
|
|
||||||
#else
|
|
||||||
|
|
||||||
#include "spdlog/sinks/ansicolor_sink.h"
|
|
||||||
|
|
||||||
#endif
|
|
||||||
#endif // SPDLOG_DISABLE_DEFAULT_LOGGER
|
|
||||||
|
|
||||||
#include <memory>
|
|
||||||
#include <string>
|
|
||||||
#include <unordered_map>
|
|
||||||
|
|
||||||
static constexpr size_t small_map_threshold = 10;
|
|
||||||
|
|
||||||
namespace spdlog {
|
|
||||||
namespace details {
|
|
||||||
|
|
||||||
registry::registry()
|
|
||||||
: formatter_(new pattern_formatter()) {
|
|
||||||
#ifndef SPDLOG_DISABLE_DEFAULT_LOGGER
|
|
||||||
// create default logger (ansicolor_stdout_sink_mt or wincolor_stdout_sink_mt in windows).
|
|
||||||
#ifdef _WIN32
|
|
||||||
auto color_sink = std::make_shared<sinks::wincolor_stdout_sink_mt>();
|
|
||||||
#else
|
|
||||||
auto color_sink = std::make_shared<sinks::ansicolor_stdout_sink_mt>();
|
|
||||||
#endif
|
|
||||||
const char *default_logger_name = "";
|
|
||||||
default_logger_ = std::make_shared<spdlog::logger>(default_logger_name, std::move(color_sink));
|
|
||||||
loggers_[default_logger_name] = default_logger_;
|
|
||||||
|
|
||||||
#endif // SPDLOG_DISABLE_DEFAULT_LOGGER
|
|
||||||
}
|
|
||||||
|
|
||||||
registry::~registry() = default;
|
|
||||||
|
|
||||||
void registry::register_logger(std::shared_ptr<logger> new_logger) {
|
|
||||||
std::lock_guard<std::mutex> lock(logger_map_mutex_);
|
|
||||||
register_logger_(std::move(new_logger));
|
|
||||||
}
|
|
||||||
|
|
||||||
void registry::initialize_logger(std::shared_ptr<logger> new_logger) {
|
|
||||||
std::lock_guard<std::mutex> lock(logger_map_mutex_);
|
|
||||||
new_logger->set_formatter(formatter_->clone());
|
|
||||||
|
|
||||||
if (err_handler_) {
|
|
||||||
new_logger->set_error_handler(err_handler_);
|
|
||||||
}
|
|
||||||
|
|
||||||
// set new level according to previously configured level or default level
|
|
||||||
auto it = log_levels_.find(new_logger->name());
|
|
||||||
auto new_level = it != log_levels_.end() ? it->second : global_log_level_;
|
|
||||||
new_logger->set_level(new_level);
|
|
||||||
|
|
||||||
new_logger->flush_on(flush_level_);
|
|
||||||
|
|
||||||
if (automatic_registration_) {
|
|
||||||
register_logger_(std::move(new_logger));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// if the map is small do a sequential search, otherwise use the standard find()
|
|
||||||
std::shared_ptr<logger> registry::get(const std::string &logger_name) {
|
|
||||||
std::lock_guard<std::mutex> lock(logger_map_mutex_);
|
|
||||||
if (loggers_.size() <= small_map_threshold) {
|
|
||||||
for (const auto &[key, val] : loggers_) {
|
|
||||||
if (logger_name == key) {
|
|
||||||
return val;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
auto found = loggers_.find(logger_name);
|
|
||||||
return found == loggers_.end() ? nullptr : found->second;
|
|
||||||
}
|
|
||||||
|
|
||||||
// if the map is small do a sequential search and avoid creating string for find(logger_name)
|
|
||||||
// otherwise use the standard find()
|
|
||||||
std::shared_ptr<logger> registry::get(std::string_view logger_name) {
|
|
||||||
std::lock_guard<std::mutex> lock(logger_map_mutex_);
|
|
||||||
if (loggers_.size() <= small_map_threshold) {
|
|
||||||
for (const auto &[key, val] : loggers_) {
|
|
||||||
if (logger_name == key) {
|
|
||||||
return val;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
// otherwise use the normal map lookup
|
|
||||||
const auto found = loggers_.find(std::string(logger_name));
|
|
||||||
return found == loggers_.end() ? nullptr : found->second;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::shared_ptr<logger> registry::get(const char *logger_name) { return get(std::string_view(logger_name)); }
|
|
||||||
|
|
||||||
std::shared_ptr<logger> registry::default_logger() {
|
|
||||||
std::lock_guard<std::mutex> lock(logger_map_mutex_);
|
|
||||||
return default_logger_;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Return raw ptr to the default logger.
|
|
||||||
// To be used directly by the spdlog default api (e.g. spdlog::info)
|
|
||||||
// This make the default API faster, but cannot be used concurrently with set_default_logger().
|
|
||||||
// e.g do not call set_default_logger() from one thread while calling spdlog::info() from another.
|
|
||||||
logger *registry::get_default_raw() const { return default_logger_.get(); }
|
|
||||||
|
|
||||||
// set default logger.
|
|
||||||
// default logger is stored in default_logger_ (for faster retrieval) and in the loggers_ map.
|
|
||||||
void registry::set_default_logger(std::shared_ptr<logger> new_default_logger) {
|
|
||||||
std::lock_guard<std::mutex> lock(logger_map_mutex_);
|
|
||||||
if (new_default_logger != nullptr) {
|
|
||||||
loggers_[new_default_logger->name()] = new_default_logger;
|
|
||||||
}
|
|
||||||
default_logger_ = std::move(new_default_logger);
|
|
||||||
}
|
|
||||||
|
|
||||||
void registry::set_tp(std::shared_ptr<thread_pool> tp) {
|
|
||||||
std::lock_guard<std::recursive_mutex> lock(tp_mutex_);
|
|
||||||
tp_ = std::move(tp);
|
|
||||||
}
|
|
||||||
|
|
||||||
std::shared_ptr<thread_pool> registry::get_tp() {
|
|
||||||
std::lock_guard<std::recursive_mutex> lock(tp_mutex_);
|
|
||||||
return tp_;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set global formatter. Each sink in each logger will get a clone of this object
|
|
||||||
void registry::set_formatter(std::unique_ptr<formatter> formatter) {
|
|
||||||
std::lock_guard<std::mutex> lock(logger_map_mutex_);
|
|
||||||
formatter_ = std::move(formatter);
|
|
||||||
for (auto &l : loggers_) {
|
|
||||||
l.second->set_formatter(formatter_->clone());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void registry::set_level(level level) {
|
|
||||||
std::lock_guard<std::mutex> lock(logger_map_mutex_);
|
|
||||||
for (auto &l : loggers_) {
|
|
||||||
l.second->set_level(level);
|
|
||||||
}
|
|
||||||
global_log_level_ = level;
|
|
||||||
}
|
|
||||||
|
|
||||||
void registry::flush_on(level level) {
|
|
||||||
std::lock_guard<std::mutex> lock(logger_map_mutex_);
|
|
||||||
for (auto &l : loggers_) {
|
|
||||||
l.second->flush_on(level);
|
|
||||||
}
|
|
||||||
flush_level_ = level;
|
|
||||||
}
|
|
||||||
|
|
||||||
void registry::set_error_handler(err_handler handler) {
|
|
||||||
std::lock_guard<std::mutex> lock(logger_map_mutex_);
|
|
||||||
for (auto &l : loggers_) {
|
|
||||||
l.second->set_error_handler(handler);
|
|
||||||
}
|
|
||||||
err_handler_ = std::move(handler);
|
|
||||||
}
|
|
||||||
|
|
||||||
void registry::apply_all(const std::function<void(const std::shared_ptr<logger>)> &fun) {
|
|
||||||
std::lock_guard<std::mutex> lock(logger_map_mutex_);
|
|
||||||
for (auto &l : loggers_) {
|
|
||||||
fun(l.second);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void registry::flush_all() {
|
|
||||||
std::lock_guard<std::mutex> lock(logger_map_mutex_);
|
|
||||||
for (auto &l : loggers_) {
|
|
||||||
l.second->flush();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void registry::drop(const std::string &logger_name) {
|
|
||||||
std::lock_guard<std::mutex> lock(logger_map_mutex_);
|
|
||||||
auto is_default_logger = default_logger_ && default_logger_->name() == logger_name;
|
|
||||||
loggers_.erase(logger_name);
|
|
||||||
if (is_default_logger) {
|
|
||||||
default_logger_.reset();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void registry::drop_all() {
|
|
||||||
std::lock_guard<std::mutex> lock(logger_map_mutex_);
|
|
||||||
loggers_.clear();
|
|
||||||
default_logger_.reset();
|
|
||||||
}
|
|
||||||
|
|
||||||
// clean all resources and threads started by the registry
|
|
||||||
void registry::shutdown() {
|
|
||||||
{
|
|
||||||
std::lock_guard<std::mutex> lock(flusher_mutex_);
|
|
||||||
periodic_flusher_.reset();
|
|
||||||
}
|
|
||||||
|
|
||||||
drop_all();
|
|
||||||
|
|
||||||
{
|
|
||||||
std::lock_guard<std::recursive_mutex> lock(tp_mutex_);
|
|
||||||
tp_.reset();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
std::recursive_mutex ®istry::tp_mutex() { return tp_mutex_; }
|
|
||||||
|
|
||||||
void registry::set_automatic_registration(bool automatic_registration) {
|
|
||||||
std::lock_guard<std::mutex> lock(logger_map_mutex_);
|
|
||||||
automatic_registration_ = automatic_registration;
|
|
||||||
}
|
|
||||||
|
|
||||||
void registry::set_levels(log_levels levels, level *global_level) {
|
|
||||||
std::lock_guard<std::mutex> lock(logger_map_mutex_);
|
|
||||||
log_levels_ = std::move(levels);
|
|
||||||
auto global_level_requested = global_level != nullptr;
|
|
||||||
global_log_level_ = global_level_requested ? *global_level : global_log_level_;
|
|
||||||
|
|
||||||
for (auto &logger : loggers_) {
|
|
||||||
auto logger_entry = log_levels_.find(logger.first);
|
|
||||||
if (logger_entry != log_levels_.end()) {
|
|
||||||
logger.second->set_level(logger_entry->second);
|
|
||||||
} else if (global_level_requested) {
|
|
||||||
logger.second->set_level(*global_level);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
registry ®istry::instance() {
|
|
||||||
static registry s_instance;
|
|
||||||
return s_instance;
|
|
||||||
}
|
|
||||||
|
|
||||||
void registry::apply_logger_env_levels(std::shared_ptr<logger> new_logger) {
|
|
||||||
std::lock_guard<std::mutex> lock(logger_map_mutex_);
|
|
||||||
auto it = log_levels_.find(new_logger->name());
|
|
||||||
auto new_level = it != log_levels_.end() ? it->second : global_log_level_;
|
|
||||||
new_logger->set_level(new_level);
|
|
||||||
}
|
|
||||||
|
|
||||||
void registry::throw_if_exists_(const std::string &logger_name) {
|
|
||||||
if (loggers_.find(logger_name) != loggers_.end()) {
|
|
||||||
throw_spdlog_ex("logger with name '" + logger_name + "' already exists");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void registry::register_logger_(std::shared_ptr<logger> new_logger) {
|
|
||||||
auto logger_name = new_logger->name();
|
|
||||||
throw_if_exists_(logger_name);
|
|
||||||
loggers_[logger_name] = std::move(new_logger);
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace details
|
|
||||||
} // namespace spdlog
|
|
@ -1,168 +0,0 @@
|
|||||||
|
|
||||||
#include "includes.h"
|
|
||||||
#include "spdlog/cfg/argv.h"
|
|
||||||
#include "spdlog/cfg/env.h"
|
|
||||||
#include "test_sink.h"
|
|
||||||
|
|
||||||
using spdlog::cfg::load_argv_levels;
|
|
||||||
using spdlog::cfg::load_env_levels;
|
|
||||||
using spdlog::sinks::test_sink_st;
|
|
||||||
|
|
||||||
TEST_CASE("env", "[cfg]") {
|
|
||||||
spdlog::drop("l1");
|
|
||||||
auto l1 = spdlog::create<test_sink_st>("l1");
|
|
||||||
#ifdef CATCH_PLATFORM_WINDOWS
|
|
||||||
_putenv_s("SPDLOG_LEVEL", "l1=warn");
|
|
||||||
#else
|
|
||||||
setenv("SPDLOG_LEVEL", "l1=warn", 1);
|
|
||||||
#endif
|
|
||||||
load_env_levels();
|
|
||||||
REQUIRE(l1->log_level() == spdlog::level::warn);
|
|
||||||
spdlog::set_default_logger(spdlog::create<test_sink_st>("cfg-default"));
|
|
||||||
REQUIRE(spdlog::default_logger()->log_level() == spdlog::level::info);
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_CASE("argv1", "[cfg]") {
|
|
||||||
spdlog::drop("l1");
|
|
||||||
const char *argv[] = {"ignore", "SPDLOG_LEVEL=l1=warn"};
|
|
||||||
load_argv_levels(2, argv);
|
|
||||||
auto l1 = spdlog::create<spdlog::sinks::test_sink_st>("l1");
|
|
||||||
REQUIRE(l1->log_level() == spdlog::level::warn);
|
|
||||||
REQUIRE(spdlog::default_logger()->log_level() == spdlog::level::info);
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_CASE("argv2", "[cfg]") {
|
|
||||||
spdlog::drop("l1");
|
|
||||||
const char *argv[] = {"ignore", "SPDLOG_LEVEL=l1=warn,trace"};
|
|
||||||
load_argv_levels(2, argv);
|
|
||||||
auto l1 = spdlog::create<test_sink_st>("l1");
|
|
||||||
REQUIRE(l1->log_level() == spdlog::level::warn);
|
|
||||||
REQUIRE(spdlog::default_logger()->log_level() == spdlog::level::trace);
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_CASE("argv3", "[cfg]") {
|
|
||||||
spdlog::set_level(spdlog::level::trace);
|
|
||||||
|
|
||||||
spdlog::drop("l1");
|
|
||||||
const char *argv[] = {"ignore", "SPDLOG_LEVEL=junk_name=warn"};
|
|
||||||
load_argv_levels(2, argv);
|
|
||||||
auto l1 = spdlog::create<test_sink_st>("l1");
|
|
||||||
REQUIRE(l1->log_level() == spdlog::level::trace);
|
|
||||||
REQUIRE(spdlog::default_logger()->log_level() == spdlog::level::trace);
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_CASE("argv4", "[cfg]") {
|
|
||||||
spdlog::set_level(spdlog::level::info);
|
|
||||||
spdlog::drop("l1");
|
|
||||||
const char *argv[] = {"ignore", "SPDLOG_LEVEL=junk"};
|
|
||||||
load_argv_levels(2, argv);
|
|
||||||
auto l1 = spdlog::create<test_sink_st>("l1");
|
|
||||||
REQUIRE(l1->log_level() == spdlog::level::info);
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_CASE("argv5", "[cfg]") {
|
|
||||||
spdlog::set_level(spdlog::level::info);
|
|
||||||
spdlog::drop("l1");
|
|
||||||
const char *argv[] = {"ignore", "ignore", "SPDLOG_LEVEL=l1=warn,trace"};
|
|
||||||
load_argv_levels(3, argv);
|
|
||||||
auto l1 = spdlog::create<test_sink_st>("l1");
|
|
||||||
REQUIRE(l1->log_level() == spdlog::level::warn);
|
|
||||||
REQUIRE(spdlog::default_logger()->log_level() == spdlog::level::trace);
|
|
||||||
spdlog::set_level(spdlog::level::info);
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_CASE("argv6", "[cfg]") {
|
|
||||||
spdlog::set_level(spdlog::level::err);
|
|
||||||
const char *argv[] = {""};
|
|
||||||
load_argv_levels(1, argv);
|
|
||||||
REQUIRE(spdlog::default_logger()->log_level() == spdlog::level::err);
|
|
||||||
spdlog::set_level(spdlog::level::info);
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_CASE("argv7", "[cfg]") {
|
|
||||||
spdlog::set_level(spdlog::level::err);
|
|
||||||
const char *argv[] = {""};
|
|
||||||
load_argv_levels(0, argv);
|
|
||||||
REQUIRE(spdlog::default_logger()->log_level() == spdlog::level::err);
|
|
||||||
spdlog::set_level(spdlog::level::info);
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_CASE("level-not-set-test1", "[cfg]") {
|
|
||||||
spdlog::drop("l1");
|
|
||||||
const char *argv[] = {"ignore", ""};
|
|
||||||
load_argv_levels(2, argv);
|
|
||||||
auto l1 = spdlog::create<spdlog::sinks::test_sink_st>("l1");
|
|
||||||
l1->set_level(spdlog::level::trace);
|
|
||||||
REQUIRE(l1->log_level() == spdlog::level::trace);
|
|
||||||
REQUIRE(spdlog::default_logger()->log_level() == spdlog::level::info);
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_CASE("level-not-set-test2", "[cfg]") {
|
|
||||||
spdlog::drop("l1");
|
|
||||||
spdlog::drop("l2");
|
|
||||||
const char *argv[] = {"ignore", "SPDLOG_LEVEL=l1=trace"};
|
|
||||||
|
|
||||||
auto l1 = spdlog::create<spdlog::sinks::test_sink_st>("l1");
|
|
||||||
l1->set_level(spdlog::level::warn);
|
|
||||||
auto l2 = spdlog::create<spdlog::sinks::test_sink_st>("l2");
|
|
||||||
l2->set_level(spdlog::level::warn);
|
|
||||||
|
|
||||||
load_argv_levels(2, argv);
|
|
||||||
|
|
||||||
REQUIRE(l1->log_level() == spdlog::level::trace);
|
|
||||||
REQUIRE(l2->log_level() == spdlog::level::warn);
|
|
||||||
REQUIRE(spdlog::default_logger()->log_level() == spdlog::level::info);
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_CASE("level-not-set-test3", "[cfg]") {
|
|
||||||
spdlog::drop("l1");
|
|
||||||
spdlog::drop("l2");
|
|
||||||
const char *argv[] = {"ignore", "SPDLOG_LEVEL=l1=trace"};
|
|
||||||
|
|
||||||
load_argv_levels(2, argv);
|
|
||||||
|
|
||||||
auto l1 = spdlog::create<spdlog::sinks::test_sink_st>("l1");
|
|
||||||
auto l2 = spdlog::create<spdlog::sinks::test_sink_st>("l2");
|
|
||||||
|
|
||||||
REQUIRE(l1->log_level() == spdlog::level::trace);
|
|
||||||
REQUIRE(l2->log_level() == spdlog::level::info);
|
|
||||||
REQUIRE(spdlog::default_logger()->log_level() == spdlog::level::info);
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_CASE("level-not-set-test4", "[cfg]") {
|
|
||||||
spdlog::drop("l1");
|
|
||||||
spdlog::drop("l2");
|
|
||||||
const char *argv[] = {"ignore", "SPDLOG_LEVEL=l1=trace,warn"};
|
|
||||||
|
|
||||||
load_argv_levels(2, argv);
|
|
||||||
|
|
||||||
auto l1 = spdlog::create<spdlog::sinks::test_sink_st>("l1");
|
|
||||||
auto l2 = spdlog::create<spdlog::sinks::test_sink_st>("l2");
|
|
||||||
|
|
||||||
REQUIRE(l1->log_level() == spdlog::level::trace);
|
|
||||||
REQUIRE(l2->log_level() == spdlog::level::warn);
|
|
||||||
REQUIRE(spdlog::default_logger()->log_level() == spdlog::level::warn);
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_CASE("level-not-set-test5", "[cfg]") {
|
|
||||||
spdlog::drop("l1");
|
|
||||||
spdlog::drop("l2");
|
|
||||||
const char *argv[] = {"ignore", "SPDLOG_LEVEL=l1=junk,warn"};
|
|
||||||
|
|
||||||
load_argv_levels(2, argv);
|
|
||||||
|
|
||||||
auto l1 = spdlog::create<spdlog::sinks::test_sink_st>("l1");
|
|
||||||
auto l2 = spdlog::create<spdlog::sinks::test_sink_st>("l2");
|
|
||||||
|
|
||||||
REQUIRE(l1->log_level() == spdlog::level::warn);
|
|
||||||
REQUIRE(l2->log_level() == spdlog::level::warn);
|
|
||||||
REQUIRE(spdlog::default_logger()->log_level() == spdlog::level::warn);
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_CASE("restore-to-default", "[cfg]") {
|
|
||||||
spdlog::drop("l1");
|
|
||||||
spdlog::drop("l2");
|
|
||||||
const char *argv[] = {"ignore", "SPDLOG_LEVEL=info"};
|
|
||||||
load_argv_levels(2, argv);
|
|
||||||
REQUIRE(spdlog::default_logger()->log_level() == spdlog::level::info);
|
|
||||||
}
|
|
@ -1,129 +0,0 @@
|
|||||||
#include <string_view>
|
|
||||||
|
|
||||||
#include "includes.h"
|
|
||||||
#include "spdlog/sinks/daily_file_sink.h"
|
|
||||||
#include "spdlog/sinks/rotating_file_sink.h"
|
|
||||||
|
|
||||||
static const char *const tested_logger_name = "null_logger";
|
|
||||||
static const char *const tested_logger_name2 = "null_logger2";
|
|
||||||
|
|
||||||
TEST_CASE("register_drop", "[registry]") {
|
|
||||||
spdlog::drop_all();
|
|
||||||
spdlog::create<spdlog::sinks::null_sink_mt>(tested_logger_name);
|
|
||||||
REQUIRE(spdlog::get(tested_logger_name) != nullptr);
|
|
||||||
// Throw if registering existing name
|
|
||||||
REQUIRE_THROWS_AS(spdlog::create<spdlog::sinks::null_sink_mt>(tested_logger_name), spdlog::spdlog_ex);
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_CASE("explicit register", "[registry]") {
|
|
||||||
spdlog::drop_all();
|
|
||||||
auto logger = std::make_shared<spdlog::logger>(tested_logger_name, std::make_shared<spdlog::sinks::null_sink_st>());
|
|
||||||
spdlog::register_logger(logger);
|
|
||||||
REQUIRE(spdlog::get(tested_logger_name) != nullptr);
|
|
||||||
// Throw if registering existing name
|
|
||||||
REQUIRE_THROWS_AS(spdlog::create<spdlog::sinks::null_sink_mt>(tested_logger_name), spdlog::spdlog_ex);
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_CASE("apply_all", "[registry]") {
|
|
||||||
spdlog::drop_all();
|
|
||||||
auto logger = std::make_shared<spdlog::logger>(tested_logger_name, std::make_shared<spdlog::sinks::null_sink_st>());
|
|
||||||
spdlog::register_logger(logger);
|
|
||||||
auto logger2 = std::make_shared<spdlog::logger>(tested_logger_name2, std::make_shared<spdlog::sinks::null_sink_st>());
|
|
||||||
spdlog::register_logger(logger2);
|
|
||||||
|
|
||||||
int counter = 0;
|
|
||||||
spdlog::apply_all([&counter](std::shared_ptr<spdlog::logger>) { counter++; });
|
|
||||||
REQUIRE(counter == 2);
|
|
||||||
|
|
||||||
counter = 0;
|
|
||||||
spdlog::drop(tested_logger_name2);
|
|
||||||
spdlog::apply_all([&counter](std::shared_ptr<spdlog::logger> l) {
|
|
||||||
REQUIRE(l->name() == tested_logger_name);
|
|
||||||
counter++;
|
|
||||||
});
|
|
||||||
REQUIRE(counter == 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_CASE("drop", "[registry]") {
|
|
||||||
spdlog::drop_all();
|
|
||||||
spdlog::create<spdlog::sinks::null_sink_mt>(tested_logger_name);
|
|
||||||
spdlog::drop(tested_logger_name);
|
|
||||||
REQUIRE_FALSE(spdlog::get(tested_logger_name));
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_CASE("drop-default", "[registry]") {
|
|
||||||
spdlog::set_default_logger(spdlog::null_logger_st(tested_logger_name));
|
|
||||||
spdlog::drop(tested_logger_name);
|
|
||||||
REQUIRE_FALSE(spdlog::default_logger());
|
|
||||||
REQUIRE_FALSE(spdlog::get(tested_logger_name));
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_CASE("drop_all", "[registry]") {
|
|
||||||
spdlog::drop_all();
|
|
||||||
spdlog::create<spdlog::sinks::null_sink_mt>(tested_logger_name);
|
|
||||||
spdlog::create<spdlog::sinks::null_sink_mt>(tested_logger_name2);
|
|
||||||
spdlog::drop_all();
|
|
||||||
REQUIRE_FALSE(spdlog::get(tested_logger_name));
|
|
||||||
REQUIRE_FALSE(spdlog::get(tested_logger_name2));
|
|
||||||
REQUIRE_FALSE(spdlog::default_logger());
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_CASE("drop non existing", "[registry]") {
|
|
||||||
spdlog::drop_all();
|
|
||||||
spdlog::create<spdlog::sinks::null_sink_mt>(tested_logger_name);
|
|
||||||
spdlog::drop("some_name");
|
|
||||||
REQUIRE_FALSE(spdlog::get("some_name"));
|
|
||||||
REQUIRE(spdlog::get(tested_logger_name));
|
|
||||||
spdlog::drop_all();
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_CASE("default logger", "[registry]") {
|
|
||||||
spdlog::drop_all();
|
|
||||||
spdlog::set_default_logger(spdlog::null_logger_st(tested_logger_name));
|
|
||||||
REQUIRE(spdlog::get(tested_logger_name) == spdlog::default_logger());
|
|
||||||
spdlog::drop_all();
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_CASE("set_default_logger(nullptr)", "[registry]") {
|
|
||||||
spdlog::set_default_logger(nullptr);
|
|
||||||
REQUIRE_FALSE(spdlog::default_logger());
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_CASE("disable automatic registration", "[registry]") {
|
|
||||||
// set some global parameters
|
|
||||||
spdlog::level level = spdlog::level::warn;
|
|
||||||
spdlog::set_level(level);
|
|
||||||
// but disable automatic registration
|
|
||||||
spdlog::set_automatic_registration(false);
|
|
||||||
auto logger1 = spdlog::create<spdlog::sinks::daily_file_sink_st>(tested_logger_name, SPDLOG_FILENAME_T("filename"), 11, 59);
|
|
||||||
auto logger2 = spdlog::create_async<spdlog::sinks::stdout_color_sink_mt>(tested_logger_name2);
|
|
||||||
// loggers should not be part of the registry
|
|
||||||
REQUIRE_FALSE(spdlog::get(tested_logger_name));
|
|
||||||
REQUIRE_FALSE(spdlog::get(tested_logger_name2));
|
|
||||||
// but make sure they are still initialized according to global defaults
|
|
||||||
REQUIRE(logger1->log_level() == level);
|
|
||||||
REQUIRE(logger2->log_level() == level);
|
|
||||||
spdlog::set_level(spdlog::level::info);
|
|
||||||
spdlog::set_automatic_registration(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_CASE("get(const char* name)", "[registry]") {
|
|
||||||
spdlog::drop_all();
|
|
||||||
spdlog::create<spdlog::sinks::null_sink_mt>(tested_logger_name);
|
|
||||||
REQUIRE(spdlog::get(tested_logger_name) != nullptr);
|
|
||||||
spdlog::drop_all();
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_CASE("get(std::string_view name)", "[registry]") {
|
|
||||||
spdlog::drop_all();
|
|
||||||
spdlog::create<spdlog::sinks::null_sink_mt>(tested_logger_name);
|
|
||||||
REQUIRE(spdlog::get(std::string_view(tested_logger_name)) != nullptr);
|
|
||||||
spdlog::drop_all();
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_CASE("get(std::string name)", "[registry]") {
|
|
||||||
spdlog::drop_all();
|
|
||||||
spdlog::create<spdlog::sinks::null_sink_mt>(tested_logger_name);
|
|
||||||
REQUIRE(spdlog::get(std::string(tested_logger_name)) != nullptr);
|
|
||||||
spdlog::drop_all();
|
|
||||||
}
|
|
Loading…
Reference in New Issue