lua日誌打印模塊

前言

本人初學lua,在學習過程中發現一些執行lua的後臺進程不容易打印調試日誌,於是就在在網上找了個能夠打印調試日誌的lua的模塊,但是用起來沒那麼方便,索然就對其進行了更改,能夠實現在不同的文件中打印調試日誌,相當於給調試日誌劃分了一些打印等級,不同的文件中打印的日誌等級不一致。代碼如下。

代碼


local M = {}
 
local tconcat = table.concat  
local tinsert = table.insert  
local srep = string.rep
 
local function local_print(str)  
    local dbg = io.open("/tmp/lucir.output", "a+")
    local str = str or ""
    if dbg then
        dbg:write(str..'\n')
        dbg:close()
    end
end

local function local_fprint(filename,str)  
    local dbg = io.open(filename, "a+")
    local str = str or ""
    if dbg then
        dbg:write(str..'\n')
        dbg:close()
    end
end

function M.fprint(filename,...)  
    local dbg = io.open(filename, "a+")
    if dbg then
        for _, o in ipairs({...}) do
            dbg:write(tostring(o)..'  ')
        end
        dbg:write("\n")
        dbg:close()
    end
end

function M.print(...)  
    local dbg = io.open("/tmp/luci.output", "a+")
    if dbg then
        for _, o in ipairs({...}) do
            dbg:write(tostring(o)..'  ')
        end
        dbg:write("\n")
        dbg:close()
    end
end

function M.print_r(data, depth)  
    local depth = depth or 3
    local cstring = ""; 
    local top_flag = true
 
    local function table_len(t)
    local i = 0
    for k, v in pairs(t) do
        i = i + 1
    end
    return i
    end
 
    local function tableprint(data,cstring, local_depth)
        if data == nil then 
            local_print("core.print data is nil");
        end 
 
        local cs = cstring .. "    ";
    if top_flag then
            local_print(cstring .."{");
        top_flag = false
    end
        if(type(data)=="table") then
            for k, v in pairs(data) do
        if type(v) ~= "table" then
            if type(v) == "string" then
                        local_print(cs..tostring(k).." = ".."'"..tostring(v).."'");
            else
                        local_print(cs..tostring(k).." = "..tostring(v));
            end
        elseif table_len(v) == 0 then
            local_print(cs..tostring(k).." = ".."{}")
        elseif local_depth < depth then
                    local_print(cs..tostring(k).." = {");
                      tableprint(v,cs,local_depth+1);
        else
            local_print(cs..tostring(k).." = ".."{*}")
        end
            end 
        else
            local_print(cs..tostring(data));
        end 
        local_print(cstring .."}");
    end 
 
    tableprint(data,cstring,0);
end

function M.fprint_r(filename,data, depth)  
    local depth = depth or 3
    local cstring = ""; 
    local top_flag = true
 
    local function table_len(t)
    local i = 0
    for k, v in pairs(t) do
        i = i + 1
    end
    return i
    end
 
    local function tableprint(data,cstring, local_depth)
        if data == nil then 
            local_fprint(filename,"core.print data is nil");
        end 
 
        local cs = cstring .. "    ";
    if top_flag then
            local_fprint(filename,cstring .."{");
        top_flag = false
    end
        if(type(data)=="table") then
            for k, v in pairs(data) do
        if type(v) ~= "table" then
            if type(v) == "string" then
                        local_fprint(filename,cs..tostring(k).." = ".."'"..tostring(v).."'");
            else
                        local_fprint(filename,cs..tostring(k).." = "..tostring(v));
            end
        elseif table_len(v) == 0 then
            local_fprint(filename,cs..tostring(k).." = ".."{}")
        elseif local_depth < depth then
                    local_fprint(filename,cs..tostring(k).." = {");
                      tableprint(v,cs,local_depth+1);
        else
            local_fprint(filename,cs..tostring(k).." = ".."{*}")
        end
            end 
        else
            local_fprint(filename,cs..tostring(data));
        end 
        local_fprint(filename,cstring .."}");
    end 
 
    tableprint(data,cstring,0);
end



return M 

說明

將該模塊文件放入lua 能夠找得到的路徑下,然後在需要打印日誌的文件中 使用 require 引用即可。
在 linux 下面,lua 運行的環境變量可以通過 package.path 和 package.cpath 進行配置。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章