Slide 11
Slide 11 text
In-Code Debugging
In-Code Debugging
Patterns
Patterns
Conditional Debug Function
Usage Examples
Stack Trace Capture
Fast Iteration Development
-- Conditional debug output (toggle ON/OFF as needed)
local DEBUG = true
local function Debug(...)
if DEBUG then
if ViragDevTool_AddData then
ViragDevTool_AddData({...}, "MyAddon")
else
print("|cFF00FF00[MyAddon]|r", ...)
end
end
end
Define conditional debug functions at the top of your addon and simply set
DEBUG = false before release to disable all debug messages.
BEST PRACTICE
Debug("Player name:", UnitName("player"))
Debug("Status:", {health=100, mana=50})
-- Stack trace function
function GetStackTrace()
return debugstack(2) -- Skip current stack frame
end
-- Record detailed information when errors occur
local success, err = pcall(function()
-- Risky operation
-- local n = nil + 1 -- Intentional error
end)
if not success then
Debug("Error:", err)
Debug("Stack trace:", GetStackTrace())
end
Use chat command /reload or /rl to reload the entire UI
•
Test addon code changes without restarting the game
•
Be aware of PLAYER_LOGOUT event firing if you have
SavedVariables
•
Inspect the _G table during debugging to identify unexpected global
variable leaks:
ViragDevTool_AddData(_G, "Global Variables")
TIP