log.c 1.3 KB

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