log.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #include "log.h"
  2. #include "usart.h"
  3. #include <stdarg.h>
  4. #include <stdio.h>
  5. #include "Freertos.h"
  6. #include "semphr.h"
  7. #include "main.h"
  8. #include "udp.h"
  9. static SemaphoreHandle_t logMutex;
  10. void LogPrint(logLevel_t logLevel,const char *file, const char *func, const int line, char * fmt, ...)
  11. {
  12. if (xSemaphoreTake(logMutex, 1000))
  13. {
  14. udp_log_start();
  15. va_list args;
  16. va_start(args, fmt);
  17. char buf[LOG_LEN_MAX];
  18. vsnprintf(buf, sizeof(buf), fmt, args);
  19. va_end(args);
  20. // char *p=pvPortMalloc(1024);
  21. char p[1024];
  22. switch (logLevel)
  23. {
  24. #ifdef _LOG_INFO
  25. case LOG_INFO:
  26. sprintf(p,"[%s][%s][%s:%4d] %s\r\n", "INFO",file,func, line, buf);
  27. break;
  28. #endif
  29. #ifdef _LOG_DEBUG
  30. case LOG_DEBUG:
  31. sprintf(p,"[%s][%s][%s:%4d] %s\r\n", "DEBUG",file, func, line, buf);
  32. break;
  33. #endif
  34. #ifdef _LOG_WARN
  35. case LOG_WARN:
  36. sprintf(p,"[%s][%s][%s:%4d] %s\r\n", "WARN", file,func, line, buf);
  37. break;
  38. #endif
  39. case LOG_ERROR:
  40. sprintf(p,"[%s][%s][%s:%4d] %s\r\n", "ERROR",file, func, line, buf);
  41. break;
  42. default:
  43. break;
  44. }
  45. udp_send_printf(p);
  46. // vPortFree(p);
  47. udp_log_close();
  48. xSemaphoreGive(logMutex);
  49. }
  50. }
  51. void log_init()
  52. {
  53. logMutex = xSemaphoreCreateMutex();//´´½¨Ò»¸ölog»¥³âÁ¿
  54. }