log.c 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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 "config.h"
  10. #include <stdarg.h>
  11. #include <stdio.h>
  12. int fputc(int ch, FILE *f)
  13. {
  14. usart_data_transmit(COM_232, (uint8_t)ch);
  15. while (RESET == usart_flag_get(COM_232, USART_FLAG_TBE));
  16. return ch;
  17. }
  18. void LogPrint(logLevel_t logLevel, const char *func, const int line, char * fmt, ...)
  19. {
  20. va_list args;
  21. va_start(args, fmt);
  22. char buf[LOG_LEN_MAX];
  23. vsnprintf(buf, sizeof(buf), fmt, args);
  24. va_end(args);
  25. switch (logLevel)
  26. {
  27. #ifdef _LOG_INFO
  28. case LOG_INFO:
  29. printf("LOG[%-5s][%-20s:%4d] %s\r\n", "INFO", func, line, buf);
  30. break;
  31. #endif
  32. #ifdef _LOG_DEBUG
  33. case LOG_DEBUG:
  34. printf("LOG[%-5s][%-20s:%4d] %s\r\n", "DEBUG", func, line, buf);
  35. break;
  36. #endif
  37. #ifdef _LOG_WARN
  38. case LOG_WARN:
  39. printf("LOG[%-5s][%-20s:%4d] %s\r\n", "WARN", func, line, buf);
  40. break;
  41. #endif
  42. case LOG_ERROR:
  43. printf("LOG[%-5s][%-20s:%4d] %s\r\n", "ERROR", func, line, buf);
  44. break;
  45. default:
  46. break;
  47. }
  48. }