Fixed async dump_backtrace
This commit is contained in:
		@@ -4,7 +4,9 @@
 | 
			
		||||
#pragma once
 | 
			
		||||
 | 
			
		||||
#ifndef SPDLOG_HEADER_ONLY
 | 
			
		||||
 | 
			
		||||
#include "spdlog/async_logger.h"
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#include "spdlog/sinks/sink.h"
 | 
			
		||||
@@ -73,12 +75,42 @@ SPDLOG_INLINE void spdlog::async_logger::backend_log_(const details::log_msg &in
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
SPDLOG_INLINE void spdlog::async_logger::backend_flush_(){SPDLOG_TRY{for (auto &sink : sinks_){sink->flush();
 | 
			
		||||
SPDLOG_INLINE void spdlog::async_logger::backend_flush_()
 | 
			
		||||
{
 | 
			
		||||
    SPDLOG_TRY
 | 
			
		||||
    {
 | 
			
		||||
        for (auto &sink : sinks_){sink->flush();}
 | 
			
		||||
    }
 | 
			
		||||
    SPDLOG_LOGGER_CATCH()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
SPDLOG_INLINE void spdlog::async_logger::dump_backtrace_()
 | 
			
		||||
{
 | 
			
		||||
    if (auto pool_ptr = thread_pool_.lock())
 | 
			
		||||
    {
 | 
			
		||||
        pool_ptr->post_dump_backtrace(shared_from_this(), overflow_policy_);
 | 
			
		||||
    }
 | 
			
		||||
    else
 | 
			
		||||
    {
 | 
			
		||||
        SPDLOG_THROW(spdlog_ex("async dumptrace: thread pool doesn't exist anymore"));
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
SPDLOG_LOGGER_CATCH()
 | 
			
		||||
 | 
			
		||||
SPDLOG_INLINE void spdlog::async_logger::backend_dump_backtrace_()
 | 
			
		||||
{
 | 
			
		||||
    SPDLOG_TRY
 | 
			
		||||
    {
 | 
			
		||||
        if (backtrace_sink_) {
 | 
			
		||||
            auto tracer = static_cast<sinks::backtrace_sink_mt *>(backtrace_sink_.get());
 | 
			
		||||
            tracer->dump_backtrace(name());
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    SPDLOG_LOGGER_CATCH()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
SPDLOG_INLINE std::shared_ptr<spdlog::logger> spdlog::async_logger::clone(std::string new_name)
 | 
			
		||||
{
 | 
			
		||||
    auto cloned = std::make_shared<spdlog::async_logger>(std::move(new_name), sinks_.begin(), sinks_.end(), thread_pool_, overflow_policy_);
 | 
			
		||||
 
 | 
			
		||||
@@ -51,11 +51,14 @@ public:
 | 
			
		||||
 | 
			
		||||
    std::shared_ptr<logger> clone(std::string new_name) override;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
protected:
 | 
			
		||||
    void sink_it_(const details::log_msg &msg) override;
 | 
			
		||||
    void flush_() override;
 | 
			
		||||
    void dump_backtrace_() override ;
 | 
			
		||||
    void backend_log_(const details::log_msg &incoming_log_msg);
 | 
			
		||||
    void backend_flush_();
 | 
			
		||||
    void backend_dump_backtrace_();
 | 
			
		||||
 | 
			
		||||
private:
 | 
			
		||||
    std::weak_ptr<details::thread_pool> thread_pool_;
 | 
			
		||||
 
 | 
			
		||||
@@ -62,6 +62,11 @@ void SPDLOG_INLINE thread_pool::post_flush(async_logger_ptr &&worker_ptr, async_
 | 
			
		||||
    post_async_msg_(async_msg(std::move(worker_ptr), async_msg_type::flush), overflow_policy);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SPDLOG_INLINE thread_pool::post_dump_backtrace(async_logger_ptr &&worker_ptr, async_overflow_policy overflow_policy)
 | 
			
		||||
{
 | 
			
		||||
    post_async_msg_(async_msg(std::move(worker_ptr), async_msg_type::dump_backtrace), overflow_policy);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
size_t SPDLOG_INLINE thread_pool::overrun_counter()
 | 
			
		||||
{
 | 
			
		||||
    return q_.overrun_counter();
 | 
			
		||||
@@ -109,6 +114,12 @@ bool SPDLOG_INLINE thread_pool::process_next_msg_()
 | 
			
		||||
        return true;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    case async_msg_type ::dump_backtrace:
 | 
			
		||||
    {
 | 
			
		||||
        incoming_async_msg.worker_ptr->backend_dump_backtrace_();
 | 
			
		||||
        return true;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    case async_msg_type::terminate:
 | 
			
		||||
    {
 | 
			
		||||
        return false;
 | 
			
		||||
 
 | 
			
		||||
@@ -24,7 +24,8 @@ enum class async_msg_type
 | 
			
		||||
{
 | 
			
		||||
    log,
 | 
			
		||||
    flush,
 | 
			
		||||
    terminate
 | 
			
		||||
    terminate,
 | 
			
		||||
    dump_backtrace
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
#include "spdlog/details/log_msg_buffer.h"
 | 
			
		||||
@@ -96,6 +97,7 @@ public:
 | 
			
		||||
 | 
			
		||||
    void post_log(async_logger_ptr &&worker_ptr, const details::log_msg &msg, async_overflow_policy overflow_policy);
 | 
			
		||||
    void post_flush(async_logger_ptr &&worker_ptr, async_overflow_policy overflow_policy);
 | 
			
		||||
    void post_dump_backtrace(async_logger_ptr &&worker_ptr, async_overflow_policy overflow_policy);
 | 
			
		||||
    size_t overrun_counter();
 | 
			
		||||
 | 
			
		||||
private:
 | 
			
		||||
 
 | 
			
		||||
@@ -158,13 +158,10 @@ SPDLOG_INLINE void logger::disable_backtrace()
 | 
			
		||||
 | 
			
		||||
SPDLOG_INLINE void logger::dump_backtrace()
 | 
			
		||||
{
 | 
			
		||||
    if (backtrace_sink_)
 | 
			
		||||
    {
 | 
			
		||||
        auto tracer = static_cast<sinks::backtrace_sink_mt *>(backtrace_sink_.get());
 | 
			
		||||
        tracer->dump_backtrace(name());
 | 
			
		||||
    }
 | 
			
		||||
    dump_backtrace_();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
// flush functions
 | 
			
		||||
SPDLOG_INLINE void logger::flush()
 | 
			
		||||
{
 | 
			
		||||
@@ -239,6 +236,15 @@ SPDLOG_INLINE void logger::flush_()
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
SPDLOG_INLINE void logger::dump_backtrace_()
 | 
			
		||||
{
 | 
			
		||||
    if (backtrace_sink_)
 | 
			
		||||
    {
 | 
			
		||||
        auto tracer = static_cast<sinks::backtrace_sink_mt *>(backtrace_sink_.get());
 | 
			
		||||
        tracer->dump_backtrace(name());
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
SPDLOG_INLINE bool logger::should_flush_(const details::log_msg &msg)
 | 
			
		||||
{
 | 
			
		||||
    auto flush_level = flush_level_.load(std::memory_order_relaxed);
 | 
			
		||||
 
 | 
			
		||||
@@ -325,7 +325,6 @@ public:
 | 
			
		||||
 | 
			
		||||
    void enable_backtrace(size_t n_messages = 16);
 | 
			
		||||
    void disable_backtrace();
 | 
			
		||||
 | 
			
		||||
    void dump_backtrace();
 | 
			
		||||
 | 
			
		||||
    // flush functions
 | 
			
		||||
@@ -355,6 +354,7 @@ protected:
 | 
			
		||||
 | 
			
		||||
    virtual void sink_it_(const details::log_msg &msg);
 | 
			
		||||
    virtual void flush_();
 | 
			
		||||
    virtual void dump_backtrace_();
 | 
			
		||||
    bool should_flush_(const details::log_msg &msg);
 | 
			
		||||
 | 
			
		||||
    // handle errors during logging.
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user