Changeset

11059:ad89e3cc67b6

util.helpers: when logging events, log individual handler calls
author Matthew Wild <mwild1@gmail.com>
date Thu, 03 Sep 2020 13:00:43 +0100
parents 11058:c99afee1c548
children 11060:19dd9522f107
files util/helpers.lua
diffstat 1 files changed, 15 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/util/helpers.lua	Thu Sep 03 12:59:43 2020 +0100
+++ b/util/helpers.lua	Thu Sep 03 13:00:43 2020 +0100
@@ -23,12 +23,27 @@
 		logger("debug", "%s firing event: %s", name, event);
 		return f(event, ...);
 	end
+
+	local function event_handler_hook(handler, event_name, event_data)
+		logger("debug", "calling handler for %s: %s", event_name, handler);
+		local ok, ret = pcall(handler, event_data);
+		if not ok then
+			logger("error", "error in event handler %s: %s", handler, ret);
+			error(ret);
+		end
+		if ret ~= nil then
+			logger("debug", "event chain ended for %s by %s with result: %s", event_name, handler, ret);
+		end
+		return ret;
+	end
+	events.set_debug_hook(event_handler_hook);
 	events[events.fire_event] = f;
 	return events;
 end
 
 local function revert_log_events(events)
 	events.fire_event, events[events.fire_event] = events[events.fire_event], nil; -- :))
+	events.set_debug_hook(nil);
 end
 
 local function log_host_events(host)