-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDebug.lua
More file actions
executable file
·181 lines (152 loc) · 5.12 KB
/
Debug.lua
File metadata and controls
executable file
·181 lines (152 loc) · 5.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
local ADDON_NAME, ADDON_VARS = ...
-------------------------------------------------------------------------------
-- Module Loading / Exporting
-------------------------------------------------------------------------------
---@class Debuggers -- IntelliJ-EmmyLua annotation
---@field error Debug always shown, highest priority messages
---@field warn Debug end user visible messages
---@field info Debug dev dev messages
---@field trace Debug tedious dev messages
---@class DebugLevel -- IntelliJ-EmmyLua annotation
local DEBUG_LEVEL = {
ALL_MSGS = 0,
ALL = 0,
TRACE = 2,
INFO = 4,
WARN = 6,
ERROR = 8,
NONE = 10,
}
---@class Debug -- IntelliJ-EmmyLua annotation
local Debug = { }
ADDON_VARS.Debug = Debug
ADDON_VARS.DEBUG_LEVEL = DEBUG_LEVEL
-------------------------------------------------------------------------------
-- Constants
-------------------------------------------------------------------------------
local ERR_MSG = "DEBUGGER SYNTAX ERROR: invoke as debugInfo:func() not debugInfo.func()"
-------------------------------------------------------------------------------
-- Functions / Methods
-------------------------------------------------------------------------------
local function isDebuggerObj(zelf)
return zelf and zelf.isDebug
end
local function newInstance(isSilent)
local newInstance = {
isSilent = isSilent,
isDebug = true
}
setmetatable(newInstance, { __index = Debug })
return newInstance
end
---@return Debuggers -- IntelliJ-EmmyLua annotation
function Debug:newDebugger(showOnlyMessagesAtOrAbove)
local isValidNoiseLevel = type(showOnlyMessagesAtOrAbove) == "number"
assert(isValidNoiseLevel, "Debugger:newDebugger() Invalid Noise Level: '".. tostring(showOnlyMessagesAtOrAbove) .."'")
local debugger = { }
debugger.error = newInstance(showOnlyMessagesAtOrAbove > DEBUG_LEVEL.ERROR)
debugger.warn = newInstance(showOnlyMessagesAtOrAbove > DEBUG_LEVEL.WARN)
debugger.info = newInstance(showOnlyMessagesAtOrAbove > DEBUG_LEVEL.INFO)
debugger.trace = newInstance(showOnlyMessagesAtOrAbove > DEBUG_LEVEL.TRACE)
return debugger
end
function Debug:new(...)
local d = self:newDebugger(...)
return d.trace, d.info, d.warn, d.error
end
function Debug:isMute()
assert(isDebuggerObj(self), ERR_MSG)
return self.isSilent
end
function Debug:isActive()
assert(isDebuggerObj(self), ERR_MSG)
return not self.isSilent
end
function Debug:dump(...)
assert(isDebuggerObj(self), ERR_MSG)
if self.isSilent then return end
DevTools_Dump(...)
end
function Debug:print(...)
assert(isDebuggerObj(self), ERR_MSG)
if self.isSilent then return end
print(...)
end
function table.pack(...)
return { n = select("#", ...), ... }
end
function Debug:out(indentChar, indentWidth, label, ...)
assert(isDebuggerObj(self), ERR_MSG)
if self.isSilent then return end
local indent = string.rep(indentChar or "#", indentWidth or 40)
--local args = { ... } -- this may be where the nils are getting shortchanged
local args = table.pack(...)
local out = { indent, " ", label or "", " " }
--for i,v in ipairs(args) do
for i=1,args.n do
local v = args[i]
local isOdd = i%2 == 1
if isOdd then
-- table.insert(out, " .. ")
table.insert(out, self:asString(v))
else
table.insert(out, ": ")
table.insert(out, self:asString(v))
if i~= args.n then
table.insert(out, " .. ")
end
end
end
local str = table.concat(out,"")
print(str)
end
local function getName(obj, default)
assert(isDebuggerObj(self), ERR_MSG)
if(obj and obj.GetName) then
return obj:GetName() or default or "UNKNOWN"
end
return default or "UNNAMED"
end
function Debug:messengerForEvent(eventName, msg)
assert(isDebuggerObj(self), ERR_MSG)
return function(obj)
if self.isSilent then return end
print(getName(obj,eventName).." said ".. msg .."! ")
end
end
function Debug:makeDummyStubForCallback(obj, eventName, msg)
assert(isDebuggerObj(self), ERR_MSG)
self:print("makeDummyStubForCallback for " .. eventName)
obj:RegisterEvent(eventName);
obj:SetScript("OnEvent", self:messengerForEvent(eventName,msg))
end
function Debug:run(callback)
assert(isDebuggerObj(self), ERR_MSG)
if self.isSilent then return end
callback()
end
function Debug:dumpKeys(object)
assert(isDebuggerObj(self), ERR_MSG)
if self.isSilent then return end
if not object then
self:print("NiL")
return
end
local isNumeric = true
for k,v in pairs(object) do
if (type(k) ~= "number") then isNumeric = false end
end
local keys = {}
for k, v in pairs(object or {}) do
local key = isNumeric and k or self:asString(k)
table.insert(keys,key)
end
table.sort(keys)
for i, k in ipairs(keys) do
self:print(k.." <-> ".. self:asString(object[k]))
end
end
function Debug:asString(v)
assert(isDebuggerObj(self), ERR_MSG)
return ((v==nil)and"nil") or ((type(v) == "string") and v) or tostring(v) -- or
end