Refactored logger
This commit is contained in:
		@@ -48,10 +48,6 @@ SPDLOG_INLINE bool backtracer::enabled() const
 | 
			
		||||
    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)
 | 
			
		||||
{
 | 
			
		||||
 
 | 
			
		||||
@@ -31,7 +31,6 @@ public:
 | 
			
		||||
    void enable(size_t size);
 | 
			
		||||
    void disable();
 | 
			
		||||
    bool enabled() const;
 | 
			
		||||
    explicit operator bool() const;
 | 
			
		||||
    void push_back(const log_msg &msg);
 | 
			
		||||
 | 
			
		||||
    // 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)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 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)
 | 
			
		||||
{
 | 
			
		||||
    for (auto &sink : sinks_)
 | 
			
		||||
@@ -207,7 +219,7 @@ SPDLOG_INLINE void logger::flush_()
 | 
			
		||||
SPDLOG_INLINE void logger::dump_backtrace_()
 | 
			
		||||
{
 | 
			
		||||
    using details::log_msg;
 | 
			
		||||
    if (tracer_)
 | 
			
		||||
    if (tracer_.enabled())
 | 
			
		||||
    {
 | 
			
		||||
        sink_it_(log_msg{name(), level::info, "****************** Backtrace Start ******************"});
 | 
			
		||||
        tracer_.foreach_pop([this](const log_msg &msg) { this->sink_it_(msg); });
 | 
			
		||||
 
 | 
			
		||||
@@ -73,11 +73,11 @@ public:
 | 
			
		||||
 | 
			
		||||
    void swap(spdlog::logger &other) SPDLOG_NOEXCEPT;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    template<typename... Args>
 | 
			
		||||
    void log(source_loc loc, level::level_enum lvl, string_view_t fmt, const Args &... args)
 | 
			
		||||
    {
 | 
			
		||||
        auto level_enabled = should_log(lvl);
 | 
			
		||||
        if (!level_enabled && !tracer_)
 | 
			
		||||
        if (!should_log(lvl) && !tracer_.enabled())
 | 
			
		||||
        {
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
@@ -86,14 +86,7 @@ public:
 | 
			
		||||
            memory_buf_t buf;
 | 
			
		||||
            fmt::format_to(buf, fmt, args...);
 | 
			
		||||
            details::log_msg log_msg(loc, name_, lvl, string_view_t(buf.data(), buf.size()));
 | 
			
		||||
            if (level_enabled)
 | 
			
		||||
            {
 | 
			
		||||
                sink_it_(log_msg);
 | 
			
		||||
            }
 | 
			
		||||
            if (tracer_)
 | 
			
		||||
            {
 | 
			
		||||
                tracer_.push_back(log_msg);
 | 
			
		||||
            }
 | 
			
		||||
            log_it_(log_msg);
 | 
			
		||||
        }
 | 
			
		||||
        SPDLOG_LOGGER_CATCH()
 | 
			
		||||
    }
 | 
			
		||||
@@ -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>
 | 
			
		||||
    void log(source_loc loc, level::level_enum lvl, const T &msg)
 | 
			
		||||
    {
 | 
			
		||||
        auto level_enabled = should_log(lvl);
 | 
			
		||||
        if (!level_enabled && !tracer_)
 | 
			
		||||
        if (!should_log(lvl) && !tracer_.enabled())
 | 
			
		||||
        {
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
        SPDLOG_TRY
 | 
			
		||||
        {
 | 
			
		||||
            details::log_msg log_msg(loc, name_, lvl, msg);
 | 
			
		||||
            if (level_enabled)
 | 
			
		||||
            {
 | 
			
		||||
                sink_it_(log_msg);
 | 
			
		||||
            }
 | 
			
		||||
            if (tracer_)
 | 
			
		||||
            {
 | 
			
		||||
                tracer_.push_back(log_msg);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        SPDLOG_LOGGER_CATCH()
 | 
			
		||||
 | 
			
		||||
        details::log_msg log_msg(loc, name_, lvl, msg);
 | 
			
		||||
        log_it_(log_msg);
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    void log(level::level_enum lvl, string_view_t msg)
 | 
			
		||||
@@ -228,8 +211,7 @@ public:
 | 
			
		||||
    template<typename... Args>
 | 
			
		||||
    void log(source_loc loc, level::level_enum lvl, wstring_view_t fmt, const Args &... args)
 | 
			
		||||
    {
 | 
			
		||||
        auto level_enabled = should_log(lvl);
 | 
			
		||||
        if (!level_enabled && !tracer_)
 | 
			
		||||
        if (!should_log(lvl) && !tracer_.enabled())
 | 
			
		||||
        {
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
@@ -242,15 +224,7 @@ public:
 | 
			
		||||
            memory_buf_t 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()));
 | 
			
		||||
 | 
			
		||||
            if (level_enabled)
 | 
			
		||||
            {
 | 
			
		||||
                sink_it_(log_msg);
 | 
			
		||||
            }
 | 
			
		||||
            if (tracer_)
 | 
			
		||||
            {
 | 
			
		||||
                tracer_.push_back(log_msg);
 | 
			
		||||
            }
 | 
			
		||||
            log_it_(log_msg);
 | 
			
		||||
        }
 | 
			
		||||
        SPDLOG_LOGGER_CATCH()
 | 
			
		||||
    }
 | 
			
		||||
@@ -301,7 +275,7 @@ public:
 | 
			
		||||
    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)
 | 
			
		||||
    {
 | 
			
		||||
        if (!should_log(lvl))
 | 
			
		||||
        if (!should_log(lvl) && !tracer_.enabled())
 | 
			
		||||
        {
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
@@ -310,9 +284,8 @@ public:
 | 
			
		||||
        {
 | 
			
		||||
            memory_buf_t buf;
 | 
			
		||||
            details::os::wstr_to_utf8buf(msg, buf);
 | 
			
		||||
 | 
			
		||||
            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()
 | 
			
		||||
    }
 | 
			
		||||
@@ -367,6 +340,9 @@ protected:
 | 
			
		||||
    err_handler custom_err_handler_{nullptr};
 | 
			
		||||
    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 flush_();
 | 
			
		||||
    void dump_backtrace_();
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user