Log In  


While creating a program that uses Lua tables, I found it troublesome to check values, so I created this.
It is convenient to save it with a name such as dprt.lua and use it by including it.

--If you no longer need the debug display, you can disable it all together 
--by changing DEBUG_FLG to false.
--Also, if you want to display it on the host, set DEBUG_TO_HOST to true
DEBUG_FLG = true
DEBUG_TO_HOST = true

function dprt(v)
	local function dbg(v)
		local buf, t, i = "", type(v), 0
		if t == "table" then
			buf = "table{"
			for i in pairs(v) do
				if v[i] != nil then
					buf = string.format("%s \n\t%s: %s", buf, i,dbg(v[i]))
				end
			end
			buf = string.format("%s \n},", buf)
		elseif t == "userdata" then
			buf = "userdata{"
			for i=0,#v-1 do
				buf = string.format("%s %s: %s", buf, i, dbg(v[i]))
			end
			buf = string.format("%s \n},", buf)
		elseif t == "number" then
			buf = string.format("%s,", v)
		elseif t == "string" then
			buf = string.format("'%s',", v)
		elseif t == "boolean" then
			if v then
				buf = "true,"
			else
				buf = "false,"
			end
		elseif t == "function" then
			buf = "function{"
			local info = debug.getinfo(v)
			for i in pairs(info) do
				buf = string.format("%s, %s: %s", buf, i, info[i])
			end
			buf = string.format("%s },", buf)
		elseif t == "nil" then
			buf = "nil,"
		end 
		return buf
	end
	if DEBUG_FLG != true then return "" end
	if DEBUG_TO_HOST == true then printh(dbg(v)) else print(dbg(v)) end
end

For example, you can use it like this:

on_event("mouse", function(msg)
    dprt(msg)
end)



[Please log in to post a comment]