When debugging ion configs or just exploring things, there didn't seem
to be any good way of just dumping arbitrary objects and seeing what
they were. So I came up with this:
dbg = {}
function dbg.echo(s)
dbg.echon(s)
dbg.echon('\n')
end
function dbg.echon(s)
io.stderr:write(dbg.tostring(s))
end
function dbg.tostring(obj)
local mt
local tname
local o
local sep
if type(obj) == 'userdata' then
mt = getmetatable(obj)
o = '['
if mt and mt.__index then
tname = mt.__index.__typename
if tname then
o = o .. tname
end
end
if obj.name then
if tname and tname == 'WClientWin' then
o = o .. ': ' ..
dbg.tostring(obj:get_ident())
else
o = o .. ': ' .. obj:name()
end
end
return o .. ']'
elseif type(obj) == 'table' then
o = '{'
sep = ''
for k, v in pairs(obj)
do
o = o .. sep .. dbg.tostring(k) .. '='
o = o .. dbg.tostring(v)
sep = ', '
end
return o .. '}'
end
return tostring(obj)
end
That does a reasonable job of dumping most things, but still isn't quite
perfect. Can this be improved upon? Is there a better way to do it?
Also, could something like this be included in the standard
distribution, or maybe in the scripts repository, or even in the
scripting documentation? It's very useful and makes it much easier to
figure things out.
Jason