spdlog/include/c11log/sinks/async_sink.h

107 lines
2.8 KiB
C
Raw Normal View History

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