Merge pull request #1307 from eudoxos/ringbuffer-sink
Add ringbuffer sink
This commit is contained in:
		@@ -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()
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										46
									
								
								include/spdlog/sinks/ringbuffer_sink-inl.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										46
									
								
								include/spdlog/sinks/ringbuffer_sink-inl.h
									
									
									
									
									
										Normal file
									
								
							@@ -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<typename Mutex>
 | 
			
		||||
SPDLOG_INLINE ringbuffer_sink<Mutex>::ringbuffer_sink(size_t buf_size)
 | 
			
		||||
{
 | 
			
		||||
    buf_=details::circular_q<details::log_msg_buffer>(buf_size);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
template<typename Mutex>
 | 
			
		||||
SPDLOG_INLINE void ringbuffer_sink<Mutex>::sink_it_(const details::log_msg &msg)
 | 
			
		||||
{
 | 
			
		||||
    buf_.push_back(details::log_msg_buffer{msg});
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
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(size_t i=0; i<buf_.size(); i++){
 | 
			
		||||
        num++;
 | 
			
		||||
        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;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
} // namespace sinks
 | 
			
		||||
} // namespace spdlog
 | 
			
		||||
							
								
								
									
										60
									
								
								include/spdlog/sinks/ringbuffer_sink.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										60
									
								
								include/spdlog/sinks/ringbuffer_sink.h
									
									
									
									
									
										Normal file
									
								
							@@ -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 <mutex>
 | 
			
		||||
#include <string>
 | 
			
		||||
#include <vector>
 | 
			
		||||
 | 
			
		||||
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:
 | 
			
		||||
    details::circular_q<details::log_msg_buffer> 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
 | 
			
		||||
		Reference in New Issue
	
	Block a user