sbgPlatform.c
Go to the documentation of this file.
1 #include <sbgCommon.h>
2 #include <stdarg.h>
3 #include <time.h>
4 
5 //----------------------------------------------------------------------//
6 //- Include specific header for WIN32 and UNIX platforms -//
7 //----------------------------------------------------------------------//
8 #ifdef WIN32
9  #include <windows.h>
10 #elif defined(__APPLE__)
11  #include <mach/mach_time.h>
12 #else
13  #include <unistd.h>
14 #endif
15 
16 //----------------------------------------------------------------------//
17 //- Specific timing methods to reimplement for your platform -//
18 //----------------------------------------------------------------------//
19 
25 {
26 #ifdef WIN32
27  //
28  // Return the current time in ms
29  //
30  return clock() / (CLOCKS_PER_SEC / 1000);
31 #elif defined(__APPLE__)
32  mach_timebase_info_data_t timeInfo;
33  mach_timebase_info(&timeInfo);
34 
35  //
36  // Return the current time in ms
37  //
38  return (mach_absolute_time() * timeInfo.numer / timeInfo.denom) / 1000000.0;
39 #else
40  struct timespec now;
41  clock_gettime(CLOCK_REALTIME, &now);
42 
43  //
44  // Return the current time in ms
45  //
46  return now.tv_sec * 1000 + now.tv_nsec / 1000000;
47 #endif
48 }
49 
54 SBG_COMMON_LIB_API void sbgSleep(uint32_t ms)
55 {
56  //
57  // Implementation valid for both WIN and UNIX systems
58  //
59  #ifdef WIN32
60  Sleep(ms);
61  #else
62  usleep(ms*1000);
63  #endif
64 }
65 
66 //----------------------------------------------------------------------//
67 //- Specific logging methods to reimplement for your platform -//
68 //----------------------------------------------------------------------//
69 
80 SBG_COMMON_LIB_API void sbgPlatformDebugLogMsg(const char *pFileName, const char *pFunctionName, uint32_t line, const char *pCategory, SbgDebugLogType logType, SbgErrorCode errorCode, const char *pFormat, ...)
81 {
82  char errorMsg[SBG_CONFIG_LOG_MAX_SIZE];
83  va_list args;
84 
85  //
86  // Initialize the list of variable arguments on the latest function argument
87  //
88  va_start(args, pFormat);
89 
90  //
91  // Generate the error message string
92  //
93  vsprintf(errorMsg, pFormat, args);
94 
95  //
96  // Close the list of variable arguments
97  //
98  va_end(args);
99 
100  //
101  // Log the correct message according to the log type
102  //
103  switch (logType)
104  {
106  fprintf(stderr, "*ERR * [%s]%s: %s\n\r", sbgErrorCodeToString(errorCode), pFunctionName, errorMsg);
107  break;
109  fprintf(stderr, "*WARN* [%s]%s: %s\n\r", sbgErrorCodeToString(errorCode), pFunctionName, errorMsg);
110  break;
112  fprintf(stderr, "*INFO* %s\n\r", errorMsg);
113  break;
115  fprintf(stderr, "*DBG * %s\n\r", errorMsg);
116  break;
117  default:
118  fprintf(stderr, "*UKNW*\t[%s]%s(%u): %s\n\r", sbgErrorCodeToString(errorCode), pFunctionName, line, errorMsg);
119  }
120 }
#define SBG_COMMON_LIB_API
Definition: sbgDefines.h:58
#define SBG_CONFIG_LOG_MAX_SIZE
Definition: sbgCommon.h:101
SBG_COMMON_LIB_API uint32_t sbgGetTime(void)
Definition: sbgPlatform.c:24
static const char * sbgErrorCodeToString(SbgErrorCode errorCode)
Definition: sbgErrorCodes.h:72
SBG_COMMON_LIB_API void sbgPlatformDebugLogMsg(const char *pFileName, const char *pFunctionName, uint32_t line, const char *pCategory, SbgDebugLogType logType, SbgErrorCode errorCode, const char *pFormat,...)
Definition: sbgPlatform.c:80
SBG_COMMON_LIB_API void sbgSleep(uint32_t ms)
Definition: sbgPlatform.c:54
Main header file for SBG Systems common C library.
enum _SbgDebugLogType SbgDebugLogType
enum _SbgErrorCode SbgErrorCode


sbg_driver
Author(s): SBG Systems
autogenerated on Thu Oct 22 2020 03:47:22