diff --git a/include/spdlog/details/circular_q.h b/include/spdlog/details/circular_q.h index 4a87940d..60b8d157 100644 --- a/include/spdlog/details/circular_q.h +++ b/include/spdlog/details/circular_q.h @@ -72,6 +72,27 @@ public: return v_[head_]; } + // Return number of elements actually stored + size_t size() const + { + if (tail_ >= head_) + { + return tail_ - head_; + } + else + { + return max_items_ - (head_ - tail_ ); + } + } + + // 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 new file mode 100644 index 00000000..d218a975 --- /dev/null +++ b/include/spdlog/sinks/ringbuffer_sink-inl.h @@ -0,0 +1,46 @@ +// 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" + +namespace spdlog { +namespace sinks { + +template +SPDLOG_INLINE ringbuffer_sink::ringbuffer_sink(size_t buf_size) +{ + buf_=details::circular_q(buf_size); +} + +template +SPDLOG_INLINE void ringbuffer_sink::sink_it_(const details::log_msg &msg) +{ + buf_.push_back(details::log_msg_buffer{msg}); +} + +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(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; +} + +} // 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..a5933744 --- /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 "spdlog/details/circular_q.h" +#include "spdlog/details/log_msg_buffer.h" + +#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: + details::circular_q 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