mirror of
https://github.com/gabime/spdlog.git
synced 2025-01-12 17:00:25 +08:00
Replace backtrace trigger with dump_backtrace()
This commit is contained in:
parent
8d8aacf5e9
commit
dd33c16aae
@ -67,9 +67,9 @@ SPDLOG_INLINE void registry::initialize_logger(std::shared_ptr<logger> new_logge
|
|||||||
new_logger->set_level(level_);
|
new_logger->set_level(level_);
|
||||||
new_logger->flush_on(flush_level_);
|
new_logger->flush_on(flush_level_);
|
||||||
|
|
||||||
if (backtrace_level_ != level::off)
|
if (backtrace_n_messages_ > 0)
|
||||||
{
|
{
|
||||||
new_logger->enable_backtrace(backtrace_level_, backtrace_n_messages_);
|
new_logger->enable_backtrace(backtrace_n_messages_);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (automatic_registration_)
|
if (automatic_registration_)
|
||||||
@ -140,15 +140,14 @@ SPDLOG_INLINE void registry::set_formatter(std::unique_ptr<formatter> formatter)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
SPDLOG_INLINE void registry::enable_backtrace(level::level_enum trigger_level, size_t n_messages)
|
SPDLOG_INLINE void registry::enable_backtrace(size_t n_messages)
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(logger_map_mutex_);
|
std::lock_guard<std::mutex> lock(logger_map_mutex_);
|
||||||
backtrace_level_ = trigger_level;
|
|
||||||
backtrace_n_messages_ = n_messages;
|
backtrace_n_messages_ = n_messages;
|
||||||
|
|
||||||
for (auto &l : loggers_)
|
for (auto &l : loggers_)
|
||||||
{
|
{
|
||||||
l.second->enable_backtrace(trigger_level, n_messages);
|
l.second->enable_backtrace(n_messages);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -52,7 +52,7 @@ public:
|
|||||||
// Set global formatter. Each sink in each logger will get a clone of this object
|
// Set global formatter. Each sink in each logger will get a clone of this object
|
||||||
void set_formatter(std::unique_ptr<formatter> formatter);
|
void set_formatter(std::unique_ptr<formatter> formatter);
|
||||||
|
|
||||||
void enable_backtrace(level::level_enum trigger_level, size_t n_messages);
|
void enable_backtrace(size_t n_messages);
|
||||||
|
|
||||||
void set_level(level::level_enum log_level);
|
void set_level(level::level_enum log_level);
|
||||||
|
|
||||||
@ -96,7 +96,6 @@ private:
|
|||||||
std::unique_ptr<periodic_worker> periodic_flusher_;
|
std::unique_ptr<periodic_worker> periodic_flusher_;
|
||||||
std::shared_ptr<logger> default_logger_;
|
std::shared_ptr<logger> default_logger_;
|
||||||
bool automatic_registration_ = true;
|
bool automatic_registration_ = true;
|
||||||
level::level_enum backtrace_level_ = level::off;
|
|
||||||
size_t backtrace_n_messages_ = 0;
|
size_t backtrace_n_messages_ = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -120,20 +120,29 @@ SPDLOG_INLINE void logger::set_pattern(std::string pattern, pattern_time_type ti
|
|||||||
set_formatter(std::move(new_formatter));
|
set_formatter(std::move(new_formatter));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// create new backtrace sink and hand it all sinks
|
// create new backtrace sink and hand it all sinks
|
||||||
SPDLOG_INLINE void logger::enable_backtrace(level::level_enum trigger_level, size_t n_messages)
|
SPDLOG_INLINE void logger::enable_backtrace(size_t n_messages)
|
||||||
{
|
{
|
||||||
if (!backtrace_enabled_)
|
if (!backtrace_enabled_)
|
||||||
{
|
{
|
||||||
backtrace_enabled_ = true;
|
backtrace_enabled_ = true;
|
||||||
auto backtrace_sink = std::make_shared<spdlog::sinks::backtrace_sink_mt>(trigger_level, n_messages);
|
auto backtrace_sink = std::make_shared<spdlog::sinks::backtrace_sink_mt>(n_messages);
|
||||||
backtrace_sink->set_sinks(std::move(sinks()));
|
backtrace_sink->set_sinks(std::move(sinks()));
|
||||||
sinks().push_back(std::move(backtrace_sink));
|
sinks().push_back(std::move(backtrace_sink));
|
||||||
this->set_level(spdlog::level::trace);
|
this->set_level(spdlog::level::trace);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SPDLOG_INLINE void logger::dump_backtrace()
|
||||||
|
{
|
||||||
|
if (backtrace_enabled_)
|
||||||
|
{
|
||||||
|
// auto backtrace_sink = static_cast<std::shared_ptr<sinks::backtrace_sink_mt>>(sinks_[0]);
|
||||||
|
auto *first_sink = sinks_[0].get();
|
||||||
|
auto tracer = static_cast<sinks::backtrace_sink_mt *>(first_sink);
|
||||||
|
tracer->dump_backtrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// flush functions
|
// flush functions
|
||||||
SPDLOG_INLINE void logger::flush()
|
SPDLOG_INLINE void logger::flush()
|
||||||
|
@ -323,7 +323,9 @@ public:
|
|||||||
|
|
||||||
void set_pattern(std::string pattern, pattern_time_type time_type = pattern_time_type::local);
|
void set_pattern(std::string pattern, pattern_time_type time_type = pattern_time_type::local);
|
||||||
|
|
||||||
void enable_backtrace(level::level_enum trigger_level = level::err, size_t n_messages = 16);
|
void enable_backtrace(size_t n_messages = 16);
|
||||||
|
|
||||||
|
void dump_backtrace();
|
||||||
|
|
||||||
// flush functions
|
// flush functions
|
||||||
void flush();
|
void flush();
|
||||||
|
@ -22,41 +22,37 @@ template<typename Mutex>
|
|||||||
class backtrace_sink : public dist_sink<Mutex>
|
class backtrace_sink : public dist_sink<Mutex>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
explicit backtrace_sink(level::level_enum trigger_level = spdlog::level::err, size_t n_messages = 16)
|
explicit backtrace_sink(size_t n_messages = 16)
|
||||||
: trigger_level_{trigger_level}
|
: traceback_msgs_{n_messages}
|
||||||
, traceback_msgs_{n_messages}
|
|
||||||
{}
|
{}
|
||||||
|
|
||||||
protected:
|
void dump_backtrace()
|
||||||
level::level_enum trigger_level_;
|
{
|
||||||
|
std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
|
||||||
|
dump_backtrace_();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected:
|
||||||
details::circular_q<details::log_msg_buffer> traceback_msgs_;
|
details::circular_q<details::log_msg_buffer> traceback_msgs_;
|
||||||
|
|
||||||
// if current message is high enough, trigger a backtrace log,
|
// save the messages in a circular queue
|
||||||
// otherwise save the message in the queue for future trigger.
|
|
||||||
void sink_it_(const details::log_msg &msg) override
|
void sink_it_(const details::log_msg &msg) override
|
||||||
{
|
|
||||||
if (msg.level < trigger_level_)
|
|
||||||
{
|
{
|
||||||
traceback_msgs_.push_back(details::log_msg_buffer(msg));
|
traceback_msgs_.push_back(details::log_msg_buffer(msg));
|
||||||
}
|
|
||||||
if (msg.level > level::debug)
|
if (msg.level > level::debug)
|
||||||
{
|
{
|
||||||
dist_sink<Mutex>::sink_it_(msg);
|
dist_sink<Mutex>::sink_it_(msg);
|
||||||
}
|
}
|
||||||
if (msg.level >= trigger_level_)
|
|
||||||
{
|
|
||||||
log_backtrace_(msg.logger_name);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void log_backtrace_(const string_view_t &logger_name)
|
void dump_backtrace_()
|
||||||
{
|
{
|
||||||
if (traceback_msgs_.empty())
|
if (traceback_msgs_.empty())
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
const char *logger_name = "??????";
|
||||||
dist_sink<Mutex>::sink_it_(
|
dist_sink<Mutex>::sink_it_(
|
||||||
details::log_msg{logger_name, level::info, "********************* Backtrace Start *********************"});
|
details::log_msg{logger_name, level::info, "********************* Backtrace Start *********************"});
|
||||||
|
|
||||||
|
@ -32,9 +32,9 @@ SPDLOG_INLINE void set_pattern(std::string pattern, pattern_time_type time_type)
|
|||||||
set_formatter(std::unique_ptr<spdlog::formatter>(new pattern_formatter(std::move(pattern), time_type)));
|
set_formatter(std::unique_ptr<spdlog::formatter>(new pattern_formatter(std::move(pattern), time_type)));
|
||||||
}
|
}
|
||||||
|
|
||||||
SPDLOG_INLINE void enable_backtrace(level::level_enum trigger_level, size_t n_messages)
|
SPDLOG_INLINE void enable_backtrace(size_t n_messages)
|
||||||
{
|
{
|
||||||
details::registry::instance().enable_backtrace(trigger_level, n_messages);
|
details::registry::instance().enable_backtrace(n_messages);
|
||||||
}
|
}
|
||||||
|
|
||||||
SPDLOG_INLINE void set_level(level::level_enum log_level)
|
SPDLOG_INLINE void set_level(level::level_enum log_level)
|
||||||
|
@ -61,7 +61,7 @@ void set_formatter(std::unique_ptr<spdlog::formatter> formatter);
|
|||||||
void set_pattern(std::string pattern, pattern_time_type time_type = pattern_time_type::local);
|
void set_pattern(std::string pattern, pattern_time_type time_type = pattern_time_type::local);
|
||||||
|
|
||||||
// enable global backtrace support
|
// enable global backtrace support
|
||||||
void enable_backtrace(level::level_enum trigger_level = level::err, size_t n_messages = 16);
|
void enable_backtrace(size_t n_messages);
|
||||||
|
|
||||||
// Set global logging level
|
// Set global logging level
|
||||||
void set_level(level::level_enum log_level);
|
void set_level(level::level_enum log_level);
|
||||||
|
Loading…
Reference in New Issue
Block a user