mirror of
https://github.com/gabime/spdlog.git
synced 2024-11-16 00:45:48 +08:00
async_sink to use queue of pointers of log_msgs - faster than moving..
This commit is contained in:
parent
a9abfbb005
commit
59013b3504
@ -53,7 +53,7 @@ namespace sinks
|
|||||||
class async_sink : public base_sink < details::null_mutex >
|
class async_sink : public base_sink < details::null_mutex >
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
using q_type = details::blocking_queue < details::log_msg > ;
|
using q_type = details::blocking_queue < std::unique_ptr<details::log_msg> > ;
|
||||||
|
|
||||||
explicit async_sink(const q_type::size_type max_queue_size);
|
explicit async_sink(const q_type::size_type max_queue_size);
|
||||||
|
|
||||||
@ -64,14 +64,18 @@ public:
|
|||||||
q_type& q();
|
q_type& q();
|
||||||
//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::milliseconds& timeout);
|
void shutdown(const std::chrono::milliseconds& timeout);
|
||||||
|
void set_formatter(formatter_ptr formatter) { _formatter = formatter; }
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void _sink_it(const details::log_msg& msg) override;
|
void _sink_it(const details::log_msg& msg) override;
|
||||||
void _thread_loop();
|
|
||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
void _thread_loop();
|
||||||
std::vector<std::shared_ptr<sink>> _sinks;
|
std::vector<std::shared_ptr<sink>> _sinks;
|
||||||
std::atomic<bool> _active;
|
std::atomic<bool> _active;
|
||||||
q_type _q;
|
q_type _q;
|
||||||
@ -80,7 +84,8 @@ private:
|
|||||||
//Last exception thrown from the back thread
|
//Last exception thrown from the back thread
|
||||||
std::shared_ptr<spdlog_ex> _last_backthread_ex;
|
std::shared_ptr<spdlog_ex> _last_backthread_ex;
|
||||||
|
|
||||||
|
//formatter
|
||||||
|
formatter_ptr _formatter;
|
||||||
//will throw last back thread exception or if backthread no active
|
//will throw last back thread exception or if backthread no active
|
||||||
void _push_sentry();
|
void _push_sentry();
|
||||||
|
|
||||||
@ -108,8 +113,11 @@ inline spdlog::sinks::async_sink::~async_sink()
|
|||||||
|
|
||||||
inline void spdlog::sinks::async_sink::_sink_it(const details::log_msg& msg)
|
inline void spdlog::sinks::async_sink::_sink_it(const details::log_msg& msg)
|
||||||
{
|
{
|
||||||
|
using namespace spdlog::details;
|
||||||
_push_sentry();
|
_push_sentry();
|
||||||
_q.push(msg);
|
//_q.push(std::move(msg));
|
||||||
|
auto msg_p = std::unique_ptr<log_msg>(new log_msg(msg));
|
||||||
|
_q.push(std::move(msg_p));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -124,11 +132,12 @@ inline void spdlog::sinks::async_sink::_thread_loop()
|
|||||||
{
|
{
|
||||||
if (!_active)
|
if (!_active)
|
||||||
return;
|
return;
|
||||||
|
_formatter->format(*msg);
|
||||||
for (auto &s : _sinks)
|
for (auto &s : _sinks)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
s->log(msg);
|
s->log(*msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
catch (const std::exception& ex)
|
catch (const std::exception& ex)
|
||||||
|
Loading…
Reference in New Issue
Block a user