mirror of
https://github.com/gabime/spdlog.git
synced 2024-12-25 10:01:33 +08:00
Added enable_backtrace to spdlog_api
This commit is contained in:
parent
65407539bb
commit
a6d8b52686
@ -67,10 +67,17 @@ 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)
|
||||
{
|
||||
new_logger->enable_backtrace(backtrace_level_, backtrace_n_messages_);
|
||||
}
|
||||
|
||||
if (automatic_registration_)
|
||||
{
|
||||
register_logger_(std::move(new_logger));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(logger_map_mutex_);
|
||||
|
@ -52,6 +52,8 @@ 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 set_level(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::shared_ptr<logger> default_logger_;
|
||||
bool automatic_registration_ = true;
|
||||
level::level_enum backtrace_level_ = level::off;
|
||||
size_t backtrace_n_messages_ = 0;
|
||||
};
|
||||
|
||||
} // namespace details
|
||||
|
@ -8,6 +8,7 @@
|
||||
#endif
|
||||
|
||||
#include "spdlog/sinks/sink.h"
|
||||
#include "spdlog/sinks/backtrace-sink.h"
|
||||
#include "spdlog/details/pattern_formatter.h"
|
||||
|
||||
#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));
|
||||
}
|
||||
|
||||
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
|
||||
SPDLOG_INLINE void logger::flush()
|
||||
{
|
||||
|
@ -323,6 +323,8 @@ 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);
|
||||
|
||||
// flush functions
|
||||
void flush();
|
||||
void flush_on(level::level_enum log_level);
|
||||
|
@ -40,7 +40,7 @@ template<typename Mutex>
|
||||
class backtrace_sink : public dist_sink<Mutex>
|
||||
{
|
||||
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}
|
||||
, traceback_msgs_{n_messages}
|
||||
{}
|
||||
@ -76,7 +76,7 @@ protected:
|
||||
}
|
||||
|
||||
dist_sink<Mutex>::sink_it_(
|
||||
details::log_msg{logger_name, level::info, "********************* [Backtrace Start] *********************"});
|
||||
details::log_msg{logger_name, level::info, "********************* Backtrace Start *********************"});
|
||||
|
||||
do
|
||||
{
|
||||
@ -86,7 +86,7 @@ protected:
|
||||
} while (!traceback_msgs_.empty());
|
||||
|
||||
dist_sink<Mutex>::sink_it_(
|
||||
details::log_msg{logger_name, level::info, "********************* [Backtrace End] ***********************"});
|
||||
details::log_msg{logger_name, level::info, "********************* Backtrace End ***********************"});
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -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)));
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
details::registry::instance().set_level(log_level);
|
||||
|
@ -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");
|
||||
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
|
||||
void set_level(level::level_enum log_level);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user