Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/gabime/spdlog.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLucas CHOLLET <lucas.chollet@free.fr>2022-07-17 19:36:11 +0300
committerLucas CHOLLET <lucas.chollet@free.fr>2022-07-17 21:28:39 +0300
commitdfe1009080f39630a4931c46dc1e7f67bdb21ed9 (patch)
tree76b887d74bf3c47abcf9cd9b6c2c2eee81d932d0 /include
parent6c95f4c8168401badff89d0c16e3887ea91ea9ad (diff)
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));
Diffstat (limited to 'include')
-rw-r--r--include/spdlog/details/periodic_worker-inl.h21
-rw-r--r--include/spdlog/details/periodic_worker.h21
-rw-r--r--include/spdlog/details/registry-inl.h7
-rw-r--r--include/spdlog/details/registry.h10
-rw-r--r--include/spdlog/spdlog-inl.h5
-rw-r--r--include/spdlog/spdlog.h6
6 files changed, 33 insertions, 37 deletions
diff --git a/include/spdlog/details/periodic_worker-inl.h b/include/spdlog/details/periodic_worker-inl.h
index d4abbda3..520a2b33 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<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()
{
diff --git a/include/spdlog/details/periodic_worker.h b/include/spdlog/details/periodic_worker.h
index a300218d..919ba7ed 100644
--- a/include/spdlog/details/periodic_worker.h
+++ b/include/spdlog/details/periodic_worker.h
@@ -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
diff --git a/include/spdlog/details/registry-inl.h b/include/spdlog/details/registry-inl.h
index c55b5eea..e6ecc9b0 100644
--- a/include/spdlog/details/registry-inl.h
+++ b/include/spdlog/details/registry-inl.h
@@ -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_);
diff --git a/include/spdlog/details/registry.h b/include/spdlog/details/registry.h
index 97473ea3..6a4a5abc 100644
--- a/include/spdlog/details/registry.h
+++ b/include/spdlog/details/registry.h
@@ -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);
diff --git a/include/spdlog/spdlog-inl.h b/include/spdlog/spdlog-inl.h
index 2b875fae..708399c1 100644
--- a/include/spdlog/spdlog-inl.h
+++ b/include/spdlog/spdlog-inl.h
@@ -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);
diff --git a/include/spdlog/spdlog.h b/include/spdlog/spdlog.h
index 65d3e9d5..8facbdcd 100644
--- a/include/spdlog/spdlog.h
+++ b/include/spdlog/spdlog.h
@@ -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));