فهرست منبع

add: bsp/win32改为异步日志

Wendal Chen 4 سال پیش
والد
کامیت
8929cd26df
3فایلهای تغییر یافته به همراه44 افزوده شده و 6 حذف شده
  1. 36 6
      bsp/win32/port/luat_log_win32.c
  2. 4 0
      bsp/win32/src/main_posix.c
  3. 4 0
      bsp/win32/src/main_win32.c

+ 36 - 6
bsp/win32/port/luat_log_win32.c

@@ -2,13 +2,39 @@
 #include "luat_base.h"
 #include "luat_log.h"
 #include "luat_uart.h"
+#include "luat_malloc.h"
 #include "printf.h"
 
 #include <stdio.h>
 
+#include "FreeRTOS.h"
+#include "queue.h"
+#include "task.h"
+
+typedef struct log_msg {
+    char* buff;
+}log_msg_t;
+
+static QueueHandle_t xQueue = NULL;
+
 static uint8_t luat_log_uart_port = 0;
 static uint8_t luat_log_level_cur = LUAT_LOG_DEBUG;
 
+void luat_log_thread_task(void* args) {
+    log_msg_t msg = {0};
+    while (1) {
+        if (pdTRUE == xQueueReceive(xQueue, &msg, -1)) {
+            printf("%s", msg.buff);
+            luat_heap_free(msg.buff);  
+        };
+    }
+}
+
+void luat_log_init_win32(void) {
+    xQueue = xQueueCreate(1024, sizeof(void*));
+    xTaskCreate( luat_log_thread_task, "log", 1024*2, NULL, 23, NULL );
+}
+
 void luat_log_set_uart_port(int port) {
     luat_log_uart_port = port;
 }
@@ -19,11 +45,15 @@ void luat_print(const char* _str) {
 
 void luat_nprint(char *s, size_t l) {
     //luat_uart_write(luat_log_uart_port, s, l);
-    for (size_t i = 0; i < l; i++)
-    {
-        putc(*(s++), stdout);
-    }
-    
+    char* buff = luat_heap_malloc(l + 1);
+    if (buff == NULL)
+        return;
+    memcpy(buff, s, l);
+    buff[l] = 0x00;
+    log_msg_t msg = {
+        .buff = buff
+    };
+    xQueueSendFromISR(xQueue, &msg, NULL);
 }
 
 void luat_log_set_level(int level) {
@@ -35,7 +65,7 @@ int luat_log_get_level() {
 #define LOGLOG_SIZE 1024
 void luat_log_log(int level, const char* tag, const char* _fmt, ...) {
     if (luat_log_level_cur > level) return;
-    char buff[LOGLOG_SIZE];
+    char buff[LOGLOG_SIZE] = {0};
     char *tmp = (char *)buff;
     switch (level)
         {

+ 4 - 0
bsp/win32/src/main_posix.c

@@ -13,6 +13,8 @@
 #define LUAT_HEAP_SIZE (1024*1024)
 uint8_t luavm_heap[LUAT_HEAP_SIZE] = {0};
 
+void luat_log_init_win32(void);
+
 static void _luat_main(void* args) {
     luat_main();
 }
@@ -52,6 +54,8 @@ int main(int argc, char** argv) {
     
     bpool(luavm_heap, LUAT_HEAP_SIZE);
 
+    luat_log_init_win32();
+
 #ifdef LUAT_USE_LVGL
     lv_init();
     xTaskCreate( _lvgl_handler, "lvgl", 1024*2, NULL, 23, NULL );

+ 4 - 0
bsp/win32/src/main_win32.c

@@ -15,6 +15,8 @@
 #define LUAT_HEAP_SIZE (4096*1024)
 uint8_t luavm_heap[LUAT_HEAP_SIZE] = {0};
 
+void luat_log_init_win32(void);
+
 static void _luat_main(void* args) {
     luat_main();
 }
@@ -68,6 +70,8 @@ int main(int argc, char** argv) {
         }
     }
 
+    luat_log_init_win32();
+
     SetConsoleCtrlHandler(consoleHandler, TRUE);
     bpool(luavm_heap, LUAT_HEAP_SIZE);
 #ifdef LUAT_USE_LVGL