-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSFUtils_Logger.lua
More file actions
230 lines (202 loc) · 8.2 KB
/
SFUtils_Logger.lua
File metadata and controls
230 lines (202 loc) · 8.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
local sfutil = LibSFUtils or {}
-- logger object that prints to chat
-- Use Create() to create a logger instance of this type
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 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 to anywhere
-- Use Create() to create a logger instance of this type
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,
}
-- logger object that prints to chat when enabled
-- Use Create() to create a logger instance of this type
local printDebug = {
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,tf)
if not self then return end
if self.enabled == tf then return end
self.enabled = tf
if tf == true then
self.Error = activePrintDebug.Error
self.Warn = activePrintDebug.Warn
self.Info = activePrintDebug.Info
self.Debug = activePrintDebug.Debug
else
self.Error = nilPrintDebug.Error
self.Warn = nilPrintDebug.Warn
self.Info = nilPrintDebug.Info
self.Debug = nilPrintDebug.Debug
end
end,
}
--[[
function sfutil.SafeLoggerFunction(namespace, loggervar, addonname)
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)
--[[
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
if not namespace[loggervar] then
mylogger = sfutil.Createlogger(addonname)
mylogger:SetEnabled(true)
end
namespace[loggervar] = mylogger
return mylogger
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)
-- initialize the logger for an addon
local logger = nil
if LibDebugLogger then
logger = LibDebugLogger:Create(addonName)
logger.sfdb = "with LibDebugLogger"
else
logger = printDebug:Create(addonName)
logger.sfdb = "with printDebug"
end
-- Debug-level messages are turned off on creation by default.
-- You must logger:SetDebug(true) in order to turn on debug-level
-- messages. (And with the LibDebugLogger, you must ALSO turn on
-- collection of debug-level messages in the Debug Log Viewer -> LibDebugLogger
-- settings.)
-- save the original Debug() so that we can turn it on and off
logger.SFsvDebugFn = logger.Debug
logger.SetDebug = function(self,truefalse)
-- Note: For Debug to work, you must ALSO enable debug messages in the
-- Debug Log Viewer -> LibLogDebugger settings!!!
logger.SFenableDebug = truefalse
-- change the Debug() function to send or not send according total
-- if debug is enabled here.
if logger.SFenableDebug == false then
-- will not send anything to LibDebugLogger or to chat
logger.Debug = nilPrintDebug.Debug
else
-- send debug messages to the original Debug()
logger.Debug = logger.SFsvDebugFn
end
end
-- debug is OFF by default. Can be changed by settings or other programmatic means
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