Added enable_backtrace to spdlog_api

This commit is contained in:
gabime 2019-08-23 02:28:52 +03:00
parent 65407539bb
commit a6d8b52686
7 changed files with 46 additions and 3 deletions

View File

@ -67,10 +67,17 @@ 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)
{
new_logger->enable_backtrace(backtrace_level_, backtrace_n_messages_);
}
if (automatic_registration_) if (automatic_registration_)
{ {
register_logger_(std::move(new_logger)); register_logger_(std::move(new_logger));
} }
} }
SPDLOG_INLINE std::shared_ptr<logger> registry::get(const std::string &logger_name) SPDLOG_INLINE std::shared_ptr<logger> registry::get(const std::string &logger_name)
@ -135,6 +142,18 @@ 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)
{
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);
}
}
SPDLOG_INLINE void registry::set_level(level::level_enum log_level) SPDLOG_INLINE void registry::set_level(level::level_enum log_level)
{ {
std::lock_guard<std::mutex> lock(logger_map_mutex_); std::lock_guard<std::mutex> lock(logger_map_mutex_);

View File

@ -52,6 +52,8 @@ 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 set_level(level::level_enum log_level); void set_level(level::level_enum log_level);
void flush_on(level::level_enum log_level); void flush_on(level::level_enum log_level);
@ -94,6 +96,8 @@ 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;
}; };
} // namespace details } // namespace details

View File

@ -8,6 +8,7 @@
#endif #endif
#include "spdlog/sinks/sink.h" #include "spdlog/sinks/sink.h"
#include "spdlog/sinks/backtrace-sink.h"
#include "spdlog/details/pattern_formatter.h" #include "spdlog/details/pattern_formatter.h"
#include <cstdio> #include <cstdio>
@ -119,6 +120,15 @@ 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));
} }
SPDLOG_INLINE void logger::enable_backtrace(level::level_enum trigger_level, size_t n_messages)
{
auto backtrace_sink = std::make_shared<spdlog::sinks::backtrace_sink_mt>(trigger_level, n_messages);
backtrace_sink->set_sinks(std::move(sinks()));
sinks().push_back(std::move(backtrace_sink));
this->set_level(spdlog::level::trace);
}
// flush functions // flush functions
SPDLOG_INLINE void logger::flush() SPDLOG_INLINE void logger::flush()
{ {

View File

@ -323,6 +323,8 @@ 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);
// flush functions // flush functions
void flush(); void flush();
void flush_on(level::level_enum log_level); void flush_on(level::level_enum log_level);

View File

@ -40,7 +40,7 @@ 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::warn, size_t n_messages = 32) explicit backtrace_sink(level::level_enum trigger_level = spdlog::level::err, size_t n_messages = 16)
: trigger_level_{trigger_level} : trigger_level_{trigger_level}
, traceback_msgs_{n_messages} , traceback_msgs_{n_messages}
{} {}
@ -76,7 +76,7 @@ protected:
} }
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 *********************"});
do do
{ {
@ -86,7 +86,7 @@ protected:
} while (!traceback_msgs_.empty()); } while (!traceback_msgs_.empty());
dist_sink<Mutex>::sink_it_( dist_sink<Mutex>::sink_it_(
details::log_msg{logger_name, level::info, "********************* [Backtrace End] ***********************"}); details::log_msg{logger_name, level::info, "********************* Backtrace End ***********************"});
} }
}; };

View File

@ -32,6 +32,11 @@ 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)
{
details::registry::instance().enable_backtrace(trigger_level, n_messages);
}
SPDLOG_INLINE void set_level(level::level_enum log_level) SPDLOG_INLINE void set_level(level::level_enum log_level)
{ {
details::registry::instance().set_level(log_level); details::registry::instance().set_level(log_level);

View File

@ -60,6 +60,9 @@ void set_formatter(std::unique_ptr<spdlog::formatter> formatter);
// example: spdlog::set_pattern("%Y-%m-%d %H:%M:%S.%e %l : %v"); // example: spdlog::set_pattern("%Y-%m-%d %H:%M:%S.%e %l : %v");
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
void enable_backtrace(level::level_enum trigger_level = level::err, size_t n_messages = 16);
// Set global logging level // Set global logging level
void set_level(level::level_enum log_level); void set_level(level::level_enum log_level);