SystemLogMessage.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. * Module_CCS.c
  3. *
  4. * Created on: 2020/03/16
  5. * Author: foluswen
  6. */
  7. #include <sys/time.h>
  8. #include <sys/timeb.h>
  9. #include <sys/types.h>
  10. #include <sys/stat.h>
  11. #include <sys/types.h>
  12. #include <sys/ioctl.h>
  13. #include <sys/socket.h>
  14. #include <sys/ipc.h>
  15. #include <sys/shm.h>
  16. #include <sys/shm.h>
  17. #include <sys/mman.h>
  18. #include <linux/wireless.h>
  19. #include <arpa/inet.h>
  20. #include <netinet/in.h>
  21. #include <unistd.h>
  22. #include <stdarg.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <unistd.h>
  26. #include <fcntl.h>
  27. #include <termios.h>
  28. #include <errno.h>
  29. #include <errno.h>
  30. #include <string.h>
  31. #include <time.h>
  32. #include <ctype.h>
  33. #include <ifaddrs.h>
  34. #include "SystemLogMessage.h"
  35. int StoreLogMsg(const char *fmt, ...)
  36. {
  37. char Buf[4096+256];
  38. char buffer[4096];
  39. time_t CurrentTime;
  40. struct tm *tm;
  41. struct timeval tv;
  42. va_list args;
  43. va_start(args, fmt);
  44. int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  45. va_end(args);
  46. memset(Buf,0,sizeof(Buf));
  47. CurrentTime = time(NULL);
  48. tm=localtime(&CurrentTime);
  49. gettimeofday(&tv, NULL); // get microseconds, 10^-6
  50. sprintf(Buf,"echo -n \"[%04d.%02d.%02d %02d:%02d:%02d.%03ld]%s\" >> /Storage/SystemLog/[%04d.%02d]CCS_SystemLog",
  51. tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,(tv.tv_usec/1000),
  52. buffer,
  53. tm->tm_year+1900,tm->tm_mon+1);
  54. #ifdef SystemLogMessage
  55. system((const char*)Buf);
  56. #endif
  57. #ifdef ConsloePrintLog
  58. printf("[%04d.%02d.%02d %02d:%02d:%02d.%03ld]%s", tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,(tv.tv_usec/1000), buffer);
  59. #endif
  60. return rc;
  61. }