-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDebug.lua
More file actions
executable file
·145 lines (126 loc) · 3.94 KB
/
Debug.lua
File metadata and controls
executable file
·145 lines (126 loc) · 3.94 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
local ADDON_NAME, ADDON_VARS = ...
local ERR_MSG = "DEBUGGER SYNTAX ERROR: invoke via:func() not via.func()"
local CONSTANTS = {
ALL_MSGS = 0,
ALL = 0,
TRACE = 2,
INFO = 4,
WARN = 6,
ERROR = 8,
NONE = 10,
}
ADDON_VARS.DEBUG = CONSTANTS
local Debug = { }
local function isDebuggerObj(zelf)
return zelf and zelf.DEBUGGER
end
local function newInstance(isSilent)
local newInstance = {
isSilent = isSilent,
DEBUGGER = true
}
setmetatable(newInstance, { __index = Debug })
return newInstance
end
function CONSTANTS.newDebugger(showOnlyMessagesAtOrAbove)
local isValidNoiseLevel = type(showOnlyMessagesAtOrAbove) == "number"
assert(isValidNoiseLevel, "Debugger:newDebugger() Invalid Noise Level: '".. tostring(showOnlyMessagesAtOrAbove) .."'")
local debugger = { }
debugger.error = newInstance(showOnlyMessagesAtOrAbove > CONSTANTS.ERROR)
debugger.warn = newInstance(showOnlyMessagesAtOrAbove > CONSTANTS.WARN)
debugger.info = newInstance(showOnlyMessagesAtOrAbove > CONSTANTS.INFO)
debugger.trace = newInstance(showOnlyMessagesAtOrAbove > CONSTANTS.TRACE)
return debugger
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,indentWidth)
--local args = { ... } -- this may be where the nils are getting shortchanged
local args = table.pack(...)
local out = { indent, " ", label, " " }
--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
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