lua自定义log

分享个log,可以打印各种类型,包括嵌套table

cc.exports.cwblog = function(...)
    if config.debug==1 then
		local text = ""
		local xn = 0
		local function textLineT(xn)
			-- body
			for i=1,xn do
				text = text.."\t"
			end
		end

		local function printTable(i,v)
			-- body
			if type(v) == "table" then
				textLineT(xn)
				xn = xn + 1
				text = text..""..i..":Table{\n"
				table.foreach(v,printTable)
				textLineT(xn)
				text = text.."}\n"
				xn = xn - 1
			elseif type(v) == nil then
				textLineT(xn)
				text = text..i..":nil\n"
			else
				textLineT(xn)
				text = text..i..":"..tostring(v).."\n" 
			end
		end
		local function dumpParam(tab)
			for i=1, #tab do  
				if tab[i] == nil then 
					text = text.."nil\t"
				elseif type(tab[i]) == "table" then 
					xn = xn + 1
					text = text.."\ntable{\n"
					table.foreach(tab[i],printTable)
					text = text.."\t}\n"
				else
					text = text..tostring(tab[i]).."\t"
				end
			end
		end
		local x = ...
		if type(x) == "table" then
			table.foreach(x,printTable)
		else
			dumpParam({...})
		end
		print(text)
		end
end

猜你喜欢

转载自caiwb1990.iteye.com/blog/2220408