spdlog/include/c11log/sinks/async_sink.h

153 lines
3.7 KiB
C
Raw Normal View History

2014-03-22 08:11:17 -04:00
#pragma once
#include <thread>
#include <chrono>
#include <atomic>
#include <functional>
2014-03-22 08:11:17 -04:00
#include "base_sink.h"
#include "../logger.h"
#include "../details/blocking_queue.h"
2014-03-28 12:03:24 -04:00
#include "../details/log_msg.h"
2014-03-22 08:11:17 -04:00
#include<iostream>
2014-03-28 12:03:24 -04:00
2014-03-22 08:11:17 -04:00
namespace c11log
{
namespace sinks
{
static void msg_deleter(details::log_msg* msg_to_delete)
{
2014-03-30 19:31:26 -04:00
delete []msg_to_delete->msg_buf.first;
delete msg_to_delete;
}
2014-03-22 08:11:17 -04:00
class async_sink : public base_sink
{
public:
2014-03-28 12:03:24 -04:00
2014-03-22 08:11:17 -04:00
2014-05-06 10:38:11 -04:00
using queue_type = details::blocking_queue<std::unique_ptr<details::log_msg, std::function<void(details::log_msg*)>>>;
2014-03-30 19:31:26 -04:00
explicit async_sink(const queue_type::size_type max_queue_size);
2014-03-29 06:04:42 -04:00
2014-03-30 19:31:26 -04:00
//Stop logging and join the back thread
// TODO: limit with timeout of the join and kill it afterwards?
2014-03-22 08:11:17 -04:00
~async_sink();
void add_sink(logger::sink_ptr sink);
void remove_sink(logger::sink_ptr sink_ptr);
2014-03-30 19:31:26 -04:00
queue_type& q();
2014-03-22 08:11:17 -04:00
//Wait to remaining items (if any) in the queue to be written and shutdown
2014-03-29 06:04:42 -04:00
void shutdown(const std::chrono::milliseconds& timeout);
2014-03-22 08:11:17 -04:00
protected:
2014-03-28 12:03:24 -04:00
void _sink_it(const details::log_msg& msg) override;
2014-03-22 08:11:17 -04:00
void _thread_loop();
private:
c11log::logger::sinks_vector_t _sinks;
std::atomic<bool> _active;
queue_type _q;
2014-03-22 08:11:17 -04:00
std::thread _back_thread;
//Clear all remaining messages(if any), stop the _back_thread and join it
void _shutdown();
2014-03-30 19:31:26 -04:00
std::mutex _shutdown_mutex;
2014-03-22 08:11:17 -04:00
};
}
}
///////////////////////////////////////////////////////////////////////////////
// async_sink class implementation
///////////////////////////////////////////////////////////////////////////////
inline c11log::sinks::async_sink::async_sink(const queue_type::size_type max_queue_size)
2014-03-22 08:11:17 -04:00
:_sinks(),
_active(true),
_q(max_queue_size),
_back_thread(&async_sink::_thread_loop, this)
{}
inline c11log::sinks::async_sink::~async_sink()
{
_shutdown();
}
2014-03-28 12:03:24 -04:00
inline void c11log::sinks::async_sink::_sink_it(const details::log_msg& msg)
2014-03-22 08:11:17 -04:00
{
2014-03-30 19:31:26 -04:00
auto msg_size = msg.msg_buf.second;
if(!_active || !msg_size)
return;
2014-03-28 12:03:24 -04:00
//re allocate on the heap the (stack based) message
2014-05-06 10:38:11 -04:00
details::log_msg* new_msg = new details::log_msg(msg);
2014-03-29 06:04:42 -04:00
2014-03-30 19:31:26 -04:00
char *buf = new char[msg_size];
std::memcpy(buf, msg.msg_buf.first, msg_size);
new_msg->msg_buf = bufpair_t(buf, msg_size);
// Create unique_ptr with custom deleter and push it
queue_type::item_type new_shared_msg(new_msg, msg_deleter);
_q.push(std::move(new_shared_msg));
2014-03-22 08:11:17 -04:00
}
inline void c11log::sinks::async_sink::_thread_loop()
{
static std::chrono::seconds pop_timeout { 1 };
while (_active)
{
queue_type::item_type msg;
2014-03-22 08:11:17 -04:00
if (_q.pop(msg, pop_timeout))
{
for (auto &sink : _sinks)
{
2014-03-28 12:03:24 -04:00
sink->log(*msg);
2014-03-30 19:31:26 -04:00
if(!_active)
break;
2014-03-22 08:11:17 -04:00
}
}
}
}
inline void c11log::sinks::async_sink::add_sink(logger::sink_ptr sink)
{
_sinks.push_back(sink);
}
inline void c11log::sinks::async_sink::remove_sink(logger::sink_ptr sink_ptr)
{
_sinks.erase(std::remove(_sinks.begin(), _sinks.end(), sink_ptr), _sinks.end());
}
2014-03-30 16:47:42 -04:00
inline c11log::sinks::async_sink::queue_type& c11log::sinks::async_sink::q()
{
2014-03-30 19:31:26 -04:00
return _q;
2014-03-30 16:47:42 -04:00
}
2014-03-22 08:11:17 -04:00
2014-03-29 06:04:42 -04:00
inline void c11log::sinks::async_sink::shutdown(const std::chrono::milliseconds& timeout)
2014-03-22 08:11:17 -04:00
{
2014-03-30 19:31:26 -04: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 08:11:17 -04:00
_shutdown();
}
inline void c11log::sinks::async_sink::_shutdown()
{
2014-03-30 19:31:26 -04:00
std::lock_guard<std::mutex> guard(_shutdown_mutex);
2014-03-22 08:11:17 -04:00
if(_active)
{
_active = false;
if (_back_thread.joinable())
2014-03-30 19:31:26 -04:00
_back_thread.join();
2014-03-22 08:11:17 -04:00
}
}