Commit 0cb38085 authored by gabime's avatar gabime

Updated ringbuffer sink

parent cff6644b
......@@ -77,11 +77,11 @@ public:
{
if (tail_ >= head_)
{
return tail_ - head_;
return tail_ - head_;
}
else
{
return max_items_ - (head_ - tail_ );
return max_items_ - (head_ - tail_);
}
}
......@@ -90,7 +90,7 @@ public:
const T &at(size_t i) const
{
assert(i < size());
return v_[(head_+ i) % max_items_];
return v_[(head_ + i) % max_items_];
}
// Pop item from front.
......
......@@ -26,9 +26,7 @@ SPDLOG_INLINE log_msg_buffer::log_msg_buffer(const log_msg_buffer &other)
update_string_views();
}
SPDLOG_INLINE log_msg_buffer::log_msg_buffer(log_msg_buffer &&other) SPDLOG_NOEXCEPT
: log_msg{other}
, buffer{std::move(other.buffer)}
SPDLOG_INLINE log_msg_buffer::log_msg_buffer(log_msg_buffer &&other) SPDLOG_NOEXCEPT : log_msg{other}, buffer{std::move(other.buffer)}
{
update_string_views();
}
......
......@@ -3,20 +3,19 @@
#pragma once
#ifndef SPDLOG_HEADER_ONLY
#include "spdlog/sinks/ringbuffer_sink.h"
#endif
//#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<typename Mutex>
SPDLOG_INLINE ringbuffer_sink<Mutex>::ringbuffer_sink(size_t buf_size)
SPDLOG_INLINE ringbuffer_sink<Mutex>::ringbuffer_sink(size_t n_items)
{
buf_=details::circular_q<details::log_msg_buffer>(buf_size);
buf_ = details::circular_q<details::log_msg_buffer>(n_items);
}
template<typename Mutex>
......@@ -26,18 +25,31 @@ SPDLOG_INLINE void ringbuffer_sink<Mutex>::sink_it_(const details::log_msg &msg)
}
template<typename Mutex>
SPDLOG_INLINE std::vector<std::string> ringbuffer_sink<Mutex>::last(size_t lim)
SPDLOG_INLINE std::vector<std::string> ringbuffer_sink<Mutex>::formatted_messages(size_t lim)
{
std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
auto n_items = lim > 0 ? (std::min)(lim, buf_.size()) : buf_.size();
std::vector<std::string> ret;
ret.reserve(lim);
size_t num=0;
for(size_t i=0; i<buf_.size(); i++){
num++;
ret.reserve(n_items);
for (size_t i = 0; i < n_items; i++)
{
memory_buf_t formatted;
base_sink<Mutex>::formatter_->format(buf_.at(i), formatted);
ret.push_back(fmt::to_string(formatted));
if(lim>0 && num==lim) break;
}
return ret;
}
template<typename Mutex>
SPDLOG_INLINE std::vector<details::log_msg_buffer> ringbuffer_sink<Mutex>::raw_messages(size_t lim)
{
std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
auto n_items = lim > 0 ? (std::min)(lim, buf_.size()) : buf_.size();
std::vector<details::log_msg_buffer> ret;
ret.reserve(n_items);
for (size_t i = 0; i < n_items; i++)
{
ret.push_back(buf_.at(i));
}
return ret;
}
......
......@@ -3,9 +3,7 @@
#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"
......@@ -22,15 +20,47 @@ template<typename Mutex>
class ringbuffer_sink final : public base_sink<Mutex>
{
public:
explicit ringbuffer_sink(size_t buf_size);
std::vector<std::string> last(size_t lim=0);
explicit ringbuffer_sink(size_t n_items)
: q_{n_items}
{}
std::vector<details::log_msg_buffer> last_raw(size_t lim = 0)
{
std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
auto n_items = lim > 0 ? (std::min)(lim, q_.size()) : q_.size();
std::vector<details::log_msg_buffer> ret;
ret.reserve(n_items);
for (size_t i = 0; i < n_items; i++)
{
ret.push_back(q_.at(i));
}
return ret;
}
std::vector<std::string> last_formatted(size_t lim = 0)
{
std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
auto n_items = lim > 0 ? (std::min)(lim, q_.size()) : q_.size();
std::vector<std::string> ret;
ret.reserve(n_items);
for (size_t i = 0; i < n_items; i++)
{
memory_buf_t formatted;
base_sink<Mutex>::formatter_->format(q_.at(i), formatted);
ret.push_back(fmt::to_string(formatted));
}
return ret;
}
protected:
void sink_it_(const details::log_msg &msg) override;
void flush_() override {};
void sink_it_(const details::log_msg &msg) override
{
q_.push_back(details::log_msg_buffer{msg});
}
void flush_() override {}
private:
details::circular_q<details::log_msg_buffer> buf_;
details::circular_q<details::log_msg_buffer> q_;
};
using ringbuffer_sink_mt = ringbuffer_sink<std::mutex>;
......@@ -38,23 +68,4 @@ using ringbuffer_sink_st = ringbuffer_sink<details::null_mutex>;
} // namespace sinks
//
// factory functions
//
template<typename Factory = spdlog::synchronous_factory>
inline std::shared_ptr<logger> basic_logger_mt(const std::string &logger_name, size_t buf_size)
{
return Factory::template create<sinks::ringbuffer_sink_mt>(logger_name, buf_size);
}
template<typename Factory = spdlog::synchronous_factory>
inline std::shared_ptr<logger> basic_logger_st(const std::string &logger_name, size_t buf_size)
{
return Factory::template create<sinks::ringbuffer_sink_st>(logger_name, buf_size);
}
} // namespace spdlog
#ifdef SPDLOG_HEADER_ONLY
#include "ringbuffer_sink-inl.h"
#endif
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