luat_log_air101.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #include "luat_base.h"
  2. #include "luat_log.h"
  3. #include "luat_uart.h"
  4. #include "printf.h"
  5. #include "luat_malloc.h"
  6. #include "luat_mcu.h"
  7. #ifdef LUAT_USE_DBG
  8. #include "luat_cmux.h"
  9. extern luat_cmux_t cmux_ctx;
  10. #endif
  11. #ifdef LUAT_CONF_LOG_UART1
  12. #else
  13. static uint8_t luat_log_uart_port = 0;
  14. static uint8_t luat_log_level_cur = LUAT_LOG_DEBUG;
  15. #define LOGLOG_SIZE 1024
  16. // static char log_printf_buff[LOGLOG_SIZE] = {0};
  17. void luat_log_set_uart_port(int port) {
  18. luat_log_uart_port = port;
  19. }
  20. uint8_t luat_log_get_uart_port(void) {
  21. return luat_log_uart_port;
  22. }
  23. void luat_nprint(char *s, size_t l) {
  24. #ifdef LUAT_USE_SHELL
  25. if (cmux_ctx.state == 1 && cmux_ctx.log_state ==1){
  26. luat_cmux_write(LUAT_CMUX_CH_LOG, CMUX_FRAME_UIH & ~ CMUX_CONTROL_PF,s, l);
  27. }else
  28. #endif
  29. // luat_uart_write(luat_log_uart_port, s, l);
  30. printf("%.*s", l, s);
  31. }
  32. void luat_log_write(char *s, size_t l) {
  33. #ifdef LUAT_USE_SHELL
  34. if (cmux_ctx.state == 1 && cmux_ctx.log_state ==1){
  35. luat_cmux_write(LUAT_CMUX_CH_LOG, CMUX_FRAME_UIH & ~ CMUX_CONTROL_PF,s, l);
  36. }else
  37. #endif
  38. // luat_uart_write(luat_log_uart_port, s, l);
  39. printf("%.*s", l, s);
  40. }
  41. void luat_log_set_level(int level) {
  42. luat_log_level_cur = level;
  43. }
  44. int luat_log_get_level() {
  45. return luat_log_level_cur;
  46. }
  47. static const char lstr[] = {'D', 'I', 'W', 'E'};
  48. void luat_log_log(int level, const char* tag, const char* _fmt, ...) {
  49. if (luat_log_level_cur > level) return;
  50. // char log_printf_buff[LOGLOG_SIZE] = {0};
  51. char header[128] = {0};
  52. uint64_t time_ms = luat_mcu_tick64_ms();
  53. char *tmp = (char *)luat_heap_malloc(LOGLOG_SIZE);
  54. if (tmp == NULL) {
  55. return;
  56. }
  57. if (level > LUAT_LOG_ERROR) {
  58. level = LUAT_LOG_ERROR;
  59. }
  60. else if (level < LUAT_LOG_DEBUG) {
  61. level = LUAT_LOG_DEBUG;
  62. }
  63. snprintf(header, sizeof(header), "[%08llu.%03llu]%c/%s ", time_ms / 1000, time_ms % 1000, lstr[level - 1], tag);
  64. luat_log_write(header, strlen(header));
  65. va_list args;
  66. va_start(args, _fmt);
  67. int len = vsnprintf_(tmp, LOGLOG_SIZE - 2, _fmt, args);
  68. va_end(args);
  69. if (len > 0) {
  70. tmp[len] = '\n';
  71. luat_log_write(tmp, len+1);
  72. }
  73. luat_heap_free(tmp);
  74. }
  75. void luat_log_printf(int level, const char* _fmt, ...) {
  76. va_list args;
  77. if (luat_log_level_cur > level) return;
  78. char *tmp = (char *)luat_heap_malloc(LOGLOG_SIZE);
  79. if (tmp == NULL) {
  80. return;
  81. }
  82. va_start(args, _fmt);
  83. int len = vsnprintf_(tmp, LOGLOG_SIZE - 2, _fmt, args);
  84. va_end(args);
  85. if (len > 0) {
  86. tmp[len] = '\n';
  87. luat_log_write(tmp, len);
  88. }
  89. luat_heap_free(tmp);
  90. }
  91. #endif