mirror of
https://github.com/gabime/spdlog.git
synced 2024-11-27 07:14:12 +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->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_)
|
||||
@ -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_);
|
||||
backtrace_level_ = trigger_level;
|
||||
backtrace_n_messages_ = n_messages;
|
||||
|
||||
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
|
||||
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);
|
||||
|
||||
@ -96,7 +96,6 @@ private:
|
||||
std::unique_ptr<periodic_worker> periodic_flusher_;
|
||||
std::shared_ptr<logger> default_logger_;
|
||||
bool automatic_registration_ = true;
|
||||
level::level_enum backtrace_level_ = level::off;
|
||||
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));
|
||||
}
|
||||
|
||||
|
||||
// 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;
|
||||
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()));
|
||||
sinks().push_back(std::move(backtrace_sink));
|
||||
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
|
||||
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 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
|
||||
void flush();
|
||||
@ -347,7 +349,7 @@ protected:
|
||||
spdlog::level_t level_{level::info};
|
||||
spdlog::level_t flush_level_{level::off};
|
||||
err_handler custom_err_handler_{nullptr};
|
||||
bool backtrace_enabled_ {false};
|
||||
bool backtrace_enabled_{false};
|
||||
|
||||
virtual void sink_it_(const details::log_msg &msg);
|
||||
virtual void flush_();
|
||||
|
@ -22,41 +22,37 @@ template<typename Mutex>
|
||||
class backtrace_sink : public dist_sink<Mutex>
|
||||
{
|
||||
public:
|
||||
explicit backtrace_sink(level::level_enum trigger_level = spdlog::level::err, size_t n_messages = 16)
|
||||
: trigger_level_{trigger_level}
|
||||
, traceback_msgs_{n_messages}
|
||||
explicit backtrace_sink(size_t n_messages = 16)
|
||||
: traceback_msgs_{n_messages}
|
||||
{}
|
||||
|
||||
protected:
|
||||
level::level_enum trigger_level_;
|
||||
void dump_backtrace()
|
||||
{
|
||||
std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
|
||||
dump_backtrace_();
|
||||
}
|
||||
|
||||
protected:
|
||||
details::circular_q<details::log_msg_buffer> traceback_msgs_;
|
||||
|
||||
// if current message is high enough, trigger a backtrace log,
|
||||
// otherwise save the message in the queue for future trigger.
|
||||
// save the messages in a circular queue
|
||||
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)
|
||||
{
|
||||
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())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
const char *logger_name = "??????";
|
||||
dist_sink<Mutex>::sink_it_(
|
||||
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)));
|
||||
}
|
||||
|
||||
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)
|
||||
|
@ -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);
|
||||
|
||||
// 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
|
||||
void set_level(level::level_enum log_level);
|
||||
|
Loading…
Reference in New Issue
Block a user