Lua - 输出打印table表

lua自带的print函数只能打印可转化为字符串的数据,如果打印table表的话,则会和打印函数和userdata类型数据一样,输出为内存地址的形式。工作中因项目需要,可打印table表的话,对bug的查修和信息的监控将会高效的多。

打印table表的方法如下

-- log输出格式化
local function logPrint(str)
    str = os.date("\nLog output date: %Y-%m-%d %H:%M:%S \n", os.time()) .. str
    print(str)
end

-- key值格式化
local function formatKey(key)
    local t = type(key)
    if t == "number" then
        return "["..key.."]"
    elseif t == "string" then
        local n = tonumber(key)
        if n then
            return "["..key.."]"
        end
    end
    return key
end

-- 栈
local function newStack()
    local stack = {
        tableList = {}
    }
    function stack:push(t)
        table.insert(self.tableList, t)
    end
    function stack:pop()
        return table.remove(self.tableList)
    end
    function stack:contains(t)
        for _, v in ipairs(self.tableList) do
            if v == t then
                return true
            end
        end
        return false
    end
    return stack
end

-- 输出打印table表 函数
function printTable(...)
    local args = {...}
    for k, v in pairs(args) do
        local root = v
        if type(root) == "table" then
            local temp = {
                "------------------------ printTable start ------------------------\n",
                "local tableValue".." = {\n",
            }
            local stack = newStack()
            local function table2String(t, depth)
                stack:push(t)
                if type(depth) == "number" then
                    depth = depth + 1
                else
                    depth = 1
                end
                local indent = ""
                for i=1, depth do
                    indent = indent .. "    "
                end
                for k, v in pairs(t) do
                    local key = tostring(k)
                    local typeV = type(v)
                    if typeV == "table" then
                        if key ~= "__valuePrototype" then
                            if stack:contains(v) then
                                table.insert(temp, indent..formatKey(key).." = {检测到循环引用!},\n")
                            else
                                table.insert(temp, indent..formatKey(key).." = {\n")
                                table2String(v, depth)
                                table.insert(temp, indent.."},\n")
                            end
                        end
                    elseif typeV == "string" then
                        table.insert(temp, string.format("%s%s = \"%s\",\n", indent, formatKey(key), tostring(v)))
                    else
                        table.insert(temp, string.format("%s%s = %s,\n", indent, formatKey(key), tostring(v)))
                    end
                end
                stack:pop()
            end
            table2String(root)
            table.insert(temp, "}\n------------------------- printTable end -------------------------")
            logPrint(table.concat(temp))
        else
            logPrint("----------------------- printString start ------------------------\n"
                 .. tostring(root) .. "\n------------------------ printString end -------------------------")
        end
    end
end

 测试:

local testTb1 = {"cc", "dd", ["a"] = {1}}
local testTb = {"aa", ["bb"] = {1, 3, ["b"] = "test"}, "cc", "dd"}
testTb1["a"][2] = testTb1
printTable(testTb)
printTable(testTb1)
printTable("当前测试时间:" .. os.date("%Y-%m-%d %H:%M:%S", os.time()))

测试结果:

Log output date: 2020-09-01 15:19:04 
------------------------ printTable start ------------------------
local tableValue = {
    [1] = "aa",
    [2] = "cc",
    [3] = "dd",
    bb = {
        [1] = 1,
        [2] = 3,
        b = "test",
    },
}
------------------------- printTable end -------------------------

Log output date: 2020-09-01 15:19:04 
------------------------ printTable start ------------------------
local tableValue = {
    [1] = "cc",
    [2] = "dd",
    a = {
        [1] = 1,
        [2] = {检测到循环引用!},
    },
}
------------------------- printTable end -------------------------

Log output date: 2020-09-01 15:19:04 
----------------------- printString start ------------------------
当前测试时间:2020-09-01 15:19:04
------------------------ printString end -------------------------

从测试结果可以看出,准确且完整的输出了table中的内容,并对循环引用做出了相应处理。

  • 6
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值