再优化报错机制,简化代码

This commit is contained in:
MrZ626
2021-03-19 13:14:18 +08:00
parent 0e0d88d755
commit 20cab9001b
2 changed files with 40 additions and 59 deletions

View File

@@ -356,62 +356,10 @@ local function secondLoopThread()
repeat yield()until mainLoop() repeat yield()until mainLoop()
end end
function love.errorhandler(msg) function love.errorhandler(msg)
if LOADED then
--Generate error message --Generate error message
local errData={} local err={"Error:"..msg}
errData.mes={"Error:"..msg}
local c=2 local c=2
for l in debug.traceback("",2):gmatch("(.-)\n")do for l in debug.traceback("",2):gmatch("(.-)\n")do
if c>2 then
if not l:find("boot")then
errData.mes[c]=l:gsub("^\t*","")
c=c+1
end
else
errData.mes[2]="Traceback"
c=3
end
end
local errMes=table.concat(errData.mes,"\n",1,c-2)
DBP(errMes)
--Write messages to log file
love.filesystem.append("conf/error.log",
os.date("%Y/%m/%d %A %H:%M:%S\n")..
#ERRDATA.." crash(es) "..SYSTEM.."-"..VERSION_NAME.." scene: "..(SCN and SCN.cur or"NULL").."\n"..
errMes.."\n\n"
)
ins(ERRDATA,errData)
--Force quit if error too much
if #ERRDATA>=5 then return end
--Get screencapture
BG.set("none")
love.audio.stop()
gc.reset()
gc.captureScreenshot(function(_)errData.shot=gc.newImage(_)end)
gc.present()
--Create a new mainLoop thread to keep game alive
local status,resume=coroutine.status,coroutine.resume
local loopThread=coroutine.create(secondLoopThread)
repeat
local res,err=resume(loopThread)
if not res then
love.errorhandler(err)
return
end
until status(loopThread)=="dead"
else
ms.setVisible(true)
love.audio.stop()
local err={"Error:"..msg}
local trace=debug.traceback("",2)
local c=2
for l in trace:gmatch("(.-)\n")do
if c>2 then if c>2 then
if not l:find("boot")then if not l:find("boot")then
err[c]=l:gsub("^\t*","") err[c]=l:gsub("^\t*","")
@@ -422,14 +370,47 @@ function love.errorhandler(msg)
c=3 c=3
end end
end end
DBP(table.concat(err,"\n"),1,c-2) DBP(table.concat(err,"\n",1,c-2))
--Reset something
love.audio.stop()
gc.reset() gc.reset()
if LOADED and #ERRDATA<5 then
BG.set("none")
ERRDATA[#ERRDATA+1]={mes=err}
--Write messages to log file
love.filesystem.append("conf/error.log",
os.date("%Y/%m/%d %A %H:%M:%S\n")..
#ERRDATA.." crash(es) "..SYSTEM.."-"..VERSION_NAME.." scene: "..(SCN and SCN.cur or"NULL").."\n"..
table.concat(err,"\n",1,c-2).."\n\n"
)
--Get screencapture
gc.captureScreenshot(function(_)ERRDATA[#ERRDATA].shot=gc.newImage(_)end)
gc.present()
--Create a new mainLoop thread to keep game alive
local status,resume=coroutine.status,coroutine.resume
local loopThread=coroutine.create(secondLoopThread)
local res,threadErr
repeat
res,threadErr=resume(loopThread)
until status(loopThread)=="dead"
if not res then
love.errorhandler(threadErr)
return
end
else
ms.setVisible(true)
gc_setColor(1,1,1) gc_setColor(1,1,1)
SFX.fplay("error",SETTING and SETTING.voc*.8 or 0) local errorMsg
errorMsg=LOADED and
local errorMsg=text and text.errorMsg or"An error has occurred during loading.\nError info has been created, and you can send it to the author." "An error has occurred during loading.\nError info has been created, and you can send it to the author."or
return function() "Much errors occured"
while true do
love.event.pump() love.event.pump()
for E,a,b in love.event.poll()do for E,a,b in love.event.poll()do
if E=="quit"or a=="escape"then if E=="quit"or a=="escape"then
@@ -442,14 +423,14 @@ function love.errorhandler(msg)
gc.clear(.3,.5,.9) gc.clear(.3,.5,.9)
gc_push("transform") gc_push("transform")
gc.replaceTransform(xOy) gc.replaceTransform(xOy)
setFont(100)gc_print(":(",100,40,0,1.2) setFont(100)gc_print(":(",100,0,0,1.2)
setFont(40)gc.printf(errorMsg,100,200,SCR.w0-100) setFont(40)gc.printf(errorMsg,100,160,SCR.w0-100)
setFont(20) setFont(20)
gc_print(SYSTEM.."-"..VERSION_NAME,100,660) gc_print(SYSTEM.."-"..VERSION_NAME,100,660)
gc.printf(err[1],626,380,1260-626) gc.printf(err[1],450,380,1260-626)
gc_print("TRACEBACK",626,450) gc_print("TRACEBACK",450,450)
for i=4,#err-2 do for i=4,#err-2 do
gc_print(err[i],626,400+20*i) gc_print(err[i],450,400+20*i)
end end
gc_pop() gc_pop()
gc_present() gc_present()

View File

@@ -19,7 +19,7 @@ function scene.draw()
gc.clear(BGcolor) gc.clear(BGcolor)
gc.setColor(1,1,1) gc.setColor(1,1,1)
gc.draw(errorShot,100,345,nil,512/errorShot:getWidth(),288/errorShot:getHeight()) gc.draw(errorShot,100,345,nil,512/errorShot:getWidth(),288/errorShot:getHeight())
setFont(100)gc.print(":(",100,00,0,1.2) setFont(100)gc.print(":(",100,0,0,1.2)
setFont(40)gc.printf(errorText,100,160,SCR.w0-100) setFont(40)gc.printf(errorText,100,160,SCR.w0-100)
setFont(20) setFont(20)
gc.print(stateInfo,100,640) gc.print(stateInfo,100,640)