From be65b8ef4f1107d6b53594880f33b0729178cfd5 Mon Sep 17 00:00:00 2001 From: shifu-dev Date: Tue, 11 Oct 2022 19:25:29 +0530 Subject: [PATCH] Added support for log-overrun-policy in async_logger wait() --- .vscode/settings.json | 71 ++++++++++++++++++++++++ include/spdlog/async_logger-inl.h | 21 ++++--- include/spdlog/async_logger.h | 5 +- include/spdlog/details/circular_q.h | 17 ++++-- include/spdlog/details/mpmc_blocking_q.h | 2 +- include/spdlog/details/thread_pool-inl.h | 7 ++- 6 files changed, 106 insertions(+), 17 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 00000000..4cb928b9 --- /dev/null +++ b/.vscode/settings.json @@ -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" + } +} \ No newline at end of file diff --git a/include/spdlog/async_logger-inl.h b/include/spdlog/async_logger-inl.h index 54f1096b..bde68c25 100644 --- a/include/spdlog/async_logger-inl.h +++ b/include/spdlog/async_logger-inl.h @@ -79,7 +79,7 @@ SPDLOG_INLINE void spdlog::async_logger::sink_it_(const details::log_msg &msg) { if (auto pool_ptr = thread_pool_.lock()) { - on_log_dispatched_(); + on_log_dispatch_(); pool_ptr->post_log(shared_from_this(), msg, overflow_policy_); } else @@ -93,7 +93,7 @@ SPDLOG_INLINE void spdlog::async_logger::flush_() { if (auto pool_ptr = thread_pool_.lock()) { - on_log_dispatched_(); + on_log_dispatch_(); pool_ptr->post_flush(shared_from_this(), overflow_policy_); /// 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) { - on_logged_(); + on_log_write_(); 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_() { - on_logged_(); + on_log_write_(); 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_; } -SPDLOG_INLINE void spdlog::async_logger::on_logged_() +SPDLOG_INLINE void spdlog::async_logger::on_log_write_() { if (pending_log_count_ == 0) { 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) { @@ -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::async_logger::clone(std::string new_name) { auto cloned = std::make_shared(*this); diff --git a/include/spdlog/async_logger.h b/include/spdlog/async_logger.h index 3060a9c9..160e7f39 100644 --- a/include/spdlog/async_logger.h +++ b/include/spdlog/async_logger.h @@ -78,8 +78,9 @@ protected: void backend_sink_it_(const details::log_msg &incoming_log_msg); void backend_flush_(); - void on_log_dispatched_(); - void on_logged_(); + void on_log_dispatch_(); + void on_log_write_(); + void on_log_dump_(); private: std::weak_ptr thread_pool_; diff --git a/include/spdlog/details/circular_q.h b/include/spdlog/details/circular_q.h index e4fd5fd4..b1844dca 100644 --- a/include/spdlog/details/circular_q.h +++ b/include/spdlog/details/circular_q.h @@ -46,18 +46,25 @@ public: } // 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) { - v_[tail_] = std::move(item); - tail_ = (tail_ + 1) % max_items_; - - if (tail_ == head_) // overrun last item if full + auto next_tail = (tail_ + 1) % max_items_; + if (next_tail == head_) { + if (callback != nullptr) + { + callback(std::move(v_[tail_])); + } + + // overrun last item if full head_ = (head_ + 1) % max_items_; ++overrun_counter_; } + + v_[tail_] = std::move(item); + tail_ = next_tail; } } diff --git a/include/spdlog/details/mpmc_blocking_q.h b/include/spdlog/details/mpmc_blocking_q.h index 785180c1..1dca3002 100644 --- a/include/spdlog/details/mpmc_blocking_q.h +++ b/include/spdlog/details/mpmc_blocking_q.h @@ -40,7 +40,7 @@ public: } // 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 lock(queue_mutex_); diff --git a/include/spdlog/details/thread_pool-inl.h b/include/spdlog/details/thread_pool-inl.h index 369f30fe..bab005bd 100644 --- a/include/spdlog/details/thread_pool-inl.h +++ b/include/spdlog/details/thread_pool-inl.h @@ -93,7 +93,12 @@ void SPDLOG_INLINE thread_pool::post_async_msg_(async_msg &&new_msg, async_overf } 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); } }