From 8040ea2fc0379000a4d0ceeee6aebbe8a632049b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tam=C3=A1s=20B=C3=A1lint=20Misius?= Date: Wed, 13 Sep 2023 09:08:51 +0200 Subject: [PATCH] Improve error logging further I relearned how to use xpcall, yay. --- client.lua | 32 +++++++++++++---------- tptmp/client/client.lua | 56 ++++++++++++++++++++++------------------- tptmp/client/init.lua | 20 ++++++++------- 3 files changed, 60 insertions(+), 48 deletions(-) diff --git a/client.lua b/client.lua index 05558f2..02320ea 100755 --- a/client.lua +++ b/client.lua @@ -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()) @@ -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) @@ -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 @@ -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__) @@ -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) diff --git a/tptmp/client/client.lua b/tptmp/client/client.lua index 3da666b..9454f39 100644 --- a/tptmp/client/client.lua +++ b/tptmp/client/client.lua @@ -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) @@ -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") @@ -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 diff --git a/tptmp/client/init.lua b/tptmp/client/init.lua index ea97e4d..3a5330c 100644 --- a/tptmp/client/init.lua +++ b/tptmp/client/init.lua @@ -101,6 +101,7 @@ local function run() end local last_trace_str + local handle_error local should_reconnect_at local cli @@ -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 @@ -218,7 +220,7 @@ 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"))) @@ -226,7 +228,7 @@ local function run() 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")