Added support for log-overrun-policy in async_logger wait()

pull/2505/head
shifu-dev 3 years ago
parent 234f4595b2
commit be65b8ef4f

@ -0,0 +1,71 @@
{
"C_Cpp.default.configurationProvider": "ms-vscode.cmake-tools",
"editor.formatOnSave": false,
"files.associations": {
"condition_variable": "cpp",
"bitset": "cpp",
"memory": "cpp",
"random": "cpp",
"future": "cpp",
"optional": "cpp",
"array": "cpp",
"atomic": "cpp",
"bit": "cpp",
"*.tcc": "cpp",
"cctype": "cpp",
"charconv": "cpp",
"chrono": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"compare": "cpp",
"concepts": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"deque": "cpp",
"map": "cpp",
"set": "cpp",
"string": "cpp",
"unordered_map": "cpp",
"vector": "cpp",
"exception": "cpp",
"algorithm": "cpp",
"functional": "cpp",
"iterator": "cpp",
"memory_resource": "cpp",
"numeric": "cpp",
"ratio": "cpp",
"regex": "cpp",
"string_view": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"utility": "cpp",
"fstream": "cpp",
"initializer_list": "cpp",
"iomanip": "cpp",
"iosfwd": "cpp",
"iostream": "cpp",
"istream": "cpp",
"limits": "cpp",
"mutex": "cpp",
"new": "cpp",
"numbers": "cpp",
"ostream": "cpp",
"semaphore": "cpp",
"span": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"stop_token": "cpp",
"streambuf": "cpp",
"thread": "cpp",
"typeinfo": "cpp",
"variant": "cpp"
}
}

@ -79,7 +79,7 @@ SPDLOG_INLINE void spdlog::async_logger::sink_it_(const details::log_msg &msg)
{ {
if (auto pool_ptr = thread_pool_.lock()) if (auto pool_ptr = thread_pool_.lock())
{ {
on_log_dispatched_(); on_log_dispatch_();
pool_ptr->post_log(shared_from_this(), msg, overflow_policy_); pool_ptr->post_log(shared_from_this(), msg, overflow_policy_);
} }
else else
@ -93,7 +93,7 @@ SPDLOG_INLINE void spdlog::async_logger::flush_()
{ {
if (auto pool_ptr = thread_pool_.lock()) if (auto pool_ptr = thread_pool_.lock())
{ {
on_log_dispatched_(); on_log_dispatch_();
pool_ptr->post_flush(shared_from_this(), overflow_policy_); pool_ptr->post_flush(shared_from_this(), overflow_policy_);
/// this is to provide blocking functionality through logger(not async_logger) interface /// this is to provide blocking functionality through logger(not async_logger) interface
@ -113,7 +113,7 @@ SPDLOG_INLINE void spdlog::async_logger::flush_()
// //
SPDLOG_INLINE void spdlog::async_logger::backend_sink_it_(const details::log_msg &msg) SPDLOG_INLINE void spdlog::async_logger::backend_sink_it_(const details::log_msg &msg)
{ {
on_logged_(); on_log_write_();
for (auto& sink : sinks_) for (auto& sink : sinks_)
{ {
@ -135,7 +135,7 @@ SPDLOG_INLINE void spdlog::async_logger::backend_sink_it_(const details::log_msg
SPDLOG_INLINE void spdlog::async_logger::backend_flush_() SPDLOG_INLINE void spdlog::async_logger::backend_flush_()
{ {
on_logged_(); on_log_write_();
for (auto& sink : sinks_) for (auto& sink : sinks_)
{ {
@ -147,20 +147,20 @@ SPDLOG_INLINE void spdlog::async_logger::backend_flush_()
} }
} }
SPDLOG_INLINE void spdlog::async_logger::on_log_dispatched_() SPDLOG_INLINE void spdlog::async_logger::on_log_dispatch_()
{ {
++pending_log_count_; ++pending_log_count_;
} }
SPDLOG_INLINE void spdlog::async_logger::on_logged_() SPDLOG_INLINE void spdlog::async_logger::on_log_write_()
{ {
if (pending_log_count_ == 0) if (pending_log_count_ == 0)
{ {
throw_spdlog_ex("all dispatched messages by logger " throw_spdlog_ex("all dispatched messages by logger "
+ name_ + " have already been logged, but on_message_logged() was invoked"); + name_ + " have already been logged, but on_log_write_() was invoked");
} }
pending_log_count_--; --pending_log_count_;
if (pending_log_count_ == 0) if (pending_log_count_ == 0)
{ {
@ -168,6 +168,11 @@ SPDLOG_INLINE void spdlog::async_logger::on_logged_()
} }
} }
SPDLOG_INLINE void spdlog::async_logger::on_log_dump_()
{
on_log_write_();
}
SPDLOG_INLINE std::shared_ptr<spdlog::logger> spdlog::async_logger::clone(std::string new_name) SPDLOG_INLINE std::shared_ptr<spdlog::logger> spdlog::async_logger::clone(std::string new_name)
{ {
auto cloned = std::make_shared<spdlog::async_logger>(*this); auto cloned = std::make_shared<spdlog::async_logger>(*this);

@ -78,8 +78,9 @@ protected:
void backend_sink_it_(const details::log_msg &incoming_log_msg); void backend_sink_it_(const details::log_msg &incoming_log_msg);
void backend_flush_(); void backend_flush_();
void on_log_dispatched_(); void on_log_dispatch_();
void on_logged_(); void on_log_write_();
void on_log_dump_();
private: private:
std::weak_ptr<details::thread_pool> thread_pool_; std::weak_ptr<details::thread_pool> thread_pool_;

@ -46,18 +46,25 @@ public:
} }
// push back, overrun (oldest) item if no room left // push back, overrun (oldest) item if no room left
void push_back(T &&item) void push_back(T &&item, void(*callback)(T&& element) = nullptr)
{ {
if (max_items_ > 0) if (max_items_ > 0)
{ {
v_[tail_] = std::move(item); auto next_tail = (tail_ + 1) % max_items_;
tail_ = (tail_ + 1) % max_items_; if (next_tail == head_)
if (tail_ == head_) // overrun last item if full
{ {
if (callback != nullptr)
{
callback(std::move(v_[tail_]));
}
// overrun last item if full
head_ = (head_ + 1) % max_items_; head_ = (head_ + 1) % max_items_;
++overrun_counter_; ++overrun_counter_;
} }
v_[tail_] = std::move(item);
tail_ = next_tail;
} }
} }

@ -40,7 +40,7 @@ public:
} }
// enqueue immediately. overrun oldest message in the queue if no room left. // enqueue immediately. overrun oldest message in the queue if no room left.
void enqueue_nowait(T &&item) void enqueue_nowait(T &&item, void(*callback)(T&& element) = nullptr)
{ {
{ {
std::unique_lock<std::mutex> lock(queue_mutex_); std::unique_lock<std::mutex> lock(queue_mutex_);

@ -93,7 +93,12 @@ void SPDLOG_INLINE thread_pool::post_async_msg_(async_msg &&new_msg, async_overf
} }
else else
{ {
q_.enqueue_nowait(std::move(new_msg)); auto overrun_callback = [](item_type&& item)
{
item.worker_ptr->on_log_dump_();
};
q_.enqueue_nowait(std::move(new_msg), overrun_callback);
} }
} }

Loading…
Cancel
Save