API:Event Debugging
From TheWarWiki
Contents |
WAR API Help
Sometimes when you're hacking on an addon, you expect certain events to fire (or you have no idea what event would fire), and would like to see what events are firing so you can track it down.
Here's some useful code to figure it out:
for event in pairs(SystemData.Events) do -- iterate through, getting the name of all events
_G["EventDebug_" .. event] = function(...)
pprint(event, ...) -- pretty print function
end
RegisterEventHandler(SystemData.Events[event], "EventDebug_" .. event)
end
The pretty print function is from Example Pretty Print
Alternative Debugging Aid
An alternative to the debugging script. This simply does not print the most common event that was causing the output to be useless. This doesn't require any additional libraries, and if you would like to remove additional events just remove that specific event by name. I found just this one event being removed made it very usable.
for event in pairs(SystemData.Events) do -- iterate through, getting the name of all events
if event ~= "UPDATE_PROCESSED" then
_G["EventDebug_" .. event] = function(...)
d(event .. ": " .. table.concat({...}, ", "))
end
RegisterEventHandler(SystemData.Events[event], "EventDebug_" .. event)
end
end
This will print to the debug window.
Debugging Events by Macro with a Blacklist
Based on the contributions above, here is a set of macros for basic event debugging and display.
-- macro "in_table", defines a function that should be in lua but isn't
/script function in_table (e, t) for _,v in pairs(t) do if (v==e) then return true end end return false end
-- MACRO "EventBlackList", defines table for excluded events
/script EventBlacklist = {"UPDATE_PROCESSED", "PLAYER_POSITION_UPDATED", "L_BUTTON_DOWN_PROCESSED",
"R_BUTTON_DOWN_PROCESSED", "L_BUTTON_UP_PROCESSED", "R_BUTTON_UP_PROCESSED"}
-- MACRO "EventDebug", defines functions for printing the event messages and registers them, if not on EventBlacklist
/script for event in pairs(SystemData.Events) do if not in_table(event, EventBlacklist) then
_G["EventDebug_" .. event] = function(...) DevUtils.Print(event, ...) end
RegisterEventHandler(SystemData.Events[event], "EventDebug_" .. event) end end
The original prettyprint got bundled by ckknight into DevUtils, so this macro set uses DevUtils to print.
Yet Another Debugging Aid for Events, By Inclusion
Sometimes there are very specific events that you want debugged. Sometimes there are a number of events that share the same prefix or suffix that you want debugged. This function, which can be called from an addon or macro script, allows you to get those messages printed to the debug window.
local function EventDebug_AllWithString(t)
for event in pairs(SystemData.Events) do -- iterate through, getting the name of all events
if string.find(event, t) then
_G["EventDebug_" .. event] = function(...)
d(event .. ": " .. table.concat({...}, ", "))
end
RegisterEventHandler(SystemData.Events[event], "EventDebug_" .. event)
end
end
end
Hook-based Debugging Aid
Sometimes the handlers for state change or update get called without an event being fired, or sometimes you just want to keep track of every call to a function, for whatever reason. This functionality can be coded for, but is beyond the scope of this article. Contact BotanicalPuppet about his code that does this (possibly to be included in future developer utilities) or scour current projects (most of them on CurseForge) for similar code.
