spdlog/include/c11log/sinks/async_sink.h

144 lines
3.4 KiB
C
Raw Normal View History

2014-03-22 20:11:17 +08:00
#pragma once
#include <thread>
#include <chrono>
#include <atomic>
2014-05-08 07:23:07 +08:00
#include <algorithm>
2014-03-22 20:11:17 +08:00
#include "base_sink.h"
#include "../logger.h"
#include "../details/blocking_queue.h"
2014-03-29 00:03:24 +08:00
#include "../details/log_msg.h"
2014-03-22 20:11:17 +08:00
#include<iostream>
2014-03-29 00:03:24 +08:00
2014-03-22 20:11:17 +08:00
namespace c11log
{
namespace sinks
{
template<class Mutex>
class async_sink : public base_sink<Mutex>
2014-03-22 20:11:17 +08:00
{
public:
2014-05-08 07:23:07 +08:00
using q_type = details::blocking_queue<details::log_msg>;
2014-03-29 00:03:24 +08:00
2014-05-08 07:23:07 +08:00
explicit async_sink(const q_type::size_type max_queue_size);
2014-03-29 18:04:42 +08:00
2014-03-31 07:31:26 +08:00
//Stop logging and join the back thread
// TODO: limit with timeout of the join and kill it afterwards?
2014-03-22 20:11:17 +08:00
~async_sink();
void add_sink(logger::sink_ptr sink);
void remove_sink(logger::sink_ptr sink_ptr);
2014-05-08 07:23:07 +08:00
q_type& q();
2014-03-22 20:11:17 +08:00
//Wait to remaining items (if any) in the queue to be written and shutdown
2014-03-29 18:04:42 +08:00
void shutdown(const std::chrono::milliseconds& timeout);
2014-03-22 20:11:17 +08:00
protected:
2014-03-29 00:03:24 +08:00
void _sink_it(const details::log_msg& msg) override;
2014-03-22 20:11:17 +08:00
void _thread_loop();
private:
c11log::logger::sinks_vector_t _sinks;
std::atomic<bool> _active;
2014-05-08 07:23:07 +08:00
q_type _q;
2014-03-22 20:11:17 +08:00
std::thread _back_thread;
//Clear all remaining messages(if any), stop the _back_thread and join it
void _shutdown();
2014-03-31 07:31:26 +08:00
std::mutex _shutdown_mutex;
2014-03-22 20:11:17 +08:00
};
}
}
///////////////////////////////////////////////////////////////////////////////
// async_sink class implementation
///////////////////////////////////////////////////////////////////////////////
template<class Mutex>
inline c11log::sinks::async_sink<Mutex>::async_sink(const q_type::size_type max_queue_size)
2014-03-22 20:11:17 +08:00
:_sinks(),
_active(true),
_q(max_queue_size),
_back_thread(&async_sink::_thread_loop, this)
{}
template<class Mutex>
inline c11log::sinks::async_sink<Mutex>::~async_sink()
2014-03-22 20:11:17 +08:00
{
_shutdown();
}
template<class Mutex>
inline void c11log::sinks::async_sink<Mutex>::_sink_it(const details::log_msg& msg)
{
if(!_active || msg.formatted.empty())
2014-03-31 07:31:26 +08:00
return;
2014-05-08 07:23:07 +08:00
_q.push(msg);
2014-03-22 20:11:17 +08:00
}
template<class Mutex>
inline void c11log::sinks::async_sink<Mutex>::_thread_loop()
2014-03-22 20:11:17 +08:00
{
static std::chrono::seconds pop_timeout { 1 };
while (_active)
{
2014-05-08 07:23:07 +08:00
q_type::item_type msg;
2014-03-22 20:11:17 +08:00
if (_q.pop(msg, pop_timeout))
{
for (auto &sink : _sinks)
{
2014-05-08 07:23:07 +08:00
sink->log(msg);
2014-03-31 07:31:26 +08:00
if(!_active)
break;
2014-03-22 20:11:17 +08:00
}
}
}
}
template<class Mutex>
inline void c11log::sinks::async_sink<Mutex>::add_sink(logger::sink_ptr sink)
2014-03-22 20:11:17 +08:00
{
_sinks.push_back(sink);
}
template<class Mutex>
inline void c11log::sinks::async_sink<Mutex>::remove_sink(logger::sink_ptr sink)
2014-03-22 20:11:17 +08:00
{
2014-05-08 07:23:07 +08:00
_sinks.erase(std::remove(_sinks.begin(), _sinks.end(), sink), _sinks.end());
2014-03-22 20:11:17 +08:00
}
/*
template<class Mutex>
inline c11log::sinks::async_sink::q_type& c11log::sinks::async_sink<Mutex>::q()
2014-03-31 04:47:42 +08:00
{
2014-03-31 07:31:26 +08:00
return _q;
}*/
2014-03-31 04:47:42 +08:00
2014-03-22 20:11:17 +08:00
template<class Mutex>
inline void c11log::sinks::async_sink<Mutex>::shutdown(const std::chrono::milliseconds& timeout)
2014-03-22 20:11:17 +08:00
{
2014-03-31 07:31:26 +08:00
if(timeout > std::chrono::milliseconds::zero())
{
auto until = log_clock::now() + timeout;
while (_q.size() > 0 && log_clock::now() < until)
{
std::this_thread::sleep_for(std::chrono::milliseconds(2));
}
}
2014-03-22 20:11:17 +08:00
_shutdown();
}
template<class Mutex>
inline void c11log::sinks::async_sink<Mutex>::_shutdown()
2014-03-22 20:11:17 +08:00
{
2014-03-31 07:31:26 +08:00
std::lock_guard<std::mutex> guard(_shutdown_mutex);
2014-03-22 20:11:17 +08:00
if(_active)
{
_active = false;
if (_back_thread.joinable())
2014-03-31 07:31:26 +08:00
_back_thread.join();
2014-03-22 20:11:17 +08:00
}
}