mirror of
https://github.com/gabime/spdlog.git
synced 2024-11-15 16:35:45 +08:00
Expend support for any std::chrono::duration in spdlog::flush_every
This allows things like: spdlog::flush_every(std::chrono::minutes(10)); spdlog::flush_every(std::chrono::milliseconds(100));
This commit is contained in:
parent
6c95f4c816
commit
dfe1009080
@ -10,27 +10,6 @@
|
||||
namespace spdlog {
|
||||
namespace details {
|
||||
|
||||
SPDLOG_INLINE periodic_worker::periodic_worker(const std::function<void()> &callback_fun, std::chrono::seconds interval)
|
||||
{
|
||||
active_ = (interval > std::chrono::seconds::zero());
|
||||
if (!active_)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
worker_thread_ = std::thread([this, callback_fun, interval]() {
|
||||
for (;;)
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(this->mutex_);
|
||||
if (this->cv_.wait_for(lock, interval, [this] { return !this->active_; }))
|
||||
{
|
||||
return; // active_ == false, so exit this thread
|
||||
}
|
||||
callback_fun();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// stop the worker thread and join it
|
||||
SPDLOG_INLINE periodic_worker::~periodic_worker()
|
||||
{
|
||||
|
@ -20,7 +20,26 @@ namespace details {
|
||||
class SPDLOG_API periodic_worker
|
||||
{
|
||||
public:
|
||||
periodic_worker(const std::function<void()> &callback_fun, std::chrono::seconds interval);
|
||||
template<typename Rep, typename Period>
|
||||
periodic_worker(const std::function<void()> &callback_fun, std::chrono::duration<Rep, Period> interval) {
|
||||
active_ = (interval > std::chrono::duration<Rep, Period>::zero());
|
||||
if (!active_)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
worker_thread_ = std::thread([this, callback_fun, interval]() {
|
||||
for (;;)
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(this->mutex_);
|
||||
if (this->cv_.wait_for(lock, interval, [this] { return !this->active_; }))
|
||||
{
|
||||
return; // active_ == false, so exit this thread
|
||||
}
|
||||
callback_fun();
|
||||
}
|
||||
});
|
||||
}
|
||||
periodic_worker(const periodic_worker &) = delete;
|
||||
periodic_worker &operator=(const periodic_worker &) = delete;
|
||||
// stop the worker thread and join it
|
||||
|
@ -188,13 +188,6 @@ SPDLOG_INLINE void registry::flush_on(level::level_enum log_level)
|
||||
flush_level_ = log_level;
|
||||
}
|
||||
|
||||
SPDLOG_INLINE void registry::flush_every(std::chrono::seconds interval)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(flusher_mutex_);
|
||||
auto clbk = [this]() { this->flush_all(); };
|
||||
periodic_flusher_ = details::make_unique<periodic_worker>(clbk, interval);
|
||||
}
|
||||
|
||||
SPDLOG_INLINE void registry::set_error_handler(err_handler handler)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(logger_map_mutex_);
|
||||
|
@ -9,6 +9,7 @@
|
||||
// This class is thread safe
|
||||
|
||||
#include <spdlog/common.h>
|
||||
#include <spdlog/details/periodic_worker.h>
|
||||
|
||||
#include <chrono>
|
||||
#include <functional>
|
||||
@ -22,7 +23,6 @@ class logger;
|
||||
|
||||
namespace details {
|
||||
class thread_pool;
|
||||
class periodic_worker;
|
||||
|
||||
class SPDLOG_API registry
|
||||
{
|
||||
@ -61,7 +61,13 @@ public:
|
||||
|
||||
void flush_on(level::level_enum log_level);
|
||||
|
||||
void flush_every(std::chrono::seconds interval);
|
||||
template<typename Rep, typename Period>
|
||||
void flush_every(std::chrono::duration<Rep, Period> interval)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(flusher_mutex_);
|
||||
auto clbk = [this]() { this->flush_all(); };
|
||||
periodic_flusher_ = details::make_unique<periodic_worker>(clbk, interval);
|
||||
}
|
||||
|
||||
void set_error_handler(err_handler handler);
|
||||
|
||||
|
@ -67,11 +67,6 @@ SPDLOG_INLINE void flush_on(level::level_enum log_level)
|
||||
details::registry::instance().flush_on(log_level);
|
||||
}
|
||||
|
||||
SPDLOG_INLINE void flush_every(std::chrono::seconds interval)
|
||||
{
|
||||
details::registry::instance().flush_every(interval);
|
||||
}
|
||||
|
||||
SPDLOG_INLINE void set_error_handler(void (*handler)(const std::string &msg))
|
||||
{
|
||||
details::registry::instance().set_error_handler(handler);
|
||||
|
@ -81,7 +81,11 @@ SPDLOG_API void flush_on(level::level_enum log_level);
|
||||
|
||||
// Start/Restart a periodic flusher thread
|
||||
// Warning: Use only if all your loggers are thread safe!
|
||||
SPDLOG_API void flush_every(std::chrono::seconds interval);
|
||||
template<typename Rep, typename Period>
|
||||
inline void flush_every(std::chrono::duration<Rep, Period> interval)
|
||||
{
|
||||
details::registry::instance().flush_every(interval);
|
||||
}
|
||||
|
||||
// Set global error handler
|
||||
SPDLOG_API void set_error_handler(void (*handler)(const std::string &msg));
|
||||
|
Loading…
Reference in New Issue
Block a user