stdout_loggers now thread safe across all instances (share same stdout_sink_mt)

This commit is contained in:
gabime 2015-04-18 01:47:30 +03:00
parent d0874afe3a
commit 2a0d00929c
2 changed files with 17 additions and 5 deletions

View File

@ -72,22 +72,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 create<spdlog::sinks::stdout_sink_mt>(logger_name); 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 create<spdlog::sinks::stdout_sink_st>(logger_name); 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 create<spdlog::sinks::stderr_sink_mt>(logger_name); 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 create<spdlog::sinks::stderr_sink_st>(logger_name); return details::registry::instance().create(logger_name, spdlog::sinks::stderr_sink_st::instance());
} }
#ifdef __linux__ #ifdef __linux__

View File

@ -37,9 +37,14 @@ namespace sinks
template <class Mutex> template <class Mutex>
class stdout_sink : public ostream_sink<Mutex> class stdout_sink : public ostream_sink<Mutex>
{ {
using MyType = stdout_sink<Mutex>;
public: public:
stdout_sink() : ostream_sink<Mutex>(std::cout, true) {} stdout_sink() : ostream_sink<Mutex>(std::cout, true) {}
static std::shared_ptr<MyType> instance()
{
static std::shared_ptr<MyType> instance = std::make_shared<MyType>();
return instance;
}
}; };
typedef stdout_sink<details::null_mutex> stdout_sink_st; typedef stdout_sink<details::null_mutex> stdout_sink_st;
@ -49,8 +54,15 @@ typedef stdout_sink<std::mutex> stdout_sink_mt;
template <class Mutex> template <class Mutex>
class stderr_sink : public ostream_sink<Mutex> class stderr_sink : public ostream_sink<Mutex>
{ {
using MyType = stderr_sink<Mutex>;
public: public:
stderr_sink() : ostream_sink<Mutex>(std::cerr, true) {} stderr_sink() : ostream_sink<Mutex>(std::cerr, true) {}
static std::shared_ptr<MyType> instance()
{
static std::shared_ptr<MyType> instance = std::make_shared<MyType>();
return instance;
}
}; };
typedef stderr_sink<std::mutex> stderr_sink_mt; typedef stderr_sink<std::mutex> stderr_sink_mt;