log.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. * log.c
  3. *
  4. * Created on: Nov 7, 2022
  5. * Author: tangm
  6. */
  7. #include "log.h"
  8. #include "usart.h"
  9. #include <stdarg.h>
  10. #include <stdio.h>
  11. #include "ucos_ii.h"
  12. #include "main.h"
  13. #include "udp_send.h"
  14. #include "stdlib.h"
  15. #include "malloc.h"
  16. static OS_EVENT *logMutex;
  17. uint8_t perr;
  18. void LogPrint(logLevel_t logLevel,const char *file, const char *func, const int line, char * fmt, ...)
  19. {
  20. OSMutexPend(logMutex, 5000, &perr);
  21. if(perr == OS_ERR_NONE)
  22. {
  23. udp_log_start();
  24. va_list args;
  25. va_start(args, fmt);
  26. char buf[LOG_LEN_MAX];
  27. vsnprintf(buf, sizeof(buf), fmt, args);
  28. va_end(args);
  29. char *p=mymalloc(SRAMEX, 1024);
  30. switch (logLevel)
  31. {
  32. #ifdef _LOG_INFO
  33. case LOG_INFO:
  34. sprintf(p,"[%s][%s][%s:%4d] %s\r\n", "INFO", file, func, line, buf);
  35. break;
  36. #endif
  37. #ifdef _LOG_DEBUG
  38. case LOG_DEBUG:
  39. sprintf(p,"[%s][%s][%s:%4d] %s\r\n", "DEBUG",file, func, line, buf);
  40. break;
  41. #endif
  42. #ifdef _LOG_WARN
  43. case LOG_WARN:
  44. sprintf(p,"[%s][%s][%s:%4d] %s\r\n", "WARN", file, func, line, buf);
  45. break;
  46. #endif
  47. case LOG_ERROR:
  48. sprintf(p,"[%s][%s][%s:%4d] %s\r\n", "ERROR",file, func, line, buf);
  49. break;
  50. default:
  51. break;
  52. }
  53. udp_send_printf(p);
  54. myfree(SRAMEX, p);
  55. udp_log_close();
  56. perr = OSMutexPost(logMutex);
  57. }
  58. }
  59. void log_init()
  60. {
  61. logMutex = OSMutexCreate (0, &perr);//´´½¨Ò»¸ölog»¥³âÁ¿
  62. OSTimeDly(100);
  63. }