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 进行配置。

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