mirror of
https://github.com/gabime/spdlog.git
synced 2025-01-15 18:22:07 +08:00
wip1 - renamed to context
This commit is contained in:
parent
9d42415114
commit
365eb899ed
@ -162,7 +162,7 @@ set(SPDLOG_HEADERS
|
|||||||
"include/spdlog/details/null_mutex.h"
|
"include/spdlog/details/null_mutex.h"
|
||||||
"include/spdlog/details/os.h"
|
"include/spdlog/details/os.h"
|
||||||
"include/spdlog/details/periodic_worker.h"
|
"include/spdlog/details/periodic_worker.h"
|
||||||
"include/spdlog/details/registry.h"
|
"include/spdlog/details/context.h"
|
||||||
"include/spdlog/details/synchronous_factory.h"
|
"include/spdlog/details/synchronous_factory.h"
|
||||||
"include/spdlog/details/thread_pool.h"
|
"include/spdlog/details/thread_pool.h"
|
||||||
"include/spdlog/fmt/bin_to_hex.h"
|
"include/spdlog/fmt/bin_to_hex.h"
|
||||||
@ -203,7 +203,7 @@ set(SPDLOG_SRCS
|
|||||||
"src/details/log_msg.cpp"
|
"src/details/log_msg.cpp"
|
||||||
"src/details/log_msg_buffer.cpp"
|
"src/details/log_msg_buffer.cpp"
|
||||||
"src/details/periodic_worker.cpp"
|
"src/details/periodic_worker.cpp"
|
||||||
"src/details/registry.cpp"
|
"src/details/context.cpp"
|
||||||
"src/details/thread_pool.cpp"
|
"src/details/thread_pool.cpp"
|
||||||
"src/sinks/base_sink.cpp"
|
"src/sinks/base_sink.cpp"
|
||||||
"src/sinks/basic_file_sink.cpp"
|
"src/sinks/basic_file_sink.cpp"
|
||||||
|
@ -73,10 +73,6 @@ int main(int, char *[]) {
|
|||||||
file_events_example();
|
file_events_example();
|
||||||
replace_default_logger_example();
|
replace_default_logger_example();
|
||||||
|
|
||||||
// Flush all *registered* loggers using a worker thread every 3 seconds.
|
|
||||||
// note: registered loggers *must* be thread safe for this to work correctly!
|
|
||||||
spdlog::flush_every(std::chrono::seconds(3));
|
|
||||||
|
|
||||||
// Apply some function on all registered loggers
|
// Apply some function on all registered loggers
|
||||||
spdlog::apply_all([&](std::shared_ptr<spdlog::logger> l) { l->info("End of example."); });
|
spdlog::apply_all([&](std::shared_ptr<spdlog::logger> l) { l->info("End of example."); });
|
||||||
|
|
||||||
@ -192,10 +188,6 @@ void trace_example() {
|
|||||||
SPDLOG_TRACE("Some trace message.. {} ,{}", 1, 3.23);
|
SPDLOG_TRACE("Some trace message.. {} ,{}", 1, 3.23);
|
||||||
// debug from default logger
|
// debug from default logger
|
||||||
SPDLOG_DEBUG("Some debug message.. {} ,{}", 1, 3.23);
|
SPDLOG_DEBUG("Some debug message.. {} ,{}", 1, 3.23);
|
||||||
|
|
||||||
// trace from logger object
|
|
||||||
auto logger = spdlog::get("file_logger");
|
|
||||||
SPDLOG_LOGGER_TRACE(logger, "another trace message");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// stopwatch example
|
// stopwatch example
|
||||||
|
@ -19,7 +19,7 @@
|
|||||||
#include <mutex>
|
#include <mutex>
|
||||||
|
|
||||||
#include "./async_logger.h"
|
#include "./async_logger.h"
|
||||||
#include "./details/registry.h"
|
#include "./details/context.h"
|
||||||
#include "./details/thread_pool.h"
|
#include "./details/thread_pool.h"
|
||||||
|
|
||||||
namespace spdlog {
|
namespace spdlog {
|
||||||
@ -35,7 +35,7 @@ template <async_overflow_policy OverflowPolicy = async_overflow_policy::block>
|
|||||||
struct async_factory_impl {
|
struct async_factory_impl {
|
||||||
template <typename Sink, typename... SinkArgs>
|
template <typename Sink, typename... SinkArgs>
|
||||||
static std::shared_ptr<async_logger> create(std::string logger_name, SinkArgs &&...args) {
|
static std::shared_ptr<async_logger> create(std::string logger_name, SinkArgs &&...args) {
|
||||||
auto ®istry_inst = details::registry::instance();
|
auto ®istry_inst = details::context::instance();
|
||||||
|
|
||||||
// create global thread pool if not already exists
|
// create global thread pool if not already exists
|
||||||
auto &mutex = registry_inst.tp_mutex();
|
auto &mutex = registry_inst.tp_mutex();
|
||||||
@ -72,7 +72,7 @@ inline void init_thread_pool(size_t q_size,
|
|||||||
std::function<void()> on_thread_start,
|
std::function<void()> on_thread_start,
|
||||||
std::function<void()> on_thread_stop) {
|
std::function<void()> on_thread_stop) {
|
||||||
auto tp = std::make_shared<details::thread_pool>(q_size, thread_count, on_thread_start, on_thread_stop);
|
auto tp = std::make_shared<details::thread_pool>(q_size, thread_count, on_thread_start, on_thread_stop);
|
||||||
details::registry::instance().set_tp(std::move(tp));
|
details::context::instance().set_tp(std::move(tp));
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void init_thread_pool(size_t q_size, size_t thread_count, std::function<void()> on_thread_start) {
|
inline void init_thread_pool(size_t q_size, size_t thread_count, std::function<void()> on_thread_start) {
|
||||||
@ -84,5 +84,5 @@ inline void init_thread_pool(size_t q_size, size_t thread_count) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// get the global thread pool.
|
// get the global thread pool.
|
||||||
inline std::shared_ptr<spdlog::details::thread_pool> thread_pool() { return details::registry::instance().get_tp(); }
|
inline std::shared_ptr<spdlog::details::thread_pool> thread_pool() { return details::context::instance().get_tp(); }
|
||||||
} // namespace spdlog
|
} // namespace spdlog
|
||||||
|
@ -2,8 +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 "../details/context.h"
|
||||||
#include "../details/os.h"
|
#include "../details/os.h"
|
||||||
#include "../details/registry.h"
|
|
||||||
#include "./helpers.h"
|
#include "./helpers.h"
|
||||||
|
|
||||||
//
|
//
|
||||||
|
@ -25,19 +25,15 @@ class logger;
|
|||||||
namespace details {
|
namespace details {
|
||||||
class thread_pool;
|
class thread_pool;
|
||||||
|
|
||||||
class SPDLOG_API registry {
|
class SPDLOG_API context {
|
||||||
public:
|
public:
|
||||||
using log_levels = std::unordered_map<std::string, level>;
|
using log_levels = std::unordered_map<std::string, level>;
|
||||||
|
|
||||||
static registry &instance();
|
static context &instance();
|
||||||
registry(const registry &) = delete;
|
context(const context &) = delete;
|
||||||
registry &operator=(const registry &) = delete;
|
context &operator=(const context &) = delete;
|
||||||
|
|
||||||
void register_logger(std::shared_ptr<logger> new_logger);
|
|
||||||
void initialize_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();
|
std::shared_ptr<logger> default_logger();
|
||||||
|
|
||||||
// Return raw ptr to the default logger.
|
// Return raw ptr to the default logger.
|
||||||
@ -62,18 +58,6 @@ public:
|
|||||||
|
|
||||||
void flush_on(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 set_error_handler(err_handler handler);
|
||||||
|
|
||||||
void apply_all(const std::function<void(const std::shared_ptr<logger>)> &fun);
|
void apply_all(const std::function<void(const std::shared_ptr<logger>)> &fun);
|
||||||
@ -97,8 +81,8 @@ public:
|
|||||||
void apply_logger_env_levels(std::shared_ptr<logger> new_logger);
|
void apply_logger_env_levels(std::shared_ptr<logger> new_logger);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
registry();
|
context();
|
||||||
~registry();
|
~context();
|
||||||
|
|
||||||
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);
|
@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "./registry.h"
|
#include "./context.h"
|
||||||
|
|
||||||
namespace spdlog {
|
namespace spdlog {
|
||||||
|
|
||||||
@ -15,7 +15,7 @@ struct synchronous_factory {
|
|||||||
static std::shared_ptr<spdlog::logger> create(std::string logger_name, SinkArgs &&...args) {
|
static std::shared_ptr<spdlog::logger> create(std::string logger_name, SinkArgs &&...args) {
|
||||||
auto sink = std::make_shared<Sink>(std::forward<SinkArgs>(args)...);
|
auto sink = std::make_shared<Sink>(std::forward<SinkArgs>(args)...);
|
||||||
auto new_logger = std::make_shared<spdlog::logger>(std::move(logger_name), std::move(sink));
|
auto new_logger = std::make_shared<spdlog::logger>(std::move(logger_name), std::move(sink));
|
||||||
details::registry::instance().initialize_logger(new_logger);
|
details::context::instance().initialize_logger(new_logger);
|
||||||
return new_logger;
|
return new_logger;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -16,7 +16,7 @@
|
|||||||
#include <string_view>
|
#include <string_view>
|
||||||
|
|
||||||
#include "./common.h"
|
#include "./common.h"
|
||||||
#include "./details/registry.h"
|
#include "./details/context.h"
|
||||||
#include "./details/synchronous_factory.h"
|
#include "./details/synchronous_factory.h"
|
||||||
#include "./logger.h"
|
#include "./logger.h"
|
||||||
|
|
||||||
@ -71,13 +71,6 @@ SPDLOG_API bool should_log(level level);
|
|||||||
// Set global flush level
|
// Set global flush level
|
||||||
SPDLOG_API void flush_on(level level);
|
SPDLOG_API void flush_on(level level);
|
||||||
|
|
||||||
// Start/Restart a periodic flusher thread
|
|
||||||
// Warning: Use only if all your loggers are thread safe!
|
|
||||||
template <typename Rep, typename Period>
|
|
||||||
inline void flush_every(std::chrono::duration<Rep, Period> interval) {
|
|
||||||
details::registry::instance().flush_every(interval);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set global error handler
|
// Set global error handler
|
||||||
SPDLOG_API void set_error_handler(void (*handler)(const std::string &msg));
|
SPDLOG_API void set_error_handler(void (*handler)(const std::string &msg));
|
||||||
|
|
||||||
|
@ -8,7 +8,7 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
#include "spdlog/details/registry.h"
|
#include "spdlog/details/context.h"
|
||||||
|
|
||||||
namespace spdlog {
|
namespace spdlog {
|
||||||
namespace cfg {
|
namespace cfg {
|
||||||
@ -91,7 +91,7 @@ void load_levels(const std::string &input) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
details::registry::instance().set_levels(std::move(levels), global_level_found ? &global_level : nullptr);
|
details::context::instance().set_levels(std::move(levels), global_level_found ? &global_level : nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace helpers
|
} // namespace helpers
|
||||||
|
@ -1,9 +1,8 @@
|
|||||||
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
|
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
|
||||||
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
||||||
|
|
||||||
#include "spdlog/details/registry.h"
|
|
||||||
|
|
||||||
#include "spdlog/common.h"
|
#include "spdlog/common.h"
|
||||||
|
#include "spdlog/details/context.h"
|
||||||
#include "spdlog/logger.h"
|
#include "spdlog/logger.h"
|
||||||
#include "spdlog/pattern_formatter.h"
|
#include "spdlog/pattern_formatter.h"
|
||||||
|
|
||||||
@ -27,7 +26,7 @@ static constexpr size_t small_map_threshold = 10;
|
|||||||
namespace spdlog {
|
namespace spdlog {
|
||||||
namespace details {
|
namespace details {
|
||||||
|
|
||||||
registry::registry()
|
context::context()
|
||||||
: formatter_(new pattern_formatter()) {
|
: formatter_(new pattern_formatter()) {
|
||||||
#ifndef SPDLOG_DISABLE_DEFAULT_LOGGER
|
#ifndef SPDLOG_DISABLE_DEFAULT_LOGGER
|
||||||
// create default logger (ansicolor_stdout_sink_mt or wincolor_stdout_sink_mt in windows).
|
// create default logger (ansicolor_stdout_sink_mt or wincolor_stdout_sink_mt in windows).
|
||||||
@ -43,14 +42,9 @@ registry::registry()
|
|||||||
#endif // SPDLOG_DISABLE_DEFAULT_LOGGER
|
#endif // SPDLOG_DISABLE_DEFAULT_LOGGER
|
||||||
}
|
}
|
||||||
|
|
||||||
registry::~registry() = default;
|
context::~context() = default;
|
||||||
|
|
||||||
void registry::register_logger(std::shared_ptr<logger> new_logger) {
|
void context::initialize_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_);
|
std::lock_guard<std::mutex> lock(logger_map_mutex_);
|
||||||
new_logger->set_formatter(formatter_->clone());
|
new_logger->set_formatter(formatter_->clone());
|
||||||
|
|
||||||
@ -70,41 +64,7 @@ void registry::initialize_logger(std::shared_ptr<logger> new_logger) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// if the map is small do a sequential search, otherwise use the standard find()
|
std::shared_ptr<logger> context::default_logger() {
|
||||||
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_);
|
std::lock_guard<std::mutex> lock(logger_map_mutex_);
|
||||||
return default_logger_;
|
return default_logger_;
|
||||||
}
|
}
|
||||||
@ -113,11 +73,11 @@ std::shared_ptr<logger> registry::default_logger() {
|
|||||||
// To be used directly by the spdlog default api (e.g. spdlog::info)
|
// 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().
|
// 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.
|
// 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(); }
|
logger *context::get_default_raw() const { return default_logger_.get(); }
|
||||||
|
|
||||||
// set default logger.
|
// set default logger.
|
||||||
// default logger is stored in default_logger_ (for faster retrieval) and in the loggers_ map.
|
// 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) {
|
void context::set_default_logger(std::shared_ptr<logger> new_default_logger) {
|
||||||
std::lock_guard<std::mutex> lock(logger_map_mutex_);
|
std::lock_guard<std::mutex> lock(logger_map_mutex_);
|
||||||
if (new_default_logger != nullptr) {
|
if (new_default_logger != nullptr) {
|
||||||
loggers_[new_default_logger->name()] = new_default_logger;
|
loggers_[new_default_logger->name()] = new_default_logger;
|
||||||
@ -125,18 +85,18 @@ void registry::set_default_logger(std::shared_ptr<logger> new_default_logger) {
|
|||||||
default_logger_ = std::move(new_default_logger);
|
default_logger_ = std::move(new_default_logger);
|
||||||
}
|
}
|
||||||
|
|
||||||
void registry::set_tp(std::shared_ptr<thread_pool> tp) {
|
void context::set_tp(std::shared_ptr<thread_pool> tp) {
|
||||||
std::lock_guard<std::recursive_mutex> lock(tp_mutex_);
|
std::lock_guard<std::recursive_mutex> lock(tp_mutex_);
|
||||||
tp_ = std::move(tp);
|
tp_ = std::move(tp);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<thread_pool> registry::get_tp() {
|
std::shared_ptr<thread_pool> context::get_tp() {
|
||||||
std::lock_guard<std::recursive_mutex> lock(tp_mutex_);
|
std::lock_guard<std::recursive_mutex> lock(tp_mutex_);
|
||||||
return tp_;
|
return tp_;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set global formatter. Each sink in each logger will get a clone of this object
|
// Set global formatter. Each sink in each logger will get a clone of this object
|
||||||
void registry::set_formatter(std::unique_ptr<formatter> formatter) {
|
void context::set_formatter(std::unique_ptr<formatter> formatter) {
|
||||||
std::lock_guard<std::mutex> lock(logger_map_mutex_);
|
std::lock_guard<std::mutex> lock(logger_map_mutex_);
|
||||||
formatter_ = std::move(formatter);
|
formatter_ = std::move(formatter);
|
||||||
for (auto &l : loggers_) {
|
for (auto &l : loggers_) {
|
||||||
@ -144,7 +104,7 @@ void registry::set_formatter(std::unique_ptr<formatter> formatter) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void registry::set_level(level level) {
|
void context::set_level(level level) {
|
||||||
std::lock_guard<std::mutex> lock(logger_map_mutex_);
|
std::lock_guard<std::mutex> lock(logger_map_mutex_);
|
||||||
for (auto &l : loggers_) {
|
for (auto &l : loggers_) {
|
||||||
l.second->set_level(level);
|
l.second->set_level(level);
|
||||||
@ -152,7 +112,7 @@ void registry::set_level(level level) {
|
|||||||
global_log_level_ = level;
|
global_log_level_ = level;
|
||||||
}
|
}
|
||||||
|
|
||||||
void registry::flush_on(level level) {
|
void context::flush_on(level level) {
|
||||||
std::lock_guard<std::mutex> lock(logger_map_mutex_);
|
std::lock_guard<std::mutex> lock(logger_map_mutex_);
|
||||||
for (auto &l : loggers_) {
|
for (auto &l : loggers_) {
|
||||||
l.second->flush_on(level);
|
l.second->flush_on(level);
|
||||||
@ -160,7 +120,7 @@ void registry::flush_on(level level) {
|
|||||||
flush_level_ = level;
|
flush_level_ = level;
|
||||||
}
|
}
|
||||||
|
|
||||||
void registry::set_error_handler(err_handler handler) {
|
void context::set_error_handler(err_handler handler) {
|
||||||
std::lock_guard<std::mutex> lock(logger_map_mutex_);
|
std::lock_guard<std::mutex> lock(logger_map_mutex_);
|
||||||
for (auto &l : loggers_) {
|
for (auto &l : loggers_) {
|
||||||
l.second->set_error_handler(handler);
|
l.second->set_error_handler(handler);
|
||||||
@ -168,21 +128,21 @@ void registry::set_error_handler(err_handler handler) {
|
|||||||
err_handler_ = std::move(handler);
|
err_handler_ = std::move(handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
void registry::apply_all(const std::function<void(const std::shared_ptr<logger>)> &fun) {
|
void context::apply_all(const std::function<void(const std::shared_ptr<logger>)> &fun) {
|
||||||
std::lock_guard<std::mutex> lock(logger_map_mutex_);
|
std::lock_guard<std::mutex> lock(logger_map_mutex_);
|
||||||
for (auto &l : loggers_) {
|
for (auto &l : loggers_) {
|
||||||
fun(l.second);
|
fun(l.second);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void registry::flush_all() {
|
void context::flush_all() {
|
||||||
std::lock_guard<std::mutex> lock(logger_map_mutex_);
|
std::lock_guard<std::mutex> lock(logger_map_mutex_);
|
||||||
for (auto &l : loggers_) {
|
for (auto &l : loggers_) {
|
||||||
l.second->flush();
|
l.second->flush();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void registry::drop(const std::string &logger_name) {
|
void context::drop(const std::string &logger_name) {
|
||||||
std::lock_guard<std::mutex> lock(logger_map_mutex_);
|
std::lock_guard<std::mutex> lock(logger_map_mutex_);
|
||||||
auto is_default_logger = default_logger_ && default_logger_->name() == logger_name;
|
auto is_default_logger = default_logger_ && default_logger_->name() == logger_name;
|
||||||
loggers_.erase(logger_name);
|
loggers_.erase(logger_name);
|
||||||
@ -191,14 +151,14 @@ void registry::drop(const std::string &logger_name) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void registry::drop_all() {
|
void context::drop_all() {
|
||||||
std::lock_guard<std::mutex> lock(logger_map_mutex_);
|
std::lock_guard<std::mutex> lock(logger_map_mutex_);
|
||||||
loggers_.clear();
|
loggers_.clear();
|
||||||
default_logger_.reset();
|
default_logger_.reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
// clean all resources and threads started by the registry
|
// clean all resources and threads started by the registry
|
||||||
void registry::shutdown() {
|
void context::shutdown() {
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(flusher_mutex_);
|
std::lock_guard<std::mutex> lock(flusher_mutex_);
|
||||||
periodic_flusher_.reset();
|
periodic_flusher_.reset();
|
||||||
@ -212,14 +172,14 @@ void registry::shutdown() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::recursive_mutex ®istry::tp_mutex() { return tp_mutex_; }
|
std::recursive_mutex &context::tp_mutex() { return tp_mutex_; }
|
||||||
|
|
||||||
void registry::set_automatic_registration(bool automatic_registration) {
|
void context::set_automatic_registration(bool automatic_registration) {
|
||||||
std::lock_guard<std::mutex> lock(logger_map_mutex_);
|
std::lock_guard<std::mutex> lock(logger_map_mutex_);
|
||||||
automatic_registration_ = automatic_registration;
|
automatic_registration_ = automatic_registration;
|
||||||
}
|
}
|
||||||
|
|
||||||
void registry::set_levels(log_levels levels, level *global_level) {
|
void context::set_levels(log_levels levels, level *global_level) {
|
||||||
std::lock_guard<std::mutex> lock(logger_map_mutex_);
|
std::lock_guard<std::mutex> lock(logger_map_mutex_);
|
||||||
log_levels_ = std::move(levels);
|
log_levels_ = std::move(levels);
|
||||||
auto global_level_requested = global_level != nullptr;
|
auto global_level_requested = global_level != nullptr;
|
||||||
@ -235,25 +195,25 @@ void registry::set_levels(log_levels levels, level *global_level) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
registry ®istry::instance() {
|
context &context::instance() {
|
||||||
static registry s_instance;
|
static context s_instance;
|
||||||
return s_instance;
|
return s_instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
void registry::apply_logger_env_levels(std::shared_ptr<logger> new_logger) {
|
void context::apply_logger_env_levels(std::shared_ptr<logger> new_logger) {
|
||||||
std::lock_guard<std::mutex> lock(logger_map_mutex_);
|
std::lock_guard<std::mutex> lock(logger_map_mutex_);
|
||||||
auto it = log_levels_.find(new_logger->name());
|
auto it = log_levels_.find(new_logger->name());
|
||||||
auto new_level = it != log_levels_.end() ? it->second : global_log_level_;
|
auto new_level = it != log_levels_.end() ? it->second : global_log_level_;
|
||||||
new_logger->set_level(new_level);
|
new_logger->set_level(new_level);
|
||||||
}
|
}
|
||||||
|
|
||||||
void registry::throw_if_exists_(const std::string &logger_name) {
|
void context::throw_if_exists_(const std::string &logger_name) {
|
||||||
if (loggers_.find(logger_name) != loggers_.end()) {
|
if (loggers_.find(logger_name) != loggers_.end()) {
|
||||||
throw_spdlog_ex("logger with name '" + logger_name + "' already exists");
|
throw_spdlog_ex("logger with name '" + logger_name + "' already exists");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void registry::register_logger_(std::shared_ptr<logger> new_logger) {
|
void context::register_logger_(std::shared_ptr<logger> new_logger) {
|
||||||
auto logger_name = new_logger->name();
|
auto logger_name = new_logger->name();
|
||||||
throw_if_exists_(logger_name);
|
throw_if_exists_(logger_name);
|
||||||
loggers_[logger_name] = std::move(new_logger);
|
loggers_[logger_name] = std::move(new_logger);
|
@ -12,16 +12,10 @@
|
|||||||
|
|
||||||
namespace spdlog {
|
namespace spdlog {
|
||||||
|
|
||||||
void initialize_logger(std::shared_ptr<logger> logger) { details::registry::instance().initialize_logger(std::move(logger)); }
|
void initialize_logger(std::shared_ptr<logger> logger) { details::context::instance().initialize_logger(std::move(logger)); }
|
||||||
|
|
||||||
std::shared_ptr<logger> get(const std::string &name) { return details::registry::instance().get(name); }
|
|
||||||
|
|
||||||
std::shared_ptr<logger> get(std::string_view name) { return details::registry::instance().get(name); }
|
|
||||||
|
|
||||||
std::shared_ptr<logger> get(const char *name) { return details::registry::instance().get(name); }
|
|
||||||
|
|
||||||
void set_formatter(std::unique_ptr<spdlog::formatter> formatter) {
|
void set_formatter(std::unique_ptr<spdlog::formatter> formatter) {
|
||||||
details::registry::instance().set_formatter(std::move(formatter));
|
details::context::instance().set_formatter(std::move(formatter));
|
||||||
}
|
}
|
||||||
|
|
||||||
void set_pattern(std::string pattern, pattern_time_type time_type) {
|
void set_pattern(std::string pattern, pattern_time_type time_type) {
|
||||||
@ -32,35 +26,33 @@ level get_level() { return default_logger_raw()->log_level(); }
|
|||||||
|
|
||||||
bool should_log(level level) { return default_logger_raw()->should_log(level); }
|
bool should_log(level level) { return default_logger_raw()->should_log(level); }
|
||||||
|
|
||||||
void set_level(level level) { details::registry::instance().set_level(level); }
|
void set_level(level level) { details::context::instance().set_level(level); }
|
||||||
|
|
||||||
void flush_on(level level) { details::registry::instance().flush_on(level); }
|
void flush_on(level level) { details::context::instance().flush_on(level); }
|
||||||
|
|
||||||
void set_error_handler(void (*handler)(const std::string &msg)) { details::registry::instance().set_error_handler(handler); }
|
void set_error_handler(void (*handler)(const std::string &msg)) { details::context::instance().set_error_handler(handler); }
|
||||||
|
|
||||||
void register_logger(std::shared_ptr<logger> logger) { details::registry::instance().register_logger(std::move(logger)); }
|
void apply_all(const std::function<void(std::shared_ptr<logger>)> &fun) { details::context::instance().apply_all(fun); }
|
||||||
|
|
||||||
void apply_all(const std::function<void(std::shared_ptr<logger>)> &fun) { details::registry::instance().apply_all(fun); }
|
void drop(const std::string &name) { details::context::instance().drop(name); }
|
||||||
|
|
||||||
void drop(const std::string &name) { details::registry::instance().drop(name); }
|
void drop_all() { details::context::instance().drop_all(); }
|
||||||
|
|
||||||
void drop_all() { details::registry::instance().drop_all(); }
|
void shutdown() { details::context::instance().shutdown(); }
|
||||||
|
|
||||||
void shutdown() { details::registry::instance().shutdown(); }
|
|
||||||
|
|
||||||
void set_automatic_registration(bool automatic_registration) {
|
void set_automatic_registration(bool automatic_registration) {
|
||||||
details::registry::instance().set_automatic_registration(automatic_registration);
|
details::context::instance().set_automatic_registration(automatic_registration);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<spdlog::logger> default_logger() { return details::registry::instance().default_logger(); }
|
std::shared_ptr<spdlog::logger> default_logger() { return details::context::instance().default_logger(); }
|
||||||
|
|
||||||
spdlog::logger *default_logger_raw() { return details::registry::instance().get_default_raw(); }
|
spdlog::logger *default_logger_raw() { return details::context::instance().get_default_raw(); }
|
||||||
|
|
||||||
void set_default_logger(std::shared_ptr<spdlog::logger> default_logger) {
|
void set_default_logger(std::shared_ptr<spdlog::logger> default_logger) {
|
||||||
details::registry::instance().set_default_logger(std::move(default_logger));
|
details::context::instance().set_default_logger(std::move(default_logger));
|
||||||
}
|
}
|
||||||
|
|
||||||
void apply_logger_env_levels(std::shared_ptr<logger> logger) {
|
void apply_logger_env_levels(std::shared_ptr<logger> logger) {
|
||||||
details::registry::instance().apply_logger_env_levels(std::move(logger));
|
details::context::instance().apply_logger_env_levels(std::move(logger));
|
||||||
}
|
}
|
||||||
} // namespace spdlog
|
} // namespace spdlog
|
||||||
|
@ -34,7 +34,6 @@ set(SPDLOG_UTESTS_SOURCES
|
|||||||
test_misc.cpp
|
test_misc.cpp
|
||||||
test_pattern_formatter.cpp
|
test_pattern_formatter.cpp
|
||||||
test_async.cpp
|
test_async.cpp
|
||||||
test_registry.cpp
|
|
||||||
test_macros.cpp
|
test_macros.cpp
|
||||||
utils.cpp
|
utils.cpp
|
||||||
main.cpp
|
main.cpp
|
||||||
|
@ -89,16 +89,6 @@ TEST_CASE("flush", "[async]") {
|
|||||||
REQUIRE(test_sink->flush_counter() == 1);
|
REQUIRE(test_sink->flush_counter() == 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("async periodic flush", "[async]") {
|
|
||||||
auto logger = spdlog::create_async<spdlog::sinks::test_sink_mt>("as");
|
|
||||||
auto test_sink = std::static_pointer_cast<spdlog::sinks::test_sink_mt>(logger->sinks()[0]);
|
|
||||||
|
|
||||||
spdlog::flush_every(std::chrono::seconds(1));
|
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(1700));
|
|
||||||
REQUIRE(test_sink->flush_counter() == 1);
|
|
||||||
spdlog::flush_every(std::chrono::seconds(0));
|
|
||||||
spdlog::drop_all();
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_CASE("tp->wait_empty() ", "[async]") {
|
TEST_CASE("tp->wait_empty() ", "[async]") {
|
||||||
auto test_sink = std::make_shared<spdlog::sinks::test_sink_mt>();
|
auto test_sink = std::make_shared<spdlog::sinks::test_sink_mt>();
|
||||||
|
@ -73,18 +73,6 @@ TEST_CASE("to_level_enum", "[convert_to_level_enum]") {
|
|||||||
REQUIRE(spdlog::level_from_str("null") == spdlog::level::off);
|
REQUIRE(spdlog::level_from_str("null") == spdlog::level::off);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("periodic flush", "[periodic_flush]") {
|
|
||||||
using spdlog::sinks::test_sink_mt;
|
|
||||||
auto logger = spdlog::create<test_sink_mt>("periodic_flush");
|
|
||||||
auto test_sink = std::static_pointer_cast<test_sink_mt>(logger->sinks()[0]);
|
|
||||||
|
|
||||||
spdlog::flush_every(std::chrono::seconds(1));
|
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(1250));
|
|
||||||
REQUIRE(test_sink->flush_counter() == 1);
|
|
||||||
spdlog::flush_every(std::chrono::seconds(0));
|
|
||||||
spdlog::drop_all();
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_CASE("clone-logger", "[clone]") {
|
TEST_CASE("clone-logger", "[clone]") {
|
||||||
using spdlog::sinks::test_sink_mt;
|
using spdlog::sinks::test_sink_mt;
|
||||||
auto test_sink = std::make_shared<test_sink_mt>();
|
auto test_sink = std::make_shared<test_sink_mt>();
|
||||||
|
@ -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
Block a user