From 01f20286ed24d005f529a7bbd94f2e8e4ebcbc9f Mon Sep 17 00:00:00 2001 From: Rich Alloway Date: Mon, 23 Apr 2018 11:11:18 -0400 Subject: [PATCH] Check in code to handle missing clock_gettime() on most versions of Mac OS X --- include/ZmqLogger.h | 6 ++++++ src/ZmqLogger.cpp | 10 ++++++++++ 2 files changed, 16 insertions(+) diff --git a/include/ZmqLogger.h b/include/ZmqLogger.h index c134f2cf..100718b2 100644 --- a/include/ZmqLogger.h +++ b/include/ZmqLogger.h @@ -41,6 +41,12 @@ #include #include +// OS X does not have clock_gettime, use clock_get_time (https://gist.github.com/jbenet/1087739) +#ifdef __MACH__ +#include +#include +#endif + using namespace std; diff --git a/src/ZmqLogger.cpp b/src/ZmqLogger.cpp index 96bccbbf..0c466246 100644 --- a/src/ZmqLogger.cpp +++ b/src/ZmqLogger.cpp @@ -179,7 +179,17 @@ void ZmqLogger::AppendDebugMethod(string method_name, string arg1_name, float ar // Prepend current time (hires) to message struct timespec ts; +#ifdef __MACH__ // OS X does not have clock_gettime, use clock_get_time (https://gist.github.com/jbenet/1087739) + clock_serv_t cclock; + mach_timespec_t mts; + host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock); + clock_get_time(cclock, &mts); + mach_port_deallocate(mach_task_self(), cclock); + ts->tv_sec = mts.tv_sec; + ts->tv_nsec = mts.tv_nsec; +#else clock_gettime(CLOCK_REALTIME, &ts); +#endif char timebuf[80]; strftime(timebuf, sizeof timebuf, "%F %T", localtime(&ts.tv_sec)); message << timebuf << "." << std::setiosflags(std::ios::right) << std::setw(9) << std::setfill('0') << ts.tv_nsec << " ";