Added the possibility to enforce messages

This commit is contained in:
WolverinDEV 2019-11-23 18:19:26 +01:00
parent 1549ff12f1
commit b96ea2331f
10 changed files with 75 additions and 59 deletions

View File

@ -56,7 +56,7 @@ SPDLOG_INLINE void spdlog::async_logger::backend_sink_it_(const details::log_msg
{ {
for (auto &sink : sinks_) for (auto &sink : sinks_)
{ {
if (sink->should_log(msg.level)) if (msg.level.forced || sink->should_log(msg.level.value))
{ {
SPDLOG_TRY SPDLOG_TRY
{ {

View File

@ -149,6 +149,15 @@ enum level_enum
off = SPDLOG_LEVEL_OFF, off = SPDLOG_LEVEL_OFF,
}; };
struct forceable {
level_enum value{level_enum::off};
bool forced{false};
forceable() = default;
explicit forceable(level_enum level) : value{level} {}
explicit forceable(level_enum level, bool forced) : value{level}, forced{forced} {}
};
#if !defined(SPDLOG_LEVEL_NAMES) #if !defined(SPDLOG_LEVEL_NAMES)
#define SPDLOG_LEVEL_NAMES \ #define SPDLOG_LEVEL_NAMES \
{ \ { \

View File

@ -12,7 +12,7 @@
namespace spdlog { namespace spdlog {
namespace details { namespace details {
SPDLOG_INLINE log_msg::log_msg(spdlog::source_loc loc, string_view_t logger_name, spdlog::level::level_enum lvl, spdlog::string_view_t msg) SPDLOG_INLINE log_msg::log_msg(spdlog::source_loc loc, string_view_t logger_name, spdlog::level::forceable lvl, spdlog::string_view_t msg)
: logger_name(logger_name) : logger_name(logger_name)
, level(lvl) , level(lvl)
#ifndef SPDLOG_NO_DATETIME #ifndef SPDLOG_NO_DATETIME
@ -26,7 +26,7 @@ SPDLOG_INLINE log_msg::log_msg(spdlog::source_loc loc, string_view_t logger_name
, payload(msg) , payload(msg)
{} {}
SPDLOG_INLINE log_msg::log_msg(string_view_t logger_name, spdlog::level::level_enum lvl, spdlog::string_view_t msg) SPDLOG_INLINE log_msg::log_msg(string_view_t logger_name, spdlog::level::forceable lvl, spdlog::string_view_t msg)
: log_msg(source_loc{}, logger_name, lvl, msg) : log_msg(source_loc{}, logger_name, lvl, msg)
{} {}

View File

@ -11,12 +11,12 @@ namespace details {
struct log_msg struct log_msg
{ {
log_msg() = default; log_msg() = default;
log_msg(source_loc loc, string_view_t logger_name, level::level_enum lvl, string_view_t msg); log_msg(source_loc loc, string_view_t logger_name, level::forceable lvl, string_view_t msg);
log_msg(string_view_t logger_name, level::level_enum lvl, string_view_t msg); log_msg(string_view_t logger_name, level::forceable lvl, string_view_t msg);
log_msg(const log_msg &other) = default; log_msg(const log_msg &other) = default;
string_view_t logger_name; string_view_t logger_name;
level::level_enum level{level::off}; level::forceable level{level::off, false};
log_clock::time_point time; log_clock::time_point time;
size_t thread_id{0}; size_t thread_id{0};

View File

@ -114,7 +114,7 @@ public:
void format(const details::log_msg &msg, const std::tm &, memory_buf_t &dest) override void format(const details::log_msg &msg, const std::tm &, memory_buf_t &dest) override
{ {
string_view_t &level_name = level::to_string_view(msg.level); string_view_t &level_name = level::to_string_view(msg.level.value);
ScopedPadder p(level_name.size(), padinfo_, dest); ScopedPadder p(level_name.size(), padinfo_, dest);
fmt_helper::append_string_view(level_name, dest); fmt_helper::append_string_view(level_name, dest);
} }
@ -131,7 +131,7 @@ public:
void format(const details::log_msg &msg, const std::tm &, memory_buf_t &dest) override void format(const details::log_msg &msg, const std::tm &, memory_buf_t &dest) override
{ {
string_view_t level_name{level::to_short_c_str(msg.level)}; string_view_t level_name{level::to_short_c_str(msg.level.value)};
ScopedPadder p(level_name.size(), padinfo_, dest); ScopedPadder p(level_name.size(), padinfo_, dest);
fmt_helper::append_string_view(level_name, dest); fmt_helper::append_string_view(level_name, dest);
} }
@ -959,7 +959,7 @@ public:
// wrap the level name with color // wrap the level name with color
msg.color_range_start = dest.size(); msg.color_range_start = dest.size();
// fmt_helper::append_string_view(level::to_c_str(msg.level), dest); // fmt_helper::append_string_view(level::to_c_str(msg.level), dest);
fmt_helper::append_string_view(level::to_string_view(msg.level), dest); fmt_helper::append_string_view(level::to_string_view(msg.level.value), dest);
msg.color_range_end = dest.size(); msg.color_range_end = dest.size();
dest.push_back(']'); dest.push_back(']');
dest.push_back(' '); dest.push_back(' ');

View File

@ -171,7 +171,7 @@ SPDLOG_INLINE void logger::sink_it_(const details::log_msg &msg)
{ {
for (auto &sink : sinks_) for (auto &sink : sinks_)
{ {
if (sink->should_log(msg.level)) if (msg.level.forced || sink->should_log(msg.level.value))
{ {
SPDLOG_TRY SPDLOG_TRY
{ {
@ -204,16 +204,16 @@ SPDLOG_INLINE void logger::dump_backtrace_()
using details::log_msg; using details::log_msg;
if (tracer_) if (tracer_)
{ {
sink_it_(log_msg{name(), level::info, "****************** Backtrace Start ******************"}); sink_it_(log_msg{name(), level::forceable{level::info, false}, "****************** Backtrace Start ******************"});
tracer_.foreach_pop([this](const log_msg &msg) { this->sink_it_(msg); }); tracer_.foreach_pop([this](const log_msg &msg) { this->sink_it_(msg); });
sink_it_(log_msg{name(), level::info, "****************** Backtrace End ********************"}); sink_it_(log_msg{name(), level::forceable{level::info, false}, "****************** Backtrace End ********************"});
} }
} }
SPDLOG_INLINE bool logger::should_flush_(const details::log_msg &msg) SPDLOG_INLINE bool logger::should_flush_(const details::log_msg &msg)
{ {
auto flush_level = flush_level_.load(std::memory_order_relaxed); auto flush_level = flush_level_.load(std::memory_order_relaxed);
return (msg.level >= flush_level) && (msg.level != level::off); return (msg.level.value >= flush_level) && (msg.level.value != level::off);
} }
SPDLOG_INLINE void logger::err_handler_(const std::string &msg) SPDLOG_INLINE void logger::err_handler_(const std::string &msg)

View File

@ -74,24 +74,24 @@ public:
void swap(spdlog::logger &other) SPDLOG_NOEXCEPT; void swap(spdlog::logger &other) SPDLOG_NOEXCEPT;
template<typename... Args> template<typename... Args>
void log(source_loc loc, level::level_enum lvl, string_view_t fmt, const Args &... args) void log(source_loc loc, level::level_enum lvl, string_view_t fmt, const Args &... args) {
{ log(loc, level::forceable{lvl, false}, fmt, args...);
auto level_enabled = should_log(lvl); }
if (!level_enabled && !tracer_)
{ template<typename... Args>
void log(source_loc loc, level::forceable lvl, string_view_t fmt, const Args &... args) {
auto level_enabled = lvl.forced || should_log(lvl.value);
if (!level_enabled && !tracer_) {
return; return;
} }
SPDLOG_TRY SPDLOG_TRY {
{
memory_buf_t buf; memory_buf_t buf;
fmt::format_to(buf, fmt, args...); fmt::format_to(buf, fmt, args...);
details::log_msg log_msg(loc, name_, lvl, string_view_t(buf.data(), buf.size())); details::log_msg log_msg(loc, name_, lvl, string_view_t(buf.data(), buf.size()));
if (level_enabled) if (level_enabled) {
{
sink_it_(log_msg); sink_it_(log_msg);
} }
if (tracer_) if (tracer_) {
{
tracer_.push_back(log_msg); tracer_.push_back(log_msg);
} }
} }
@ -148,22 +148,23 @@ public:
// T can be statically converted to string_view // T can be statically converted to string_view
template<class T, typename std::enable_if<std::is_convertible<const T &, spdlog::string_view_t>::value, T>::type * = nullptr> template<class T, typename std::enable_if<std::is_convertible<const T &, spdlog::string_view_t>::value, T>::type * = nullptr>
void log(source_loc loc, level::level_enum lvl, const T &msg) void log(source_loc loc, level::level_enum lvl, const T &msg) {
{ log<T>(loc, level::forceable{lvl, false}, msg);
auto level_enabled = should_log(lvl); }
if (!level_enabled && !tracer_)
{ // T can be statically converted to string_view
template<class T, typename std::enable_if<std::is_convertible<const T &, spdlog::string_view_t>::value, T>::type * = nullptr>
void log(source_loc loc, level::forceable lvl, const T &msg) {
auto level_enabled = lvl.forced || should_log(lvl.value);
if (!level_enabled && !tracer_) {
return; return;
} }
SPDLOG_TRY SPDLOG_TRY {
{
details::log_msg log_msg(loc, name_, lvl, msg); details::log_msg log_msg(loc, name_, lvl, msg);
if (level_enabled) if (level_enabled) {
{
sink_it_(log_msg); sink_it_(log_msg);
} }
if (tracer_) if (tracer_) {
{
tracer_.push_back(log_msg); tracer_.push_back(log_msg);
} }
} }
@ -228,7 +229,13 @@ public:
template<typename... Args> template<typename... Args>
void log(source_loc loc, level::level_enum lvl, wstring_view_t fmt, const Args &... args) void log(source_loc loc, level::level_enum lvl, wstring_view_t fmt, const Args &... args)
{ {
auto level_enabled = should_log(lvl); log(loc, level::forceable{lvl, false}, fmt, args...);
}
template<typename... Args>
void log(source_loc loc, level::forceable lvl, wstring_view_t fmt, const Args &... args)
{
auto level_enabled = lvl.forced || should_log(lvl.level);
if (!level_enabled && !tracer_) if (!level_enabled && !tracer_)
{ {
return; return;

View File

@ -51,7 +51,7 @@ SPDLOG_INLINE void ansicolor_sink<ConsoleMutex>::log(const details::log_msg &msg
// before color range // before color range
print_range_(formatted, 0, msg.color_range_start); print_range_(formatted, 0, msg.color_range_start);
// in color range // in color range
print_ccode_(colors_[msg.level]); print_ccode_(colors_[msg.level.value]);
print_range_(formatted, msg.color_range_start, msg.color_range_end); print_range_(formatted, msg.color_range_start, msg.color_range_end);
print_ccode_(reset); print_ccode_(reset);
// after color range // after color range

View File

@ -55,7 +55,7 @@ protected:
{ {
for (auto &sink : sinks_) for (auto &sink : sinks_)
{ {
if (sink->should_log(msg.level)) if (msg.level.forced || sink->should_log(msg.level.value))
{ {
sink->log(msg); sink->log(msg);
} }

View File

@ -83,7 +83,7 @@ private:
// //
int syslog_prio_from_level(const details::log_msg &msg) const int syslog_prio_from_level(const details::log_msg &msg) const
{ {
return syslog_levels_.at(static_cast<levels_array::size_type>(msg.level)); return syslog_levels_.at(static_cast<levels_array::size_type>(msg.level.value));
} }
}; };