Skip to content

Commit

Permalink
Improve error logging further
Browse files Browse the repository at this point in the history
I relearned how to use xpcall, yay.
  • Loading branch information
LBPHacker committed Sep 13, 2023
1 parent 491e4dc commit 8040ea2
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 48 deletions.
32 changes: 19 additions & 13 deletions client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,17 @@ if OUTPUT then
end
end

local env = setmetatable({}, { __index = function(_, key)
return rawget(_G, key) or error("__index on env: " .. tostring(key), 2)
local env__ = setmetatable({}, { __index = function(_, key)
error("__index on env: " .. tostring(key), 2)
end, __newindex = function(_, key)
error("__newindex on env: " .. tostring(key), 2)
end })
local _ENV = env
for key, value in pairs(_G) do
rawset(env__, key, value)
end
local _ENV = env__
if rawget(_G, "setfenv") then
setfenv(1, env)
setfenv(1, env__)
end

math.randomseed(os.time())
Expand Down Expand Up @@ -77,8 +80,8 @@ local function xpcall_wrap(func, handler)
if handler then
handler(err)
end
print(err)
print(debug.traceback())
print(debug.traceback(err, 2))
return err
end)
if oargs then
return unpackn(oargs)
Expand All @@ -105,13 +108,13 @@ local function require(modname)
if rawget(_G, "setfenv") then
func, err = loadstring(content, "=" .. relative)
else
func, err = load(content, "=" .. relative, "bt", env)
func, err = load(content, "=" .. relative, "bt", env__)
end
if not func then
error(err, 0)
end
if rawget(_G, "setfenv") then
setfenv(func, env)
setfenv(func, env__)
end
local ok = true
local err_outer
Expand All @@ -135,18 +138,21 @@ local function require(modname)
end
return loaded[modname]
end
rawset(env, "require", require)
rawset(env, "xpcall_wrap", xpcall_wrap)
rawset(env__, "require", require)
rawset(env__, "xpcall_wrap", xpcall_wrap)

local main_module = require(MAIN_MODULE)
if OUTPUT then
local handle = assert(io.open(OUTPUT, "w"))
handle:write([[
local env__ = setmetatable({}, { __index = function(_, key)
return rawget(_G, key) or error("__index on env: " .. tostring(key), 2)
error("__index on env: " .. tostring(key), 2)
end, __newindex = function(_, key)
error("__newindex on env: " .. tostring(key), 2)
end })
for key, value in pairs(_G) do
rawset(env__, key, value)
end
local _ENV = env__
if rawget(_G, "setfenv") then
setfenv(1, env__)
Expand Down Expand Up @@ -183,8 +189,8 @@ local function xpcall_wrap(func, handler)
if handler then
handler(err)
end
print(err)
print(debug.traceback())
print(debug.traceback(err, 2))
return err
end)
if oargs then
return unpackn(oargs)
Expand Down
56 changes: 30 additions & 26 deletions tptmp/client/client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1059,7 +1059,10 @@ function client_i:start()
handler(self)
end
end, function(err)
print(debug.traceback(err, 2))
if self.handle_error_func_ then
self.handle_error_func_(err)
end
return err
end)
if not ok then
error(err)
Expand Down Expand Up @@ -1105,7 +1108,7 @@ function client_i:tick_resume_()
local ok, err = coroutine.resume(self.proto_coro_)
if not ok then
self.proto_coro_ = nil
error(err)
error("proto coroutine: " .. err, 0)
end
if self.proto_coro_ and coroutine.status(self.proto_coro_) == "dead" then
error("proto coroutine terminated")
Expand Down Expand Up @@ -1414,31 +1417,32 @@ end
local function new(params)
local now = socket.gettime()
return setmetatable({
host_ = params.host,
port_ = params.port,
secure_ = params.secure,
event_log_ = params.event_log,
backlog_ = params.backlog,
rx_ = buffer_list.new({ limit = config.recvq_limit }),
tx_ = buffer_list.new({ limit = config.sendq_limit }),
connecting_since_ = now,
last_ping_sent_at_ = now,
last_ping_received_at_ = now,
status_ = "ready",
window_ = params.window,
profile_ = params.profile,
localcmd_ = params.localcmd,
initial_room_ = params.initial_room,
set_id_func_ = params.set_id_func,
get_id_func_ = params.get_id_func,
set_qa_func_ = params.set_qa_func,
get_qa_func_ = params.get_qa_func,
log_event_func_ = params.log_event_func,
should_reconnect_func_ = params.should_reconnect_func,
host_ = params.host,
port_ = params.port,
secure_ = params.secure,
event_log_ = params.event_log,
backlog_ = params.backlog,
rx_ = buffer_list.new({ limit = config.recvq_limit }),
tx_ = buffer_list.new({ limit = config.sendq_limit }),
connecting_since_ = now,
last_ping_sent_at_ = now,
last_ping_received_at_ = now,
status_ = "ready",
window_ = params.window,
profile_ = params.profile,
localcmd_ = params.localcmd,
initial_room_ = params.initial_room,
set_id_func_ = params.set_id_func,
get_id_func_ = params.get_id_func,
set_qa_func_ = params.set_qa_func,
get_qa_func_ = params.get_qa_func,
log_event_func_ = params.log_event_func,
handle_error_func_ = params.handle_error_func,
should_reconnect_func_ = params.should_reconnect_func,
should_not_reconnect_func_ = params.should_not_reconnect_func,
id_to_member = {},
nick_colour_seed_ = 0,
fps_sync_ = false,
id_to_member = {},
nick_colour_seed_ = 0,
fps_sync_ = false,
}, client_m)
end

Expand Down
20 changes: 11 additions & 9 deletions tptmp/client/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ local function run()
end

local last_trace_str
local handle_error

local should_reconnect_at
local cli
Expand Down Expand Up @@ -163,13 +164,14 @@ local function run()
end,
new_client_func = function(params)
should_reconnect_at = nil
params.window = win
params.profile = prof
params.set_id_func = set_id
params.get_id_func = get_id
params.set_qa_func = set_qa
params.get_qa_func = get_qa
params.log_event_func = log_event
params.window = win
params.profile = prof
params.set_id_func = set_id
params.get_id_func = get_id
params.set_qa_func = set_qa
params.get_qa_func = get_qa
params.log_event_func = log_event
params.handle_error_func = handle_error
params.should_reconnect_func = function()
should_reconnect = true
end
Expand Down Expand Up @@ -218,15 +220,15 @@ local function run()
end
end

local function handle_error(err)
function handle_error(err)
if not last_trace_str then
local handle = io.open(config.trace_path, "wb")
handle:write(("TPTMP %s %s\n"):format(config.versionstr, os.date("!%Y-%m-%dT%H:%M:%SZ")))
handle:close()
win:backlog_push_error("An error occurred and its trace has been saved to " .. config.trace_path .. "; please find this file in your data folder and attach it when reporting this to developers")
win:backlog_push_error("Top-level error: " .. tostring(err))
end
local str = tostring(err) .. "\n" .. debug.traceback() .. "\n"
local str = debug.traceback(err, 2) .. "\n"
if last_trace_str ~= str then
last_trace_str = str
local handle = io.open(config.trace_path, "ab")
Expand Down

0 comments on commit 8040ea2

Please sign in to comment.