From a9380451354e0b2420c112d98f3016ceb5f0c950 Mon Sep 17 00:00:00 2001 From: Joao Moreno Date: Thu, 11 Jan 2018 14:48:28 +0100 Subject: [PATCH 1/2] use Sleep in Windows instead of sleep_for fixes #609 --- include/spdlog/details/async_log_helper.h | 5 ++--- include/spdlog/details/file_helper.h | 2 +- include/spdlog/details/os.h | 9 +++++++++ include/spdlog/sinks/android_sink.h | 3 ++- 4 files changed, 14 insertions(+), 5 deletions(-) diff --git a/include/spdlog/details/async_log_helper.h b/include/spdlog/details/async_log_helper.h index 7c3dcf44..bf1cb4a0 100644 --- a/include/spdlog/details/async_log_helper.h +++ b/include/spdlog/details/async_log_helper.h @@ -358,7 +358,6 @@ inline void spdlog::details::async_log_helper::set_formatter(formatter_ptr msg_f // spin, yield or sleep. use the time passed since last message as a hint inline void spdlog::details::async_log_helper::sleep_or_yield(const spdlog::log_clock::time_point& now, const spdlog::log_clock::time_point& last_op_time) { - using namespace std::this_thread; using std::chrono::milliseconds; using std::chrono::microseconds; @@ -374,10 +373,10 @@ inline void spdlog::details::async_log_helper::sleep_or_yield(const spdlog::log_ // sleep for 20 ms upto 200 ms if (time_since_op <= milliseconds(200)) - return sleep_for(milliseconds(20)); + return spdlog::details::os::sleep_for_millis(20); // sleep for 500 ms - return sleep_for(milliseconds(500)); + return spdlog::details::os::sleep_for_millis(500); } // wait for the queue to be empty diff --git a/include/spdlog/details/file_helper.h b/include/spdlog/details/file_helper.h index 72fc0936..330d08da 100644 --- a/include/spdlog/details/file_helper.h +++ b/include/spdlog/details/file_helper.h @@ -55,7 +55,7 @@ public: if (!os::fopen_s(&_fd, fname, mode)) return; - std::this_thread::sleep_for(std::chrono::milliseconds(open_interval)); + spdlog::details::os::sleep_for_millis(open_interval); } throw spdlog_ex("Failed opening file " + os::filename_to_str(_filename) + " for writing", errno); diff --git a/include/spdlog/details/os.h b/include/spdlog/details/os.h index aaf949ee..a97d6af6 100644 --- a/include/spdlog/details/os.h +++ b/include/spdlog/details/os.h @@ -362,6 +362,15 @@ inline size_t thread_id() } +// from https://github.com/gabime/spdlog/issues/609 +inline void sleep_for_millis(int milliseconds) +{ +#if defined(_WIN32) + Sleep(milliseconds); +#else + std::this_thread::sleep_for(std::chrono::milliseconds(milliseconds)); +#endif +} // wchar support for windows file names (SPDLOG_WCHAR_FILENAMES must be defined) #if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES) diff --git a/include/spdlog/sinks/android_sink.h b/include/spdlog/sinks/android_sink.h index 63f6d874..241c33ba 100644 --- a/include/spdlog/sinks/android_sink.h +++ b/include/spdlog/sinks/android_sink.h @@ -8,6 +8,7 @@ #if defined(__ANDROID__) #include "sink.h" +#include "../details/os.h" #include #include @@ -43,7 +44,7 @@ public: int retry_count = 0; while ((ret == -11/*EAGAIN*/) && (retry_count < SPDLOG_ANDROID_RETRIES)) { - std::this_thread::sleep_for(std::chrono::milliseconds(5)); + spdlog::details::os::sleep_for_millis(5); ret = __android_log_write(priority, _tag.c_str(), msg_output); retry_count++; } From 357a63d9149141a292c4bf3f0972f4540ad5c354 Mon Sep 17 00:00:00 2001 From: Joao Moreno Date: Thu, 11 Jan 2018 21:58:02 +0100 Subject: [PATCH 2/2] fix spdlog namespace --- include/spdlog/details/async_log_helper.h | 4 ++-- include/spdlog/details/file_helper.h | 4 ++-- include/spdlog/sinks/android_sink.h | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/include/spdlog/details/async_log_helper.h b/include/spdlog/details/async_log_helper.h index bf1cb4a0..bba9bc0e 100644 --- a/include/spdlog/details/async_log_helper.h +++ b/include/spdlog/details/async_log_helper.h @@ -373,10 +373,10 @@ inline void spdlog::details::async_log_helper::sleep_or_yield(const spdlog::log_ // sleep for 20 ms upto 200 ms if (time_since_op <= milliseconds(200)) - return spdlog::details::os::sleep_for_millis(20); + return details::os::sleep_for_millis(20); // sleep for 500 ms - return spdlog::details::os::sleep_for_millis(500); + return details::os::sleep_for_millis(500); } // wait for the queue to be empty diff --git a/include/spdlog/details/file_helper.h b/include/spdlog/details/file_helper.h index 330d08da..dff788ff 100644 --- a/include/spdlog/details/file_helper.h +++ b/include/spdlog/details/file_helper.h @@ -55,7 +55,7 @@ public: if (!os::fopen_s(&_fd, fname, mode)) return; - spdlog::details::os::sleep_for_millis(open_interval); + details::os::sleep_for_millis(open_interval); } throw spdlog_ex("Failed opening file " + os::filename_to_str(_filename) + " for writing", errno); @@ -129,7 +129,7 @@ public: if (ext_index == filename_t::npos || ext_index == 0 || ext_index == fname.size() - 1) return std::make_tuple(fname, spdlog::filename_t()); - // treat casese like "/etc/rc.d/somelogfile or "/abc/.hiddenfile" + // treat casese like "/etc/rc.d/somelogfile or "/abc/.hiddenfile" auto folder_index = fname.rfind(details::os::folder_sep); if (folder_index != fname.npos && folder_index >= ext_index - 1) return std::make_tuple(fname, spdlog::filename_t()); diff --git a/include/spdlog/sinks/android_sink.h b/include/spdlog/sinks/android_sink.h index 241c33ba..e3766755 100644 --- a/include/spdlog/sinks/android_sink.h +++ b/include/spdlog/sinks/android_sink.h @@ -44,7 +44,7 @@ public: int retry_count = 0; while ((ret == -11/*EAGAIN*/) && (retry_count < SPDLOG_ANDROID_RETRIES)) { - spdlog::details::os::sleep_for_millis(5); + details::os::sleep_for_millis(5); ret = __android_log_write(priority, _tag.c_str(), msg_output); retry_count++; }