table.print = function(t, tname, print_one_level)
if type(t) ~= "table" then
print("deep_print error, parameter accept is not a table!")
return
end
local _deep_count = 0
local print_one_table
local max_deep = deep or 10
local printed_tables = {}
local t_path = {}
tname = tname or "root_table"
print_one_table = function(tb, tb_name, print_one_level)
tb_name = tb_name or "table"
table.insert(t_path, tb_name)
local tpath, i, tname = ""
for i, pname in pairs(t_path) do
tpath = tpath.."."..pname
end
printed_tables[tb] = tpath
_deep_count = _deep_count + 1
local k, v, str
local tab = string.rep(" ", _deep_count*4)
--print(string.format("%s [ 0x%s ]t\n%s { ", tab, string.sub(tostring(tb), 8), tab))
print(string.format("%s {", tab))
for k, v in pairs(tb) do
if type(v) == "table" then
if printed_tables[v] then
str = string.format("%s %s = [ %s ]t", tab, output_value(k), printed_tables[v])
print(str)
elseif not print_one_level then
str = string.format("%s %s = ", tab, output_value(k))
print(str)
print_one_table(v, tostring(k))
else
str = string.format("%s %s = %s", tab, output_value(k), output_value(v))
print(str)
end
else
str = string.format("%s %s = %s", tab, output_value(k), output_value(v))
print(str)
end
end
print(tab.." }")
table.remove(t_path)
_deep_count = _deep_count - 1
end
print_one_table(t, tname, print_one_level)
printed_tables = nil
end
output_value = function(value)
if not value then return "nil" end
local str, value_type
value_type = type(value)
if value_type == "number" then
str = string.format("[ %d ]n", value)
elseif value_type == "string" then
str = string.format("[ \"%s\" ]s", value)
elseif value_type == "table" then
str = string.format("[ 0x%s ]t", string.sub(tostring(value), 8))
elseif value_type == "function" then
str = string.format("[ 0x%s ]f", string.sub(tostring(value), 11))
elseif value_type == "userdata" then
str = string.format("[ 0x%s ]u", string.sub(tostring(value), 11))
else
str = string.format("[ \"%s\" ]%s", tostring(value), type(value))
end
return str
end
Lua 深打印
猜你喜欢
转载自blog.csdn.net/ooomyself/article/details/78422091
今日推荐
周排行