Commit fcc809f4 authored by gabime's avatar gabime

Refactored logger

parent f3369677
...@@ -48,10 +48,6 @@ SPDLOG_INLINE bool backtracer::enabled() const ...@@ -48,10 +48,6 @@ SPDLOG_INLINE bool backtracer::enabled() const
return enabled_.load(std::memory_order_relaxed); return enabled_.load(std::memory_order_relaxed);
} }
SPDLOG_INLINE backtracer::operator bool() const
{
return enabled();
}
SPDLOG_INLINE void backtracer::push_back(const log_msg &msg) SPDLOG_INLINE void backtracer::push_back(const log_msg &msg)
{ {
......
...@@ -31,7 +31,6 @@ public: ...@@ -31,7 +31,6 @@ public:
void enable(size_t size); void enable(size_t size);
void disable(); void disable();
bool enabled() const; bool enabled() const;
explicit operator bool() const;
void push_back(const log_msg &msg); void push_back(const log_msg &msg);
// pop all items in the q and apply the given fun on each of them. // pop all items in the q and apply the given fun on each of them.
......
...@@ -172,6 +172,18 @@ SPDLOG_INLINE std::shared_ptr<logger> logger::clone(std::string logger_name) ...@@ -172,6 +172,18 @@ SPDLOG_INLINE std::shared_ptr<logger> logger::clone(std::string logger_name)
} }
// protected methods // protected methods
SPDLOG_INLINE void logger::log_it_(const details::log_msg &log_msg)
{
if (should_log(log_msg.level))
{
sink_it_(log_msg);
}
if (tracer_.enabled())
{
tracer_.push_back(log_msg);
}
}
SPDLOG_INLINE void logger::sink_it_(const details::log_msg &msg) SPDLOG_INLINE void logger::sink_it_(const details::log_msg &msg)
{ {
for (auto &sink : sinks_) for (auto &sink : sinks_)
...@@ -207,7 +219,7 @@ SPDLOG_INLINE void logger::flush_() ...@@ -207,7 +219,7 @@ SPDLOG_INLINE void logger::flush_()
SPDLOG_INLINE void logger::dump_backtrace_() SPDLOG_INLINE void logger::dump_backtrace_()
{ {
using details::log_msg; using details::log_msg;
if (tracer_) if (tracer_.enabled())
{ {
sink_it_(log_msg{name(), level::info, "****************** Backtrace Start ******************"}); sink_it_(log_msg{name(), level::info, "****************** 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); });
......
...@@ -73,11 +73,11 @@ public: ...@@ -73,11 +73,11 @@ 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)
{ {
auto level_enabled = should_log(lvl); if (!should_log(lvl) && !tracer_.enabled())
if (!level_enabled && !tracer_)
{ {
return; return;
} }
...@@ -86,14 +86,7 @@ public: ...@@ -86,14 +86,7 @@ public:
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) log_it_(log_msg);
{
sink_it_(log_msg);
}
if (tracer_)
{
tracer_.push_back(log_msg);
}
} }
SPDLOG_LOGGER_CATCH() SPDLOG_LOGGER_CATCH()
} }
...@@ -150,24 +143,14 @@ public: ...@@ -150,24 +143,14 @@ public:
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)
{ {
auto level_enabled = should_log(lvl); if (!should_log(lvl) && !tracer_.enabled())
if (!level_enabled && !tracer_)
{ {
return; return;
} }
SPDLOG_TRY
{ details::log_msg log_msg(loc, name_, lvl, msg);
details::log_msg log_msg(loc, name_, lvl, msg); log_it_(log_msg);
if (level_enabled)
{
sink_it_(log_msg);
}
if (tracer_)
{
tracer_.push_back(log_msg);
}
}
SPDLOG_LOGGER_CATCH()
} }
void log(level::level_enum lvl, string_view_t msg) void log(level::level_enum lvl, string_view_t msg)
...@@ -228,8 +211,7 @@ public: ...@@ -228,8 +211,7 @@ 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); if (!should_log(lvl) && !tracer_.enabled())
if (!level_enabled && !tracer_)
{ {
return; return;
} }
...@@ -242,15 +224,7 @@ public: ...@@ -242,15 +224,7 @@ public:
memory_buf_t buf; memory_buf_t buf;
details::os::wstr_to_utf8buf(wstring_view_t(wbuf.data(), wbuf.size()), buf); details::os::wstr_to_utf8buf(wstring_view_t(wbuf.data(), wbuf.size()), buf);
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()));
log_it_(log_msg);
if (level_enabled)
{
sink_it_(log_msg);
}
if (tracer_)
{
tracer_.push_back(log_msg);
}
} }
SPDLOG_LOGGER_CATCH() SPDLOG_LOGGER_CATCH()
} }
...@@ -301,7 +275,7 @@ public: ...@@ -301,7 +275,7 @@ public:
template<class T, typename std::enable_if<is_convertible_to_wstring_view<const T &>::value, T>::type * = nullptr> template<class T, typename std::enable_if<is_convertible_to_wstring_view<const 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)
{ {
if (!should_log(lvl)) if (!should_log(lvl) && !tracer_.enabled())
{ {
return; return;
} }
...@@ -310,9 +284,8 @@ public: ...@@ -310,9 +284,8 @@ public:
{ {
memory_buf_t buf; memory_buf_t buf;
details::os::wstr_to_utf8buf(msg, buf); details::os::wstr_to_utf8buf(msg, buf);
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()));
sink_it_(log_msg); log_it_(log_msg);
} }
SPDLOG_LOGGER_CATCH() SPDLOG_LOGGER_CATCH()
} }
...@@ -367,6 +340,9 @@ protected: ...@@ -367,6 +340,9 @@ protected:
err_handler custom_err_handler_{nullptr}; err_handler custom_err_handler_{nullptr};
details::backtracer tracer_; details::backtracer tracer_;
// log the given message (if the given log level is high enough),
// and save backtrace (if backtrace is enabled).
void log_it_(const details::log_msg &log_msg);
virtual void sink_it_(const details::log_msg &msg); virtual void sink_it_(const details::log_msg &msg);
virtual void flush_(); virtual void flush_();
void dump_backtrace_(); void dump_backtrace_();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment