From acf32be842d654770fadb4f7a799ac9fd260860f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=A1clav=20=C5=A0milauer?= Date: Fri, 8 Nov 2019 15:29:20 +0100 Subject: [PATCH 1/7] Add ringbuffer sink (requires boost::circular_buffer) Ringbuffer sink keeps user-given number of most recent log messages in memory and returns them upon request (using the ringbuffer_sink::last method). This can be useful for e.g. remote debugging of a running app. --- include/spdlog/sinks/ringbuffer_sink-inl.h | 48 +++++++++++++++++ include/spdlog/sinks/ringbuffer_sink.h | 60 ++++++++++++++++++++++ 2 files changed, 108 insertions(+) create mode 100644 include/spdlog/sinks/ringbuffer_sink-inl.h create mode 100644 include/spdlog/sinks/ringbuffer_sink.h diff --git a/include/spdlog/sinks/ringbuffer_sink-inl.h b/include/spdlog/sinks/ringbuffer_sink-inl.h new file mode 100644 index 00000000..413fb57b --- /dev/null +++ b/include/spdlog/sinks/ringbuffer_sink-inl.h @@ -0,0 +1,48 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +#ifndef SPDLOG_HEADER_ONLY +#include "spdlog/sinks/ringbuffer_sink.h" +#endif + +#include "spdlog/common.h" +#include "spdlog/details/os.h" + +#include + +namespace spdlog { +namespace sinks { + +template +SPDLOG_INLINE ringbuffer_sink::ringbuffer_sink(size_t buf_size) +{ + buf.set_capacity(buf_size); +} + +template +SPDLOG_INLINE void ringbuffer_sink::sink_it_(const details::log_msg &msg) +{ + memory_buf_t formatted; + base_sink::formatter_->format(msg, formatted); + buf.push_front(fmt::to_string(formatted)); +} + +template +SPDLOG_INLINE std::vector ringbuffer_sink::last(size_t lim) +{ + std::lock_guard lock(base_sink::mutex_); + std::vector ret; + ret.reserve(lim); + size_t num=0; + for(const std::string& msg: buf){ + num++; + ret.push_back(msg); + if(lim>0 && num==lim) break; + } + return ret; +} + +} // namespace sinks +} // namespace spdlog diff --git a/include/spdlog/sinks/ringbuffer_sink.h b/include/spdlog/sinks/ringbuffer_sink.h new file mode 100644 index 00000000..35b5e86b --- /dev/null +++ b/include/spdlog/sinks/ringbuffer_sink.h @@ -0,0 +1,60 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +#include "spdlog/details/null_mutex.h" +#include "spdlog/sinks/base_sink.h" +#include "spdlog/details/synchronous_factory.h" + +#include +#include +#include + +#include + +namespace spdlog { +namespace sinks { +/* + * Ring buffer sink + */ +template +class ringbuffer_sink final : public base_sink +{ +public: + explicit ringbuffer_sink(size_t buf_size); + std::vector last(size_t lim=0); + +protected: + void sink_it_(const details::log_msg &msg) override; + void flush_() override {}; + +private: + boost::circular_buffer buf; +}; + +using ringbuffer_sink_mt = ringbuffer_sink; +using ringbuffer_sink_st = ringbuffer_sink; + +} // namespace sinks + +// +// factory functions +// +template +inline std::shared_ptr basic_logger_mt(const std::string &logger_name, size_t buf_size) +{ + return Factory::template create(logger_name, buf_size); +} + +template +inline std::shared_ptr basic_logger_st(const std::string &logger_name, size_t buf_size) +{ + return Factory::template create(logger_name, buf_size); +} + +} // namespace spdlog + +#ifdef SPDLOG_HEADER_ONLY +#include "ringbuffer_sink-inl.h" +#endif From 6f0cb6365ed6bb1c2fa6cf25b6f3471474b2ae0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=A1clav=20=C5=A0milauer?= Date: Fri, 8 Nov 2019 19:25:31 +0100 Subject: [PATCH 2/7] Move ringbuffer_sink to spdlog::details::circular_q, enhance its API: size(), at(i) --- include/spdlog/details/circular_q.h | 14 ++++++++++++++ include/spdlog/sinks/ringbuffer_sink-inl.h | 10 ++++------ include/spdlog/sinks/ringbuffer_sink.h | 5 ++--- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/include/spdlog/details/circular_q.h b/include/spdlog/details/circular_q.h index 4a87940d..aac4223f 100644 --- a/include/spdlog/details/circular_q.h +++ b/include/spdlog/details/circular_q.h @@ -72,6 +72,20 @@ public: return v_[head_]; } + // Return number of elements actually stored + size_t size() const + { + return (tail_ - head_) % max_items_; + } + + // Return const reference to item by index. + // If index is out of range 0…size()-1, the behavior is undefined. + const T &at(size_t i) const + { + assert(i < size()); + return v_[(head_+ i) % max_items_]; + } + // Pop item from front. // If there are no elements in the container, the behavior is undefined. void pop_front() diff --git a/include/spdlog/sinks/ringbuffer_sink-inl.h b/include/spdlog/sinks/ringbuffer_sink-inl.h index 413fb57b..d0f1a45b 100644 --- a/include/spdlog/sinks/ringbuffer_sink-inl.h +++ b/include/spdlog/sinks/ringbuffer_sink-inl.h @@ -10,15 +10,13 @@ #include "spdlog/common.h" #include "spdlog/details/os.h" -#include - namespace spdlog { namespace sinks { template SPDLOG_INLINE ringbuffer_sink::ringbuffer_sink(size_t buf_size) { - buf.set_capacity(buf_size); + buf=details::circular_q(buf_size); } template @@ -26,7 +24,7 @@ SPDLOG_INLINE void ringbuffer_sink::sink_it_(const details::log_msg &msg) { memory_buf_t formatted; base_sink::formatter_->format(msg, formatted); - buf.push_front(fmt::to_string(formatted)); + buf.push_back(fmt::to_string(formatted)); } template @@ -36,9 +34,9 @@ SPDLOG_INLINE std::vector ringbuffer_sink::last(size_t lim) std::vector ret; ret.reserve(lim); size_t num=0; - for(const std::string& msg: buf){ + for(size_t i=0; i0 && num==lim) break; } return ret; diff --git a/include/spdlog/sinks/ringbuffer_sink.h b/include/spdlog/sinks/ringbuffer_sink.h index 35b5e86b..c51628f4 100644 --- a/include/spdlog/sinks/ringbuffer_sink.h +++ b/include/spdlog/sinks/ringbuffer_sink.h @@ -6,13 +6,12 @@ #include "spdlog/details/null_mutex.h" #include "spdlog/sinks/base_sink.h" #include "spdlog/details/synchronous_factory.h" +#include "spdlog/details/circular_q.h" #include #include #include -#include - namespace spdlog { namespace sinks { /* @@ -30,7 +29,7 @@ protected: void flush_() override {}; private: - boost::circular_buffer buf; + details::circular_q buf; }; using ringbuffer_sink_mt = ringbuffer_sink; From bad728446564bc705ee5b348015a8ef0afcb7336 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=A1clav=20=C5=A0milauer?= Date: Sat, 9 Nov 2019 09:30:27 +0100 Subject: [PATCH 3/7] Fix cicular_q::size() --- include/spdlog/details/circular_q.h | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/include/spdlog/details/circular_q.h b/include/spdlog/details/circular_q.h index aac4223f..d07f0725 100644 --- a/include/spdlog/details/circular_q.h +++ b/include/spdlog/details/circular_q.h @@ -75,7 +75,14 @@ public: // Return number of elements actually stored size_t size() const { - return (tail_ - head_) % max_items_; + if (tail_ > head_) + { + return (tail_ - head_) % max_items; + } + else + { + return max_items - (head_ - tail_ ) % max_items; + } } // Return const reference to item by index. From 042045b99842d07ba193f36a4226a9232524398c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=A1clav=20=C5=A0milauer?= Date: Sat, 9 Nov 2019 09:35:05 +0100 Subject: [PATCH 4/7] typo fix --- include/spdlog/details/circular_q.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/spdlog/details/circular_q.h b/include/spdlog/details/circular_q.h index d07f0725..f06cd8e5 100644 --- a/include/spdlog/details/circular_q.h +++ b/include/spdlog/details/circular_q.h @@ -77,11 +77,11 @@ public: { if (tail_ > head_) { - return (tail_ - head_) % max_items; + return (tail_ - head_) % max_items_; } else { - return max_items - (head_ - tail_ ) % max_items; + return max_items_ - (head_ - tail_ ) % max_items_; } } From daef0a23747690091eb62aeeebdafd90672febfc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=A1clav=20=C5=A0milauer?= Date: Sat, 9 Nov 2019 13:15:57 +0100 Subject: [PATCH 5/7] Fix circular_q::size() --- include/spdlog/details/circular_q.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/spdlog/details/circular_q.h b/include/spdlog/details/circular_q.h index f06cd8e5..a37b7770 100644 --- a/include/spdlog/details/circular_q.h +++ b/include/spdlog/details/circular_q.h @@ -77,11 +77,11 @@ public: { if (tail_ > head_) { - return (tail_ - head_) % max_items_; + return tail_ - head_; } else { - return max_items_ - (head_ - tail_ ) % max_items_; + return max_items_ - (head_ - tail_ ); } } From 62e09e73f7437c2d022611397f39b10dd21d7c2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=A1clav=20=C5=A0milauer?= Date: Sat, 9 Nov 2019 13:48:04 +0100 Subject: [PATCH 6/7] defer formatting, use log_msg_buffer for intermediate storage --- include/spdlog/sinks/ringbuffer_sink-inl.h | 12 ++++++------ include/spdlog/sinks/ringbuffer_sink.h | 3 ++- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/include/spdlog/sinks/ringbuffer_sink-inl.h b/include/spdlog/sinks/ringbuffer_sink-inl.h index d0f1a45b..d218a975 100644 --- a/include/spdlog/sinks/ringbuffer_sink-inl.h +++ b/include/spdlog/sinks/ringbuffer_sink-inl.h @@ -16,15 +16,13 @@ namespace sinks { template SPDLOG_INLINE ringbuffer_sink::ringbuffer_sink(size_t buf_size) { - buf=details::circular_q(buf_size); + buf_=details::circular_q(buf_size); } template SPDLOG_INLINE void ringbuffer_sink::sink_it_(const details::log_msg &msg) { - memory_buf_t formatted; - base_sink::formatter_->format(msg, formatted); - buf.push_back(fmt::to_string(formatted)); + buf_.push_back(details::log_msg_buffer{msg}); } template @@ -34,9 +32,11 @@ SPDLOG_INLINE std::vector ringbuffer_sink::last(size_t lim) std::vector ret; ret.reserve(lim); size_t num=0; - for(size_t i=0; i::formatter_->format(buf_.at(i), formatted); + ret.push_back(fmt::to_string(formatted)); if(lim>0 && num==lim) break; } return ret; diff --git a/include/spdlog/sinks/ringbuffer_sink.h b/include/spdlog/sinks/ringbuffer_sink.h index c51628f4..a5933744 100644 --- a/include/spdlog/sinks/ringbuffer_sink.h +++ b/include/spdlog/sinks/ringbuffer_sink.h @@ -7,6 +7,7 @@ #include "spdlog/sinks/base_sink.h" #include "spdlog/details/synchronous_factory.h" #include "spdlog/details/circular_q.h" +#include "spdlog/details/log_msg_buffer.h" #include #include @@ -29,7 +30,7 @@ protected: void flush_() override {}; private: - details::circular_q buf; + details::circular_q buf_; }; using ringbuffer_sink_mt = ringbuffer_sink; From 63837530ed5575c87f740842d68c2137bc900683 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=A1clav=20=C5=A0milauer?= Date: Sat, 9 Nov 2019 19:26:49 +0100 Subject: [PATCH 7/7] Fix circular_q::size() empty case --- include/spdlog/details/circular_q.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/spdlog/details/circular_q.h b/include/spdlog/details/circular_q.h index a37b7770..60b8d157 100644 --- a/include/spdlog/details/circular_q.h +++ b/include/spdlog/details/circular_q.h @@ -75,7 +75,7 @@ public: // Return number of elements actually stored size_t size() const { - if (tail_ > head_) + if (tail_ >= head_) { return tail_ - head_; }