luat_log_air101.c 2.6 KB

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