spdlog/include/c11log/sinks/async_sink.h

99 lines
2.4 KiB
C
Raw Normal View History

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 <mutex>
#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 21:52:10 +08:00
class async_sink : public base_sink {
2014-01-25 17:09:04 +08:00
public:
2014-01-25 21:52:10 +08:00
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());
~async_sink();
void add_sink(logger::sink_ptr_t sink);
void remove_sink(logger::sink_ptr_t sink_ptr);
2014-01-25 17:09:04 +08:00
2014-01-25 21:52:10 +08:00
protected:
void sink_it_(const std::string& msg) override;
void thread_loop_();
private:
c11log::logger::sinks_vector_t sinks_;
bool active_ = true;
const std::chrono::seconds timeout_;
c11log::details::blocking_queue<std::string> q_;
std::thread back_thread_;
void shutdown_();
};
}
}
2014-01-25 17:09:04 +08:00
2014-01-25 21:52:10 +08:00
//
// async_sink inline impl
//
2014-01-25 17:09:04 +08:00
2014-01-25 21:52:10 +08:00
inline c11log::sinks::async_sink::async_sink(const std::size_t max_queue_size, const std::chrono::seconds& timeout)
:q_(max_queue_size),
timeout_(timeout),
back_thread_(&async_sink::thread_loop_, this)
{}
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)
{
q_.push(msg, timeout_);
}
2014-01-25 17:09:04 +08:00
2014-01-25 21:52:10 +08:00
inline void c11log::sinks::async_sink::thread_loop_()
{
std::string msg;
while (active_)
{
if (q_.pop(msg, timeout_))
{
std::lock_guard<std::mutex> lock(mutex_);
for (auto &sink : sinks_)
{
if (active_)
sink->log(msg, _level);
else
break;
}
}
}
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)
{
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());
2014-01-25 17:09:04 +08:00
}
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 21:52:10 +08:00
{
std::lock_guard<std::mutex> lock(mutex_);
active_ = false;
}
q_.clear();
back_thread_.join();
}
2014-01-25 17:09:04 +08:00