```lua
local function to_readable( val )
if type(val) == "string" then
return "\"" .. val .. "\""
end
return val
end
--- @
param data 要打印的字符串
--- @
param [max_level] table 要展开打印的计数,默认 nil 表示全部展开
--- @
param [prefix] 用于在递归时传递缩进,该参数不供用户使用于
local recursion = {}
local function var_dump(data, max_level, prefix)
if type(prefix) ~= "string" then
prefix = ""
end
if type(data) ~= "table" then
print(prefix .. tostring(data))
elseif recursion[data] then
print(data,"dumplicate") --重复递归
else
recursion[data] = true
print(data)
local prefix_next = prefix .. " "
print(prefix .. "{")
for k,v in pairs(data) do
io.stdout:write(prefix_next .. tostring( to_readable(k) ) .. " = ")
if type(v) ~= "table" or (type(max_level) == "number" and max_level <= 1) then
print( to_readable(v) )
else
var_dump(v, max_level - 1, prefix_next)
end
end
print(prefix .. "}")
end
end
--[[
eg: local b = {aaa="aaa",bbb="bbb",ccc="ccc"}
]]
function vd(data, max_level)
var_dump(data, max_level or 20)
recursion = {} --释放内存
end
```