lua中怎么捕获错误异常信息
展开全部
xpcall 为什么这个方法不能满足需求 你使用xpcall 不是可以输出异常信息吗 然后你把debug.traceback()的信息写入文件不是就行了
main.lua中__G__TRACKBACK__里插入
可以将错误日志写入文件
也可以上报到服务器
package.path = package.path .. ";.app/protobuf/?.lua;./scripts/app/protobuf/?.lua;"
package.cpath = package.cpath .. ';../protobuf/?.so'
print(package.path)
function __G__TRACKBACK__(errorMessage)
print("__G__TRACKBACK__111")
local path = CCFileUtils:sharedFileUtils():getWritablePath().."log.txt"
print(path)
io.writefile(path, "\r\n"..errorMessage, "a+b")
if "ASSERT FAILED ON LUA EXECUTE: CCHTTPRequest::getResponseString() - request not completed" ~= tostring(errorMessage) and
"ASSERT FAILED ON LUA EXECUTE: Request not completed" ~= tostring(errorMessage) then
print("----------------------------------------")
print("LUA ERROR: " .. tostring(errorMessage) .. "\n")
print(debug.traceback("", 2))
print("----------------------------------------")
end
end
local function startGame( ... )
collectgarbage("setpause", 100)
collectgarbage("setstepmul", 5000)
require("app.MyApp").new():run()
end
xpcall(startGame,__G__TRACKBACK__)
startGame()
main.lua中__G__TRACKBACK__里插入
可以将错误日志写入文件
也可以上报到服务器
package.path = package.path .. ";.app/protobuf/?.lua;./scripts/app/protobuf/?.lua;"
package.cpath = package.cpath .. ';../protobuf/?.so'
print(package.path)
function __G__TRACKBACK__(errorMessage)
print("__G__TRACKBACK__111")
local path = CCFileUtils:sharedFileUtils():getWritablePath().."log.txt"
print(path)
io.writefile(path, "\r\n"..errorMessage, "a+b")
if "ASSERT FAILED ON LUA EXECUTE: CCHTTPRequest::getResponseString() - request not completed" ~= tostring(errorMessage) and
"ASSERT FAILED ON LUA EXECUTE: Request not completed" ~= tostring(errorMessage) then
print("----------------------------------------")
print("LUA ERROR: " .. tostring(errorMessage) .. "\n")
print(debug.traceback("", 2))
print("----------------------------------------")
end
end
local function startGame( ... )
collectgarbage("setpause", 100)
collectgarbage("setstepmul", 5000)
require("app.MyApp").new():run()
end
xpcall(startGame,__G__TRACKBACK__)
startGame()
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询