Common.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Common.c
  3. *
  4. * Created on: 2021年8月7日
  5. * Author: Wendell
  6. */
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <stdarg.h>
  11. #include <time.h>
  12. #include <sys/timeb.h>
  13. int StoreSysLogMsg(const char *fmt, ...)
  14. {
  15. char Buf[4096+256];
  16. char buffer[4096];
  17. va_list args;
  18. struct timeb SeqEndTime;
  19. struct tm *tm;
  20. va_start(args, fmt);
  21. int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  22. va_end(args);
  23. memset(Buf,0,sizeof(Buf));
  24. ftime(&SeqEndTime);
  25. SeqEndTime.time = time(NULL);
  26. tm=localtime(&SeqEndTime.time);
  27. sprintf(Buf,"echo \'%04d-%02d-%02d %02d:%02d:%02d:%03d - %s\' >> /Storage/SystemLog/[%04d.%02d]SystemLog",
  28. tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,SeqEndTime.millitm,
  29. buffer,
  30. tm->tm_year+1900,tm->tm_mon+1);
  31. system(Buf);
  32. return rc;
  33. }
  34. int StorePsuLogMsg(const char *fmt, ...)
  35. {
  36. char Buf[4096+256];
  37. char buffer[4096];
  38. va_list args;
  39. struct timeb SeqEndTime;
  40. struct tm *tm;
  41. va_start(args, fmt);
  42. int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  43. va_end(args);
  44. memset(Buf,0,sizeof(Buf));
  45. ftime(&SeqEndTime);
  46. SeqEndTime.time = time(NULL);
  47. tm=localtime(&SeqEndTime.time);
  48. sprintf(Buf,"echo \"%04d-%02d-%02d %02d:%02d:%02d:%03d - %s\" >> /Storage/SystemLog/[%04d.%02d]PsuLog",
  49. tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,SeqEndTime.millitm,
  50. buffer,
  51. tm->tm_year+1900,tm->tm_mon+1);
  52. system(Buf);
  53. return rc;
  54. }
  55. void GetClockTime(struct timespec *_now_time)
  56. {
  57. clock_gettime(CLOCK_MONOTONIC, _now_time);
  58. }
  59. // return value unit: 1us
  60. unsigned long GetTimeoutValue(struct timespec _start_time)
  61. {
  62. struct timespec ts_end;
  63. unsigned long ret = 0;
  64. clock_gettime(CLOCK_MONOTONIC, &ts_end);
  65. ret = ((unsigned long)(ts_end.tv_sec - _start_time.tv_sec) * 1000000) + ((unsigned long)((ts_end.tv_nsec / 1000) - (_start_time.tv_nsec/ 1000)));
  66. /*
  67. printf("\r\n TimeInterval: %ld.%09ld - %ld.%09ld = %ld.%06ld ns",
  68. ts_end.tv_sec, ts_end.tv_nsec, _start_time.tv_sec, _start_time.tv_nsec,
  69. (ret / 1000000), (ret % 1000000));
  70. */
  71. #if 0
  72. struct timespec ts_interval;
  73. ts_interval.tv_sec = ts_end.tv_sec - _start_time.tv_sec;
  74. ts_interval.tv_nsec = ts_end.tv_nsec - _start_time.tv_nsec;
  75. if(ts_interval.tv_nsec < 0)
  76. {
  77. ts_interval.tv_nsec += 1000000000;
  78. ts_interval.tv_sec--;
  79. }
  80. #endif
  81. return ret;
  82. }
  83. // return value unit: 1s
  84. unsigned long GetSecTimeoutValue(struct timespec _start_time)
  85. {
  86. struct timespec ts_end;
  87. unsigned long ret = 0;
  88. clock_gettime(CLOCK_MONOTONIC, &ts_end);
  89. ret = ((unsigned long)(ts_end.tv_sec - _start_time.tv_sec) * 1000) + ((unsigned long)((ts_end.tv_nsec / 1000000) - (_start_time.tv_nsec / 1000000)));
  90. /*
  91. printf("\r\n TimeInterval: %ld.%09ld - %ld.%09ld = %ld.%03ld ms",
  92. ts_end.tv_sec, ts_end.tv_nsec, _start_time.tv_sec, _start_time.tv_nsec,
  93. (ret / 1000), (ret % 1000));
  94. */
  95. ret /= 1000;
  96. return ret;
  97. }