mirror of
https://github.com/gabime/spdlog.git
synced 2025-09-28 17:19:34 +08:00
Replaced backtace implementation
This commit is contained in:
@@ -50,18 +50,6 @@ SPDLOG_INLINE void spdlog::async_logger::flush_()
|
||||
}
|
||||
}
|
||||
|
||||
SPDLOG_INLINE void spdlog::async_logger::dump_backtrace_()
|
||||
{
|
||||
if (auto pool_ptr = thread_pool_.lock())
|
||||
{
|
||||
pool_ptr->post_dump_backtrace(shared_from_this(), overflow_policy_);
|
||||
}
|
||||
else
|
||||
{
|
||||
SPDLOG_THROW(spdlog_ex("async dump_backtrace: thread pool doesn't exist anymore"));
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// backend functions - called from the thread pool to do the actual job
|
||||
//
|
||||
@@ -90,11 +78,6 @@ SPDLOG_INLINE void spdlog::async_logger::backend_flush_()
|
||||
spdlog::logger::flush_();
|
||||
}
|
||||
|
||||
SPDLOG_INLINE void spdlog::async_logger::backend_dump_backtrace_()
|
||||
{
|
||||
spdlog::logger::dump_backtrace_();
|
||||
}
|
||||
|
||||
SPDLOG_INLINE std::shared_ptr<spdlog::logger> spdlog::async_logger::clone(std::string new_name)
|
||||
{
|
||||
auto cloned = std::make_shared<spdlog::async_logger>(*this);
|
||||
|
@@ -54,10 +54,8 @@ public:
|
||||
protected:
|
||||
void sink_it_(const details::log_msg &msg) override;
|
||||
void flush_() override;
|
||||
void dump_backtrace_() override;
|
||||
void backend_sink_it_(const details::log_msg &incoming_log_msg);
|
||||
void backend_flush_();
|
||||
void backend_dump_backtrace_();
|
||||
|
||||
private:
|
||||
std::weak_ptr<details::thread_pool> thread_pool_;
|
||||
|
45
include/spdlog/details/backtracer.h
Normal file
45
include/spdlog/details/backtracer.h
Normal file
@@ -0,0 +1,45 @@
|
||||
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
|
||||
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "spdlog/common.h"
|
||||
#include "spdlog/details/log_msg_buffer.h"
|
||||
#include "spdlog/details/circular_q.h"
|
||||
#include "spdlog/sinks/sink.h"
|
||||
#include <mutex>
|
||||
|
||||
// Store log messages in circular buffer.
|
||||
// Useful for storing debug data in case of error/warning happens.
|
||||
|
||||
namespace spdlog {
|
||||
namespace details {
|
||||
class backtracer
|
||||
{
|
||||
std::mutex mutex_;
|
||||
circular_q<log_msg_buffer> messages_;
|
||||
|
||||
public:
|
||||
explicit backtracer(size_t n_message)
|
||||
: messages_{n_message}
|
||||
{}
|
||||
|
||||
void add_msg(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 give fun on each of them.
|
||||
void foreach_pop(std::function<void(const details::log_msg)> fun)
|
||||
{
|
||||
while (!messages_.empty())
|
||||
{
|
||||
log_msg_buffer popped;
|
||||
messages_.pop_front(popped);
|
||||
fun(popped);
|
||||
}
|
||||
}
|
||||
};
|
||||
} // namespace details
|
||||
} // namespace spdlog
|
@@ -62,11 +62,6 @@ void SPDLOG_INLINE thread_pool::post_flush(async_logger_ptr &&worker_ptr, async_
|
||||
post_async_msg_(async_msg(std::move(worker_ptr), async_msg_type::flush), overflow_policy);
|
||||
}
|
||||
|
||||
void SPDLOG_INLINE thread_pool::post_dump_backtrace(async_logger_ptr &&worker_ptr, async_overflow_policy overflow_policy)
|
||||
{
|
||||
post_async_msg_(async_msg(std::move(worker_ptr), async_msg_type::dump_backtrace), overflow_policy);
|
||||
}
|
||||
|
||||
size_t SPDLOG_INLINE thread_pool::overrun_counter()
|
||||
{
|
||||
return q_.overrun_counter();
|
||||
@@ -114,18 +109,17 @@ bool SPDLOG_INLINE thread_pool::process_next_msg_()
|
||||
return true;
|
||||
}
|
||||
|
||||
case async_msg_type ::dump_backtrace:
|
||||
{
|
||||
incoming_async_msg.worker_ptr->backend_dump_backtrace_();
|
||||
return true;
|
||||
}
|
||||
|
||||
case async_msg_type::terminate:
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
default:
|
||||
{
|
||||
assert(false && "Unexpected async_msg_type");
|
||||
}
|
||||
assert(false && "Unexpected async_msg_type");
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@@ -24,8 +24,7 @@ enum class async_msg_type
|
||||
{
|
||||
log,
|
||||
flush,
|
||||
terminate,
|
||||
dump_backtrace
|
||||
terminate
|
||||
};
|
||||
|
||||
#include "spdlog/details/log_msg_buffer.h"
|
||||
@@ -97,7 +96,6 @@ public:
|
||||
|
||||
void post_log(async_logger_ptr &&worker_ptr, const details::log_msg &msg, async_overflow_policy overflow_policy);
|
||||
void post_flush(async_logger_ptr &&worker_ptr, async_overflow_policy overflow_policy);
|
||||
void post_dump_backtrace(async_logger_ptr &&worker_ptr, async_overflow_policy overflow_policy);
|
||||
size_t overrun_counter();
|
||||
|
||||
private:
|
||||
|
@@ -8,7 +8,7 @@
|
||||
#endif
|
||||
|
||||
#include "spdlog/sinks/sink.h"
|
||||
#include "spdlog/sinks/backtrace_sink.h"
|
||||
#include "spdlog/details/backtracer.h"
|
||||
#include "spdlog/details/pattern_formatter.h"
|
||||
|
||||
#include <cstdio>
|
||||
@@ -22,7 +22,7 @@ SPDLOG_INLINE logger::logger(const logger &other)
|
||||
, level_(other.level_.load(std::memory_order_relaxed))
|
||||
, flush_level_(other.flush_level_.load(std::memory_order_relaxed))
|
||||
, custom_err_handler_(other.custom_err_handler_)
|
||||
, backtrace_sink_(other.backtrace_sink_)
|
||||
, tracer_(other.tracer_)
|
||||
{}
|
||||
|
||||
SPDLOG_INLINE logger::logger(logger &&other) SPDLOG_NOEXCEPT : name_(std::move(other.name_)),
|
||||
@@ -30,7 +30,7 @@ SPDLOG_INLINE logger::logger(logger &&other) SPDLOG_NOEXCEPT : name_(std::move(o
|
||||
level_(other.level_.load(std::memory_order_relaxed)),
|
||||
flush_level_(other.flush_level_.load(std::memory_order_relaxed)),
|
||||
custom_err_handler_(std::move(other.custom_err_handler_)),
|
||||
backtrace_sink_(std::move(other.backtrace_sink_))
|
||||
tracer_(std::move(other.tracer_))
|
||||
|
||||
{}
|
||||
|
||||
@@ -56,7 +56,7 @@ SPDLOG_INLINE void logger::swap(spdlog::logger &other) SPDLOG_NOEXCEPT
|
||||
other.flush_level_.store(tmp);
|
||||
|
||||
custom_err_handler_.swap(other.custom_err_handler_);
|
||||
backtrace_sink_.swap(other.backtrace_sink_);
|
||||
tracer_.swap(other.tracer_);
|
||||
}
|
||||
|
||||
SPDLOG_INLINE void swap(logger &a, logger &b)
|
||||
@@ -66,6 +66,11 @@ SPDLOG_INLINE void swap(logger &a, logger &b)
|
||||
|
||||
SPDLOG_INLINE void logger::log(source_loc loc, level::level_enum lvl, string_view_t msg)
|
||||
{
|
||||
if (tracer_)
|
||||
{
|
||||
save_backtrace_(details::log_msg(loc, string_view_t(name_), lvl, msg));
|
||||
}
|
||||
|
||||
if (!should_log(lvl))
|
||||
{
|
||||
return;
|
||||
@@ -87,16 +92,7 @@ SPDLOG_INLINE bool logger::should_log(level::level_enum msg_level) const
|
||||
|
||||
SPDLOG_INLINE void logger::set_level(level::level_enum log_level)
|
||||
{
|
||||
if (backtrace_sink_)
|
||||
{
|
||||
auto tracer = static_cast<sinks::backtrace_sink_mt *>(backtrace_sink_.get());
|
||||
tracer->set_filter_level(log_level);
|
||||
level_.store(level::trace);
|
||||
}
|
||||
else
|
||||
{
|
||||
level_.store(log_level);
|
||||
}
|
||||
level_.store(log_level);
|
||||
}
|
||||
|
||||
SPDLOG_INLINE level::level_enum logger::level() const
|
||||
@@ -136,24 +132,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
|
||||
SPDLOG_INLINE void logger::enable_backtrace(size_t n_messages)
|
||||
{
|
||||
if (!backtrace_sink_)
|
||||
{
|
||||
backtrace_sink_ = std::make_shared<sinks::backtrace_sink_mt>(std::move(sinks_), level(), n_messages);
|
||||
sinks().push_back(backtrace_sink_);
|
||||
level_.store(level::trace); // pass all messages to the backtrace sink.
|
||||
}
|
||||
tracer_ = std::make_shared<details::backtracer>(n_messages);
|
||||
}
|
||||
|
||||
// restore orig sinks and level and delete the backtrace sink
|
||||
SPDLOG_INLINE void logger::disable_backtrace()
|
||||
{
|
||||
if (backtrace_sink_)
|
||||
{
|
||||
auto tracer = static_cast<sinks::backtrace_sink_mt *>(backtrace_sink_.get());
|
||||
sinks_ = std::move(tracer->sinks());
|
||||
level_.store(tracer->get_filter_level());
|
||||
backtrace_sink_.reset();
|
||||
}
|
||||
tracer_.reset();
|
||||
}
|
||||
|
||||
SPDLOG_INLINE void logger::dump_backtrace()
|
||||
@@ -235,16 +220,19 @@ SPDLOG_INLINE void logger::flush_()
|
||||
}
|
||||
}
|
||||
|
||||
SPDLOG_INLINE void logger::save_backtrace_(const details::log_msg &msg)
|
||||
{
|
||||
tracer_->add_msg(msg);
|
||||
}
|
||||
|
||||
SPDLOG_INLINE void logger::dump_backtrace_()
|
||||
{
|
||||
if (backtrace_sink_)
|
||||
using details::log_msg;
|
||||
if (tracer_)
|
||||
{
|
||||
auto tracer = static_cast<sinks::backtrace_sink_mt *>(backtrace_sink_.get());
|
||||
SPDLOG_TRY
|
||||
{
|
||||
tracer->dump_backtrace(name());
|
||||
}
|
||||
SPDLOG_LOGGER_CATCH()
|
||||
sink_it_(log_msg{name(), level::info, "****************** Backtrace Start ******************"});
|
||||
tracer_->foreach_pop([this](const details::log_msg &msg) { this->sink_it_(msg); });
|
||||
sink_it_(log_msg{name(), level::info, "****************** Backtrace End ********************"});
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -38,6 +38,11 @@
|
||||
#endif
|
||||
|
||||
namespace spdlog {
|
||||
|
||||
namespace details {
|
||||
class backtracer;
|
||||
}
|
||||
|
||||
class logger
|
||||
{
|
||||
public:
|
||||
@@ -81,6 +86,10 @@ public:
|
||||
fmt::format_to(buf, fmt, args...);
|
||||
details::log_msg log_msg(loc, name_, lvl, string_view_t(buf.data(), buf.size()));
|
||||
sink_it_(log_msg);
|
||||
if (tracer_)
|
||||
{
|
||||
save_backtrace_(log_msg);
|
||||
}
|
||||
}
|
||||
SPDLOG_LOGGER_CATCH()
|
||||
}
|
||||
@@ -149,6 +158,10 @@ public:
|
||||
template<class T, typename std::enable_if<std::is_convertible<const T &, spdlog::string_view_t>::value, T>::type * = nullptr>
|
||||
void log(source_loc loc, level::level_enum lvl, const T &msg)
|
||||
{
|
||||
if (tracer_)
|
||||
{
|
||||
save_backtrace_(details::log_msg(loc, name_, lvl, msg));
|
||||
}
|
||||
if (!should_log(lvl))
|
||||
{
|
||||
return;
|
||||
@@ -164,6 +177,13 @@ public:
|
||||
T>::type * = nullptr>
|
||||
void log(source_loc loc, level::level_enum lvl, const T &msg)
|
||||
{
|
||||
if (tracer_)
|
||||
{
|
||||
fmt::memory_buffer buf;
|
||||
fmt::format_to(buf, "{}", msg);
|
||||
save_backtrace_(details::log_msg(loc, name_, lvl, string_view_t(buf.data(), buf.size())));
|
||||
}
|
||||
|
||||
if (!should_log(lvl))
|
||||
{
|
||||
return;
|
||||
@@ -323,7 +343,9 @@ public:
|
||||
|
||||
void set_pattern(std::string pattern, pattern_time_type time_type = pattern_time_type::local);
|
||||
|
||||
void enable_backtrace(size_t n_messages = 16);
|
||||
// backtrace support.
|
||||
// efficiently store all debug/trace messages in a circular buffer until needed for debugging.
|
||||
void enable_backtrace(size_t n_messages);
|
||||
void disable_backtrace();
|
||||
void dump_backtrace();
|
||||
|
||||
@@ -349,12 +371,13 @@ protected:
|
||||
spdlog::level_t level_{level::info};
|
||||
spdlog::level_t flush_level_{level::off};
|
||||
err_handler custom_err_handler_{nullptr};
|
||||
sink_ptr backtrace_sink_;
|
||||
// bool backtrace_enabled_{false};
|
||||
std::shared_ptr<details::backtracer> tracer_;
|
||||
|
||||
virtual void sink_it_(const details::log_msg &msg);
|
||||
virtual void flush_();
|
||||
virtual void dump_backtrace_();
|
||||
|
||||
void save_backtrace_(const details::log_msg &msg);
|
||||
void dump_backtrace_();
|
||||
bool should_flush_(const details::log_msg &msg);
|
||||
|
||||
// handle errors during logging.
|
||||
|
@@ -1,89 +0,0 @@
|
||||
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
|
||||
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "dist_sink.h"
|
||||
#include "spdlog/common.h"
|
||||
#include "spdlog/details/log_msg_buffer.h"
|
||||
#include "spdlog/details/circular_q.h"
|
||||
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
#include <chrono>
|
||||
|
||||
// Store log messages in circular buffer.
|
||||
// If it encounters a message with high enough level, it will send all previous message to it child sinks.
|
||||
// Useful for storing debug data in case of error/warning happens.
|
||||
|
||||
namespace spdlog {
|
||||
namespace sinks {
|
||||
template<typename Mutex>
|
||||
class backtrace_sink : public dist_sink<Mutex>
|
||||
{
|
||||
public:
|
||||
backtrace_sink(std::vector<std::shared_ptr<sink>> &&child_sinks, spdlog::level::level_enum filter_level, size_t n_messages)
|
||||
: filter_level_{filter_level}
|
||||
, traceback_msgs_{n_messages}
|
||||
{
|
||||
dist_sink<Mutex>::set_sinks(std::move(child_sinks));
|
||||
}
|
||||
|
||||
void set_filter_level(spdlog::level::level_enum filter_level)
|
||||
{
|
||||
std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
|
||||
filter_level_ = filter_level;
|
||||
}
|
||||
|
||||
spdlog::level::level_enum get_filter_level()
|
||||
{
|
||||
std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
|
||||
return filter_level_;
|
||||
}
|
||||
|
||||
void dump_backtrace(string_view_t logger_name)
|
||||
{
|
||||
std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
|
||||
dump_backtrace_(logger_name);
|
||||
}
|
||||
|
||||
protected:
|
||||
spdlog::level::level_enum filter_level_;
|
||||
details::circular_q<details::log_msg_buffer> traceback_msgs_;
|
||||
|
||||
// save the messages in a circular queue
|
||||
void sink_it_(const details::log_msg &msg) override
|
||||
{
|
||||
traceback_msgs_.push_back(details::log_msg_buffer(msg));
|
||||
|
||||
if (msg.level >= filter_level_)
|
||||
{
|
||||
dist_sink<Mutex>::sink_it_(msg);
|
||||
}
|
||||
}
|
||||
|
||||
void dump_backtrace_(string_view_t logger_name)
|
||||
{
|
||||
if (traceback_msgs_.empty())
|
||||
{
|
||||
return;
|
||||
}
|
||||
dist_sink<Mutex>::sink_it_(
|
||||
details::log_msg{logger_name, level::info, "********************* Backtrace Start *********************"});
|
||||
|
||||
do
|
||||
{
|
||||
details::log_msg_buffer popped;
|
||||
traceback_msgs_.pop_front(popped);
|
||||
dist_sink<Mutex>::sink_it_(popped);
|
||||
} while (!traceback_msgs_.empty());
|
||||
|
||||
dist_sink<Mutex>::sink_it_(
|
||||
details::log_msg{logger_name, level::info, "********************* Backtrace End ***********************"});
|
||||
}
|
||||
};
|
||||
|
||||
using backtrace_sink_mt = backtrace_sink<std::mutex>;
|
||||
|
||||
} // namespace sinks
|
||||
} // namespace spdlog
|
Reference in New Issue
Block a user