diff --git a/include/spdlog/details/logger_impl.h b/include/spdlog/details/logger_impl.h index 1a920c03..2494e689 100644 --- a/include/spdlog/details/logger_impl.h +++ b/include/spdlog/details/logger_impl.h @@ -36,7 +36,8 @@ template inline spdlog::logger::logger(const std::string& logger_name, const It& begin, const It& end) : _name(logger_name), _sinks(begin, end), - _formatter(std::make_shared("%+")) + _formatter(std::make_shared("%+")), + _shared_mux( nullptr ) { // no support under vs2013 for member initialization for std::atomic @@ -300,8 +301,18 @@ inline void spdlog::logger::_log_msg(details::log_msg& msg) { _formatter->format(msg); for (auto &sink : _sinks) + { + if ( ! _shared_mux ) + { sink->log(msg); } + else + { + std::lock_guard lock( *_shared_mux ); + sink->log(msg); + } + } +} inline void spdlog::logger::_set_pattern(const std::string& pattern) { diff --git a/include/spdlog/details/spdlog_impl.h b/include/spdlog/details/spdlog_impl.h index 34805597..852b99bc 100644 --- a/include/spdlog/details/spdlog_impl.h +++ b/include/spdlog/details/spdlog_impl.h @@ -65,9 +65,11 @@ inline std::shared_ptr spdlog::daily_logger_st(const std::string // Create stdout/stderr loggers -inline std::shared_ptr spdlog::stdout_logger_mt(const std::string& logger_name) +inline std::shared_ptr spdlog::stdout_logger_mt(const std::string& logger_name, std::mutex *shared) { - return create(logger_name); + std::shared_ptr l = create(logger_name); + l->set_shared_mux( shared ); + return l; } inline std::shared_ptr spdlog::stdout_logger_st(const std::string& logger_name) @@ -75,9 +77,11 @@ inline std::shared_ptr spdlog::stdout_logger_st(const std::strin return create(logger_name); } -inline std::shared_ptr spdlog::stderr_logger_mt(const std::string& logger_name) +inline std::shared_ptr spdlog::stderr_logger_mt(const std::string& logger_name, std::mutex *shared) { - return create(logger_name); + std::shared_ptr l = create(logger_name); + l->set_shared_mux( shared ); + return l; } inline std::shared_ptr spdlog::stderr_logger_st(const std::string& logger_name) diff --git a/include/spdlog/logger.h b/include/spdlog/logger.h index cf97fc80..c3dcff65 100644 --- a/include/spdlog/logger.h +++ b/include/spdlog/logger.h @@ -33,6 +33,7 @@ #include #include +#include #include "sinks/base_sink.h" #include "common.h" @@ -107,6 +108,7 @@ public: void set_pattern(const std::string&); void set_formatter(formatter_ptr); + void set_shared_mux( std::mutex *m ) { _shared_mux = m; } protected: virtual void _log_msg(details::log_msg&); @@ -124,6 +126,7 @@ protected: std::vector _sinks; formatter_ptr _formatter; std::atomic_int _level; + std::mutex *_shared_mux; }; } diff --git a/include/spdlog/spdlog.h b/include/spdlog/spdlog.h index cd80a90c..f973d896 100644 --- a/include/spdlog/spdlog.h +++ b/include/spdlog/spdlog.h @@ -29,6 +29,7 @@ #pragma once +#include #include "common.h" #include "logger.h" @@ -93,9 +94,9 @@ std::shared_ptr daily_logger_st(const std::string& logger_name, const st // // Create stdout/stderr loggers // -std::shared_ptr stdout_logger_mt(const std::string& logger_name); +std::shared_ptr stdout_logger_mt(const std::string& logger_name, std::mutex *shared = nullptr); std::shared_ptr stdout_logger_st(const std::string& logger_name); -std::shared_ptr stderr_logger_mt(const std::string& logger_name); +std::shared_ptr stderr_logger_mt(const std::string& logger_name, std::mutex *shared = nullptr); std::shared_ptr stderr_logger_st(const std::string& logger_name);