Update the use of clock_gettime(2)
authorThiago Macieira <thiago.macieira@intel.com>
Wed, 8 Apr 2015 20:03:02 +0000 (13:03 -0700)
committerErich Keane <erich.keane@intel.com>
Thu, 9 Apr 2015 19:47:30 +0000 (19:47 +0000)
clock_gettime(2) is a POSIX feature, but CLOCK_REALTIME_COARSE is a
Linux extension. So instead of using clock_gettime only on Linux, try to
use the POSIX macros instead. This way, we'll use clock_gettime on
systems that support it, falling back to gettimeofday if they don't.

POSIX.1-2008 requires clock_gettime(2) functionality, so up the
_POSIX_C_SOURCE macro requirement to the 2008 version. This will request
the 2008 API if it's available on some systems (QNX is known to not
enable all the API it has available). Older versions of POSIX don't
require the timer support.

Change-Id: I27eaacb532114dd188c4ffff13d3229d285d21cb
Signed-off-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/677
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Charlie Lenahan <charlie.lenahan@intel.com>
Reviewed-by: Erich Keane <erich.keane@intel.com>
resource/csdk/connectivity/common/src/logger.c
resource/csdk/logger/src/logger.c

index 6e1098d..0ca40ae 100644 (file)
 // (IEEE Std 1003.1b-1993) specification
 //
 // For this specific file, see use of clock_gettime,
-// Refer http://man7.org/linux/man-pages/man2/clock_gettime.2.html
+// Refer to http://pubs.opengroup.org/stage7tc1/functions/clock_gettime.html
+// and to http://man7.org/linux/man-pages/man2/clock_gettime.2.html
 #ifndef _POSIX_C_SOURCE
-#define _POSIX_C_SOURCE 199309L
+#define _POSIX_C_SOURCE 200809L
 #endif
 
 // Platform check can be extended to check and/or define more, or could be
 // clock_gettime() support.
 #ifdef HAVE_UNISTD_H
 #include <unistd.h>
-#endif
 
-#ifdef  _POSIX_TIMERS
+// if we have unistd.h, we're a Unix system
 #include <time.h>
+#include <sys/time.h>
 #endif
 
 #include "logger.h"
@@ -152,16 +153,30 @@ void OICLog(LogLevel level, const char *tag, const char *logStr)
     }
     else
     {
-        struct timespec when = {};
         int min = 0;
         int sec = 0;
         int ms = 0;
-        if (!clock_gettime(CLOCK_REALTIME_COARSE, &when))
+#ifdef _POSIX_TIMERS
+        struct timespec when = {};
+        clockid_t clk = CLOCK_REALTIME;
+#ifdef CLOCK_REALTIME_COARSE
+        clk = CLOCK_REALTIME_COARSE;
+#endif
+        if (!clock_gettime(clk, &when))
         {
             min = (when.tv_sec / 60) % 60;
             sec = when.tv_sec % 60;
             ms = when.tv_nsec / 1000000;
         }
+#else
+        struct timeval now;
+        if (!gettimeofday(&now, NULL))
+        {
+            min = (now.tv_sec / 60) % 60;
+            sec = now.tv_sec % 60;
+            ms = now.tv_usec * 1000;
+        }
+#endif
         printf("%02d:%02d.%03d %s: %s: %s\n", min, sec, ms, LEVEL[level], tag, logStr);
     }
 #endif
index 020a7b0..8f4e809 100644 (file)
 // (IEEE Std 1003.1b-1993) specification
 //
 // For this specific file, see use of clock_gettime,
-// Refer http://man7.org/linux/man-pages/man2/clock_gettime.2.html
+// Refer to http://pubs.opengroup.org/stage7tc1/functions/clock_gettime.html
+// and to http://man7.org/linux/man-pages/man2/clock_gettime.2.html
 #ifndef _POSIX_C_SOURCE
-#define _POSIX_C_SOURCE 199309L
+#define _POSIX_C_SOURCE 200809L
 #endif
 
 // Platform check can be extended to check and/or define more, or could be
 // clock_gettime() support.
 #ifdef HAVE_UNISTD_H
 #include <unistd.h>
-#endif
 
-#ifdef  _POSIX_TIMERS
+// if we have unistd.h, we're a Unix system
 #include <time.h>
-#endif
-
-#ifdef __APPLE__
 #include <sys/time.h>
 #endif
 
@@ -129,25 +126,23 @@ void OCLogv(LogLevel level, const char * tag, const char * format, ...) {
     OCLog(level, tag, buffer);
 }
 
-#if defined(__linux__)
-static void  osalGetTime(int *min,int *sec, int *ms)
+static void osalGetTime(int *min,int *sec, int *ms)
 {
     if (min && sec && ms)
     {
+#ifdef _POSIX_TIMERS
         struct timespec when = {};
-        if (!clock_gettime(CLOCK_REALTIME_COARSE, &when))
+        clockid_t clk = CLOCK_REALTIME;
+#ifdef CLOCK_REALTIME_COARSE
+        clk = CLOCK_REALTIME_COARSE;
+#endif
+        if (!clock_gettime(clk, &when))
         {
             *min = (when.tv_sec / 60) % 60;
             *sec = when.tv_sec % 60;
             *ms = when.tv_nsec / 1000000;
         }
-    }
-}
-#elif defined(__APPLE__)
-static void  osalGetTime(int *min,int *sec, int *ms)
-{
-    if (min && sec && ms)
-    {
+#else
         struct timeval now;
         if (!gettimeofday(&now, NULL))
         {
@@ -155,9 +150,9 @@ static void  osalGetTime(int *min,int *sec, int *ms)
             *sec = now.tv_sec % 60;
             *ms = now.tv_usec * 1000;
         }
+#endif
     }
 }
-#endif
 
 /**
  * Output a log string with the specified priority level.