Commit acf32be8 authored by Václav Šmilauer's avatar Václav Šmilauer

Add ringbuffer sink (requires boost::circular_buffer)

Ringbuffer sink keeps user-given number of most recent log messages in
memory and returns them upon request (using the ringbuffer_sink::last
method). This can be useful for e.g. remote debugging of a running app.
parent de2c07ac
// 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"
#include<boost/circular_buffer.hpp>
namespace spdlog {
namespace sinks {
template<typename Mutex>
SPDLOG_INLINE ringbuffer_sink<Mutex>::ringbuffer_sink(size_t buf_size)
{
buf.set_capacity(buf_size);
}
template<typename Mutex>
SPDLOG_INLINE void ringbuffer_sink<Mutex>::sink_it_(const details::log_msg &msg)
{
memory_buf_t formatted;
base_sink<Mutex>::formatter_->format(msg, formatted);
buf.push_front(fmt::to_string(formatted));
}
template<typename Mutex>
SPDLOG_INLINE std::vector<std::string> ringbuffer_sink<Mutex>::last(size_t lim)
{
std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
std::vector<std::string> ret;
ret.reserve(lim);
size_t num=0;
for(const std::string& msg: buf){
num++;
ret.push_back(msg);
if(lim>0 && num==lim) break;
}
return ret;
}
} // namespace sinks
} // namespace spdlog
// 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 <mutex>
#include <string>
#include <vector>
#include<boost/circular_buffer.hpp>
namespace spdlog {
namespace sinks {
/*
* Ring buffer sink
*/
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);
protected:
void sink_it_(const details::log_msg &msg) override;
void flush_() override {};
private:
boost::circular_buffer<std::string> buf;
};
using ringbuffer_sink_mt = ringbuffer_sink<std::mutex>;
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