diff --git a/example/example.cpp b/example/example.cpp index eb58102d..54e0ab73 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -35,13 +35,14 @@ int main(int argc, char* argv[]) for(unsigned int i = 1; i <= howmany ; ++i) my_logger.info() << "Hello logger: " << i; - as->shutdown(milliseconds(500)); + //as->shutdown(milliseconds(500)); + auto s = howmany - as->q().size(); auto delta = system_clock::now() - start; auto delta_d = duration_cast> (delta).count(); - cout_logger.info() << "Total:" << format(howmany); + cout_logger.info() << "Total:" << format(s); cout_logger.info() << "Delta:" << format(delta_d); - cout_logger.info() << "Rate:" << format(howmany/delta_d) << "/sec"; + cout_logger.info() << "Rate:" << format(s/delta_d) << "/sec"; return 0; } diff --git a/include/c11log/sinks/async_sink.h b/include/c11log/sinks/async_sink.h index 93cc7977..6d0d064e 100644 --- a/include/c11log/sinks/async_sink.h +++ b/include/c11log/sinks/async_sink.h @@ -38,7 +38,7 @@ public: ~async_sink(); void add_sink(logger::sink_ptr sink); void remove_sink(logger::sink_ptr sink_ptr); - + queue_type& q(); //Wait to remaining items (if any) in the queue to be written and shutdown void shutdown(const std::chrono::milliseconds& timeout); @@ -82,20 +82,13 @@ inline void c11log::sinks::async_sink::_sink_it(const details::log_msg& msg) if(!_active || !msg_size) return; //re allocate on the heap the (stack based) message - auto new_msg = new details::log_msg(); - *new_msg = msg; + auto new_msg = new details::log_msg(msg); char *buf = new char[msg_size]; std::memcpy(buf, msg.msg_buf.first, msg_size); new_msg->msg_buf = bufpair_t(buf, msg_size); -/* - auto new_shared_msg = queue_type::item_type(new_msg, [](const details::log_msg* msg_to_delete) - { - delete []msg_to_delete->msg_buf.first; - delete msg_to_delete; - }); - * */ - queue_type::item_type new_shared_msg(new_msg, msg_deleter); + // Create unique_ptr with custom deleter and push it + queue_type::item_type new_shared_msg(new_msg, msg_deleter); _q.push(std::move(new_shared_msg)); } @@ -127,6 +120,11 @@ inline void c11log::sinks::async_sink::remove_sink(logger::sink_ptr sink_ptr) _sinks.erase(std::remove(_sinks.begin(), _sinks.end(), sink_ptr), _sinks.end()); } +inline c11log::sinks::async_sink::queue_type& c11log::sinks::async_sink::q() +{ + return _q; +} + inline void c11log::sinks::async_sink::shutdown(const std::chrono::milliseconds& timeout) {