Repository navigation
Expand file tree
/
Copy pathSFUtils_Logger.lua
More file actions
346 lines (287 loc) · 12.2 KB
/
Copy pathSFUtils_Logger.lua
File metadata and controls
346 lines (287 loc) · 12.2 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
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
local sfutil = LibSFUtils
assert(sfutil, "LibSFUtils_Global must be loaded before this file")
-- logger object that prints to chat
-- activePrintDebug is the functional print-to-chat logger implementation.
-- It can be used directly (in place of LibDebugLogger) and therefore performs
-- its own enabled-state checks.
-- printDebug also uses these methods as its enabled-state implementations.
local activePrintDebug = {
Error = function(self,...)
if not self.enabled then return end
print("["..self.addonName.."] ERROR: "..string.format(...))
end,
Warn = function(self,...)
if not self.enabled then return end
print("["..self.addonName.."] WARN: "..string.format(...))
end,
Info = function(self,...)
if not self.enabled then return end
print("["..self.addonName.."] INFO: "..string.format(...))
end,
Debug = function(self,...)
if not self.enabled or not self.SFenableDebug then return end
print("["..self.addonName.."] DEBUG: "..string.format(...))
end,
Create = function(self,name)
local o = setmetatable({}, { __index = self})
o.addonName = name
return o
end,
SetEnabled = function(self,truefalse) self.enabled = truefalse end,
}
-- logger object that does not print anywhere
-- Use nilPrintDebug:Create() or sfutil.CreateNilLogger() to create a logger instance of this type
-- nilPrintDebug is the disabled/no-op implementation table.
local nilPrintDebug = {
Error = function(self,...) end,
Warn = function(self,...) end,
Info = function(self,...) end,
Debug = function(self,...) end,
Create = function(self,name)
local o = setmetatable({}, { __index = self})
o.addonName = name
return o
end,
SetEnabled=function(self,truefalse) self.enabled = truefalse end,
}
-- helper for the printDebug table below (and the CreateLogger())
local function UpdateDebugMethod(self)
if self.enabled and self.SFenableDebug then
if self.SFsvDebugFn == nil then
self.Debug = activePrintDebug.Debug
else
self.Debug = self.SFsvDebugFn
end
else
self.Debug = nilPrintDebug.Debug
end
end
-- logger object that prints to chat when enabled
-- Use Create() to create a logger instance of this type
-- printDebug is the prototype/factory and state-management table.
local printDebug = {
Error = activePrintDebug.Error,
Warn = activePrintDebug.Warn,
Info = activePrintDebug.Info,
Debug = activePrintDebug.Debug,
Create = activePrintDebug.Create,
SetEnabled = function(self, tf)
if self.enabled == tf then return end
self.enabled = tf
if tf then
self.Error = activePrintDebug.Error
self.Warn = activePrintDebug.Warn
self.Info = activePrintDebug.Info
else
self.Error = nilPrintDebug.Error
self.Warn = nilPrintDebug.Warn
self.Info = nilPrintDebug.Info
end
UpdateDebugMethod(self)
end,
}
--[[ Returns a function to return a logger object (creating it if necessary first).
If a logger gets created, it will be enabled by default, and its DEBUG-level
messages will be disabled by default. in the log.
When disabled, no messages are WRITTEN to the log from this addon.
Note: Enabling debug-level messages with SetDebug(true) may (probably will) cause
some lagging in the game and tons of messages in the log viewer.
Note: If you want to use this with LibDebugLogger, you have to require the LibDebuggLogger library in
your addon's manifest!
namespace = the table that you want the logger object stored into. If this
is not a table, we assume it is the name of the table that is found in _G.
If it is a string, it will be used for the addonname parameter and the name of the
table to find (or create) in _G.
loggervar = the name of the logger instance to use.
addonname = the name of the addon this logger object is associated with.
The function returned by this function runs quicker than sfutil.SafeLogger() because
the preliminary safety checking is done only before the function is created - not every
time it is run.
This function allows you to call the logger without having to worry about it having been
created yet.
Example of use:
MyAddon_Logger = sfutil.SafeLoggerFunction( MyAddon, "logger", MyAddon.name)
...
MyAddon_Logger():SetDebug(true)
MyAddon_Logger():Info("This is a test of the Emergency Broadcast System.")
MyAddon_Logger():Debug("This is only a test.")
--]]
function sfutil.SafeLoggerFunction(namespace, loggervar, addonname)
if type(namespace) ~= "table" then
addonname = namespace
namespace = _G[addonname]
if not namespace then
_G[addonname] = {}
namespace = _G[addonname]
end
end
return function()
if not namespace[loggervar] then
local mylogger
mylogger = sfutil.Createlogger(addonname)
mylogger:SetEnabled(true)
namespace[loggervar] = mylogger
end
return namespace[loggervar]
end
end
--[[ Returns a logger object (creating it if necessary first)
Parameters:
namespace = the table that you want the logger object stored into. Typically the
namespace table for your addon that you use - such as MyAddon = {}
If this is not a table, we assume it is the name of the table that is found in _G.
If it is a string, it will be used for the addonname parameter and the name of the
table to find (or create) in _G.
loggervar = the name of the logger instance to use.
addonname = the name of the addon this logger object is associated with.
Recommend that you create a specific logger function using sfutils.SafeLoggerFunction()
instead of using this function.
--]]
function sfutil.SafeLogger(namespace, loggervar, addonname)
if type(namespace) ~= "table" then
addonname = namespace
namespace = _G[addonname]
if not namespace then
_G[addonname] = {}
namespace = _G[addonname]
end
end
local mylogger = namespace[loggervar]
if not mylogger then
mylogger = sfutil.Createlogger(addonname)
mylogger:SetEnabled(true)
namespace[loggervar] = mylogger
end
return mylogger
end
local function SetDebug(self, truefalse)
self.SFenableDebug = truefalse
UpdateDebugMethod(self)
end
-- create an instance of a logger (with LibDebugLogger if available, otherwise just print to chat)
-- By default the logger is set to be not enabled (i.e. output nothing).
-- You can use :SetEnabled(true) to turn on output.
-- You can use :SetDebug(true) to turn on debug-level output as well.
function sfutil.Createlogger(addonName)
local logger
if LibDebugLogger then
logger = LibDebugLogger:Create(addonName)
logger.sfdb = "with LibDebugLogger"
-- Save the LibDebugLogger implementation.
logger.SFsvDebugFn = logger.Debug
else
logger = printDebug:Create(addonName)
logger.sfdb = "with printDebug"
end
logger.SetDebug = SetDebug
logger:SetDebug(false)
return logger
end
-- create an instance of a nil logger
-- This logger will never output anything (useful for completely turning off logging)
-- It has the same API as the real loggers in order to be replaceable with them, just no output ever.
function sfutil.CreateNilLogger(addonName)
-- initialize the logger for an addon
local logger = nilPrintDebug:Create(addonName)
logger.sfdb = "with nilPrintDebug"
logger.SetDebug=function(self,truefalse)
self.SFenableDebug = truefalse
end
return logger
end
--[[ InitSafeLogger(namespace, loggervar, addonname)
Initialize a set of safe, lazy logger functions for an addon. (Uses
LibDebugLogger as the basis logger if it is loaded first otherwise
it will log to chat.)
Returns three functions:
loggerFn - returns the addon's logger, creating it if necessary
logDebug - logs a DEBUG-level message if debug logging is enabled
logWouldDebug - returns true if DEBUG-level logging is currently enabled
The returned functions do not require the logger to already exist. The
logger is created on the first call to any of the returned functions.
This allows the functions to be initialized early in an addon's load
sequence without creating a dependency on logger initialization order.
loggerFn has the same behavior as the function returned by
SafeLoggerFunction(). It returns the same logger instance on subsequent
calls.
logDebug(message, ...)
Logs a debug message using the addon's logger.
Parameters:
message - the message to log, or nil
... - optional additional message parameters
* If message contains ESO zo_strformat placeholders (<<1>>, <<2>>,
etc.), zo_strformat() is used to format the message.
* Otherwise, SF.str() is used to process the message and its additional
parameters, converting them to strings to concatenate (without delimiters).
No message is generated when debug logging is disabled.
logWouldDebug()
Returns true when the logger is enabled and DEBUG-level logging is
enabled. This can be used to avoid performing expensive operations
solely to construct a debug message.
Example:
if logWouldDebug() then
local details = BuildExpensiveDebugString()
logDebug("Details: <<1>>", details)
end
Parameters:
namespace - table containing the logger instance. If this is not a
table, it is treated as the addon's global table name.
The table is created in _G if it does not already exist.
loggervar - string containing the name of the logger instance stored
in namespace.
addonname - name associated with the logger. When namespace is a
string, it is also used as the global table name.
Returns:
loggerFn - function returning the lazily-created logger
logDebug - function for filtered DEBUG-level logging
logWouldDebug - function returning whether DEBUG logging is active
Example:
local Logger, logDebug, logWouldDebug =
sfutil.InitSafeLogger(MyAddon, "logger", MyAddon.name)
-- Full logger API:
Logger():Info("Addon initialized")
Logger():Warn("Something unexpected happened")
-- Convenience debug logging:
logDebug("Processing item <<1>>", itemId)
-- Avoid expensive debug processing when debug logging is disabled:
if logWouldDebug() then
local details = BuildDebugDetails()
logDebug("Details: <<1>>", details)
end
Note:
InitSafeLogger() initializes the logging functions but does not
immediately create the logger. This makes the returned functions
safe to define in files that may load before the logger is otherwise
needed or initialized.
--]]
function sfutil.InitSafeLogger(namespace, loggervar, addonname)
local loggerFn = sfutil.SafeLoggerFunction(
namespace,
loggervar,
addonname
)
local function logDebug(message, ...)
if message == nil then return end
local logger = loggerFn()
if not logger.enabled or not logger.SFenableDebug then
return
end
local paramCount = select("#", ...)
if paramCount == 0 then
logger:Debug(message)
return
end
if type(message) == "string"
and message:find("<<%d+>>", 1)
then
logger:Debug(zo_strformat(message, ...))
return
end
logger:Debug(sfutil.str(message, ...))
end
local function logWouldDebug()
local logger = loggerFn()
return logger.enabled and logger.SFenableDebug
end
return loggerFn, logDebug, logWouldDebug
end