forked from b-morgan/Skillet-Classic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DebugAids.lua
510 lines (483 loc) · 13.6 KB
/
DebugAids.lua
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
local addonName,addonTable = ...
local isRetail = WOW_PROJECT_ID == WOW_PROJECT_MAINLINE
local isClassic = WOW_PROJECT_ID == WOW_PROJECT_CLASSIC
local isBCC = WOW_PROJECT_ID == WOW_PROJECT_BURNING_CRUSADE_CLASSIC
local isWrath = WOW_PROJECT_ID == WOW_PROJECT_WRATH_CLASSIC
local DA
if isRetail then
DA = _G[addonName] -- for DebugAids.lua
else
DA = LibStub("AceAddon-3.0"):GetAddon("Skillet") -- for DebugAids.lua
end
local tek = tekDebug and tekDebug:GetFrame("Skillet")
--
-- Chat and Debugging Aids
--
-- Add the first two lines of this file to all
-- the files in your addon (that use these functions).
-- Add this file (DebugAids.lua) to the .toc for
-- your addon. The functions and variables in this
-- file are added to the global table of the addon.
--
-- Note:
-- For addons using ACE, the file with the
-- "LibStub("AceAddon-3.0"):NewAddon(...)" may need
-- to add a "local DA = <>" after the LibStub with
-- "<>" changed to the name of the addon.
--
-- Incorporate the commented out code at the bottom
-- of this file (or the equivalent) into your addon
-- for run time control of these debugging functions.
--
-- DA.DebugLog is a circular table which has DA.MAXDEBUG entries.
--
-- Setting DA.WarnLog, DA.DebugLogging, or DA.TraceLog
-- will add the respective text to the circular table
-- without printing to the default chat frame.
--
-- Setting DA.WarnShow or DA.TraceShow will cause
-- DA.WARN or DA.TRACE to print their argument(s)
-- to the default chat frame.
--
-- Setting DA.DebugShow will cause DA.DEBUG to print
-- its argument(s) to the default chat frame if
-- its first argument, level, is less than DA.DebugLevel.
-- This level should be a number between 0 and 9. If it
-- is not a number between 0 and 9, it is assumed to
-- be the first argument to be printed.
--
-- DA.DebugLevel should constrained to be a
-- number between 1 and 10.
--
-- DA.LogLevel is a boolean with false meaning log all
-- DA.DEBUG calls regardless of level and true meaning
-- only log calls when they are less than DA.DebugLevel
--
--
DA.WarnShow = false
DA.WarnLog = true
DA.DebugShow = false
DA.DebugLogging = true
DA.DebugLevel = 1
DA.TableDump = false
DA.TraceShow = false
DA.TraceLog = false
DA.TraceLog2 = false
DA.ProfileShow = false
DA.DebugLog = {} -- Add to SavedVariables for debugging
DA.MAXDEBUG = 4000
DA.DebugProfile = {} -- Add to SavedVariables for debugging
DA.MAXPROFILE = 2000
DA.STATUS_COLOR = "|c0033CCFF"
DA.DEBUG_COLOR = "|c00A0FF00"
DA.TRACE_COLOR = "|c0000FFA0"
DA.WARN_COLOR = "|c0000FFE0"
local function tekD(text)
print(text)
if tek then
tek:AddMessage(text)
end
end
function DA.CHAT(text)
print(DA.STATUS_COLOR..addonName..": "..text)
end
--
-- If any logging is enabled, insert text into the debug log
--
function DA.MARK(text)
if DA.WarnLog or DA.DebugLogging or DA.TraceLog then
table.insert(DA.DebugLog,date().."(M): "..text)
if (table.getn(DA.DebugLog) > DA.MAXDEBUG) then
table.remove(DA.DebugLog,1)
end
end
end
--
-- Print and MARK the text
--
function DA.MARK2(text)
print(text)
DA.MARK(text)
end
--
-- CHAT and MARK the text
--
function DA.MARK3(text)
DA.CHAT(text)
DA.MARK(text)
end
function DA.WARN(...)
if not DA.WarnLog and not DA.DebugLogging then return "" end
local text = ""
local comma = ""
for i = 1, select("#", ...), 1 do
if (i > 2) then
comma = ", "
end
local value = select(i,...)
local vtype = type(value)
if (vtype == "nil") then
text = text..comma.."(nil)"
elseif (vtype == "number") then
text = text..comma..tostring(value)
elseif (vtype == "string") then
local t = string.sub(value,1,2)
if t == ", " then
text = text..value
else
text = text..comma..value
end
elseif (vtype == "boolean") then
if (value) then
text = text..comma.."true"
else
text = text..comma.."false"
end
elseif (vtype == "table" or vtype == "function" or vtype == "thread" or vtype == "userdata") then
text = text..comma.."("..vtype..")"
else
text = text..comma.."(unknown)"
end
end
if (DA.WarnShow or DA.DebugShow) then
tekD(DA.WARN_COLOR..addonName..": "..text)
end
table.insert(DA.DebugLog,date().."(W): "..text)
if (table.getn(DA.DebugLog) > DA.MAXDEBUG) then
table.remove(DA.DebugLog,1)
end
end
function DA.DEBUG(...)
if not DA.DebugLogging then return "" end
local k = select("#",...)
local level = select(1, ...)
local text = ""
local comma = ""
local j = 2
local t = type(level)
if (t == "number" and level == -1) then
return
end
if (t ~= "number" or level < 0 or level > 10) then
level = 0 -- assume this is a deprecated call and
j = 1 -- process it as the first parameter.
end
for i = j, k, 1 do
if (i > 2) then
comma = ", "
end
local value = select(i,...)
local vtype = type(value)
if (vtype == "nil") then
text = text..comma.."(nil)"
elseif (vtype == "number") then
text = text..comma..tostring(value)
elseif (vtype == "string") then
t = string.sub(value,1,2)
if t == ", " then
text = text..value
else
text = text..comma..value
end
elseif (vtype == "boolean") then
if (value) then
text = text..comma.."true"
else
text = text..comma.."false"
end
elseif (vtype == "table" or vtype == "function" or vtype == "thread" or vtype == "userdata") then
text = text..comma.."("..vtype..")"
else
text = text..comma.."(unknown)"
end
end
local dlevel = tonumber(DA.DebugLevel) -- sanity check
if not dlevel then dlevel = 1
elseif dlevel < 1 then dlevel = 1
elseif dlevel > 9 then dlevel = 10 end
if (DA.DebugShow and level < dlevel) then
tekD(DA.DEBUG_COLOR..addonName..": "..text)
end
if (not DA.LogLevel or level < dlevel) then
table.insert(DA.DebugLog,date().."(D"..level.."): "..text)
if (table.getn(DA.DebugLog) > DA.MAXDEBUG) then
table.remove(DA.DebugLog,1)
end
end
end
function DA.TRACE2(...)
if not DA.TraceLog2 then return "" end
DA.TRACE(...)
end
function DA.TRACE(...)
if not DA.TraceLog then return "" end
local text = ""
local comma = ""
for i = 1, select("#", ...), 1 do
if (i > 2) then
comma = ", "
end
local value = select(i,...)
local vtype = type(value)
if (vtype == "nil") then
text = text..comma.."(nil)"
elseif (vtype == "number") then
text = text..comma..tostring(value)
elseif (vtype == "string") then
local t = string.sub(value,1,2)
if t == ", " then
text = text..value
else
text = text..comma..value
end
elseif (vtype == "boolean") then
if (value) then
text = text..comma.."true"
else
text = text..comma.."false"
end
elseif (vtype == "table" or vtype == "function" or vtype == "thread" or vtype == "userdata") then
text = text..comma.."("..vtype..")"
else
text = text..comma.."(unknown)"
end
end
if (DA.TraceShow) then
tekD(DA.TRACE_COLOR..addonName..": "..text)
end
table.insert(DA.DebugLog,date().."(T): "..text)
if (table.getn(DA.DebugLog) > DA.MAXDEBUG) then
table.remove(DA.DebugLog,1)
end
end
--
-- Convert a table into a string with line breaks and indents.
-- if specified, m is the maximum recursion depth.
--
function DA.DUMP(o,m,n)
if o and type(o) == 'table' then
if not DA.TableDump then return "{table}" end
local s
local i = ""
if n then
i = string.rep(" ",n)
else
n = 0
end
s = i..'{\n'
for k,v in pairs(o) do
if type(k) ~= 'number' then
k = "'"..k.."'"
end
if m and n > m then
s = s..i..'['..k..'] = {table}\n'
else
s = s..i..'['..k..'] = '..DA.DUMP(v,m,n+1)..'\n'
end
end
return string.gsub(s..i..'}\n',"\n\n","\n")
else
return tostring(o)
end
end
--
-- Convert a table into a one line string.
-- if specified, m is the maximum recursion depth.
--
function DA.DUMP1(o,m,n)
if o and type(o) == 'table' then
if not DA.TableDump then return "{table}" end
local s
if not n then n = 0 end
s = '{ '
for k,v in pairs(o) do
if type(k) ~= 'number' then
k = "'"..k.."'"
end
if m and n > m then
s = s..'['..k..'] = {table}, '
else
s = s..'['..k..'] = '..DA.DUMP1(v,m,n+1)..', '
end
end
if strlen(s) > 2 then
return strsub(s,1,strlen(s)-2)..' }'
else
return s..'}'
end
else
return tostring(o)
end
end
function DA.PROFILE(text)
local stackstring = debugstack(2, 5, 0) -- start, countTop, countBot
local now = date()
table.insert(DA.DebugProfile, now..": "..tostring(text).."\n"..stackstring)
if (table.getn(DA.DebugProfile) > DA.MAXPROFILE) then
table.remove(DA.DebugProfile, 1)
end
if (DA.ProfileShow) then
print(DA.DEBUG_COLOR..addonName..": "..text)
end
if DA.DebugLogging then
table.insert(DA.DebugLog, now.."(P): "..tostring(text))
if (table.getn(DA.DebugLog) > DA.MAXDEBUG) then
table.remove(DA.DebugLog, 1)
end
end
end
--
-- Convert a link into a printable string
--
function DA.PLINK(text)
if text then
return text:gsub('\124','\124\124')
end
return nil
end
function DA.TABLE(text, tab)
if not DA.DebugShow then return "" end
if ViragDevTool_AddData then
ViragDevTool_AddData(tab, addonName..": "..text)
end
end
function DA.DebugAidsStatus()
print("WarnShow= "..tostring(DA.WarnShow)..", WarnLog= "..tostring(DA.WarnLog))
print("DebugShow= "..tostring(DA.DebugShow)..", DebugLogging= "..tostring(DA.DebugLogging)..", DebugLevel= "..tostring(DA.DebugLevel))
print("TraceShow= "..tostring(DA.TraceShow)..", TraceLog= "..tostring(DA.TraceLog)..", TraceLog2= "..tostring(DA.TraceLog2))
print("ProfileShow= "..tostring(DA.ProfileShow))
print("TableDump= "..tostring(DA.TableDump))
print("LogLevel= "..tostring(DA.LogLevel))
print("#DebugLog= "..tostring(#DA.DebugLog).." ("..tostring(DA.MAXDEBUG)..")")
print("#DebugProfile= "..tostring(#DA.DebugProfile).." ("..tostring(DA.MAXPROFILE)..")")
end
--
-- These example functions should be incorporated
-- into the slash command processing function
-- (or somewhere else) in your addon.
--
--[[
function DA.Command(msg)
local _,_,command,options = string.find(msg,"([%w%p]+)%s*(.*)$")
command = string.lower(command)
options = string.lower(options)
if(command == "warn") then
DA.Warn() -- Undocumented: Enable warning output
elseif(command == "wlog") then
DA.WarnL() -- Undocumented: Enable warning logging
elseif(command == "debug") then
DA.Debug() -- Undocumented: Enable debug output
elseif(command == "dlog") then
DA.DebugL() -- Undocumented: Enable debug logging
elseif(command == "dlevel") then
DA.DLevel(options) -- Undocumented: Set debug level
elseif(command == "tdump") then
DA.TDump(options) -- Undocumented: Enable table dumps (recursive functions)
elseif(command == "trace") then
DA.Trace() -- Undocumented: Enable trace output
elseif(command == "tlog") then
DA.TraceL() -- Undocumented: Clear debug storage
elseif(command == "profile") then
DA.Profile() -- Undocumented: Enable trace output
elseif(command == "clearlog") then
DA.ClearDebugLog() -- Undocumented: Clear debug storage
elseif(command == "clearprofile") then
DA.ClearProfileLog() -- Undocumented: Clear debug storage
end
end
--
-- Example Slash Command
-- Replace ? with the appropriate name.
--
function DA.OnLoad()
SLASH_?1 = "/?"
SlashCmdList["?"] = DA.Command
end
]]--
--
-- Slash command functions
--
-- These functions are called from the slash command snippet at
-- the beginning of this file.
--
--[[
function DA.Warn()
DA.WarnShow = not DA.WarnShow
if (DA.WarnShow) then
DA.WARN("Warning output enabled.")
else
DA.CHAT("Warning output disabled.")
end
end
function DA.WarnL()
DA.WarnLog = not DA.WarnLog
if (DA.WarnLog) then
DA.CHAT("Warning logging enabled.")
else
DA.CHAT("Warning logging disabled.")
end
end
function DA.Debug()
DA.DebugShow = not DA.DebugShow
if (DA.DebugShow) then
DA.DEBUG(0,"Debug output enabled.")
else
DA.CHAT("Debug output disabled.")
end
end
function DA.DebugL()
DA.DebugLogging = not DA.DebugLogging
if (DA.DebugLogging) then
DA.CHAT(0,"Debug logging enabled.")
else
DA.CHAT("Debug logging disabled.")
end
end
function DA.DLevel(options)
local dl = tonumber(options)
if not dl then dl = 1
elseif dl < 1 then dl = 1
elseif dl > 9 then dl = 10 end
DA.DebugLevel = dl
end
function DA.TDump()
DA.TableDump= not DA.TableDump
if (DA.TableDump) then
DA.CHAT("Table dump enabled.")
else
DA.CHAT("Table dump disabled.")
end
end
function DA.Trace()
DA.TraceShow = not DA.TraceShow
if (DA.TraceShow) then
DA.TRACE("Trace output enabled.")
else
DA.CHAT("Trace output disabled.")
end
end
function DA.TraceL()
DA.TraceLog = not DA.TraceLog
if (DA.TraceLog) then
DA.CHAT("Trace logging enabled.")
else
DA.CHAT("Trace logging disabled.")
end
end
function DA.Profile()
DA.ProfileShow = not DA.ProfileShow
if (DA.ProfileShow) then
DA.CHAT("Profile output enabled.")
else
DA.CHAT("Profile output disabled.")
end
end
function DA.ClearDebugLog()
DA.CHAT("DebugLog initialized.")
DA.DebugLog = {}
end
function DA.ClearProfileLog()
DA.CHAT("ProfileLog initialized.")
DA.DebugProfile = {}
end
]]--