Skip to content

Commit

Permalink
use Sleep in Windows instead of sleep_for
Browse files Browse the repository at this point in the history
  • Loading branch information
joaomoreno committed Jan 11, 2018
1 parent 8cce45e commit 2002e28
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 5 deletions.
5 changes: 2 additions & 3 deletions include/spdlog/details/async_log_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,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;

Expand All @@ -376,10 +375,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
Expand Down
2 changes: 1 addition & 1 deletion include/spdlog/details/file_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class file_helper
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);
Expand Down
9 changes: 9 additions & 0 deletions include/spdlog/details/os.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion include/spdlog/sinks/android_sink.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#if defined(__ANDROID__)

#include "sink.h"
#include "../details/os.h"

#include <mutex>
#include <string>
Expand Down Expand Up @@ -43,7 +44,7 @@ class android_sink : public sink
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++;
}
Expand Down

0 comments on commit 2002e28

Please sign in to comment.