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 <atomic>
#include "../common.h"
#include "../sinks/sink.h"
#include "./mpmc_bounded_q.h"
#include "./log_msg.h"
@ -108,13 +109,11 @@ public:
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);
//Stop logging and join the back thread
~async_log_helper();
void add_sink(sink_ptr sink);
void remove_sink(sink_ptr sink_ptr);
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);
@ -122,24 +121,23 @@ public:
private:
std::atomic<bool> _active;
formatter_ptr _formatter;
std::vector<std::shared_ptr<sinks::sink>> _sinks;
std::atomic<bool> _active;
q_type _q;
std::thread _worker_thread;
std::mutex _mutex;
// last exception thrown from the worker thread
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
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<sink_ptr>& 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;
_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<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)
{
_formatter = msg_formatter;
}
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) :
logger(logger_name, begin, end),
_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) :