Commit 6f0cb636 authored by Václav Šmilauer's avatar Václav Šmilauer

Move ringbuffer_sink to spdlog::details::circular_q, enhance its API: size(), at(i)

parent acf32be8
...@@ -72,6 +72,20 @@ public: ...@@ -72,6 +72,20 @@ public:
return v_[head_]; 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. // Pop item from front.
// If there are no elements in the container, the behavior is undefined. // If there are no elements in the container, the behavior is undefined.
void pop_front() void pop_front()
......
...@@ -10,15 +10,13 @@ ...@@ -10,15 +10,13 @@
#include "spdlog/common.h" #include "spdlog/common.h"
#include "spdlog/details/os.h" #include "spdlog/details/os.h"
#include<boost/circular_buffer.hpp>
namespace spdlog { namespace spdlog {
namespace sinks { namespace sinks {
template<typename Mutex> template<typename Mutex>
SPDLOG_INLINE ringbuffer_sink<Mutex>::ringbuffer_sink(size_t buf_size) SPDLOG_INLINE ringbuffer_sink<Mutex>::ringbuffer_sink(size_t buf_size)
{ {
buf.set_capacity(buf_size); buf=details::circular_q<std::string>(buf_size);
} }
template<typename Mutex> template<typename Mutex>
...@@ -26,7 +24,7 @@ SPDLOG_INLINE void ringbuffer_sink<Mutex>::sink_it_(const details::log_msg &msg) ...@@ -26,7 +24,7 @@ SPDLOG_INLINE void ringbuffer_sink<Mutex>::sink_it_(const details::log_msg &msg)
{ {
memory_buf_t formatted; memory_buf_t formatted;
base_sink<Mutex>::formatter_->format(msg, formatted); base_sink<Mutex>::formatter_->format(msg, formatted);
buf.push_front(fmt::to_string(formatted)); buf.push_back(fmt::to_string(formatted));
} }
template<typename Mutex> template<typename Mutex>
...@@ -36,9 +34,9 @@ SPDLOG_INLINE std::vector<std::string> ringbuffer_sink<Mutex>::last(size_t lim) ...@@ -36,9 +34,9 @@ SPDLOG_INLINE std::vector<std::string> ringbuffer_sink<Mutex>::last(size_t lim)
std::vector<std::string> ret; std::vector<std::string> ret;
ret.reserve(lim); ret.reserve(lim);
size_t num=0; size_t num=0;
for(const std::string& msg: buf){ for(size_t i=0; i<buf.size(); i++){
num++; num++;
ret.push_back(msg); ret.push_back(buf.at(i));
if(lim>0 && num==lim) break; if(lim>0 && num==lim) break;
} }
return ret; return ret;
......
...@@ -6,13 +6,12 @@ ...@@ -6,13 +6,12 @@
#include "spdlog/details/null_mutex.h" #include "spdlog/details/null_mutex.h"
#include "spdlog/sinks/base_sink.h" #include "spdlog/sinks/base_sink.h"
#include "spdlog/details/synchronous_factory.h" #include "spdlog/details/synchronous_factory.h"
#include "spdlog/details/circular_q.h"
#include <mutex> #include <mutex>
#include <string> #include <string>
#include <vector> #include <vector>
#include<boost/circular_buffer.hpp>
namespace spdlog { namespace spdlog {
namespace sinks { namespace sinks {
/* /*
...@@ -30,7 +29,7 @@ protected: ...@@ -30,7 +29,7 @@ protected:
void flush_() override {}; void flush_() override {};
private: private:
boost::circular_buffer<std::string> buf; details::circular_q<std::string> buf;
}; };
using ringbuffer_sink_mt = ringbuffer_sink<std::mutex>; using ringbuffer_sink_mt = ringbuffer_sink<std::mutex>;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment