From 16ea28cedf803a4a7019dce45329b0d93f327e54 Mon Sep 17 00:00:00 2001 From: Serhii Charykov Date: Tue, 18 Jun 2019 14:55:38 +0300 Subject: [PATCH] Allow flush period to be any duration instead of std::chrono:seconds --- include/spdlog/details/periodic_worker-inl.h | 21 ------------------- include/spdlog/details/periodic_worker.h | 22 +++++++++++++++++++- include/spdlog/details/registry-inl.h | 7 ------- include/spdlog/details/registry.h | 8 ++++++- include/spdlog/spdlog-inl.h | 5 ----- include/spdlog/spdlog.h | 7 ++++++- 6 files changed, 34 insertions(+), 36 deletions(-) diff --git a/include/spdlog/details/periodic_worker-inl.h b/include/spdlog/details/periodic_worker-inl.h index 1af6288a..8fb0ffef 100644 --- a/include/spdlog/details/periodic_worker-inl.h +++ b/include/spdlog/details/periodic_worker-inl.h @@ -10,27 +10,6 @@ namespace spdlog { namespace details { -SPDLOG_INLINE periodic_worker::periodic_worker(const std::function &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 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() { diff --git a/include/spdlog/details/periodic_worker.h b/include/spdlog/details/periodic_worker.h index d3b5c639..5994c930 100644 --- a/include/spdlog/details/periodic_worker.h +++ b/include/spdlog/details/periodic_worker.h @@ -20,7 +20,27 @@ namespace details { class periodic_worker { public: - periodic_worker(const std::function &callback_fun, std::chrono::seconds interval); + template + SPDLOG_INLINE periodic_worker(const std::function &callback_fun, std::chrono::duration interval) + { + active_ = (interval > std::chrono::duration::zero()); + if (!active_) + { + return; + } + + worker_thread_ = std::thread([this, callback_fun, interval]() { + for (;;) + { + std::unique_lock 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 diff --git a/include/spdlog/details/registry-inl.h b/include/spdlog/details/registry-inl.h index ffecfedb..df8ff87d 100644 --- a/include/spdlog/details/registry-inl.h +++ b/include/spdlog/details/registry-inl.h @@ -156,13 +156,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 lock(flusher_mutex_); - std::function clbk = std::bind(®istry::flush_all, this); - periodic_flusher_ = details::make_unique(clbk, interval); -} - SPDLOG_INLINE void registry::set_error_handler(void (*handler)(const std::string &msg)) { std::lock_guard lock(logger_map_mutex_); diff --git a/include/spdlog/details/registry.h b/include/spdlog/details/registry.h index f96a7698..70243157 100644 --- a/include/spdlog/details/registry.h +++ b/include/spdlog/details/registry.h @@ -56,7 +56,13 @@ public: void flush_on(level::level_enum log_level); - void flush_every(std::chrono::seconds interval); + template + void flush_every(std::chrono::duration interval) + { + std::lock_guard lock(flusher_mutex_); + std::function clbk = std::bind(®istry::flush_all, this); + periodic_flusher_ = details::make_unique(clbk, interval); + } void set_error_handler(void (*handler)(const std::string &msg)); diff --git a/include/spdlog/spdlog-inl.h b/include/spdlog/spdlog-inl.h index 5f6c8fb1..6d540677 100644 --- a/include/spdlog/spdlog-inl.h +++ b/include/spdlog/spdlog-inl.h @@ -42,11 +42,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); diff --git a/include/spdlog/spdlog.h b/include/spdlog/spdlog.h index dfa42145..0b05615e 100644 --- a/include/spdlog/spdlog.h +++ b/include/spdlog/spdlog.h @@ -10,6 +10,7 @@ #pragma once #include "spdlog/common.h" +#include "spdlog/details/periodic_worker.h" #include "spdlog/details/registry.h" #include "spdlog/logger.h" #include "spdlog/version.h" @@ -67,7 +68,11 @@ void flush_on(level::level_enum log_level); // Start/Restart a periodic flusher thread // Warning: Use only if all your loggers are thread safe! -void flush_every(std::chrono::seconds interval); +template +inline void flush_every(std::chrono::duration interval) +{ + details::registry::instance().flush_every(interval); +} // Set global error handler void set_error_handler(void (*handler)(const std::string &msg));