try different apprach to backtracer object

This commit is contained in:
gabime 2019-09-05 00:39:11 +03:00
parent 3fd3c47e6d
commit f795297e15
5 changed files with 182 additions and 101 deletions

View File

@ -16,20 +16,60 @@ namespace spdlog {
namespace details { namespace details {
class backtracer class backtracer
{ {
std::mutex mutex_; mutable std::mutex mutex_;
size_t n_messages_; std::atomic<bool> enabled_ {false};
circular_q<log_msg_buffer> messages_; circular_q<log_msg_buffer> messages_;
public: public:
explicit backtracer(size_t n_messages) : n_messages_{n_messages}, messages_{n_messages} backtracer() = default;
{} backtracer(const backtracer& other)
size_t n_messages() const
{ {
return n_messages_; std::lock_guard<std::mutex> lock(other.mutex_);
enabled_ = other.enabled();
messages_ = other.messages_;
} }
void add(const log_msg &msg) 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_ }; std::lock_guard<std::mutex> lock{ mutex_ };
messages_.push_back(log_msg_buffer{ msg }); messages_.push_back(log_msg_buffer{ msg });
@ -47,5 +87,6 @@ public:
} }
} }
}; };
} // namespace details } // namespace details
} // namespace spdlog } // namespace spdlog

View File

@ -14,13 +14,37 @@ class circular_q
public: public:
using item_type = T; using item_type = T;
// empty cir
circular_q() = default;
explicit circular_q(size_t max_items) explicit circular_q(size_t max_items)
: max_items_(max_items + 1) // one item is reserved as marker for full q : max_items_(max_items + 1) // one item is reserved as marker for full q
, v_(max_items_) , v_(max_items_)
{} {}
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 // push back, overrun (oldest) item if no room left
void push_back(T &&item) void push_back(T &&item)
{
if(max_items_ > 0)
{ {
v_[tail_] = std::move(item); v_[tail_] = std::move(item);
tail_ = (tail_ + 1) % max_items_; tail_ = (tail_ + 1) % max_items_;
@ -31,14 +55,18 @@ public:
++overrun_counter_; ++overrun_counter_;
} }
} }
}
// Pop item from front. // Pop item from front.
// If there are no elements in the container, the behavior is undefined. // If there are no elements in the container, the behavior is undefined.
void pop_front(T &popped_item) void pop_front(T &popped_item)
{
if(max_items_ > 0)
{ {
popped_item = std::move(v_[head_]); popped_item = std::move(v_[head_]);
head_ = (head_ + 1) % max_items_; head_ = (head_ + 1) % max_items_;
} }
}
bool empty() bool empty()
{ {
@ -57,12 +85,23 @@ public:
} }
private: private:
size_t max_items_; size_t max_items_ = 0;
typename std::vector<T>::size_type head_ = 0; typename std::vector<T>::size_type head_ = 0;
typename std::vector<T>::size_type tail_ = 0; typename std::vector<T>::size_type tail_ = 0;
size_t overrun_counter_ = 0; size_t overrun_counter_ = 0;
std::vector<T> v_; 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 details
} // namespace spdlog } // namespace spdlog

View File

@ -47,6 +47,14 @@ public:
update_string_views(); 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_buffer &operator=(log_msg_buffer &&other)
{ {
log_msg::operator=(std::move(other)); log_msg::operator=(std::move(other));

View File

@ -24,10 +24,6 @@ SPDLOG_INLINE logger::logger(const logger &other)
, custom_err_handler_(other.custom_err_handler_) , custom_err_handler_(other.custom_err_handler_)
, tracer_(other.tracer_) , tracer_(other.tracer_)
{ {
if (tracer_)
{
enable_backtrace(tracer_->n_messages());
}
} }
SPDLOG_INLINE logger::logger(logger &&other) SPDLOG_NOEXCEPT : name_(std::move(other.name_)), SPDLOG_INLINE logger::logger(logger &&other) SPDLOG_NOEXCEPT : name_(std::move(other.name_)),
@ -61,7 +57,7 @@ SPDLOG_INLINE void logger::swap(spdlog::logger &other) SPDLOG_NOEXCEPT
other.flush_level_.store(tmp); other.flush_level_.store(tmp);
custom_err_handler_.swap(other.custom_err_handler_); custom_err_handler_.swap(other.custom_err_handler_);
tracer_.swap(other.tracer_); std::swap(tracer_, other.tracer_);
} }
SPDLOG_INLINE void swap(logger &a, logger &b) SPDLOG_INLINE void swap(logger &a, logger &b)
@ -116,13 +112,13 @@ SPDLOG_INLINE void logger::set_pattern(std::string pattern, pattern_time_type ti
// create new backtrace sink and move to it all our child sinks // create new backtrace sink and move to it all our child sinks
SPDLOG_INLINE void logger::enable_backtrace(size_t n_messages) SPDLOG_INLINE void logger::enable_backtrace(size_t n_messages)
{ {
tracer_ = std::make_shared<details::backtracer>(n_messages); tracer_.enable(n_messages);
} }
// restore orig sinks and level and delete the backtrace sink // restore orig sinks and level and delete the backtrace sink
SPDLOG_INLINE void logger::disable_backtrace() SPDLOG_INLINE void logger::disable_backtrace()
{ {
tracer_.reset(); tracer_.disable();
} }
SPDLOG_INLINE void logger::dump_backtrace() SPDLOG_INLINE void logger::dump_backtrace()
@ -206,7 +202,7 @@ SPDLOG_INLINE void logger::flush_()
SPDLOG_INLINE void logger::backtrace_add_(const details::log_msg &msg) SPDLOG_INLINE void logger::backtrace_add_(const details::log_msg &msg)
{ {
tracer_->add(msg); tracer_.push_back(msg);
} }
SPDLOG_INLINE void logger::dump_backtrace_() SPDLOG_INLINE void logger::dump_backtrace_()
@ -215,7 +211,7 @@ SPDLOG_INLINE void logger::dump_backtrace_()
if (tracer_) if (tracer_)
{ {
sink_it_(log_msg{name(), level::info, "****************** Backtrace Start ******************"}); sink_it_(log_msg{name(), level::info, "****************** Backtrace Start ******************"});
tracer_->foreach_pop([this](const details::log_msg &msg) { this->sink_it_(msg); }); tracer_.foreach_pop([this](const details::log_msg &msg) { this->sink_it_(msg); });
sink_it_(log_msg{name(), level::info, "****************** Backtrace End ********************"}); sink_it_(log_msg{name(), level::info, "****************** Backtrace End ********************"});
} }
} }

View File

@ -17,6 +17,7 @@
#include "spdlog/common.h" #include "spdlog/common.h"
#include "spdlog/details/log_msg.h" #include "spdlog/details/log_msg.h"
#include "spdlog/details/backtracer.h"
#ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT #ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT
#include "spdlog/details/os.h" #include "spdlog/details/os.h"
@ -39,10 +40,6 @@
namespace spdlog { namespace spdlog {
namespace details {
class backtracer;
}
class logger class logger
{ {
public: public:
@ -365,7 +362,7 @@ protected:
spdlog::level_t level_{level::info}; spdlog::level_t level_{level::info};
spdlog::level_t flush_level_{level::off}; spdlog::level_t flush_level_{level::off};
err_handler custom_err_handler_{nullptr}; err_handler custom_err_handler_{nullptr};
std::shared_ptr<details::backtracer> tracer_; details::backtracer tracer_;
virtual void sink_it_(const details::log_msg &msg); virtual void sink_it_(const details::log_msg &msg);
virtual void flush_(); virtual void flush_();