canary/data/libs/debugging/dump.lua

60 lines
1.3 KiB
Lua
Raw Permalink Normal View History

2021-04-26 21:04:01 -03:00
-- recursive dump function
function dumpLevel(input, level)
local indent = ""
2021-04-26 21:04:01 -03:00
for i = 1, level do
indent = indent .. " "
2021-04-26 21:04:01 -03:00
end
if type(input) == "table" then
local str = "{ \n"
2021-04-26 21:04:01 -03:00
local lines = {}
for k, v in pairs(input) do
if type(k) ~= "number" then
2021-04-26 21:04:01 -03:00
k = '"' .. k .. '"'
end
if type(v) == "string" then
2021-04-26 21:04:01 -03:00
v = '"' .. v .. '"'
end
table.insert(lines, indent .. " [" .. k .. "] = " .. dumpLevel(v, level + 1))
2021-04-26 21:04:01 -03:00
end
return str .. table.concat(lines, ",\n") .. "\n" .. indent .. "}"
2021-04-26 21:04:01 -03:00
end
return tostring(input)
end
-- Return a string representation of input for debugging purposes
function dump(input)
return dumpLevel(input, 0)
end
-- Call the dump function and print it to console
function pdump(input)
local dump_str = dump(input)
logger.debug(dump_str)
2021-04-26 21:04:01 -03:00
return dump_str
end
-- Call the dump function with a title and print it beautifully to the console
function tdump(title, input)
local title_fill = ""
2021-04-26 21:04:01 -03:00
for i = 1, title:len() do
title_fill = title_fill .. "="
2021-04-26 21:04:01 -03:00
end
local header_str = "\n====" .. title_fill .. "====\n"
header_str = header_str .. "=== " .. title .. " ===\n"
header_str = header_str .. "====" .. title_fill .. "====\n"
2021-04-26 21:04:01 -03:00
local dump_str = dump(input)
local footer_str = "\n====" .. title_fill .. "====\n"
2021-04-26 21:04:01 -03:00
logger.debug(header_str .. dump_str .. footer_str)
2021-04-26 21:04:01 -03:00
return dump_str
end