mirror of
https://github.com/gabime/spdlog.git
synced 2025-10-02 11:29:01 +08:00
try different apprach to backtracer object
This commit is contained in:
@@ -13,39 +13,80 @@
|
||||
// Useful for storing debug data in case of error/warning happens.
|
||||
|
||||
namespace spdlog {
|
||||
namespace details {
|
||||
class backtracer
|
||||
{
|
||||
std::mutex mutex_;
|
||||
size_t n_messages_;
|
||||
circular_q<log_msg_buffer> messages_;
|
||||
|
||||
public:
|
||||
explicit backtracer(size_t n_messages) : n_messages_{n_messages}, messages_{n_messages}
|
||||
{}
|
||||
|
||||
size_t n_messages() const
|
||||
{
|
||||
return n_messages_;
|
||||
}
|
||||
|
||||
void add(const log_msg &msg)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock{mutex_};
|
||||
messages_.push_back(log_msg_buffer{msg});
|
||||
}
|
||||
|
||||
// pop all items in the q and apply the given fun on each of them.
|
||||
void foreach_pop(std::function<void(const details::log_msg &)> fun)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock{mutex_};
|
||||
while (!messages_.empty())
|
||||
namespace details {
|
||||
class backtracer
|
||||
{
|
||||
log_msg_buffer popped;
|
||||
messages_.pop_front(popped);
|
||||
fun(popped);
|
||||
}
|
||||
}
|
||||
};
|
||||
} // namespace details
|
||||
mutable std::mutex mutex_;
|
||||
std::atomic<bool> enabled_ {false};
|
||||
circular_q<log_msg_buffer> messages_;
|
||||
|
||||
public:
|
||||
backtracer() = default;
|
||||
backtracer(const backtracer& other)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(other.mutex_);
|
||||
enabled_ = other.enabled();
|
||||
messages_ = other.messages_;
|
||||
}
|
||||
|
||||
backtracer(backtracer&& other) SPDLOG_NOEXCEPT
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(other.mutex_);
|
||||
enabled_ = other.enabled();
|
||||
messages_ = std::move(other.messages_);
|
||||
}
|
||||
|
||||
backtracer& operator=(backtracer other)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
enabled_ = other.enabled();
|
||||
messages_ = other.messages_;
|
||||
return *this;
|
||||
}
|
||||
|
||||
void enable(size_t size)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock{ mutex_ };
|
||||
enabled_.store(true, std::memory_order_relaxed);
|
||||
messages_ = circular_q<log_msg_buffer>{ size };
|
||||
}
|
||||
|
||||
|
||||
void disable()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock{ mutex_ };
|
||||
enabled_.store(false, std::memory_order_relaxed);
|
||||
}
|
||||
|
||||
|
||||
bool enabled() const
|
||||
{
|
||||
return enabled_.load(std::memory_order_relaxed);
|
||||
}
|
||||
|
||||
operator bool() const
|
||||
{
|
||||
return enabled();
|
||||
}
|
||||
|
||||
void push_back(const log_msg &msg)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock{ mutex_ };
|
||||
messages_.push_back(log_msg_buffer{ msg });
|
||||
}
|
||||
|
||||
// pop all items in the q and apply the given fun on each of them.
|
||||
void foreach_pop(std::function<void(const details::log_msg &)> fun)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock{ mutex_ };
|
||||
while (!messages_.empty())
|
||||
{
|
||||
log_msg_buffer popped;
|
||||
messages_.pop_front(popped);
|
||||
fun(popped);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace details
|
||||
} // namespace spdlog
|
@@ -7,62 +7,101 @@
|
||||
#include <vector>
|
||||
|
||||
namespace spdlog {
|
||||
namespace details {
|
||||
template<typename T>
|
||||
class circular_q
|
||||
{
|
||||
public:
|
||||
using item_type = T;
|
||||
|
||||
explicit circular_q(size_t max_items)
|
||||
: max_items_(max_items + 1) // one item is reserved as marker for full q
|
||||
, v_(max_items_)
|
||||
{}
|
||||
|
||||
// push back, overrun (oldest) item if no room left
|
||||
void push_back(T &&item)
|
||||
{
|
||||
v_[tail_] = std::move(item);
|
||||
tail_ = (tail_ + 1) % max_items_;
|
||||
|
||||
if (tail_ == head_) // overrun last item if full
|
||||
namespace details {
|
||||
template<typename T>
|
||||
class circular_q
|
||||
{
|
||||
head_ = (head_ + 1) % max_items_;
|
||||
++overrun_counter_;
|
||||
}
|
||||
}
|
||||
public:
|
||||
using item_type = T;
|
||||
|
||||
// Pop item from front.
|
||||
// If there are no elements in the container, the behavior is undefined.
|
||||
void pop_front(T &popped_item)
|
||||
{
|
||||
popped_item = std::move(v_[head_]);
|
||||
head_ = (head_ + 1) % max_items_;
|
||||
}
|
||||
// empty cir
|
||||
circular_q() = default;
|
||||
|
||||
bool empty()
|
||||
{
|
||||
return tail_ == head_;
|
||||
}
|
||||
explicit circular_q(size_t max_items)
|
||||
: max_items_(max_items + 1) // one item is reserved as marker for full q
|
||||
, v_(max_items_)
|
||||
{}
|
||||
|
||||
bool full()
|
||||
{
|
||||
// head is ahead of the tail by 1
|
||||
return ((tail_ + 1) % max_items_) == head_;
|
||||
}
|
||||
|
||||
size_t overrun_counter() const
|
||||
{
|
||||
return overrun_counter_;
|
||||
}
|
||||
|
||||
private:
|
||||
size_t max_items_;
|
||||
typename std::vector<T>::size_type head_ = 0;
|
||||
typename std::vector<T>::size_type tail_ = 0;
|
||||
size_t overrun_counter_ = 0;
|
||||
std::vector<T> v_;
|
||||
|
||||
};
|
||||
} // namespace details
|
||||
circular_q(const circular_q&) = default;
|
||||
circular_q& operator=(const circular_q&) = default;
|
||||
|
||||
// move cannot be default,
|
||||
// since we need to reset head_, tail_, etc to zero in the moved object
|
||||
circular_q(circular_q&& other) SPDLOG_NOEXCEPT:
|
||||
{
|
||||
copy_moveable(std::move(other));
|
||||
}
|
||||
|
||||
circular_q& operator=(circular_q&& other) SPDLOG_NOEXCEPT
|
||||
{
|
||||
copy_moveable(std::move(other));
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
// push back, overrun (oldest) item if no room left
|
||||
void push_back(T &&item)
|
||||
{
|
||||
if(max_items_ > 0)
|
||||
{
|
||||
v_[tail_] = std::move(item);
|
||||
tail_ = (tail_ + 1) % max_items_;
|
||||
|
||||
if (tail_ == head_) // overrun last item if full
|
||||
{
|
||||
head_ = (head_ + 1) % max_items_;
|
||||
++overrun_counter_;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Pop item from front.
|
||||
// If there are no elements in the container, the behavior is undefined.
|
||||
void pop_front(T &popped_item)
|
||||
{
|
||||
if(max_items_ > 0)
|
||||
{
|
||||
popped_item = std::move(v_[head_]);
|
||||
head_ = (head_ + 1) % max_items_;
|
||||
}
|
||||
}
|
||||
|
||||
bool empty()
|
||||
{
|
||||
return tail_ == head_;
|
||||
}
|
||||
|
||||
bool full()
|
||||
{
|
||||
// head is ahead of the tail by 1
|
||||
return ((tail_ + 1) % max_items_) == head_;
|
||||
}
|
||||
|
||||
size_t overrun_counter() const
|
||||
{
|
||||
return overrun_counter_;
|
||||
}
|
||||
|
||||
private:
|
||||
size_t max_items_ = 0;
|
||||
typename std::vector<T>::size_type head_ = 0;
|
||||
typename std::vector<T>::size_type tail_ = 0;
|
||||
size_t overrun_counter_ = 0;
|
||||
std::vector<T> v_;
|
||||
|
||||
|
||||
void copy_moveable(circular_q&& other) SPDLOG_NOEXCEPT
|
||||
{
|
||||
max_items_ = other.max_items_;
|
||||
head_ = other.head_;
|
||||
tail_ = other.tail_;
|
||||
overrun_counter_ = other.overrun_counter_,
|
||||
v_ = std::move(other.v_);
|
||||
other.max_items_ = 0; // disable other
|
||||
}
|
||||
|
||||
};
|
||||
} // namespace details
|
||||
} // namespace spdlog
|
||||
|
@@ -47,6 +47,14 @@ public:
|
||||
update_string_views();
|
||||
}
|
||||
|
||||
log_msg_buffer &operator=(log_msg_buffer &other)
|
||||
{
|
||||
log_msg::operator=(other);
|
||||
buffer.append(other.buffer.begin(), other.buffer.end());
|
||||
update_string_views();
|
||||
return *this;
|
||||
}
|
||||
|
||||
log_msg_buffer &operator=(log_msg_buffer &&other)
|
||||
{
|
||||
log_msg::operator=(std::move(other));
|
||||
|
Reference in New Issue
Block a user