Prechádzať zdrojové kódy

update: 实现log库的日志函数无lua内存变动

Wendal Chen 3 rokov pred
rodič
commit
e6bc31156b
1 zmenil súbory, kde vykonal 97 pridanie a 58 odobranie
  1. 97 58
      luat/modules/luat_lib_log.c

+ 97 - 58
luat/modules/luat_lib_log.c

@@ -142,114 +142,149 @@ int l_log_get_level(lua_State *L) {
     return 1;
 }
 
-static int l_log_2_log(lua_State *L, const char* LEVEL) {
+static const char* log2str(lua_State* L, int index, char *ntmp, size_t *len) {
+    const char* stmp = NULL;
+    switch(lua_type(L, index)) {
+    case LUA_TNUMBER:
+        if (lua_isinteger(L, index)) {
+            #ifdef LUAT_USE_VM_64bit
+            sprintf_(ntmp, "%lld", lua_tointeger(L, index));
+            #else
+            sprintf_(ntmp, "%d", lua_tointeger(L, index));
+            #endif
+        }
+        else {
+            sprintf_(ntmp, "%.7g", lua_tonumber(L, index));
+        }
+        break;
+    case LUA_TBOOLEAN :
+        if (lua_toboolean(L, index)) {
+            stmp = STR_TURE;
+        }
+        else {
+            stmp = STR_FALSE;
+        }
+        break;
+    case LUA_TNIL :
+        break;
+    case LUA_TFUNCTION:
+        if (lua_iscfunction(L, index)) {
+            sprintf_(ntmp, "function: %p", lua_tocfunction(L, index));
+        }
+        else {
+            sprintf_(ntmp, "function: %p", lua_topointer(L, index));
+        }
+        break;
+    case LUA_TTABLE:
+        sprintf_(ntmp, "table: %p", lua_topointer(L, index));
+        break;
+    case LUA_TLIGHTUSERDATA :
+        sprintf_(ntmp, "lightuserdata: %p", lua_topointer(L, index));
+        break;
+    case LUA_TUSERDATA:
+        sprintf_(ntmp, "userdata: %p", lua_topointer(L, index));
+        break;
+    case LUA_TTHREAD :
+        sprintf_(ntmp, "thread: %p", lua_topointer(L, index));
+        break;
+    case LUA_TSTRING :
+        stmp = luaL_checklstring(L, index, len);
+        break;
+    }
+    if (stmp == NULL)
+        *len = strlen(ntmp);
+    return stmp;
+}
+
+static void l_log_2_log(lua_State *L, const char* LEVEL) {
     // 是不是什么都不传呀?
     int argc = lua_gettop(L);
     if (argc < 1) {
         // 最起码传1个参数
-        return 0;
+        return;
     }
     char buff[LOG_BUFF_LEN] = {0};
-    char tmp[LOG_BUFF_LEN];
+    char tmp[LOG_BUFF_LEN] = {0};
+    size_t len = 0;
+    const char* stmp = NULL;
+    char ntmp[64] = {0};
+    stmp = log2str(L, 1, ntmp, &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));
+        snprintf_(tmp, LOG_BUFF_LEN, "%s/user.%s", LEVEL, stmp == NULL ? ntmp : stmp);
         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));
+        snprintf_(buff, LOG_BUFF_LEN - 1, "%s/user.%s", LEVEL, stmp == NULL ? ntmp : stmp);
         break;
     }
     lua_remove(L, 1); // remove tag
     int top = lua_gettop(L);
     if (top < 1) {
         // 没有更多数据, 那直接输出然后返回吧
+        buff[LOG_BUFF_LEN - 1] = 0;// 确保结束
         luat_log_write(buff, strlen(buff));
         return 0;
     }
-    size_t len = 0;
-    const char* stmp;
-    char ntmp[64] = {0};
+    stmp = NULL;
+    len = 0;
+    memset(ntmp, 0, 64);
     for (size_t i = 0; i < top; i++)
     {
         ntmp[0] = 0;
         len = 0;
         int index = i + 1;
-        switch(lua_type(L, index)) {
-        case LUA_TNUMBER:
-            if (lua_isinteger(L, i+1)) {
-                #ifdef LUAT_USE_VM_64bit
-                sprintf_(ntmp, "%lld", lua_tointeger(L, index));
-                #else
-                sprintf_(ntmp, "%d", lua_tointeger(L, index));
-                #endif
-            }
-            else {
-                sprintf_(ntmp, "%.7g", lua_tonumber(L, index));
-            }
-            stmp = ntmp;
-            len = strlen(ntmp);
-            break;
-        case LUA_TBOOLEAN :
-            if (lua_toboolean(L, index)) {
-                stmp = STR_TURE;
-            }
-            else {
-                stmp = STR_FALSE;
-            }
-            len = strlen(stmp);
-            break;
-        case LUA_TNIL :
-            break;
-        case LUA_TFUNCTION:
-            if (lua_iscfunction(L, index)) {
-                sprintf_(ntmp, "function %p", lua_tocfunction(L, index));
-            }
-            else {
-                sprintf_(ntmp, "function %p", lua_topointer(L, index));
-            }
-            stmp = ntmp;
-            len = strlen(ntmp);
-            break;
-        default:
-            stmp = lua_tolstring(L, index, &len);
-            break;
-        }
+        stmp = log2str(L, index, ntmp, &len);
         if (len == 0) {
             // 直接跳过?
             continue;
         }
         if (len + 1 > LOG_BUFF_LEN) {
             if (strlen(buff) > 0) {
+                #ifdef LUAT_LOG_NO_NEWLINE
                 luat_log_write(buff, strlen(buff));
-                luat_log_write("\n", 1);
+                #else
+                buff[strlen(buff)] = '\n';
+                buff[strlen(buff)] = 0;
+                luat_log_write(buff, strlen(buff));
+                #endif
                 buff[0] = 0;
             }
-            luat_log_write(stmp, len);
+            luat_log_write(stmp == NULL ? ntmp : stmp, len);
             luat_log_write("\n", 1);
         }
         else if (strlen(buff) + len + 2 > LOG_BUFF_LEN) {
             if (strlen(buff) > 0) {
+                #ifdef LUAT_LOG_NO_NEWLINE
                 luat_log_write(buff, strlen(buff));
+                #else
+                buff[strlen(buff)] = '\n';
+                buff[strlen(buff)] = 0;
+                luat_log_write(buff, strlen(buff));
+                #endif
                 buff[0] = 0;
-                luat_log_write("\n", 1);
             }
-            memcpy(buff + strlen(buff), stmp, len);
+            memcpy(buff + strlen(buff), stmp == NULL ? ntmp : stmp, len);
             len = 0;
         }
         else {
             buff[strlen(buff)] = '\t';
-            memcpy(buff + strlen(buff), stmp, len);
+            memcpy(buff + strlen(buff), stmp == NULL ? ntmp : stmp, len);
         }
     }
     if (strlen(buff)) {
+        #ifdef LUAT_LOG_NO_NEWLINE
         luat_log_write(buff, strlen(buff));
-        luat_log_write("\n", 1);
+        #else
+        buff[strlen(buff)] = '\n';
+        buff[strlen(buff)] = 0;
+        luat_log_write(buff, strlen(buff));
+        #endif
     }
     return 0;
 }
@@ -266,7 +301,8 @@ log.debug("onenet", "connect ok")
 */
 static int l_log_debug(lua_State *L) {
     if (luat_log_get_level() > LUAT_LOG_DEBUG) return 0;
-    return l_log_2_log(L, "D");
+    l_log_2_log(L, "D");
+    return 0;
 }
 
 /*
@@ -281,7 +317,8 @@ log.info("onenet", "connect ok")
 */
 static int l_log_info(lua_State *L) {
     if (luat_log_get_level() > LUAT_LOG_INFO) return 0;
-    return l_log_2_log(L, "I");
+    l_log_2_log(L, "I");
+    return 0;
 }
 
 /*
@@ -296,7 +333,8 @@ log.warn("onenet", "connect ok")
 */
 static int l_log_warn(lua_State *L) {
     if (luat_log_get_level() > LUAT_LOG_WARN) return 0;
-    return l_log_2_log(L, "W");
+    l_log_2_log(L, "W");
+    return 0;
 }
 
 /*
@@ -311,7 +349,8 @@ log.error("onenet", "connect ok")
 */
 static int l_log_error(lua_State *L) {
     if (luat_log_get_level() > LUAT_LOG_ERROR) return 0;
-    return l_log_2_log(L, "E");
+    l_log_2_log(L, "E");
+    return 0;
 }
 
 #include "rotable2.h"