From dd33c16aae8e2dc35155e38003be4fcfe9ccf498 Mon Sep 17 00:00:00 2001 From: gabime Date: Fri, 23 Aug 2019 14:47:40 +0300 Subject: [PATCH] Replace backtrace trigger with dump_backtrace() --- include/spdlog/details/registry-inl.h | 9 ++++---- include/spdlog/details/registry.h | 3 +-- include/spdlog/logger-inl.h | 17 +++++++++++---- include/spdlog/logger.h | 6 ++++-- include/spdlog/sinks/backtrace-sink.h | 30 ++++++++++++--------------- include/spdlog/spdlog-inl.h | 4 ++-- include/spdlog/spdlog.h | 2 +- 7 files changed, 38 insertions(+), 33 deletions(-) diff --git a/include/spdlog/details/registry-inl.h b/include/spdlog/details/registry-inl.h index d6dbb451..d6d1aa07 100644 --- a/include/spdlog/details/registry-inl.h +++ b/include/spdlog/details/registry-inl.h @@ -67,9 +67,9 @@ SPDLOG_INLINE void registry::initialize_logger(std::shared_ptr 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) } } -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 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); } } diff --git a/include/spdlog/details/registry.h b/include/spdlog/details/registry.h index b09db533..73be7b28 100644 --- a/include/spdlog/details/registry.h +++ b/include/spdlog/details/registry.h @@ -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); - 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_flusher_; std::shared_ptr default_logger_; bool automatic_registration_ = true; - level::level_enum backtrace_level_ = level::off; size_t backtrace_n_messages_ = 0; }; diff --git a/include/spdlog/logger-inl.h b/include/spdlog/logger-inl.h index 3bf05df2..d0f5a0c7 100644 --- a/include/spdlog/logger-inl.h +++ b/include/spdlog/logger-inl.h @@ -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(trigger_level, n_messages); + auto backtrace_sink = std::make_shared(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>(sinks_[0]); + auto *first_sink = sinks_[0].get(); + auto tracer = static_cast(first_sink); + tracer->dump_backtrace(); + } +} // flush functions SPDLOG_INLINE void logger::flush() diff --git a/include/spdlog/logger.h b/include/spdlog/logger.h index 778f7976..62f8b729 100644 --- a/include/spdlog/logger.h +++ b/include/spdlog/logger.h @@ -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_(); diff --git a/include/spdlog/sinks/backtrace-sink.h b/include/spdlog/sinks/backtrace-sink.h index 897a7c8b..cb42bde9 100644 --- a/include/spdlog/sinks/backtrace-sink.h +++ b/include/spdlog/sinks/backtrace-sink.h @@ -22,41 +22,37 @@ template class backtrace_sink : public dist_sink { 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 lock(base_sink::mutex_); + dump_backtrace_(); + } +protected: details::circular_q 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::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::sink_it_( details::log_msg{logger_name, level::info, "********************* Backtrace Start *********************"}); diff --git a/include/spdlog/spdlog-inl.h b/include/spdlog/spdlog-inl.h index b46ee2b2..2e4ee652 100644 --- a/include/spdlog/spdlog-inl.h +++ b/include/spdlog/spdlog-inl.h @@ -32,9 +32,9 @@ SPDLOG_INLINE void set_pattern(std::string pattern, pattern_time_type time_type) set_formatter(std::unique_ptr(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) diff --git a/include/spdlog/spdlog.h b/include/spdlog/spdlog.h index e96e9d82..4254f49d 100644 --- a/include/spdlog/spdlog.h +++ b/include/spdlog/spdlog.h @@ -61,7 +61,7 @@ void set_formatter(std::unique_ptr 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);