
	tracer 	= {}
	tracer.height 	= 0
	tracer.limit 	= 37
	tracer.width 	= 280
	tracer.strings = {}
	tracer.funcs  = {}

	toLog 			= {}
	logShift 		= 1
	logHeight 		= 500
	logMaxheight 	= 500
	logNum 			= 0
	tracer.x 		= tracer.x or -tracer.width
	logx 			= logx 	 or -tracer.width


	local rect = {}
function renderDebug()
	
	if LOVR then 
		rl.BeginBlendMode(0)
	  rl.EndBlendMode()
	end
	if level.isAnim and (not animationPanel or not animationPanel.loaded) then return end
	
	bench.push("tracing")
	
	pushPost()
	gfx.reset()

	traceStats()
	drawTrace()
	drawLog()

	if patterns.atlas and editor and editor.debug.show_patterns then 
		gfx.draw(patterns.atlas,0,0,0,0.2,0.2)
	end
	popPost()


	bench.pop("tracing")
end

function toggleTrace()
	if shiftDown and altDown and cmdDown and keypressed("w") then
		game.profile=2
		tempTip("Profiling")
	end
	if keypressed(";") then
		traceEnabled = not traceEnabled
	end
	if keypressed("/") then
		logEnabled = not logEnabled
	end
	if keypressed("h") then
		helpEnabled = not helpEnabled
		if not traceEnabled then 
			traceEnabled=true
		end
	end
	if editor and editor.show_help then 
		traceEnabled=true
		helpEnabled=true
	end
end

function drawTrace()
	-- TRACE
	tracer.x 	=easeTo(tracer.x,not (traceEnabled and not suppressTrace) and -tracer.width-20 or -8, 2)

	
	gfx.setFont(fonts.inconsolata,12)
	if tracer.x<-tracer.width+50 then 
		gfx.setColor(0,0,0,1)
		-- gfx.print(math.min(ltimer.getFPS( ),60),screenWidth-11,screenHeight-11)
		gfx.setColor(1,1,1,1)
		-- gfx.print(math.min(ltimer.getFPS( ),60),screenWidth-12,screenHeight-12)
		-- gfx.print(inspect(scene))
		return 
	end
	gfx.setColor(0.1,0.1,0.1,0.5)
	gfx.setFont(fonts.inconsolata,11)
	
	-- if LOVR or config.flatUI  then
		gfx.setColor(0.1,0.1,0.1,1)

		gfx.rectangle("fill",tracer.x,-8,tracer.width,tracer.height+4,2) 
	-- else
	-- 	editor.drawContextBack({
	-- 		x=tracer.x,
	-- 		y=-8,
	-- 		width=tracer.width,
	-- 		height=tracer.height+4,
	-- 		frost=10,
	-- 		-- dark=true,
	-- 		shadowless=true
	-- 	})
	-- end

	tracer.height = 8


	for i,tp in ipairs(tracer.strings) do 
		-- if i>3 then break end
		tracer.height=tracer.height+11
		if mouseIn(setTable(rect,"w",tracer.width,"h",11,"x",tracer.x,"y",(i-1)*11),mouse) then 
			gfx.setColor(0,0,0,0.5)
			gfx.rectangle("fill",rect.x,rect.y+0.5,rect.w,rect.h+2)
			if tracer.funcs[i] then
				mouse.set(tracer.funcs[i])
				if type(tracer.funcs[i][1])=="table" then 
					mouse.setR(tracer.funcs[i])
				end
			end
		end
		if tracer.funcs[i] and tracer.funcs[i][1] == bench.fold and bench.folded[tracer.funcs[i][2]] then 
			gfx.setColor(0,0,0,0.5)
			gfx.rectangle("fill",rect.x,rect.y+0.5,rect.w,rect.h+2)
		end
		gfx.setColor(1,1,1,1)
		gfx.print( tp:sub(1,43),tracer.x+16,(i-1)*11) 
		-- print(tp:sub(1,43),tracer.x+16,(i-1)*11)
	end

end

function drawLog()
	logx=easeTo(logx,(logEnabled and not suppressTrace) and  -8 or -tracer.width-20, 2)

	if logx<-tracer.width then return end
	

	logHeight = mc(16,logHeight,mc(32,screenHeight-tracer.height-logShift,screenHeight/2))
	
	gfx.setColor(0.1,0.1,0.1,0.5)

	if config.flatUI then 
		gfx.setColor(0.1,0.1,0.1,0.99)
		gfx.rectangle("fill",logx,screenHeight-logHeight-logShift-2,tracer.width,logHeight+3,2) 
	else
		editor.drawContextBack( {
			x		=	logx,
			y		=	screenHeight-logHeight-logShift-2,
			width 	=	tracer.width,
			height 	=	logHeight+3,
			frost 	=	0,
			shadowless= true
		})
	end

	gfx.setColor(1,1,1,1)
	gfx.setFont(fonts.inconsolata,13)
	while #toLog>logHeight/16-1 do 
		table.remove(toLog,1)
	end

		for i,lg in ipairs(toLog) do 
			gfx.print((tostring(logNum-#toLog+i)..". "..lg):sub(1,32),logx+18, mf(i*16-8+screenHeight-logHeight-logShift)) 
		end

end

function trace(val,...)

	local args  		= {...}
 	local newline 		= type(val)=="table" and args[1] or false
 	local sub 			= type(val)=="table" and args[2] or false
 	local nameString	= ""

 	if #tracer.strings*12>screenHeight then return end
	if type(val) == "table" then 
		if type(val[1])=="function" or (type(val[1])=="table" and type(val[1][1])=="function") then 
			table.insert(tracer.strings,args[1])
			tracer.funcs[#tracer.strings] = val 
		else
			for k,v in pairs(val) do 
				trace(k,v)
			end
		end
	else
		local printString = ""
		for i,a in ipairs(lume.concat({val},args)) do
			if type(a) == "number" then a = round(a,3) end
			if type(a) == "string" and WINDOWS then a = a:gsub("cmd","ctl") end
			printString = printString..(i>1 and ", " or "")..tostring(a)
		end
		table.insert(tracer.strings,printString)
	end
end


function traceTable()
	
end

function log(val,...)
	logNum = logNum+1
	print(inspect(val))
	local args  		= {...}
 	local newline 		= type(val)=="table" and args[1] or false
 	local sub 			= type(val)=="table" and args[2] or false
 	local nameString	= ""

 	-- if #toLog*16>screenHeight-64 then return end
	if type(val) == "table" then 
		if lume.count(val) == 0 then return end
		for i,n in pairs(val) do
			if newline then
				log(n)
			else
				if type(n) == "table" then 
					log(n)
				else
					local newlineN = (i~=#val and ", " or "")
					nameString = nameString..tostring(n)..newlineN
					table.insert(toLog, nameString)
					print(printString)
				end
			end
		end
	else
		local printString = ""
		for i,a in ipairs(lume.concat({val},args)) do
			printString = printString..(i>1 and ", " or "")..tostring(a)
		end
		table.insert(toLog,printString)
		
	end
end

local tracehelp = " [/ trace] [\\ log] [h help]"
function traceReset()
	lume.clear(tracer.strings)
	trace(ltimer.getFPS( )..tracehelp..(hiSpec and " hi" or " lo"))	
	toggleTrace()
end

function numlines(s)
  return # s:gsub("[^\n]", "")
end

function traceStats(  )
	if not (game.state=="Menu" or game.state=="Load") and editor.debug.gfx_stats then  
		local stats = lume.merge(gfx.getStats(),LOVR and {} or gfx.getSystemLimits())
		local garbage = (collectgarbage("count")-game.frameGarbageStart)
		trace("")
		game.frameGarbage = garbage>0 and garbage or game.frameGarbage or 0
		
		if game.frameTimeIter==0 then game.frameTime = mean(game.frameTimes) end
		if LOVR then 
			 trace({
    	"canvases    - "..  stats.textures,
      "tex mem     - "..round((stats.texturememory/1024)/1024,2).." MB",
      "total garbage- "..round(collectgarbage("count")/1024,0).."mb",
      
    },true)
			else
    trace({
    	-- "drawFuncs    "..  #drawFuncs,
    	-- "max size    - ".. stats.texturesize,
    	"canvases    - "..  stats.canvases,
      "images      - "..  (stats.images-interface_images),
       --"Fonts        "..  stats.fonts,
      --"switches    - "..  stats.canvasswitches,
      "draw calls  - "..  stats.drawcalls,
      "tex mem     - "..round((stats.texturememory/1024)/1024,2).." MB",
      --"terrain     - "..tostring(terrainVerts ),
      "frame time  - "..round(game.frameTime*1000).." ms",
      "frame garbage- "..round(game.frameGarbage,0).."kb",
      "total garbage- "..round(collectgarbage("count")/1024,0).."mb",
      
    },true)

		end
   end
end

