Module:Debug

From The Satanic Wiki
Jump to navigation Jump to search

Documentation for this module may be created at Module:Debug/doc

--[[ //Helps debug lua modules using console by creating a pseudo frame 
and other useful functions
Syntax:
require("Module:debug").frame(childtable, parenttable)
Example:
frame = require("Module:debug").frame({'aaa','sss'},{'zz'=8,'1'})
p.main(frame)
Todo:
Add more debugging tools ]]--

local p = {}
local dumpObj = require('Module:Inspect').inspect
local frameTools = require("Module:FrameTools")

-- e.g. {"abc","zz"} parameters from a page calling this module directly
function p.frame(args, pArgs)
    return frameTools.makePseudoFrame({}, pArgs or {}, args or {})
end

-- Generates a tracebackfor a function
-- ex. function fFunction() x=1 +c end ; p.trace(fFunction, var1,var2)
function p.trace(errFunction, ...)
    local function testFunc ()
         return errFunction(unpack(arg))
    end
    
    local success, result = xpcall(testFunc,
        function(err) 
            mw.log(debug.traceback(err) )
            return debug.traceback(err) 
        end)
        
    return success, result
end 

-- Outputs the information from a obj (readable table, string, number, etc)
function p.log(obj)
    return dumpObj(obj)
end

return p