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;