From 03abdf49a01e8ca4525b6a76a48e2ca56309da2d Mon Sep 17 00:00:00 2001 From: Jaakko Rantala Date: Wed, 20 May 2020 19:05:24 +0300 Subject: [PATCH] Fixed ringbuffer_sink last_raw and last_formatted giving the first lim number of elements instead of last. --- include/spdlog/sinks/ringbuffer_sink.h | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/include/spdlog/sinks/ringbuffer_sink.h b/include/spdlog/sinks/ringbuffer_sink.h index 16f7cb56..1ee3f691 100644 --- a/include/spdlog/sinks/ringbuffer_sink.h +++ b/include/spdlog/sinks/ringbuffer_sink.h @@ -28,10 +28,11 @@ public: std::vector last_raw(size_t lim = 0) { std::lock_guard lock(base_sink::mutex_); - auto n_items = lim > 0 ? (std::min)(lim, q_.size()) : q_.size(); + auto items_available = q_.size(); + auto n_items = lim > 0 ? (std::min)(lim, items_available) : items_available; std::vector ret; ret.reserve(n_items); - for (size_t i = 0; i < n_items; i++) + for (size_t i = (items_available - n_items); i < items_available; i++) { ret.push_back(q_.at(i)); } @@ -41,10 +42,11 @@ public: std::vector last_formatted(size_t lim = 0) { std::lock_guard lock(base_sink::mutex_); - auto n_items = lim > 0 ? (std::min)(lim, q_.size()) : q_.size(); + auto items_available = q_.size(); + auto n_items = lim > 0 ? (std::min)(lim, items_available) : items_available; std::vector ret; ret.reserve(n_items); - for (size_t i = 0; i < n_items; i++) + for (size_t i = (items_available - n_items); i < items_available; i++) { memory_buf_t formatted; base_sink::formatter_->format(q_.at(i), formatted);