async_sink
This commit is contained in:
		@@ -2,7 +2,6 @@
 | 
			
		||||
 | 
			
		||||
#include <thread>
 | 
			
		||||
#include <chrono>
 | 
			
		||||
#include <mutex>
 | 
			
		||||
#include <atomic>
 | 
			
		||||
 | 
			
		||||
#include "base_sink.h"
 | 
			
		||||
@@ -11,39 +10,45 @@
 | 
			
		||||
 | 
			
		||||
namespace c11log {
 | 
			
		||||
namespace sinks {
 | 
			
		||||
class async_sink : public base_sink {
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
class async_sink : public base_sink
 | 
			
		||||
{
 | 
			
		||||
public:
 | 
			
		||||
    using size_type = c11log::details::blocking_queue<std::string>::size_type;    
 | 
			
		||||
    explicit async_sink(const std::size_t max_queue_size, const std::chrono::seconds& timeout = std::chrono::seconds::max());        
 | 
			
		||||
    using size_type = c11log::details::blocking_queue<std::string>::size_type;
 | 
			
		||||
 | 
			
		||||
    explicit async_sink(const size_type max_queue_size);
 | 
			
		||||
    ~async_sink();
 | 
			
		||||
    void add_sink(logger::sink_ptr_t sink);
 | 
			
		||||
    void remove_sink(logger::sink_ptr_t sink_ptr);    
 | 
			
		||||
    
 | 
			
		||||
    void remove_sink(logger::sink_ptr_t sink_ptr);
 | 
			
		||||
 | 
			
		||||
protected:    
 | 
			
		||||
    //Wait to remaining items (if any) in the queue to be written and shutdown
 | 
			
		||||
    void shutdown(const std::chrono::seconds& timeout);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
protected:
 | 
			
		||||
    void sink_it_(const std::string& msg) override;
 | 
			
		||||
    void thread_loop_();
 | 
			
		||||
    
 | 
			
		||||
private:    
 | 
			
		||||
 | 
			
		||||
private:
 | 
			
		||||
    c11log::logger::sinks_vector_t sinks_;
 | 
			
		||||
    bool active_ = true;    
 | 
			
		||||
    const std::chrono::seconds timeout_;
 | 
			
		||||
    std::atomic<bool> active_ { true };
 | 
			
		||||
    const std::chrono::seconds push_pop_timeout_;
 | 
			
		||||
    c11log::details::blocking_queue<std::string> q_;
 | 
			
		||||
    std::thread back_thread_; 
 | 
			
		||||
    std::thread back_thread_;
 | 
			
		||||
    //Clear all remaining messages(if any), stop the back_thread_ and join it
 | 
			
		||||
    void shutdown_();
 | 
			
		||||
};
 | 
			
		||||
}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
//
 | 
			
		||||
// async_sink inline impl
 | 
			
		||||
//
 | 
			
		||||
///////////////////////////////////////////////////////////////////////////////
 | 
			
		||||
// async_sink class implementation
 | 
			
		||||
///////////////////////////////////////////////////////////////////////////////
 | 
			
		||||
 | 
			
		||||
inline c11log::sinks::async_sink::async_sink(const std::size_t max_queue_size, const std::chrono::seconds& timeout)
 | 
			
		||||
inline c11log::sinks::async_sink::async_sink(const std::size_t max_queue_size)
 | 
			
		||||
    :q_(max_queue_size),
 | 
			
		||||
    timeout_(timeout),
 | 
			
		||||
    back_thread_(&async_sink::thread_loop_, this)
 | 
			
		||||
     push_pop_timeout_(std::chrono::seconds(2)),
 | 
			
		||||
     back_thread_(&async_sink::thread_loop_, this)
 | 
			
		||||
{}
 | 
			
		||||
 | 
			
		||||
inline c11log::sinks::async_sink::~async_sink()
 | 
			
		||||
@@ -52,23 +57,21 @@ inline c11log::sinks::async_sink::~async_sink()
 | 
			
		||||
}
 | 
			
		||||
inline void c11log::sinks::async_sink::sink_it_(const std::string& msg)
 | 
			
		||||
{
 | 
			
		||||
    q_.push(msg, timeout_);
 | 
			
		||||
    q_.push(msg, push_pop_timeout_);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
inline void c11log::sinks::async_sink::thread_loop_()
 | 
			
		||||
{   
 | 
			
		||||
{
 | 
			
		||||
    std::string msg;
 | 
			
		||||
    while (active_) 
 | 
			
		||||
    {        
 | 
			
		||||
        if (q_.pop(msg, timeout_))
 | 
			
		||||
    while (active_)
 | 
			
		||||
    {
 | 
			
		||||
        if (q_.pop(msg, push_pop_timeout_))
 | 
			
		||||
        {
 | 
			
		||||
            std::lock_guard<std::mutex> lock(mutex_);
 | 
			
		||||
            for (auto &sink : sinks_)
 | 
			
		||||
            {
 | 
			
		||||
                if (active_)
 | 
			
		||||
                    sink->log(msg, _level);
 | 
			
		||||
                else
 | 
			
		||||
                    break;
 | 
			
		||||
                sink->log(msg, _level);
 | 
			
		||||
                if (!active_)
 | 
			
		||||
                    return;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
@@ -76,23 +79,32 @@ inline void c11log::sinks::async_sink::thread_loop_()
 | 
			
		||||
 | 
			
		||||
inline void c11log::sinks::async_sink::add_sink(logger::sink_ptr_t sink)
 | 
			
		||||
{
 | 
			
		||||
    std::lock_guard<std::mutex> lock(mutex_);
 | 
			
		||||
    sinks_.push_back(sink);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
inline void c11log::sinks::async_sink::remove_sink(logger::sink_ptr_t sink_ptr)
 | 
			
		||||
{
 | 
			
		||||
    std::lock_guard<std::mutex> lock(mutex_);
 | 
			
		||||
    sinks_.erase(std::remove(sinks_.begin(), sinks_.end(), sink_ptr), sinks_.end());
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
inline void c11log::sinks::async_sink::shutdown(const std::chrono::seconds &timeout)
 | 
			
		||||
{
 | 
			
		||||
    auto until = std::chrono::system_clock::now() + timeout;
 | 
			
		||||
    while (q_.size() > 0 && std::chrono::system_clock::now() < until)
 | 
			
		||||
    {
 | 
			
		||||
        std::this_thread::sleep_for(std::chrono::milliseconds(200));
 | 
			
		||||
    }
 | 
			
		||||
    shutdown_();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
inline void c11log::sinks::async_sink::shutdown_()
 | 
			
		||||
{
 | 
			
		||||
    {        
 | 
			
		||||
        std::lock_guard<std::mutex> lock(mutex_);
 | 
			
		||||
        active_ = false;        
 | 
			
		||||
    if(active_)
 | 
			
		||||
    {
 | 
			
		||||
        active_ = false;
 | 
			
		||||
        if (back_thread_.joinable())
 | 
			
		||||
            back_thread_.join();
 | 
			
		||||
    }
 | 
			
		||||
    q_.clear();
 | 
			
		||||
    back_thread_.join();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -125,7 +125,7 @@ private:
 | 
			
		||||
 */
 | 
			
		||||
class midnight_file_sink:public details::rotating_file_sink_base {
 | 
			
		||||
public:
 | 
			
		||||
    midnight_file_sink(const std::string& base_filename, const std::string& extension):
 | 
			
		||||
    midnight_file_sink(const std::string& base_filename, const std::string& extension = "txt"):
 | 
			
		||||
        _base_filename(base_filename),
 | 
			
		||||
        _extension(extension),
 | 
			
		||||
        _midnight_tp { _calc_midnight_tp() }
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user