debug: xpcall entire program body, write crash log to .crash.log

Error is now saved to disk even if the window closes instantly.
Uses xpcall with debug.traceback for full stack trace.
This commit is contained in:
MayaTheShy
2026-03-22 18:47:15 -04:00
parent 82d74a01b5
commit a0740b81f5

View File

@@ -15,6 +15,17 @@
local _baseDir = fs.getDir(shell.getRunningProgram())
local function _path(rel) return fs.combine(_baseDir, rel) end
-- Write crash info to a file so we can always read it
local function _crashLog(err)
local f = fs.open(_path(".crash.log"), "w")
if f then
f.write(tostring(err) .. "\n" .. (debug and debug.traceback and debug.traceback() or ""))
f.close()
end
end
local ok, err = xpcall(function()
-- Override dofile to load modules into our _ENV so they inherit
-- Opus's require/package (CC:Tweaked dofile uses _G instead).
local _ccDofile = dofile
@@ -685,9 +696,16 @@ local function main()
)
end
local ok, err = pcall(main)
main()
end, function(e) return tostring(e) .. "\n" .. debug.traceback() end)
if not ok then
_crashLog(err)
printError(tostring(err))
print("")
print("Crash log saved to: " .. _path(".crash.log"))
print("")
print("Press any key to exit...")
os.pullEvent("key")
os.pullEvent("char")
end