Jump to content

Module:SimpleDebug

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Jmarchn (talk | contribs) at 13:52, 20 October 2019 (Created page with 'local p = {} p.s = '' p.allidx = false p.dec = -1 p.maxlines = { num = 100, doerror = true } p.enabled = true local LinCount = 0 function p.breakline...'). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

local p = {}

p.s = ''
p.allidx = false
p.dec = -1
p.maxlines = {
	num = 100,
    doerror = true
    }
p.enabled = true

local LinCount = 0

function p.breakline ()
	if p.s ~= '' then
		LinCount = LinCount + 1
	end	
	p.s = p.s..'\n\n'
	if (LinCount > p.maxlines.num) and p.maxlines.doerror then
		error (p.s,0)
	end	
end	

function p.var (avar, where)
	if p.enabled then
		if type(avar) == 'table' then
			local wds = '{ '
			for k,v in pairs(avar) do
				if (p.allidx == true) or (type(k) ~= 'number')  then 
					wds = wds..'['..k..']='..p.var(v)..', '
				else	
					wds = wds..p.var(v)..', '
				end
			end
			EndStr = wds .. '} '
		elseif type(avar) == 'number' then
			if (p.dec == -1) or (avar == math.floor(avar)) then
				EndStr = tostring(avar)
			else
				EndStr = tostring (math.floor ((avar*10^p.dec)+0.5) / (10^p.dec))
			end	
		elseif type(avar) == 'boolean' then
			if avar == true then
				EndStr = 'true'
			else
				EndStr = 'false'
			end	
		else	
			EndStr = '"'..tostring(avar)..'"'
		end
		if where == nil then
			return EndStr
		else	
			return where.. ' => '..EndStr
		end
	end	
end --var

function p.array (anarray, where)
	if p.enabled then
		local EndStr = ''
		if type(anarray) ~= 'table' then
			EndStr = 'The param to debug is not a table'
		else
			local s = ''
			local C = 0
			function Concat(wds)
				if s ~= '' then
					s = s..' - '
				end
				s = s..wds
			end	
			for i, avar in pairs(anarray) do
				Concat (p.var(avar))
				C = C + 1
			end
			local Dif = #anarray - C
			if Dif == 1 then
				s = '1 variable is nil! '..s
			elseif Dif > 1 then	
				s = Dif..' variables are nil! '..s
			end	
			EndStr = s
		end	    	
		if where == nil then
			return EndStr
		else	
			return where.. ' => '..EndStr
		end
	end	
end --array

function p.namearray (anarray, where)
	if p.enabled then
		local EndStr = ''
		if type(anarray) ~= 'table' then
			EndStr = 'The param to debug is not a table'
		elseif math.mod(#anarray,2) ~= 0 then	
			EndStr = 'Any parameter has not a name or variable; or any variable is nil'
		else
			local s = ''
			local C = 0
			local IsName = true
			function Concat(wds)
				if s ~= '' then
					if IsName then
						s = s..' - '
					else	
						s = s..': '
					end	
				end
				s = s..wds
			end
			for i, avar in pairs(anarray) do
				if IsName then
					Concat (avar)
					IsName = false
				else	
					Concat (p.var(avar))
					IsName = true
				end	
				C = C + 1
			end
			local Dif = #anarray - C
			if Dif == 1 then
				s = '1 variable is nil! '..s
			elseif Dif > 1 then	
				s = Dif..' variables are nil! '..s
			end	
			EndStr = s
		end	    	
		if where == nil then
			return EndStr
		else	
			return where.. ' => '..EndStr
		end
	end	
end --namearray

function p.wheretos (where)
	if p.enabled and (LinCount < p.maxlines.num) then
		if p.s ~= '' then
			p.breakline ()
		end	
		if where == nil then
			where =  '"where" not assigned'
		end	
		p.s = p.s..where
	end	
end --wheretos

function p.arraytos (anarray, where)
	if p.enabled and (LinCount < p.maxlines.num) then
		if p.s ~= '' then
			p.breakline ()
		end	
		p.s = p.s..p.array (anarray, where)
	end	
end --arraytos

function p.namearraytos (anarray, where)
	if p.enabled and (LinCount < p.maxlines.num) then
		if p.s ~= '' then
			p.breakline ()
		end	
		p.s = p.s..p.namearray (anarray, where)
	end	
end --namearraytos

return p