debugEnabled=false
fDebugEnabled=false
nbDebugMessages=1

debugFileName=nil






function initFDebug(filename)
	debugFile=io.open(filename, "a+")
	if debugFile==nil then
		debugForced("io.open failed")
	end
	--debugFile:setvbuf ("no")
	debugFile:write("###########################################################\r\n")
	debugFile:write("#                                                         #\r\n")
	debugFile:write("#            debug log from "..os.date("%m/%d/%Y %l:%M%s %p").."           #\r\n")
	debugFile:write("#                      environment="..environment.."                     #\r\n")
	debugFile:write("#                                                         #\r\n")
	debugFile:write("###########################################################\r\n")
	debugFile:flush()
	logFileInitialized=true
	debugFile:close()
	debugFileName=filename
end



function debug(message1, message2)
	if debugEnabled then
		debugForced(message1, message2)
	end
end

function debugForced(message1, message2)
	screen:clear()
	screen:print(10, 10, nbDebugMessages, white)
	screen:print(10, 30, message1, white)
	if message2~=nil then
		screen:print(10, 45, message2, white)
	end
	screen:print(380, 245, getMemoryInfo(), white)
	screen.waitVblankStart()
	screen.flip()
	nbDebugMessages=nbDebugMessages+1
	screen.waitVblankStart(15)
	while true do
		if Controls.read():r() then
			break
		end
	end
end

function enableDebug()
	debugEnabled=true
end

function disableDebug()
	debugEnabled=false
end

function enableFDebug()
	fDebugEnabled=true
end

function disableFDebug()
	fDebugEnabled=false
end

function fdebug(message)
	if fDebugEnabled then
		debugFile=io.open(debugFileName, "a+")
		debugFile:write(message.."\r\n")
		debugFile:flush()
		debugFile:close()
	end
end

