mirror of
https://github.com/gabime/spdlog.git
synced 2024-12-26 02:21:34 +08:00
In async mode, worker thread can now execute an optional teardown callback upon exit.
(Note: this can be helpful when a custom sink invokes a JNI callback, which implies that worker thread was previously attached to JVM, and needs to be cleanly detached upon exit)
This commit is contained in:
parent
409f24d104
commit
ee815042dd
@ -13,7 +13,7 @@
|
|||||||
// 1. Checks if its log level is enough to log the message
|
// 1. Checks if its log level is enough to log the message
|
||||||
// 2. Push a new copy of the message to a queue (or block the caller until space is available in the queue)
|
// 2. Push a new copy of the message to a queue (or block the caller until space is available in the queue)
|
||||||
// 3. will throw spdlog_ex upon log exceptions
|
// 3. will throw spdlog_ex upon log exceptions
|
||||||
// Upong destruction, logs all remaining messages in the queue before destructing..
|
// Upon destruction, logs all remaining messages in the queue before destructing..
|
||||||
|
|
||||||
#include <spdlog/common.h>
|
#include <spdlog/common.h>
|
||||||
#include <spdlog/logger.h>
|
#include <spdlog/logger.h>
|
||||||
@ -41,21 +41,24 @@ public:
|
|||||||
size_t queue_size,
|
size_t queue_size,
|
||||||
const async_overflow_policy overflow_policy = async_overflow_policy::block_retry,
|
const async_overflow_policy overflow_policy = async_overflow_policy::block_retry,
|
||||||
const std::function<void()>& worker_warmup_cb = nullptr,
|
const std::function<void()>& worker_warmup_cb = nullptr,
|
||||||
const std::chrono::milliseconds& flush_interval_ms = std::chrono::milliseconds::zero());
|
const std::chrono::milliseconds& flush_interval_ms = std::chrono::milliseconds::zero(),
|
||||||
|
const std::function<void()>& worker_teardown_cb = nullptr);
|
||||||
|
|
||||||
async_logger(const std::string& logger_name,
|
async_logger(const std::string& logger_name,
|
||||||
sinks_init_list sinks,
|
sinks_init_list sinks,
|
||||||
size_t queue_size,
|
size_t queue_size,
|
||||||
const async_overflow_policy overflow_policy = async_overflow_policy::block_retry,
|
const async_overflow_policy overflow_policy = async_overflow_policy::block_retry,
|
||||||
const std::function<void()>& worker_warmup_cb = nullptr,
|
const std::function<void()>& worker_warmup_cb = nullptr,
|
||||||
const std::chrono::milliseconds& flush_interval_ms = std::chrono::milliseconds::zero());
|
const std::chrono::milliseconds& flush_interval_ms = std::chrono::milliseconds::zero(),
|
||||||
|
const std::function<void()>& worker_teardown_cb = nullptr);
|
||||||
|
|
||||||
async_logger(const std::string& logger_name,
|
async_logger(const std::string& logger_name,
|
||||||
sink_ptr single_sink,
|
sink_ptr single_sink,
|
||||||
size_t queue_size,
|
size_t queue_size,
|
||||||
const async_overflow_policy overflow_policy = async_overflow_policy::block_retry,
|
const async_overflow_policy overflow_policy = async_overflow_policy::block_retry,
|
||||||
const std::function<void()>& worker_warmup_cb = nullptr,
|
const std::function<void()>& worker_warmup_cb = nullptr,
|
||||||
const std::chrono::milliseconds& flush_interval_ms = std::chrono::milliseconds::zero());
|
const std::chrono::milliseconds& flush_interval_ms = std::chrono::milliseconds::zero(),
|
||||||
|
const std::function<void()>& worker_teardown_cb = nullptr);
|
||||||
|
|
||||||
|
|
||||||
void flush() override;
|
void flush() override;
|
||||||
@ -71,4 +74,3 @@ private:
|
|||||||
|
|
||||||
|
|
||||||
#include <spdlog/details/async_logger_impl.h>
|
#include <spdlog/details/async_logger_impl.h>
|
||||||
|
|
||||||
|
@ -121,7 +121,8 @@ public:
|
|||||||
size_t queue_size,
|
size_t queue_size,
|
||||||
const async_overflow_policy overflow_policy = async_overflow_policy::block_retry,
|
const async_overflow_policy overflow_policy = async_overflow_policy::block_retry,
|
||||||
const std::function<void()>& worker_warmup_cb = nullptr,
|
const std::function<void()>& worker_warmup_cb = nullptr,
|
||||||
const std::chrono::milliseconds& flush_interval_ms = std::chrono::milliseconds::zero());
|
const std::chrono::milliseconds& flush_interval_ms = std::chrono::milliseconds::zero(),
|
||||||
|
const std::function<void()>& worker_teardown_cb = nullptr);
|
||||||
|
|
||||||
void log(const details::log_msg& msg);
|
void log(const details::log_msg& msg);
|
||||||
|
|
||||||
@ -157,6 +158,9 @@ private:
|
|||||||
// auto periodic sink flush parameter
|
// auto periodic sink flush parameter
|
||||||
const std::chrono::milliseconds _flush_interval_ms;
|
const std::chrono::milliseconds _flush_interval_ms;
|
||||||
|
|
||||||
|
// worker thread teardown callback
|
||||||
|
const std::function<void()> _worker_teardown_cb;
|
||||||
|
|
||||||
// worker thread
|
// worker thread
|
||||||
std::thread _worker_thread;
|
std::thread _worker_thread;
|
||||||
|
|
||||||
@ -190,7 +194,8 @@ inline spdlog::details::async_log_helper::async_log_helper(
|
|||||||
size_t queue_size,
|
size_t queue_size,
|
||||||
const async_overflow_policy overflow_policy,
|
const async_overflow_policy overflow_policy,
|
||||||
const std::function<void()>& worker_warmup_cb,
|
const std::function<void()>& worker_warmup_cb,
|
||||||
const std::chrono::milliseconds& flush_interval_ms):
|
const std::chrono::milliseconds& flush_interval_ms,
|
||||||
|
const std::function<void()>& worker_teardown_cb):
|
||||||
_formatter(formatter),
|
_formatter(formatter),
|
||||||
_sinks(sinks),
|
_sinks(sinks),
|
||||||
_q(queue_size),
|
_q(queue_size),
|
||||||
@ -199,6 +204,7 @@ inline spdlog::details::async_log_helper::async_log_helper(
|
|||||||
_overflow_policy(overflow_policy),
|
_overflow_policy(overflow_policy),
|
||||||
_worker_warmup_cb(worker_warmup_cb),
|
_worker_warmup_cb(worker_warmup_cb),
|
||||||
_flush_interval_ms(flush_interval_ms),
|
_flush_interval_ms(flush_interval_ms),
|
||||||
|
_worker_teardown_cb(worker_teardown_cb),
|
||||||
_worker_thread(&async_log_helper::worker_loop, this)
|
_worker_thread(&async_log_helper::worker_loop, this)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
@ -254,6 +260,7 @@ inline void spdlog::details::async_log_helper::worker_loop()
|
|||||||
auto last_pop = details::os::now();
|
auto last_pop = details::os::now();
|
||||||
auto last_flush = last_pop;
|
auto last_flush = last_pop;
|
||||||
while(process_next_msg(last_pop, last_flush));
|
while(process_next_msg(last_pop, last_flush));
|
||||||
|
if (_worker_teardown_cb) _worker_teardown_cb();
|
||||||
}
|
}
|
||||||
catch (const std::exception& ex)
|
catch (const std::exception& ex)
|
||||||
{
|
{
|
||||||
|
@ -23,9 +23,10 @@ inline spdlog::async_logger::async_logger(const std::string& logger_name,
|
|||||||
size_t queue_size,
|
size_t queue_size,
|
||||||
const async_overflow_policy overflow_policy,
|
const async_overflow_policy overflow_policy,
|
||||||
const std::function<void()>& worker_warmup_cb,
|
const std::function<void()>& worker_warmup_cb,
|
||||||
const std::chrono::milliseconds& flush_interval_ms) :
|
const std::chrono::milliseconds& flush_interval_ms,
|
||||||
|
const std::function<void()>& worker_teardown_cb) :
|
||||||
logger(logger_name, begin, end),
|
logger(logger_name, begin, end),
|
||||||
_async_log_helper(new details::async_log_helper(_formatter, _sinks, queue_size, overflow_policy, worker_warmup_cb, flush_interval_ms))
|
_async_log_helper(new details::async_log_helper(_formatter, _sinks, queue_size, overflow_policy, worker_warmup_cb, flush_interval_ms, worker_teardown_cb))
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -34,19 +35,21 @@ inline spdlog::async_logger::async_logger(const std::string& logger_name,
|
|||||||
size_t queue_size,
|
size_t queue_size,
|
||||||
const async_overflow_policy overflow_policy,
|
const async_overflow_policy overflow_policy,
|
||||||
const std::function<void()>& worker_warmup_cb,
|
const std::function<void()>& worker_warmup_cb,
|
||||||
const std::chrono::milliseconds& flush_interval_ms) :
|
const std::chrono::milliseconds& flush_interval_ms,
|
||||||
async_logger(logger_name, sinks.begin(), sinks.end(), queue_size, overflow_policy, worker_warmup_cb, flush_interval_ms) {}
|
const std::function<void()>& worker_teardown_cb) :
|
||||||
|
async_logger(logger_name, sinks.begin(), sinks.end(), queue_size, overflow_policy, worker_warmup_cb, flush_interval_ms, worker_teardown_cb) {}
|
||||||
|
|
||||||
inline spdlog::async_logger::async_logger(const std::string& logger_name,
|
inline spdlog::async_logger::async_logger(const std::string& logger_name,
|
||||||
sink_ptr single_sink,
|
sink_ptr single_sink,
|
||||||
size_t queue_size,
|
size_t queue_size,
|
||||||
const async_overflow_policy overflow_policy,
|
const async_overflow_policy overflow_policy,
|
||||||
const std::function<void()>& worker_warmup_cb,
|
const std::function<void()>& worker_warmup_cb,
|
||||||
const std::chrono::milliseconds& flush_interval_ms) :
|
const std::chrono::milliseconds& flush_interval_ms,
|
||||||
|
const std::function<void()>& worker_teardown_cb) :
|
||||||
async_logger(logger_name,
|
async_logger(logger_name,
|
||||||
{
|
{
|
||||||
single_sink
|
single_sink
|
||||||
}, queue_size, overflow_policy, worker_warmup_cb, flush_interval_ms) {}
|
}, queue_size, overflow_policy, worker_warmup_cb, flush_interval_ms, worker_teardown_cb) {}
|
||||||
|
|
||||||
|
|
||||||
inline void spdlog::async_logger::flush()
|
inline void spdlog::async_logger::flush()
|
||||||
|
@ -53,7 +53,7 @@ public:
|
|||||||
throw_if_exists(logger_name);
|
throw_if_exists(logger_name);
|
||||||
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, _overflow_policy, _worker_warmup_cb, _flush_interval_ms);
|
new_logger = std::make_shared<async_logger>(logger_name, sinks_begin, sinks_end, _async_q_size, _overflow_policy, _worker_warmup_cb, _flush_interval_ms, _worker_teardown_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);
|
||||||
|
|
||||||
@ -112,7 +112,7 @@ public:
|
|||||||
_level = log_level;
|
_level = log_level;
|
||||||
}
|
}
|
||||||
|
|
||||||
void set_async_mode(size_t q_size, const async_overflow_policy overflow_policy, const std::function<void()>& worker_warmup_cb, const std::chrono::milliseconds& flush_interval_ms)
|
void set_async_mode(size_t q_size, const async_overflow_policy overflow_policy, const std::function<void()>& worker_warmup_cb, const std::chrono::milliseconds& flush_interval_ms, const std::function<void()>& worker_teardown_cb)
|
||||||
{
|
{
|
||||||
std::lock_guard<Mutex> lock(_mutex);
|
std::lock_guard<Mutex> lock(_mutex);
|
||||||
_async_mode = true;
|
_async_mode = true;
|
||||||
@ -120,6 +120,7 @@ public:
|
|||||||
_overflow_policy = overflow_policy;
|
_overflow_policy = overflow_policy;
|
||||||
_worker_warmup_cb = worker_warmup_cb;
|
_worker_warmup_cb = worker_warmup_cb;
|
||||||
_flush_interval_ms = flush_interval_ms;
|
_flush_interval_ms = flush_interval_ms;
|
||||||
|
_worker_teardown_cb = worker_teardown_cb;
|
||||||
}
|
}
|
||||||
|
|
||||||
void set_sync_mode()
|
void set_sync_mode()
|
||||||
@ -153,6 +154,7 @@ private:
|
|||||||
async_overflow_policy _overflow_policy = async_overflow_policy::block_retry;
|
async_overflow_policy _overflow_policy = async_overflow_policy::block_retry;
|
||||||
std::function<void()> _worker_warmup_cb = nullptr;
|
std::function<void()> _worker_warmup_cb = nullptr;
|
||||||
std::chrono::milliseconds _flush_interval_ms;
|
std::chrono::milliseconds _flush_interval_ms;
|
||||||
|
std::function<void()> _worker_teardown_cb = nullptr;
|
||||||
};
|
};
|
||||||
#ifdef SPDLOG_NO_REGISTRY_MUTEX
|
#ifdef SPDLOG_NO_REGISTRY_MUTEX
|
||||||
typedef registry_t<spdlog::details::null_mutex> registry;
|
typedef registry_t<spdlog::details::null_mutex> registry;
|
||||||
|
@ -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 async_overflow_policy overflow_policy, const std::function<void()>& worker_warmup_cb, const std::chrono::milliseconds& flush_interval_ms)
|
inline void spdlog::set_async_mode(size_t queue_size, const async_overflow_policy overflow_policy, const std::function<void()>& worker_warmup_cb, const std::chrono::milliseconds& flush_interval_ms, const std::function<void()>& worker_teardown_cb)
|
||||||
{
|
{
|
||||||
details::registry::instance().set_async_mode(queue_size, overflow_policy, worker_warmup_cb, flush_interval_ms);
|
details::registry::instance().set_async_mode(queue_size, overflow_policy, worker_warmup_cb, flush_interval_ms, worker_teardown_cb);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void spdlog::set_sync_mode()
|
inline void spdlog::set_sync_mode()
|
||||||
@ -146,4 +146,3 @@ inline void spdlog::drop_all()
|
|||||||
{
|
{
|
||||||
details::registry::instance().drop_all();
|
details::registry::instance().drop_all();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -53,7 +53,10 @@ void set_level(level::level_enum log_level);
|
|||||||
// worker_warmup_cb (optional):
|
// worker_warmup_cb (optional):
|
||||||
// callback function that will be called in worker thread upon start (can be used to init stuff like thread affinity)
|
// callback function that will be called in worker thread upon start (can be used to init stuff like thread affinity)
|
||||||
//
|
//
|
||||||
void set_async_mode(size_t queue_size, const async_overflow_policy overflow_policy = async_overflow_policy::block_retry, const std::function<void()>& worker_warmup_cb = nullptr, const std::chrono::milliseconds& flush_interval_ms = std::chrono::milliseconds::zero());
|
// worker_teardown_cb (optional):
|
||||||
|
// callback function that will be called in worker thread upon exit
|
||||||
|
//
|
||||||
|
void set_async_mode(size_t queue_size, const async_overflow_policy overflow_policy = async_overflow_policy::block_retry, const std::function<void()>& worker_warmup_cb = nullptr, const std::chrono::milliseconds& flush_interval_ms = std::chrono::milliseconds::zero(), const std::function<void()>& worker_teardown_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