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-01-25 17:09:04 +08:00
|
|
|
namespace c11log {
|
|
|
|
namespace sinks {
|
2014-01-25 23:28:56 +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_t sink);
|
2014-01-25 23:28:56 +08: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 17:09:04 +08:00
|
|
|
|
2014-01-25 23:28:56 +08:00
|
|
|
|
|
|
|
protected:
|
2014-01-25 21:52:10 +08:00
|
|
|
void sink_it_(const std::string& msg) override;
|
|
|
|
void thread_loop_();
|
2014-01-25 23:28:56 +08:00
|
|
|
|
|
|
|
private:
|
2014-01-25 21:52:10 +08:00
|
|
|
c11log::logger::sinks_vector_t sinks_;
|
2014-01-25 23:28:56 +08:00
|
|
|
std::atomic<bool> active_ { true };
|
2014-01-25 21:52:10 +08:00
|
|
|
c11log::details::blocking_queue<std::string> q_;
|
2014-01-25 23:28:56 +08:00
|
|
|
std::thread back_thread_;
|
|
|
|
//Clear all remaining messages(if any), stop the back_thread_ and join it
|
2014-01-25 21:52:10 +08:00
|
|
|
void shutdown_();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
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-01-25 21:52:10 +08:00
|
|
|
:q_(max_queue_size),
|
2014-01-25 23:28:56 +08:00
|
|
|
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()
|
|
|
|
{
|
|
|
|
shutdown_();
|
|
|
|
}
|
|
|
|
inline void c11log::sinks::async_sink::sink_it_(const std::string& msg)
|
|
|
|
{
|
2014-01-26 07:53:23 +08:00
|
|
|
q_.push(msg);
|
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::thread_loop_()
|
2014-01-25 23:28:56 +08:00
|
|
|
{
|
2014-01-25 21:52:10 +08:00
|
|
|
std::string msg;
|
2014-01-26 07:53:23 +08:00
|
|
|
auto pop_timeout = std::chrono::seconds(1);
|
2014-01-25 23:28:56 +08:00
|
|
|
while (active_)
|
|
|
|
{
|
2014-01-26 07:53:23 +08:00
|
|
|
if (q_.pop(msg, pop_timeout))
|
2014-01-25 21:52:10 +08:00
|
|
|
{
|
|
|
|
for (auto &sink : sinks_)
|
|
|
|
{
|
2014-01-26 07:53:23 +08:00
|
|
|
sink->log(msg, static_cast<level::level_enum>(_level.load()));
|
2014-01-25 23:28:56 +08:00
|
|
|
if (!active_)
|
|
|
|
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_t sink)
|
|
|
|
{
|
|
|
|
sinks_.push_back(sink);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void c11log::sinks::async_sink::remove_sink(logger::sink_ptr_t sink_ptr)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
while (q_.size() > 0 && std::chrono::system_clock::now() < until)
|
|
|
|
{
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(200));
|
|
|
|
}
|
|
|
|
shutdown_();
|
|
|
|
}
|
|
|
|
|
2014-01-25 21:52:10 +08:00
|
|
|
inline void c11log::sinks::async_sink::shutdown_()
|
2014-01-25 17:09:04 +08:00
|
|
|
{
|
2014-01-25 23:28:56 +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
|
|
|
|