mirror of
https://github.com/gabime/spdlog.git
synced 2025-01-15 09:17:57 +00:00
Compare commits
13 Commits
653ec05c0e
...
6db8beeade
Author | SHA1 | Date | |
---|---|---|---|
|
6db8beeade | ||
|
4f66313440 | ||
|
89b5bcfdc7 | ||
|
26f706ebe3 | ||
|
0cb38085a1 | ||
|
cff6644b28 | ||
|
63837530ed | ||
|
62e09e73f7 | ||
|
daef0a2374 | ||
|
042045b998 | ||
|
bad7284465 | ||
|
6f0cb6365e | ||
|
acf32be842 |
@ -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
|
||||
|
@ -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()
|
||||
|
@ -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();
|
||||
}
|
||||
|
@ -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"));
|
||||
}
|
||||
|
72
include/spdlog/sinks/ringbuffer_sink.h
Normal file
72
include/spdlog/sinks/ringbuffer_sink.h
Normal 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
|
Loading…
Reference in New Issue
Block a user