async_sink use unique_ptr instead of shared
This commit is contained in:
		@@ -3,27 +3,35 @@
 | 
			
		||||
#include <thread>
 | 
			
		||||
#include <chrono>
 | 
			
		||||
#include <atomic>
 | 
			
		||||
#include <functional>
 | 
			
		||||
 | 
			
		||||
#include "base_sink.h"
 | 
			
		||||
#include "../logger.h"
 | 
			
		||||
#include "../details/blocking_queue.h"
 | 
			
		||||
#include "../details/log_msg.h"
 | 
			
		||||
 | 
			
		||||
#include<iostream>
 | 
			
		||||
 | 
			
		||||
using namespace std;
 | 
			
		||||
namespace c11log
 | 
			
		||||
{
 | 
			
		||||
namespace sinks
 | 
			
		||||
{
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
static void msg_deleter(details::log_msg* msg_to_delete)
 | 
			
		||||
{
 | 
			
		||||
	delete []msg_to_delete->msg_buf.first;
 | 
			
		||||
	delete msg_to_delete;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
class async_sink : public base_sink
 | 
			
		||||
{
 | 
			
		||||
public:
 | 
			
		||||
 | 
			
		||||
    using queue_t = c11log::details::blocking_queue<std::shared_ptr<details::log_msg>>;
 | 
			
		||||
    using size_type = queue_t::size_type;
 | 
			
		||||
 | 
			
		||||
    explicit async_sink(const size_type max_queue_size);
 | 
			
		||||
    using queue_type = c11log::details::blocking_queue<std::unique_ptr<details::log_msg, std::function<void(details::log_msg*)>>>;
 | 
			
		||||
	
 | 
			
		||||
    explicit async_sink(const queue_type::size_type max_queue_size);
 | 
			
		||||
 | 
			
		||||
	//Stop logging and join the back thread
 | 
			
		||||
	// TODO: limit with timeout of the join and kill it afterwards?
 | 
			
		||||
@@ -42,7 +50,7 @@ protected:
 | 
			
		||||
private:
 | 
			
		||||
    c11log::logger::sinks_vector_t _sinks;
 | 
			
		||||
    std::atomic<bool> _active;
 | 
			
		||||
    queue_t _q;
 | 
			
		||||
    queue_type _q;
 | 
			
		||||
    std::thread _back_thread;
 | 
			
		||||
    //Clear all remaining messages(if any), stop the _back_thread and join it
 | 
			
		||||
    void _shutdown();
 | 
			
		||||
@@ -55,7 +63,7 @@ private:
 | 
			
		||||
// async_sink class implementation
 | 
			
		||||
///////////////////////////////////////////////////////////////////////////////
 | 
			
		||||
 | 
			
		||||
inline c11log::sinks::async_sink::async_sink(const size_type max_queue_size)
 | 
			
		||||
inline c11log::sinks::async_sink::async_sink(const queue_type::size_type max_queue_size)
 | 
			
		||||
    :_sinks(),
 | 
			
		||||
     _active(true),
 | 
			
		||||
     _q(max_queue_size),
 | 
			
		||||
@@ -66,6 +74,8 @@ inline c11log::sinks::async_sink::~async_sink()
 | 
			
		||||
{
 | 
			
		||||
    _shutdown();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
inline void c11log::sinks::async_sink::_sink_it(const details::log_msg& msg)
 | 
			
		||||
{
 | 
			
		||||
	auto msg_size = msg.msg_buf.second;	
 | 
			
		||||
@@ -78,13 +88,15 @@ inline void c11log::sinks::async_sink::_sink_it(const details::log_msg& msg)
 | 
			
		||||
	char *buf = new char[msg_size];
 | 
			
		||||
	std::memcpy(buf, msg.msg_buf.first, msg_size);
 | 
			
		||||
	new_msg->msg_buf = bufpair_t(buf, msg_size);
 | 
			
		||||
 | 
			
		||||
    auto new_shared_msg = std::shared_ptr<details::log_msg>(new_msg, [](details::log_msg* msg_to_delete)
 | 
			
		||||
/*
 | 
			
		||||
    auto new_shared_msg = queue_type::item_type(new_msg, [](const details::log_msg* msg_to_delete)
 | 
			
		||||
    {
 | 
			
		||||
        delete []msg_to_delete->msg_buf.first;
 | 
			
		||||
        delete msg_to_delete;
 | 
			
		||||
    });
 | 
			
		||||
    _q.push(new_shared_msg);
 | 
			
		||||
	 * */
 | 
			
		||||
	 queue_type::item_type new_shared_msg(new_msg, msg_deleter);
 | 
			
		||||
    _q.push(std::move(new_shared_msg));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
inline void c11log::sinks::async_sink::_thread_loop()
 | 
			
		||||
@@ -92,12 +104,14 @@ inline void c11log::sinks::async_sink::_thread_loop()
 | 
			
		||||
    static std::chrono::seconds  pop_timeout { 1 };
 | 
			
		||||
    while (_active)
 | 
			
		||||
    {
 | 
			
		||||
        std::shared_ptr<details::log_msg> msg;
 | 
			
		||||
        queue_type::item_type msg;
 | 
			
		||||
        if (_q.pop(msg, pop_timeout))
 | 
			
		||||
        {
 | 
			
		||||
            for (auto &sink : _sinks)
 | 
			
		||||
            {
 | 
			
		||||
                sink->log(*msg);
 | 
			
		||||
				if(!_active)
 | 
			
		||||
					break;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user