|
|
@ -24,12 +24,15 @@ public:
|
|
|
|
using size_type = queue_t::size_type;
|
|
|
|
using size_type = queue_t::size_type;
|
|
|
|
|
|
|
|
|
|
|
|
explicit async_sink(const size_type max_queue_size);
|
|
|
|
explicit async_sink(const size_type max_queue_size);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//Stop logging and join the back thread
|
|
|
|
|
|
|
|
// TODO: limit with timeout of the join and kill it afterwards?
|
|
|
|
~async_sink();
|
|
|
|
~async_sink();
|
|
|
|
void add_sink(logger::sink_ptr sink);
|
|
|
|
void add_sink(logger::sink_ptr sink);
|
|
|
|
void remove_sink(logger::sink_ptr sink_ptr);
|
|
|
|
void remove_sink(logger::sink_ptr sink_ptr);
|
|
|
|
|
|
|
|
|
|
|
|
//Wait to remaining items (if any) in the queue to be written and shutdown
|
|
|
|
//Wait to remaining items (if any) in the queue to be written and shutdown
|
|
|
|
void shutdown(const std::chrono::seconds& timeout);
|
|
|
|
void shutdown(const std::chrono::milliseconds& timeout);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
protected:
|
|
|
@ -43,6 +46,7 @@ private:
|
|
|
|
std::thread _back_thread;
|
|
|
|
std::thread _back_thread;
|
|
|
|
//Clear all remaining messages(if any), stop the _back_thread and join it
|
|
|
|
//Clear all remaining messages(if any), stop the _back_thread and join it
|
|
|
|
void _shutdown();
|
|
|
|
void _shutdown();
|
|
|
|
|
|
|
|
std::mutex _shutdown_mutex;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -64,19 +68,24 @@ inline c11log::sinks::async_sink::~async_sink()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
inline void c11log::sinks::async_sink::_sink_it(const details::log_msg& msg)
|
|
|
|
inline void c11log::sinks::async_sink::_sink_it(const details::log_msg& msg)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
auto msg_size = msg.msg_buf.second;
|
|
|
|
|
|
|
|
if(!msg_size)
|
|
|
|
|
|
|
|
return;
|
|
|
|
//re allocate on the heap the (stack based) message
|
|
|
|
//re allocate on the heap the (stack based) message
|
|
|
|
auto new_msg = new details::log_msg();
|
|
|
|
auto new_msg = new details::log_msg();
|
|
|
|
*new_msg = msg;
|
|
|
|
*new_msg = msg;
|
|
|
|
auto msg_size = msg.msg_buf.second;
|
|
|
|
auto msg_size = msg.msg_buf.second;
|
|
|
|
char *buf = new char[msg_size];
|
|
|
|
|
|
|
|
std::memcpy(buf, msg.msg_buf.first, msg_size);
|
|
|
|
char *buf = new char[msg_size];
|
|
|
|
new_msg->msg_buf = bufpair_t(buf, msg_size);
|
|
|
|
std::memcpy(buf, msg.msg_buf.first, msg_size);
|
|
|
|
auto new_shared = std::shared_ptr<details::log_msg>(new_msg, [](details::log_msg* msg_to_delete)
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
delete []msg_to_delete->msg_buf.first;
|
|
|
|
delete []msg_to_delete->msg_buf.first;
|
|
|
|
delete msg_to_delete;
|
|
|
|
delete msg_to_delete;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
_q.push(new_shared);
|
|
|
|
_q.push(new_shared_msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline void c11log::sinks::async_sink::_thread_loop()
|
|
|
|
inline void c11log::sinks::async_sink::_thread_loop()
|
|
|
@ -106,23 +115,27 @@ inline void c11log::sinks::async_sink::remove_sink(logger::sink_ptr sink_ptr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
inline void c11log::sinks::async_sink::shutdown(const std::chrono::seconds &timeout)
|
|
|
|
inline void c11log::sinks::async_sink::shutdown(const std::chrono::milliseconds& timeout)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
auto until = std::chrono::system_clock::now() + timeout;
|
|
|
|
if(timeout > std::chrono::milliseconds::zero())
|
|
|
|
while (_q.size() > 0 && std::chrono::system_clock::now() < until)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
auto until = log_clock::now() + timeout;
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(200));
|
|
|
|
while (_q.size() > 0 && log_clock::now() < until)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(2));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
_shutdown();
|
|
|
|
_shutdown();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline void c11log::sinks::async_sink::_shutdown()
|
|
|
|
inline void c11log::sinks::async_sink::_shutdown()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
std::lock_guard<std::mutex> guard(_shutdown_mutex);
|
|
|
|
if(_active)
|
|
|
|
if(_active)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
_active = false;
|
|
|
|
_active = false;
|
|
|
|
if (_back_thread.joinable())
|
|
|
|
if (_back_thread.joinable())
|
|
|
|
_back_thread.join();
|
|
|
|
_back_thread.join();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|