Add shared registry capability

pull/103/head
Alexandre Petitjean 10 years ago
parent 319a62d73f
commit b34d4d793a

@ -144,12 +144,15 @@ public:
_async_mode = false; _async_mode = false;
} }
static registry_t<Mutex>& instance() static registry_t<Mutex>* instance()
{ {
static registry_t<Mutex> s_instance; static registry_t<Mutex> s_instance;
return s_instance; return &s_instance;
} }
protected:
registry_t<Mutex>() {}
private: private:
void register_logger_impl(std::shared_ptr<logger> logger) void register_logger_impl(std::shared_ptr<logger> logger)
{ {
@ -158,7 +161,6 @@ private:
throw spdlog_ex("logger with name " + logger_name + " already exists"); throw spdlog_ex("logger with name " + logger_name + " already exists");
_loggers[logger->name()] = logger; _loggers[logger->name()] = logger;
} }
registry_t<Mutex>(){}
registry_t<Mutex>(const registry_t<Mutex>&) = delete; registry_t<Mutex>(const registry_t<Mutex>&) = delete;
registry_t<Mutex>& operator=(const registry_t<Mutex>&) = delete; registry_t<Mutex>& operator=(const registry_t<Mutex>&) = delete;
Mutex _mutex; Mutex _mutex;
@ -171,10 +173,14 @@ private:
std::function<void()> _worker_warmup_cb = nullptr; std::function<void()> _worker_warmup_cb = nullptr;
std::chrono::milliseconds _flush_interval_ms; std::chrono::milliseconds _flush_interval_ms;
}; };
#ifdef SPDLOG_NO_REGISTRY_MUTEX
typedef registry_t<spdlog::details::null_mutex> registry; #ifndef SPDLOG_USE_SHARED_REGISTRY
#else # ifdef SPDLOG_NO_REGISTRY_MUTEX
typedef registry_t<std::mutex> registry; typedef registry_t<spdlog::details::null_mutex> registry;
#endif # else
typedef registry_t<std::mutex> registry;
# endif //SPDLOG_NO_REGISTRY_MUTEX
#endif //SPDLOG_USE_SHARED_REGISTRY
} }
} }

@ -0,0 +1,66 @@
/*************************************************************************/
/* spdlog - an extremely fast and easy to use c++11 logging library. */
/* Copyright (c) 2014 Gabi Melman. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#pragma once
#include "registry.h"
namespace spdlog
{
namespace details
{
template <class Mutex> class registry_shared_t : public registry_t<Mutex>
{
public:
static std::shared_ptr<registry_shared_t<Mutex> > instance()
{
if(!_instance)
_instance = std::make_shared<registry_shared_t<Mutex> >();
return _instance;
}
static void set_registry(std::shared_ptr<registry_shared_t<Mutex> > registry)
{
_instance = registry;
}
registry_shared_t<Mutex>() : registry_t<Mutex>() {}
private:
static std::shared_ptr<registry_shared_t<Mutex> > _instance;
};
#ifdef SPDLOG_USE_SHARED_REGISTRY
# ifdef SPDLOG_NO_REGISTRY_MUTEX
typedef registry_shared_t<spdlog::details::null_mutex> registry;
# else
typedef registry_shared_t<std::mutex> registry;
# endif //SPDLOG_NO_REGISTRY_MUTEX
#endif //SPDLOG_USE_SHARED_REGISTRY
template<class Mutex>
std::shared_ptr<registry_shared_t<Mutex> > registry_shared_t<Mutex>::_instance = nullptr;
}
}

@ -28,23 +28,24 @@
// Global registry functions // Global registry functions
// //
#include "registry.h" #include "registry.h"
#include "registry_shared.h"
#include "../sinks/file_sinks.h" #include "../sinks/file_sinks.h"
#include "../sinks/stdout_sinks.h" #include "../sinks/stdout_sinks.h"
#include "../sinks/syslog_sink.h" #include "../sinks/syslog_sink.h"
inline void spdlog::register_logger(std::shared_ptr<logger> logger) inline void spdlog::register_logger(std::shared_ptr<logger> logger)
{ {
return details::registry::instance().register_logger(logger); return details::registry::instance()->register_logger(logger);
} }
inline std::shared_ptr<spdlog::logger> spdlog::get(const std::string& name) inline std::shared_ptr<spdlog::logger> spdlog::get(const std::string& name)
{ {
return details::registry::instance().get(name); return details::registry::instance()->get(name);
} }
inline void spdlog::drop(const std::string &name) inline void spdlog::drop(const std::string &name)
{ {
details::registry::instance().drop(name); details::registry::instance()->drop(name);
} }
// Create multi/single threaded rotating file logger // Create multi/single threaded rotating file logger
@ -72,22 +73,22 @@ inline std::shared_ptr<spdlog::logger> spdlog::daily_logger_st(const std::string
// Create stdout/stderr loggers // Create stdout/stderr loggers
inline std::shared_ptr<spdlog::logger> spdlog::stdout_logger_mt(const std::string& logger_name) inline std::shared_ptr<spdlog::logger> spdlog::stdout_logger_mt(const std::string& logger_name)
{ {
return details::registry::instance().create(logger_name, spdlog::sinks::stdout_sink_mt::instance()); return details::registry::instance()->create(logger_name, spdlog::sinks::stdout_sink_mt::instance());
} }
inline std::shared_ptr<spdlog::logger> spdlog::stdout_logger_st(const std::string& logger_name) inline std::shared_ptr<spdlog::logger> spdlog::stdout_logger_st(const std::string& logger_name)
{ {
return details::registry::instance().create(logger_name, spdlog::sinks::stdout_sink_st::instance()); return details::registry::instance()->create(logger_name, spdlog::sinks::stdout_sink_st::instance());
} }
inline std::shared_ptr<spdlog::logger> spdlog::stderr_logger_mt(const std::string& logger_name) inline std::shared_ptr<spdlog::logger> spdlog::stderr_logger_mt(const std::string& logger_name)
{ {
return details::registry::instance().create(logger_name, spdlog::sinks::stderr_sink_mt::instance()); return details::registry::instance()->create(logger_name, spdlog::sinks::stderr_sink_mt::instance());
} }
inline std::shared_ptr<spdlog::logger> spdlog::stderr_logger_st(const std::string& logger_name) inline std::shared_ptr<spdlog::logger> spdlog::stderr_logger_st(const std::string& logger_name)
{ {
return details::registry::instance().create(logger_name, spdlog::sinks::stderr_sink_st::instance()); return details::registry::instance()->create(logger_name, spdlog::sinks::stderr_sink_st::instance());
} }
#ifdef __linux__ #ifdef __linux__
@ -103,7 +104,7 @@ inline std::shared_ptr<spdlog::logger> spdlog::syslog_logger(const std::string&
inline std::shared_ptr<spdlog::logger> spdlog::create(const std::string& logger_name, spdlog::sinks_init_list sinks) inline std::shared_ptr<spdlog::logger> spdlog::create(const std::string& logger_name, spdlog::sinks_init_list sinks)
{ {
return details::registry::instance().create(logger_name, sinks); return details::registry::instance()->create(logger_name, sinks);
} }
@ -111,44 +112,44 @@ template <typename Sink, typename... Args>
inline std::shared_ptr<spdlog::logger> spdlog::create(const std::string& logger_name, const Args&... args) inline std::shared_ptr<spdlog::logger> spdlog::create(const std::string& logger_name, const Args&... args)
{ {
sink_ptr sink = std::make_shared<Sink>(args...); sink_ptr sink = std::make_shared<Sink>(args...);
return details::registry::instance().create(logger_name, { sink }); return details::registry::instance()->create(logger_name, { sink });
} }
template<class It> template<class It>
inline std::shared_ptr<spdlog::logger> spdlog::create(const std::string& logger_name, const It& sinks_begin, const It& sinks_end) inline std::shared_ptr<spdlog::logger> spdlog::create(const std::string& logger_name, const It& sinks_begin, const It& sinks_end)
{ {
return details::registry::instance().create(logger_name, sinks_begin, sinks_end); return details::registry::instance()->create(logger_name, sinks_begin, sinks_end);
} }
inline void spdlog::set_formatter(spdlog::formatter_ptr f) inline void spdlog::set_formatter(spdlog::formatter_ptr f)
{ {
details::registry::instance().formatter(f); details::registry::instance()->formatter(f);
} }
inline void spdlog::set_pattern(const std::string& format_string) inline void spdlog::set_pattern(const std::string& format_string)
{ {
return details::registry::instance().set_pattern(format_string); return details::registry::instance()->set_pattern(format_string);
} }
inline void spdlog::set_level(level::level_enum log_level) inline void spdlog::set_level(level::level_enum log_level)
{ {
return details::registry::instance().set_level(log_level); return details::registry::instance()->set_level(log_level);
} }
inline void spdlog::set_async_mode(size_t queue_size, const async_overflow_policy overflow_policy, const std::function<void()>& worker_warmup_cb, const std::chrono::milliseconds& flush_interval_ms) inline void spdlog::set_async_mode(size_t queue_size, const async_overflow_policy overflow_policy, const std::function<void()>& worker_warmup_cb, const std::chrono::milliseconds& flush_interval_ms)
{ {
details::registry::instance().set_async_mode(queue_size, overflow_policy, worker_warmup_cb, flush_interval_ms); details::registry::instance()->set_async_mode(queue_size, overflow_policy, worker_warmup_cb, flush_interval_ms);
} }
inline void spdlog::set_sync_mode() inline void spdlog::set_sync_mode()
{ {
details::registry::instance().set_sync_mode(); details::registry::instance()->set_sync_mode();
} }
inline void spdlog::drop_all() inline void spdlog::drop_all()
{ {
details::registry::instance().drop_all(); details::registry::instance()->drop_all();
} }

@ -72,3 +72,9 @@
// Note that upon creating a logger the registry is modified by spdlog.. // Note that upon creating a logger the registry is modified by spdlog..
// #define SPDLOG_NO_REGISTRY_MUTEX // #define SPDLOG_NO_REGISTRY_MUTEX
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// Uncomment to enable use of a shared registry
// #define SPDLOG_USE_SHARED_REGISTRY
///////////////////////////////////////////////////////////////////////////////

Loading…
Cancel
Save