1
0
mirror of https://github.com/gabime/spdlog.git synced 2025-01-15 09:17:57 +00:00

Compare commits

...

13 Commits

Author SHA1 Message Date
gabime
6db8beeade Removed un needed file 2019-11-10 00:55:50 +02:00
gabime
4f66313440 Fix #1308 2019-11-10 00:35:34 +02:00
gabime
89b5bcfdc7 Updated ringbuffer sink 2019-11-10 00:18:24 +02:00
gabime
26f706ebe3 Fixed #1302 2019-11-10 00:11:39 +02:00
gabime
0cb38085a1 Updated ringbuffer sink 2019-11-10 00:08:50 +02:00
Gabi Melman
cff6644b28
Merge pull request #1307 from eudoxos/ringbuffer-sink
Add ringbuffer sink
2019-11-09 21:38:57 +02:00
Václav Šmilauer
63837530ed Fix circular_q::size() empty case 2019-11-09 19:26:49 +01:00
Václav Šmilauer
62e09e73f7 defer formatting, use log_msg_buffer for intermediate storage 2019-11-09 13:48:04 +01:00
Václav Šmilauer
daef0a2374 Fix circular_q::size() 2019-11-09 13:15:57 +01:00
Václav Šmilauer
042045b998 typo fix 2019-11-09 09:35:05 +01:00
Václav Šmilauer
bad7284465 Fix cicular_q::size() 2019-11-09 09:30:27 +01:00
Václav Šmilauer
6f0cb6365e Move ringbuffer_sink to spdlog::details::circular_q, enhance its API: size(), at(i) 2019-11-08 19:25:31 +01:00
Václav Šmilauer
acf32be842 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.
2019-11-08 15:29:20 +01:00
5 changed files with 96 additions and 5 deletions

View File

@ -62,7 +62,7 @@
#endif
#ifndef SPDLOG_FUNCTION
#define SPDLOG_FUNCTION __FUNCTION__
#define SPDLOG_FUNCTION static_cast<const char *>(__FUNCTION__)
#endif
#ifdef SPDLOG_NO_EXCEPTIONS

View File

@ -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()

View File

@ -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();
}

View File

@ -428,7 +428,7 @@ SPDLOG_INLINE bool in_terminal(FILE *file) SPDLOG_NOEXCEPT
#if (defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT) || defined(SPDLOG_WCHAR_FILENAMES)) && defined(_WIN32)
SPDLOG_INLINE void wstr_to_utf8buf(wstring_view_t wstr, memory_buf_t &target)
{
if (wstr.size() > static_cast<size_t>((std::numeric_limits<int>::max())))
if (wstr.size() > static_cast<size_t>((std::numeric_limits<int>::max)()))
{
SPDLOG_THROW(spdlog::spdlog_ex("UTF-16 string is too big to be converted to UTF-8"));
}

View File

@ -0,0 +1,72 @@
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
#pragma once
#include "spdlog/sinks/base_sink.h"
#include "spdlog/details/circular_q.h"
#include "spdlog/details/log_msg_buffer.h"
#include "spdlog/details/null_mutex.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 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
{
q_.push_back(details::log_msg_buffer{msg});
}
void flush_() override {}
private:
details::circular_q<details::log_msg_buffer> q_;
};
using ringbuffer_sink_mt = ringbuffer_sink<std::mutex>;
using ringbuffer_sink_st = ringbuffer_sink<details::null_mutex>;
} // namespace sinks
} // namespace spdlog