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中的内容,并对循环引用做出了相应处理。
版权声明:本文为qq_22337119原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。