Merge branch 'v1.x' of https://github.com/caiazza/spdlog into v1.x

pull/1216/head
Stefano Caiazza 6 years ago
commit 822a51668a

@ -8,7 +8,6 @@
#endif #endif
#include "spdlog/common.h" #include "spdlog/common.h"
#include "spdlog/details/periodic_worker.h"
#include "spdlog/logger.h" #include "spdlog/logger.h"
#include "spdlog/details/pattern_formatter.h" #include "spdlog/details/pattern_formatter.h"

@ -9,6 +9,7 @@
// This class is thread safe // This class is thread safe
#include "spdlog/common.h" #include "spdlog/common.h"
#include "spdlog/details/periodic_worker.h"
#include <chrono> #include <chrono>
#include <functional> #include <functional>
@ -27,6 +28,10 @@ class periodic_worker;
class registry class registry
{ {
public: public:
// Default constructor
registry();
~registry() = default;
registry(const registry &) = delete; registry(const registry &) = delete;
registry &operator=(const registry &) = delete; registry &operator=(const registry &) = delete;
@ -79,12 +84,21 @@ public:
void set_automatic_registration(bool automatic_regsistration); void set_automatic_registration(bool automatic_regsistration);
// Factory function to create a new logger and register it in this registry
template<typename Sink, typename... SinkArgs>
std::shared_ptr<spdlog::logger> create(std::string logger_name, 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));
this->initialize_logger(new_logger);
return new_logger;
}
// The unique global instance of the registry, usefult to access it
// from the global scope. NOTE It doesn't make it a singleton
static registry &instance(); static registry &instance();
private: private:
registry();
~registry() = default;
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);
std::mutex logger_map_mutex_, flusher_mutex_; std::mutex logger_map_mutex_, flusher_mutex_;

@ -15,10 +15,8 @@ struct synchronous_factory
template<typename Sink, typename... SinkArgs> template<typename Sink, typename... SinkArgs>
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)...); return details::registry::instance().create<Sink>(
auto new_logger = std::make_shared<spdlog::logger>(std::move(logger_name), std::move(sink)); std::move(logger_name), std::forward<SinkArgs>(args)...);
details::registry::instance().initialize_logger(new_logger);
return new_logger;
} }
}; };
} // namespace spdlog } // namespace spdlog

@ -114,3 +114,22 @@ TEST_CASE("disable automatic registration", "[registry]")
spdlog::set_level(spdlog::level::info); spdlog::set_level(spdlog::level::info);
spdlog::set_automatic_registration(true); spdlog::set_automatic_registration(true);
} }
TEST_CASE("concrete registry and global instance", "[registry]")
{
spdlog::details::registry Local{};
auto& Global = spdlog::details::registry::instance();
REQUIRE((&Global != &Local));
SECTION("register drop on local registry")
{
Local.drop_all();
auto sink = std::make_shared<spdlog::sinks::null_sink_mt>();
auto new_logger = std::make_shared<spdlog::logger>(tested_logger_name, std::move(sink));
Local.initialize_logger(new_logger);
REQUIRE(Local.get(tested_logger_name) != nullptr);
}
}

Loading…
Cancel
Save