-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlua_vardump
75 lines (73 loc) · 2.35 KB
/
lua_vardump
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
function vardump(value, depth, key)
local linePrefix = ""
local spaces = ""
if key ~= nil then
linePrefix = "["..key.."] = "
end
if depth == nil then
depth = 0
else
depth = depth + 1
for i=1, depth do spaces = spaces .. " " end
end
if type(value) == 'table' then
mTable = getmetatable(value)
if mTable == nil then
print(spaces ..linePrefix.."(table) ")
else
print(spaces .."(metatable) ")
value = mTable
end
for tableKey, tableValue in pairs(value) do
vardump(tableValue, depth, tableKey)
end
elseif type(value) == 'function'
or type(value) == 'thread'
or type(value) == 'userdata'
or value == nil
then
print(spaces..tostring(value))
else
print(spaces..linePrefix.."("..type(value)..") "..tostring(value))
end
end
--use this to get a log to console or save to file ,no print
function GetVarDump(value, depth, key)
local indentSymbol ="\t"
local contentStr = ""
local addContent=function(str ,makeNewLine)
if str~=nil and str ~='' then
contentStr =string.format('%s%s', contentStr,tostring(str))
end
end
local linePrefix = ""
local indent = ""
if key ~= nil then
linePrefix = key.." = "
end
if depth == nil then
depth = 0
else
depth = depth + 1
for i=1, depth do indent = indent .. indentSymbol end
end
if type(value) == 'table' then
local mTable = getmetatable(value)
if mTable == nil then
addContent(string.format('%s%s(table)\n%s{',indent ,linePrefix,indent,indent))
else
addContent(indent .."(metatable) ")
value = mTable
end
for tableKey, tableValue in pairs(value) do
local content = GetVarDump(tableValue, depth, tableKey)
addContent('\n'..content)
end
addContent('\n'..indent ..'}')
elseif type(value) == 'function' or type(value) == 'thread' or type(value) == 'userdata' or value == nil then
addContent(indent..tostring(value))
else
addContent(indent..linePrefix..tostring(value).." ("..type(value)..") ")
end
return contentStr
end