From 6255180e99b67e927d7e845ebdde3a3dfb6392fa Mon Sep 17 00:00:00 2001 From: gabime Date: Fri, 5 Dec 2014 20:59:38 +0200 Subject: [PATCH] small fixes in async_helper --- include/spdlog/details/async_log_helper.h | 54 ++++++++-------------- include/spdlog/details/async_logger_impl.h | 7 +-- 2 files changed, 20 insertions(+), 41 deletions(-) diff --git a/include/spdlog/details/async_log_helper.h b/include/spdlog/details/async_log_helper.h index 87aa709c..4d5e1513 100644 --- a/include/spdlog/details/async_log_helper.h +++ b/include/spdlog/details/async_log_helper.h @@ -37,6 +37,7 @@ #include #include +#include "../common.h" #include "../sinks/sink.h" #include "./mpmc_bounded_q.h" #include "./log_msg.h" @@ -108,38 +109,35 @@ public: using clock = std::chrono::steady_clock; - explicit async_log_helper(size_t queue_size); + async_log_helper(formatter_ptr formatter, const std::vector& sinks, size_t queue_size); void log(const details::log_msg& msg); //Stop logging and join the back thread - ~async_log_helper(); - void add_sink(sink_ptr sink); - void remove_sink(sink_ptr sink_ptr); + ~async_log_helper(); void set_formatter(formatter_ptr); //Wait to remaining items (if any) in the queue to be written and shutdown void shutdown(const log_clock::duration& timeout); -private: - std::vector> _sinks; - std::atomic _active; +private: + std::atomic _active; + formatter_ptr _formatter; + std::vector> _sinks; q_type _q; - std::thread _worker_thread; - std::mutex _mutex; + std::thread _worker_thread; // last exception thrown from the worker thread std::shared_ptr _last_workerthread_ex; + - // worker thread formatter - formatter_ptr _formatter; // will throw last worker thread exception or if worker thread no active void throw_if_bad_worker(); // 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 static void sleep_or_yield(const clock::time_point& last_op_time); @@ -156,11 +154,12 @@ private: /////////////////////////////////////////////////////////////////////////////// // async_sink class implementation /////////////////////////////////////////////////////////////////////////////// -inline spdlog::details::async_log_helper::async_log_helper(size_t queue_size) - :_sinks(), - _active(true), +inline spdlog::details::async_log_helper::async_log_helper(formatter_ptr formatter, const std::vector& sinks, size_t queue_size): + _active(false), + _formatter(formatter), + _sinks(sinks), _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() @@ -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; - clock::time_point last_pop = clock::now(); - size_t counter = 0; + clock::time_point last_pop = clock::now(); + _active = true; while (_active) { 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 guard(_mutex); - _sinks.push_back(s); -} - - -inline void spdlog::details::async_log_helper::remove_sink(spdlog::sink_ptr s) -{ - std::lock_guard 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) { _formatter = msg_formatter; } - - inline void spdlog::details::async_log_helper::shutdown(const log_clock::duration& timeout) { /* diff --git a/include/spdlog/details/async_logger_impl.h b/include/spdlog/details/async_logger_impl.h index 963135d3..03a09f9c 100644 --- a/include/spdlog/details/async_logger_impl.h +++ b/include/spdlog/details/async_logger_impl.h @@ -37,11 +37,8 @@ template 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), _shutdown_duration(shutdown_duration), - _async_log_helper(new details::async_log_helper(queue_size)) -{ - _async_log_helper->set_formatter(_formatter); - for (auto &s : _sinks) - _async_log_helper->add_sink(s); + _async_log_helper(new details::async_log_helper(_formatter, _sinks, queue_size)) +{ } 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) :