spdlog/include/c11log/sinks/async_sink.h

110 lines
2.7 KiB
C
Raw Normal View History

2014-01-25 17:09:04 +08:00
#pragma once
2014-01-25 21:52:10 +08:00
2014-01-25 17:09:04 +08:00
#include <thread>
2014-01-25 21:52:10 +08:00
#include <chrono>
#include <atomic>
2014-01-25 17:09:04 +08:00
#include "base_sink.h"
2014-01-25 21:52:10 +08:00
#include "../logger.h"
#include "../details/blocking_queue.h"
2014-03-07 06:52:50 +08:00
namespace c11log
{
namespace sinks
{
2014-01-25 23:28:56 +08:00
2014-03-07 06:52:50 +08:00
class async_sink : public base_sink
{
2014-01-25 17:09:04 +08:00
public:
2014-01-25 23:28:56 +08:00
using size_type = c11log::details::blocking_queue<std::string>::size_type;
explicit async_sink(const size_type max_queue_size);
2014-01-25 21:52:10 +08:00
~async_sink();
void add_sink(logger::sink_ptr sink);
void remove_sink(logger::sink_ptr sink_ptr);
2014-01-25 23:28:56 +08:00
//Wait to remaining items (if any) in the queue to be written and shutdown
void shutdown(const std::chrono::seconds& timeout);
2014-01-25 17:09:04 +08:00
2014-01-25 23:28:56 +08:00
protected:
2014-02-22 16:34:42 +08:00
void _sink_it(const std::string& msg) override;
void _thread_loop();
2014-01-25 23:28:56 +08:00
private:
2014-02-22 16:34:42 +08:00
c11log::logger::sinks_vector_t _sinks;
std::atomic<bool> _active;
c11log::details::blocking_queue<std::string> _q;
std::thread _back_thread;
//Clear all remaining messages(if any), stop the _back_thread and join it
void _shutdown();
2014-01-25 21:52:10 +08:00
};
}
}
2014-01-25 17:09:04 +08:00
2014-01-25 23:28:56 +08:00
///////////////////////////////////////////////////////////////////////////////
// async_sink class implementation
///////////////////////////////////////////////////////////////////////////////
2014-01-25 17:09:04 +08:00
2014-01-25 23:28:56 +08:00
inline c11log::sinks::async_sink::async_sink(const std::size_t max_queue_size)
2014-02-22 16:34:42 +08:00
:_sinks(),
_active(true),
_q(max_queue_size),
_back_thread(&async_sink::_thread_loop, this)
2014-01-25 21:52:10 +08:00
{}
2014-01-25 17:09:04 +08:00
2014-01-25 21:52:10 +08:00
inline c11log::sinks::async_sink::~async_sink()
{
2014-02-22 16:34:42 +08:00
_shutdown();
2014-01-25 21:52:10 +08:00
}
2014-02-22 16:34:42 +08:00
inline void c11log::sinks::async_sink::_sink_it(const std::string& msg)
2014-01-25 21:52:10 +08:00
{
2014-02-22 16:34:42 +08:00
_q.push(msg);
2014-01-25 21:52:10 +08:00
}
2014-01-25 17:09:04 +08:00
2014-02-22 16:34:42 +08:00
inline void c11log::sinks::async_sink::_thread_loop()
2014-01-25 23:28:56 +08:00
{
2014-03-04 07:23:38 +08:00
static std::chrono::seconds pop_timeout { 1 };
2014-01-25 21:52:10 +08:00
std::string msg;
2014-02-22 04:51:54 +08:00
2014-02-22 16:34:42 +08:00
while (_active) {
if (_q.pop(msg, pop_timeout)) {
for (auto &sink : _sinks) {
2014-01-26 07:53:23 +08:00
sink->log(msg, static_cast<level::level_enum>(_level.load()));
2014-02-22 16:34:42 +08:00
if (!_active)
2014-01-25 23:28:56 +08:00
return;
2014-01-25 21:52:10 +08:00
}
}
}
2014-01-25 17:09:04 +08:00
}
2014-01-25 21:52:10 +08:00
inline void c11log::sinks::async_sink::add_sink(logger::sink_ptr sink)
2014-01-25 21:52:10 +08:00
{
2014-02-22 16:34:42 +08:00
_sinks.push_back(sink);
2014-01-25 21:52:10 +08:00
}
inline void c11log::sinks::async_sink::remove_sink(logger::sink_ptr sink_ptr)
2014-01-25 21:52:10 +08:00
{
2014-02-22 16:34:42 +08:00
_sinks.erase(std::remove(_sinks.begin(), _sinks.end(), sink_ptr), _sinks.end());
2014-01-25 17:09:04 +08:00
}
2014-01-25 23:28:56 +08:00
inline void c11log::sinks::async_sink::shutdown(const std::chrono::seconds &timeout)
{
auto until = std::chrono::system_clock::now() + timeout;
2014-02-22 16:34:42 +08:00
while (_q.size() > 0 && std::chrono::system_clock::now() < until) {
2014-01-25 23:28:56 +08:00
std::this_thread::sleep_for(std::chrono::milliseconds(200));
}
2014-02-22 16:34:42 +08:00
_shutdown();
2014-01-25 23:28:56 +08:00
}
2014-02-22 16:34:42 +08:00
inline void c11log::sinks::async_sink::_shutdown()
2014-01-25 17:09:04 +08:00
{
2014-02-22 16:34:42 +08:00
if(_active) {
_active = false;
if (_back_thread.joinable())
_back_thread.join();
2014-01-25 21:52:10 +08:00
}
}
2014-01-25 17:09:04 +08:00