Enabled use of stdout from multiple threads.

pull/73/head
Laszlo Kiss 11 years ago
parent e9dbd4c0f9
commit a2b70b046c

@ -36,7 +36,8 @@ template<class It>
inline spdlog::logger::logger(const std::string& logger_name, const It& begin, const It& end) : inline spdlog::logger::logger(const std::string& logger_name, const It& begin, const It& end) :
_name(logger_name), _name(logger_name),
_sinks(begin, end), _sinks(begin, end),
_formatter(std::make_shared<pattern_formatter>("%+")) _formatter(std::make_shared<pattern_formatter>("%+")),
_shared_mux( nullptr )
{ {
// no support under vs2013 for member initialization for std::atomic // 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); _formatter->format(msg);
for (auto &sink : _sinks) for (auto &sink : _sinks)
{
if ( ! _shared_mux )
{
sink->log(msg); sink->log(msg);
} }
else
{
std::lock_guard<std::mutex> lock( *_shared_mux );
sink->log(msg);
}
}
}
inline void spdlog::logger::_set_pattern(const std::string& pattern) inline void spdlog::logger::_set_pattern(const std::string& pattern)
{ {

@ -65,9 +65,11 @@ 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, std::mutex *shared)
{ {
return create<spdlog::sinks::stdout_sink_mt>(logger_name); std::shared_ptr<spdlog::logger> l = create<spdlog::sinks::stdout_sink_mt>(logger_name);
l->set_shared_mux( shared );
return l;
} }
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)
@ -75,9 +77,11 @@ inline std::shared_ptr<spdlog::logger> spdlog::stdout_logger_st(const std::strin
return create<spdlog::sinks::stdout_sink_st>(logger_name); return create<spdlog::sinks::stdout_sink_st>(logger_name);
} }
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, std::mutex *shared)
{ {
return create<spdlog::sinks::stderr_sink_mt>(logger_name); std::shared_ptr<spdlog::logger> l = create<spdlog::sinks::stderr_sink_mt>(logger_name);
l->set_shared_mux( shared );
return l;
} }
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)

@ -33,6 +33,7 @@
#include<vector> #include<vector>
#include<memory> #include<memory>
#include<mutex>
#include "sinks/base_sink.h" #include "sinks/base_sink.h"
#include "common.h" #include "common.h"
@ -107,6 +108,7 @@ public:
void set_pattern(const std::string&); void set_pattern(const std::string&);
void set_formatter(formatter_ptr); void set_formatter(formatter_ptr);
void set_shared_mux( std::mutex *m ) { _shared_mux = m; }
protected: protected:
virtual void _log_msg(details::log_msg&); virtual void _log_msg(details::log_msg&);
@ -124,6 +126,7 @@ protected:
std::vector<sink_ptr> _sinks; std::vector<sink_ptr> _sinks;
formatter_ptr _formatter; formatter_ptr _formatter;
std::atomic_int _level; std::atomic_int _level;
std::mutex *_shared_mux;
}; };
} }

@ -29,6 +29,7 @@
#pragma once #pragma once
#include <mutex>
#include "common.h" #include "common.h"
#include "logger.h" #include "logger.h"
@ -93,9 +94,9 @@ std::shared_ptr<logger> daily_logger_st(const std::string& logger_name, const st
// //
// Create stdout/stderr loggers // Create stdout/stderr loggers
// //
std::shared_ptr<logger> stdout_logger_mt(const std::string& logger_name); std::shared_ptr<logger> stdout_logger_mt(const std::string& logger_name, std::mutex *shared = nullptr);
std::shared_ptr<logger> stdout_logger_st(const std::string& logger_name); std::shared_ptr<logger> stdout_logger_st(const std::string& logger_name);
std::shared_ptr<logger> stderr_logger_mt(const std::string& logger_name); std::shared_ptr<logger> stderr_logger_mt(const std::string& logger_name, std::mutex *shared = nullptr);
std::shared_ptr<logger> stderr_logger_st(const std::string& logger_name); std::shared_ptr<logger> stderr_logger_st(const std::string& logger_name);

Loading…
Cancel
Save