mirror of
https://github.com/gabime/spdlog.git
synced 2024-12-25 10:01:33 +08:00
Merge pull request #33 from divaykin/master
discard policies on queue overflow
This commit is contained in:
commit
08d8be3eba
@ -38,6 +38,7 @@
|
|||||||
#include <functional>
|
#include <functional>
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
#include "logger.h"
|
#include "logger.h"
|
||||||
|
#include "spdlog.h"
|
||||||
|
|
||||||
|
|
||||||
namespace spdlog
|
namespace spdlog
|
||||||
@ -52,9 +53,9 @@ class async_logger :public logger
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
template<class It>
|
template<class It>
|
||||||
async_logger(const std::string& name, const It& begin, const It& end, size_t queue_size, const std::function<void()>& worker_warmup_cb = nullptr);
|
async_logger(const std::string& name, const It& begin, const It& end, size_t queue_size, const async_queue_overflow_policy overflow_policy = async_queue_overflow_policy::block_retry, const std::function<void()>& worker_warmup_cb = nullptr);
|
||||||
async_logger(const std::string& logger_name, sinks_init_list sinks, size_t queue_size, const std::function<void()>& worker_warmup_cb = nullptr);
|
async_logger(const std::string& logger_name, sinks_init_list sinks, size_t queue_size, const async_queue_overflow_policy overflow_policy = async_queue_overflow_policy::block_retry, const std::function<void()>& worker_warmup_cb = nullptr);
|
||||||
async_logger(const std::string& logger_name, sink_ptr single_sink, size_t queue_size, const std::function<void()>& worker_warmup_cb = nullptr);
|
async_logger(const std::string& logger_name, sink_ptr single_sink, size_t queue_size, const async_queue_overflow_policy overflow_policy = async_queue_overflow_policy::block_retry, const std::function<void()>& worker_warmup_cb = nullptr);
|
||||||
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
@ -109,7 +109,7 @@ public:
|
|||||||
using clock = std::chrono::steady_clock;
|
using clock = std::chrono::steady_clock;
|
||||||
|
|
||||||
|
|
||||||
async_log_helper(formatter_ptr formatter, const std::vector<sink_ptr>& sinks, size_t queue_size, const std::function<void()>& worker_warmup_cb = nullptr);
|
async_log_helper(formatter_ptr formatter, const std::vector<sink_ptr>& sinks, size_t queue_size, const async_queue_overflow_policy overflow_policy = async_queue_overflow_policy::block_retry, const std::function<void()>& worker_warmup_cb = nullptr);
|
||||||
void log(const details::log_msg& msg);
|
void log(const details::log_msg& msg);
|
||||||
|
|
||||||
//Stop logging and join the back thread
|
//Stop logging and join the back thread
|
||||||
@ -126,6 +126,9 @@ private:
|
|||||||
// last exception thrown from the worker thread
|
// last exception thrown from the worker thread
|
||||||
std::shared_ptr<spdlog_ex> _last_workerthread_ex;
|
std::shared_ptr<spdlog_ex> _last_workerthread_ex;
|
||||||
|
|
||||||
|
// overflow policy
|
||||||
|
const async_queue_overflow_policy _overflow_policy;
|
||||||
|
|
||||||
// worker thread warmup callback - one can set thread priority, affinity, etc
|
// worker thread warmup callback - one can set thread priority, affinity, etc
|
||||||
const std::function<void()> _worker_warmup_cb;
|
const std::function<void()> _worker_warmup_cb;
|
||||||
|
|
||||||
@ -150,10 +153,11 @@ private:
|
|||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
// async_sink class implementation
|
// async_sink class implementation
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
inline spdlog::details::async_log_helper::async_log_helper(formatter_ptr formatter, const std::vector<sink_ptr>& sinks, size_t queue_size, const std::function<void()>& worker_warmup_cb):
|
inline spdlog::details::async_log_helper::async_log_helper(formatter_ptr formatter, const std::vector<sink_ptr>& sinks, size_t queue_size, const async_queue_overflow_policy overflow_policy, const std::function<void()>& worker_warmup_cb):
|
||||||
_formatter(formatter),
|
_formatter(formatter),
|
||||||
_sinks(sinks),
|
_sinks(sinks),
|
||||||
_q(queue_size),
|
_q(queue_size),
|
||||||
|
_overflow_policy(overflow_policy),
|
||||||
_worker_warmup_cb(worker_warmup_cb),
|
_worker_warmup_cb(worker_warmup_cb),
|
||||||
_worker_thread(&async_log_helper::worker_loop, this)
|
_worker_thread(&async_log_helper::worker_loop, this)
|
||||||
{}
|
{}
|
||||||
@ -178,7 +182,7 @@ inline void spdlog::details::async_log_helper::log(const details::log_msg& msg)
|
|||||||
{
|
{
|
||||||
throw_if_bad_worker();
|
throw_if_bad_worker();
|
||||||
async_msg new_msg(msg);
|
async_msg new_msg(msg);
|
||||||
if (!_q.enqueue(std::move(new_msg)))
|
if (!_q.enqueue(std::move(new_msg)) && _overflow_policy != async_queue_overflow_policy::discard_log_msg)
|
||||||
{
|
{
|
||||||
auto last_op_time = clock::now();
|
auto last_op_time = clock::now();
|
||||||
do
|
do
|
||||||
|
@ -34,17 +34,17 @@
|
|||||||
|
|
||||||
|
|
||||||
template<class It>
|
template<class It>
|
||||||
inline spdlog::async_logger::async_logger(const std::string& logger_name, const It& begin, const It& end, size_t queue_size, const std::function<void()>& worker_warmup_cb) :
|
inline spdlog::async_logger::async_logger(const std::string& logger_name, const It& begin, const It& end, size_t queue_size, const async_queue_overflow_policy overflow_policy, const std::function<void()>& worker_warmup_cb) :
|
||||||
logger(logger_name, begin, end),
|
logger(logger_name, begin, end),
|
||||||
_async_log_helper(new details::async_log_helper(_formatter, _sinks, queue_size, worker_warmup_cb))
|
_async_log_helper(new details::async_log_helper(_formatter, _sinks, queue_size, overflow_policy, worker_warmup_cb))
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
inline spdlog::async_logger::async_logger(const std::string& logger_name, sinks_init_list sinks, size_t queue_size, const std::function<void()>& worker_warmup_cb) :
|
inline spdlog::async_logger::async_logger(const std::string& logger_name, sinks_init_list sinks, size_t queue_size, const async_queue_overflow_policy overflow_policy, const std::function<void()>& worker_warmup_cb) :
|
||||||
async_logger(logger_name, sinks.begin(), sinks.end(), queue_size, worker_warmup_cb) {}
|
async_logger(logger_name, sinks.begin(), sinks.end(), queue_size, overflow_policy, worker_warmup_cb) {}
|
||||||
|
|
||||||
inline spdlog::async_logger::async_logger(const std::string& logger_name, sink_ptr single_sink, size_t queue_size, const std::function<void()>& worker_warmup_cb) :
|
inline spdlog::async_logger::async_logger(const std::string& logger_name, sink_ptr single_sink, size_t queue_size, const async_queue_overflow_policy overflow_policy, const std::function<void()>& worker_warmup_cb) :
|
||||||
async_logger(logger_name, { single_sink }, queue_size, worker_warmup_cb) {}
|
async_logger(logger_name, { single_sink }, queue_size, overflow_policy, worker_warmup_cb) {}
|
||||||
|
|
||||||
|
|
||||||
inline void spdlog::async_logger::_set_formatter(spdlog::formatter_ptr msg_formatter)
|
inline void spdlog::async_logger::_set_formatter(spdlog::formatter_ptr msg_formatter)
|
||||||
|
@ -62,7 +62,7 @@ public:
|
|||||||
return found->second;
|
return found->second;
|
||||||
std::shared_ptr<logger> new_logger;
|
std::shared_ptr<logger> new_logger;
|
||||||
if (_async_mode)
|
if (_async_mode)
|
||||||
new_logger = std::make_shared<async_logger>(logger_name, sinks_begin, sinks_end, _async_q_size, _worker_warmup_cb);
|
new_logger = std::make_shared<async_logger>(logger_name, sinks_begin, sinks_end, _async_q_size, _overflow_policy, _worker_warmup_cb);
|
||||||
else
|
else
|
||||||
new_logger = std::make_shared<logger>(logger_name, sinks_begin, sinks_end);
|
new_logger = std::make_shared<logger>(logger_name, sinks_begin, sinks_end);
|
||||||
|
|
||||||
@ -120,11 +120,12 @@ public:
|
|||||||
l.second->set_level(log_level);
|
l.second->set_level(log_level);
|
||||||
}
|
}
|
||||||
|
|
||||||
void set_async_mode(size_t q_size, const std::function<void()>& worker_warmup_cb = nullptr)
|
void set_async_mode(size_t q_size, const async_queue_overflow_policy overflow_policy, const std::function<void()>& worker_warmup_cb)
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(_mutex);
|
std::lock_guard<std::mutex> lock(_mutex);
|
||||||
_async_mode = true;
|
_async_mode = true;
|
||||||
_async_q_size = q_size;
|
_async_q_size = q_size;
|
||||||
|
_overflow_policy = overflow_policy;
|
||||||
_worker_warmup_cb = worker_warmup_cb;
|
_worker_warmup_cb = worker_warmup_cb;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -151,6 +152,7 @@ private:
|
|||||||
level::level_enum _level = level::info;
|
level::level_enum _level = level::info;
|
||||||
bool _async_mode = false;
|
bool _async_mode = false;
|
||||||
size_t _async_q_size = 0;
|
size_t _async_q_size = 0;
|
||||||
|
async_queue_overflow_policy _overflow_policy = async_queue_overflow_policy::block_retry;
|
||||||
std::function<void()> _worker_warmup_cb = nullptr;
|
std::function<void()> _worker_warmup_cb = nullptr;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -132,9 +132,9 @@ inline void spdlog::set_level(level::level_enum log_level)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
inline void spdlog::set_async_mode(size_t queue_size, const std::function<void()>& worker_warmup_cb)
|
inline void spdlog::set_async_mode(size_t queue_size, const async_queue_overflow_policy overflow_policy, const std::function<void()>& worker_warmup_cb)
|
||||||
{
|
{
|
||||||
details::registry::instance().set_async_mode(queue_size, worker_warmup_cb);
|
details::registry::instance().set_async_mode(queue_size, overflow_policy, worker_warmup_cb);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void spdlog::set_sync_mode()
|
inline void spdlog::set_sync_mode()
|
||||||
|
@ -61,9 +61,15 @@ void set_level(level::level_enum log_level);
|
|||||||
// Async mode - off by default.
|
// Async mode - off by default.
|
||||||
//
|
//
|
||||||
|
|
||||||
|
enum class async_queue_overflow_policy
|
||||||
|
{
|
||||||
|
block_retry, // Block / yield / sleep until message can be enqueued
|
||||||
|
discard_log_msg // Discard the message it enqueue fails
|
||||||
|
};
|
||||||
|
|
||||||
// Turn on async mode and set the queue size for each async_logger
|
// Turn on async mode and set the queue size for each async_logger
|
||||||
|
|
||||||
void set_async_mode(size_t queue_size, const std::function<void()>& worker_warmup_cb = nullptr);
|
void set_async_mode(size_t queue_size, const async_queue_overflow_policy overflow_policy = async_queue_overflow_policy::block_retry, const std::function<void()>& worker_warmup_cb = nullptr);
|
||||||
// Turn off async mode
|
// Turn off async mode
|
||||||
void set_sync_mode();
|
void set_sync_mode();
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user