small fixes in async_helper

This commit is contained in:
gabime 2014-12-05 20:59:38 +02:00
parent 52d02af950
commit 6255180e99
2 changed files with 20 additions and 41 deletions

View File

@ -37,6 +37,7 @@
#include <thread> #include <thread>
#include <atomic> #include <atomic>
#include "../common.h"
#include "../sinks/sink.h" #include "../sinks/sink.h"
#include "./mpmc_bounded_q.h" #include "./mpmc_bounded_q.h"
#include "./log_msg.h" #include "./log_msg.h"
@ -108,38 +109,35 @@ public:
using clock = std::chrono::steady_clock; using clock = std::chrono::steady_clock;
explicit async_log_helper(size_t queue_size); async_log_helper(formatter_ptr formatter, const std::vector<sink_ptr>& sinks, size_t queue_size);
void log(const details::log_msg& msg); void log(const details::log_msg& msg);
//Stop logging and join the back thread //Stop logging and join the back thread
~async_log_helper(); ~async_log_helper();
void add_sink(sink_ptr sink);
void remove_sink(sink_ptr sink_ptr);
void set_formatter(formatter_ptr); void set_formatter(formatter_ptr);
//Wait to remaining items (if any) in the queue to be written and shutdown //Wait to remaining items (if any) in the queue to be written and shutdown
void shutdown(const log_clock::duration& timeout); void shutdown(const log_clock::duration& timeout);
private: private:
std::vector<std::shared_ptr<sinks::sink>> _sinks; std::atomic<bool> _active;
std::atomic<bool> _active; formatter_ptr _formatter;
std::vector<std::shared_ptr<sinks::sink>> _sinks;
q_type _q; q_type _q;
std::thread _worker_thread; std::thread _worker_thread;
std::mutex _mutex;
// last exception thrown from the worker thread // last exception thrown from the worker thread
std::shared_ptr<spdlog_ex> _last_workerthread_ex; std::shared_ptr<spdlog_ex> _last_workerthread_ex;
// worker thread formatter
formatter_ptr _formatter;
// will throw last worker thread exception or if worker thread no active // will throw last worker thread exception or if worker thread no active
void throw_if_bad_worker(); void throw_if_bad_worker();
// worker thread loop // worker thread loop
void thread_loop(); void worker_loop();
// guess how much to sleep if queue is empty/full using last succesful op time as hint // guess how much to sleep if queue is empty/full using last succesful op time as hint
static void sleep_or_yield(const clock::time_point& last_op_time); static void sleep_or_yield(const clock::time_point& last_op_time);
@ -156,11 +154,12 @@ private:
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
// async_sink class implementation // async_sink class implementation
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
inline spdlog::details::async_log_helper::async_log_helper(size_t queue_size) inline spdlog::details::async_log_helper::async_log_helper(formatter_ptr formatter, const std::vector<sink_ptr>& sinks, size_t queue_size):
:_sinks(), _active(false),
_active(true), _formatter(formatter),
_sinks(sinks),
_q(queue_size), _q(queue_size),
_worker_thread(&async_log_helper::thread_loop, this) _worker_thread(&async_log_helper::worker_loop, this)
{} {}
inline spdlog::details::async_log_helper::~async_log_helper() inline spdlog::details::async_log_helper::~async_log_helper()
@ -190,12 +189,11 @@ inline void spdlog::details::async_log_helper::log(const details::log_msg& msg)
} }
inline void spdlog::details::async_log_helper::thread_loop() inline void spdlog::details::async_log_helper::worker_loop()
{ {
log_msg popped_log_msg; log_msg popped_log_msg;
clock::time_point last_pop = clock::now(); clock::time_point last_pop = clock::now();
size_t counter = 0; _active = true;
while (_active) while (_active)
{ {
q_type::item_type popped_msg; q_type::item_type popped_msg;
@ -229,27 +227,11 @@ inline void spdlog::details::async_log_helper::thread_loop()
} }
inline void spdlog::details::async_log_helper::add_sink(spdlog::sink_ptr s)
{
std::lock_guard<std::mutex> guard(_mutex);
_sinks.push_back(s);
}
inline void spdlog::details::async_log_helper::remove_sink(spdlog::sink_ptr s)
{
std::lock_guard<std::mutex> guard(_mutex);
_sinks.erase(std::remove(_sinks.begin(), _sinks.end(), s), _sinks.end());
}
inline void spdlog::details::async_log_helper::set_formatter(formatter_ptr msg_formatter) inline void spdlog::details::async_log_helper::set_formatter(formatter_ptr msg_formatter)
{ {
_formatter = msg_formatter; _formatter = msg_formatter;
} }
inline void spdlog::details::async_log_helper::shutdown(const log_clock::duration& timeout) inline void spdlog::details::async_log_helper::shutdown(const log_clock::duration& timeout)
{ {
/* /*

View File

@ -37,11 +37,8 @@ template<class It>
inline spdlog::async_logger::async_logger(const std::string& logger_name, const It& begin, const It& end, size_t queue_size, const log_clock::duration& shutdown_duration) : inline spdlog::async_logger::async_logger(const std::string& logger_name, const It& begin, const It& end, size_t queue_size, const log_clock::duration& shutdown_duration) :
logger(logger_name, begin, end), logger(logger_name, begin, end),
_shutdown_duration(shutdown_duration), _shutdown_duration(shutdown_duration),
_async_log_helper(new details::async_log_helper(queue_size)) _async_log_helper(new details::async_log_helper(_formatter, _sinks, queue_size))
{ {
_async_log_helper->set_formatter(_formatter);
for (auto &s : _sinks)
_async_log_helper->add_sink(s);
} }
inline spdlog::async_logger::async_logger(const std::string& logger_name, sinks_init_list sinks, size_t queue_size, const log_clock::duration& shutdown_duration) : inline spdlog::async_logger::async_logger(const std::string& logger_name, sinks_init_list sinks, size_t queue_size, const log_clock::duration& shutdown_duration) :