소스 검색

update: 尝试log库无内存消耗的方式进行打印

Wendal Chen 3 년 전
부모
커밋
ed27195553
1개의 변경된 파일92개의 추가작업 그리고 19개의 파일을 삭제
  1. 92 19
      luat/modules/luat_lib_log.c

+ 92 - 19
luat/modules/luat_lib_log.c

@@ -14,6 +14,9 @@
 #include "luat_rtos.h"
 #define LUAT_LOG_TAG "log"
 #include "luat_log.h"
+
+#include "printf.h"
+
 typedef struct luat_log_conf
 {
     uint8_t style;
@@ -24,11 +27,16 @@ typedef struct luat_log_conf
 #define LOG_STYLE_DEBUG_INFO    1
 #define LOG_STYLE_FULL          2
 
+#define LOG_BUFF_LEN (512)
+
+const char* STR_TURE = "true";
+const char* STR_FALSE = "false";
+
 static luat_log_conf_t lconf = {
     .style=0
 };
 
-static int add_debug_info(lua_State *L, uint8_t pos, const char* LEVEL) {
+static int add_debug_info(lua_State *L, uint8_t pos, const char* LEVEL, char* buff) {
     lua_Debug ar;
     // int arg;
     // int d = 0;
@@ -51,11 +59,9 @@ static int add_debug_info(lua_State *L, uint8_t pos, const char* LEVEL) {
     int line = ar.currentline > 64*1024 ? 0 : ar.currentline;
     // 推入文件名和行号, 注意: 源码路径的第一个字符是标识,需要跳过
     if (LEVEL)
-        lua_pushfstring(L, "%s/%s:%d", LEVEL, ar.source + 1, line);
+        snprintf_(buff, LOG_BUFF_LEN, "%s/%s:%d", LEVEL, ar.source + 1, line);
     else
-        lua_pushfstring(L, "%s:%d", ar.source + 1, line);
-    if (lua_gettop(L) > pos)
-        lua_insert(L, pos);
+        snprintf_(buff, LOG_BUFF_LEN, "%s:%d", ar.source + 1, line);
     return 1;
 }
 
@@ -143,23 +149,87 @@ static int l_log_2_log(lua_State *L, const char* LEVEL) {
         // 最起码传1个参数
         return 0;
     }
-    if (lconf.style == LOG_STYLE_NORMAL) {
-        lua_pushfstring(L, "%s/user.%s", LEVEL, lua_tostring(L, 1));
-        lua_remove(L, 1); // remove tag
-        lua_insert(L, 1);
+    char buff[LOG_BUFF_LEN] = {0};
+    char tmp[LOG_BUFF_LEN];
+    switch (lconf.style)
+    {
+    case LOG_STYLE_DEBUG_INFO:
+        add_debug_info(L, 1, LEVEL, buff);
+        break;
+    case LOG_STYLE_FULL:
+        snprintf_(tmp, LOG_BUFF_LEN, "%s/user.%s", LEVEL, lua_tostring(L, 1));
+        add_debug_info(L, 1, tmp, buff);
+        break;
+    case LOG_STYLE_NORMAL:
+    default:
+        snprintf_(buff, LOG_BUFF_LEN - 1, "%s/user.%s", LEVEL, lua_tostring(L, 1));
+        break;
     }
-    else if (lconf.style == LOG_STYLE_DEBUG_INFO) {
-        add_debug_info(L, 1, LEVEL);
+    lua_remove(L, 1); // remove tag
+    int top = lua_gettop(L);
+    if (top < 1) {
+        // 没有更多数据, 那直接输出然后返回吧
+        luat_log_write(buff, strlen(buff));
+        return 0;
     }
-    else if (lconf.style == LOG_STYLE_FULL) {
-        lua_pushfstring(L, "%s/user.%s", LEVEL, lua_tostring(L, 1));
-        lua_remove(L, 1); // remove tag
-        lua_insert(L, 1);
-        add_debug_info(L, 2, NULL);
+    size_t len = 0;
+    const char* stmp;
+    char ntmp[64] = {0};
+    for (size_t i = 0; i < top; i++)
+    {
+        ntmp[0] = 0;
+        len = 0;
+        switch(lua_type(L, i+1)) {
+        case LUA_TNUMBER:
+            if (lua_isinteger(L, i+1)) {
+                sprintf_(ntmp, "%lld", lua_tointeger(L, i+1));
+            }
+            else {
+                sprintf_(ntmp, "%.7g", lua_tonumber(L, i+1));
+            }
+            stmp = ntmp;
+            len = strlen(ntmp);
+            break;
+        case LUA_TBOOLEAN :
+            if (lua_toboolean(L, i+1)) {
+                stmp = STR_TURE;
+            }
+            else {
+                stmp = STR_FALSE;
+            }
+            len = strlen(stmp);
+            break;
+        default:
+            stmp = lua_tolstring(L, i+1, &len);
+            LLOGD(">>? %s", stmp);
+            break;
+        }
+        if (len == 0) {
+            // 直接跳过?
+            continue;
+        }
+        if (len + 1 > LOG_BUFF_LEN) {
+            if (strlen(buff) > 0) {
+                luat_log_write(buff, strlen(buff));
+                buff[0] = 0;
+            }
+            luat_log_write(stmp, len);
+        }
+        else if (strlen(buff) + len + 2 > LOG_BUFF_LEN) {
+            if (strlen(buff) > 0) {
+                luat_log_write(buff, strlen(buff));
+                buff[0] = 0;
+            }
+            memcpy(buff + strlen(buff), stmp, len);
+        }
+        else {
+            buff[strlen(buff)] = '\t';
+            memcpy(buff + strlen(buff), stmp, len);
+        }
+    }
+    if (strlen(buff)) {
+        luat_log_write(buff, strlen(buff));
     }
-    lua_getglobal(L, "print");
-    lua_insert(L, 1);
-    lua_call(L, lua_gettop(L) - 1, 0);
     return 0;
 }
 
@@ -174,6 +244,9 @@ static int l_log_2_log(lua_State *L, const char* LEVEL) {
 log.debug("onenet", "connect ok")
 */
 static int l_log_debug(lua_State *L) {
+    if (lua_isinteger(L, 2)) {
+        LLOGD(">>>>>>>>> %lld", luaL_checkinteger(L, 2));
+    }
     if (luat_log_get_level() > LUAT_LOG_DEBUG) return 0;
     return l_log_2_log(L, "D");
 }