mirror of
https://github.com/gabime/spdlog.git
synced 2025-01-12 17:00:25 +08:00
Updated ringbuffer sink
This commit is contained in:
parent
cff6644b28
commit
0cb38085a1
@ -81,7 +81,7 @@ public:
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
return max_items_ - (head_ - tail_ );
|
return max_items_ - (head_ - tail_);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -90,7 +90,7 @@ public:
|
|||||||
const T &at(size_t i) const
|
const T &at(size_t i) const
|
||||||
{
|
{
|
||||||
assert(i < size());
|
assert(i < size());
|
||||||
return v_[(head_+ i) % max_items_];
|
return v_[(head_ + i) % max_items_];
|
||||||
}
|
}
|
||||||
|
|
||||||
// Pop item from front.
|
// Pop item from front.
|
||||||
|
@ -26,9 +26,7 @@ SPDLOG_INLINE log_msg_buffer::log_msg_buffer(const log_msg_buffer &other)
|
|||||||
update_string_views();
|
update_string_views();
|
||||||
}
|
}
|
||||||
|
|
||||||
SPDLOG_INLINE log_msg_buffer::log_msg_buffer(log_msg_buffer &&other) SPDLOG_NOEXCEPT
|
SPDLOG_INLINE log_msg_buffer::log_msg_buffer(log_msg_buffer &&other) SPDLOG_NOEXCEPT : log_msg{other}, buffer{std::move(other.buffer)}
|
||||||
: log_msg{other}
|
|
||||||
, buffer{std::move(other.buffer)}
|
|
||||||
{
|
{
|
||||||
update_string_views();
|
update_string_views();
|
||||||
}
|
}
|
||||||
|
@ -3,20 +3,19 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#ifndef SPDLOG_HEADER_ONLY
|
//#ifndef SPDLOG_HEADER_ONLY
|
||||||
#include "spdlog/sinks/ringbuffer_sink.h"
|
//#include "spdlog/sinks/ringbuffer_sink.h"
|
||||||
#endif
|
//#endif
|
||||||
|
|
||||||
#include "spdlog/common.h"
|
#include "spdlog/common.h"
|
||||||
#include "spdlog/details/os.h"
|
|
||||||
|
|
||||||
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 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>
|
template<typename Mutex>
|
||||||
@ -26,18 +25,31 @@ SPDLOG_INLINE void ringbuffer_sink<Mutex>::sink_it_(const details::log_msg &msg)
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<typename Mutex>
|
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_);
|
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;
|
std::vector<std::string> ret;
|
||||||
ret.reserve(lim);
|
ret.reserve(n_items);
|
||||||
size_t num=0;
|
for (size_t i = 0; i < n_items; i++)
|
||||||
for(size_t i=0; i<buf_.size(); i++){
|
{
|
||||||
num++;
|
|
||||||
memory_buf_t formatted;
|
memory_buf_t formatted;
|
||||||
base_sink<Mutex>::formatter_->format(buf_.at(i), formatted);
|
base_sink<Mutex>::formatter_->format(buf_.at(i), formatted);
|
||||||
ret.push_back(fmt::to_string(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;
|
return ret;
|
||||||
}
|
}
|
||||||
|
@ -3,9 +3,7 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#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/circular_q.h"
|
#include "spdlog/details/circular_q.h"
|
||||||
#include "spdlog/details/log_msg_buffer.h"
|
#include "spdlog/details/log_msg_buffer.h"
|
||||||
|
|
||||||
@ -22,15 +20,47 @@ template<typename Mutex>
|
|||||||
class ringbuffer_sink final : public base_sink<Mutex>
|
class ringbuffer_sink final : public base_sink<Mutex>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
explicit ringbuffer_sink(size_t buf_size);
|
explicit ringbuffer_sink(size_t n_items)
|
||||||
std::vector<std::string> last(size_t lim=0);
|
: 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:
|
protected:
|
||||||
void sink_it_(const details::log_msg &msg) override;
|
void sink_it_(const details::log_msg &msg) override
|
||||||
void flush_() override {};
|
{
|
||||||
|
q_.push_back(details::log_msg_buffer{msg});
|
||||||
|
}
|
||||||
|
void flush_() override {}
|
||||||
|
|
||||||
private:
|
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>;
|
using ringbuffer_sink_mt = ringbuffer_sink<std::mutex>;
|
||||||
@ -38,23 +68,4 @@ using ringbuffer_sink_st = ringbuffer_sink<details::null_mutex>;
|
|||||||
|
|
||||||
} // namespace sinks
|
} // 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
|
} // namespace spdlog
|
||||||
|
|
||||||
#ifdef SPDLOG_HEADER_ONLY
|
|
||||||
#include "ringbuffer_sink-inl.h"
|
|
||||||
#endif
|
|
||||||
|
Loading…
Reference in New Issue
Block a user