log.c 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. switch (logLevel)
  22. {
  23. #ifdef _LOG_INFO
  24. case LOG_INFO:
  25. sprintf(p,"[%s][%s][%s:%4d] %s\r\n", "INFO",file,func, line, buf);
  26. break;
  27. #endif
  28. #ifdef _LOG_DEBUG
  29. case LOG_DEBUG:
  30. sprintf(p,"[%s][%s][%s:%4d] %s\r\n", "DEBUG",file, func, line, buf);
  31. break;
  32. #endif
  33. #ifdef _LOG_WARN
  34. case LOG_WARN:
  35. sprintf(p,"[%s][%s][%s:%4d] %s\r\n", "WARN", file,func, line, buf);
  36. break;
  37. #endif
  38. case LOG_ERROR:
  39. sprintf(p,"[%s][%s][%s:%4d] %s\r\n", "ERROR",file, func, line, buf);
  40. break;
  41. default:
  42. break;
  43. }
  44. udp_send_printf(p);
  45. vPortFree(p);
  46. udp_log_close();
  47. xSemaphoreGive(logMutex);
  48. }
  49. }
  50. void log_init()
  51. {
  52. logMutex = xSemaphoreCreateMutex();//´´½¨Ò»¸ölog»¥³âÁ¿
  53. }