log.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdint.h>
  4. #include <string.h>
  5. #include <unistd.h>
  6. #include <time.h>
  7. #include <stdarg.h>
  8. #include <sys/timeb.h>
  9. #include "../ShareMemory/shmMem.h"
  10. #include "../Define/define.h"
  11. #include "log.h"
  12. //------------------------------------------------------------------------------
  13. #define YES 1
  14. #define NO 0
  15. //------------------------------------------------------------------------------
  16. int StoreLogMsg(const char *fmt, ...)
  17. {
  18. char Buf[4096 + 256] = {0};
  19. char buffer[4096] = {0};
  20. int rc = -1;
  21. va_list args;
  22. struct timeb SeqEndTime;
  23. struct tm *tm;
  24. struct SysConfigData *pSysConfig = (struct SysConfigData *)GetShmSysConfigData();
  25. va_start(args, fmt);
  26. rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  27. va_end(args);
  28. ftime(&SeqEndTime);
  29. SeqEndTime.time = time(NULL);
  30. tm = localtime(&SeqEndTime.time);
  31. if (pSysConfig->SwitchDebugFlag == YES) {
  32. sprintf(Buf, "%02d:%02d:%02d:%03d - %s",
  33. tm->tm_hour,
  34. tm->tm_min,
  35. tm->tm_sec,
  36. SeqEndTime.millitm,
  37. buffer);
  38. printf("%s\r\n", Buf);
  39. } else {
  40. sprintf(Buf, "echo \"%04d-%02d-%02d %02d:%02d:%02d:%03d - %s\" >> /Storage/SystemLog/[%04d.%02d]SystemLog",
  41. tm->tm_year + 1900,
  42. tm->tm_mon + 1,
  43. tm->tm_mday,
  44. tm->tm_hour,
  45. tm->tm_min,
  46. tm->tm_sec,
  47. SeqEndTime.millitm,
  48. buffer,
  49. tm->tm_year + 1900,
  50. tm->tm_mon + 1);
  51. system(Buf);
  52. }
  53. return rc;
  54. }