-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfs22Logger.lua
437 lines (384 loc) · 13.8 KB
/
fs22Logger.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
-- FS22Log LUA Debug Class for FS22
--
-- Class init:
--
-- yourLogger = FS22Log:new(callerName, debugMode, filterOut, filterExclusive)
-- * callerName : Name of your mod, or logging section
-- * debugMode : one of the DEBUG_MODEs below, .WARNINGS suggested for production mode
-- * filterOut : names to filter **OUT** of printed output
-- * filterExclusive : names to **ONLY** print, takes precedence
--
--
-- Functions Available:
--
--
-- Print text to the log
--
-- yourLogger:print(text, logLevel, filter)
-- * text : Text to print
-- * logLevel : Log level from LOG_LEVEL below, default is .DEVEL
-- * filter : Text string for [filterOut] and [filterExclusive], default is "--"
--
--
-- Print variable contents to the log - recursively prints tables
--
-- yourLogger.printVariable(output, logLevel, filter, depthLimit, prefix, searchTerms)
-- yourLogger.printVariableIsTable(output, logLevel, filter, depthLimit, prefix, searchTerms)
-- yourLogger.printVariableOnce(output, logLevel, filter, depthLimit, prefix, searchTerms)
-- yourLogger.printVariableIfChanged(output, logLevel, filter, depthLimit, prefix, searchTerms)
--
-- The "IsTable" variant will auto-upgrade the logLevel to WARNING when the variable is not a table,
-- and ERROR if the variable is undefined or nil
--
-- The "Once" variant will print only the first time that variable is encountered, based on the value
-- of `filter`
--
-- The "IfChanged" variant will print only when the variable contents change, based on the value
-- of `filter` - note that this is only looks at the top level of the table passed.
--
-- * output : Variable to print
-- * logLevel : Log level from LOG_LEVEL below, default is .DEVEL
-- * filter : Text string for [filterOut] and [filterExclusive], default is "--"
-- * depthLimit : How deep to traverse tables, default is 2 levels (1 based option - e.g 2 == show 2 levels total)
-- * prefix : Text string for name of variable, defaults to [filter]
-- * searchTerms : Terms to search - options:
-- "string" : Search for "string" in KEYS only
-- { "string", SEARCH.TYPE } : Search for "string" using SEARCH.TYPE from below.
-- { {"table", "of", "strings"}, SEARCH.TYPE } : Search for all strings in table using SEARCH.TYPE from below.
--
--
-- Intercept function calls to print the arguments it's being called with. This method call is similar
-- to how Utils.prependedFunction() works.
--
-- Note that if you pass an invalid function, *when in debug mode*, a valid function with an error
-- message will be returned
--
-- originFunction = FS22LogFunction (logLevel, originMod, originFunctionName, originFunction)
-- * logLevel : Log level from LOG_LEVEL below, has no function is set to .WARNINGS or .ERRORS or .NONE
-- * originMod : Name of your mod for printing purposes
-- * originFunctionName : Name of the original function (string)
-- * originFunction : Original function (literal)
--
--
-- Run Levels:
-- It is highly recommended that your store this value locally in your mod, and pass it to the
-- constructor / calls to FS22LogFunction() so you can quickly toggle the level for released mods
-- without having to remove all your logging calls.
--
-- * FS22Log.DEBUG_MODE.NONE - Print nothing, ever
-- * FS22Log.DEBUG_MODE.ERRORS - Print errors
-- * FS22Log.DEBUG_MODE.WARNINGS - Print warnings, suggested for "production" mode
-- * FS22Log.DEBUG_MODE.INFO - Print information
-- * FS22Log.DEBUG_MODE.DEVEL - Print Development information
-- * FS22Log.DEBUG_MODE.VERBOSE - Print Verbose Development information
--
-- Log Levels:
-- * FS22Log.LOG_LEVEL.ERROR - Errors only
-- * FS22Log.LOG_LEVEL.WARNING - Warnings, suggested for "production" mode
-- * FS22Log.LOG_LEVEL.INFO - Information level
-- * FS22Log.LOG_LEVEL.DEVEL - Development information
-- * FS22Log.LOG_LEVEL.VERBOSE - Verbose Development information
--
-- Search Types:
-- * FS22Log.SEARCH.KEYS - Search keys only
-- * FS22Log.SEARCH.VALUES - Search values only
-- * FS22Log.SEARCH.BOTH - Search both keys and values
-- * FS22Log.SEARCH.KEYS_AND_VALUES - Search both keys and values
--
-- (c)JTSage Modding & FSG Modding. You may reuse or alter this code to your needs as nessesary with
-- no prior permission. No warrenty implied or otherwise.
FS22Log = {}
local FS22Log_mt = Class(FS22Log)
FS22Log.DEBUG_MODE = {}
FS22Log.DEBUG_MODE.NONE = 0
FS22Log.DEBUG_MODE.ERRORS = 1
FS22Log.DEBUG_MODE.WARNINGS = 2
FS22Log.DEBUG_MODE.INFO = 3
FS22Log.DEBUG_MODE.DEVEL = 4
FS22Log.DEBUG_MODE.VERBOSE = 5
FS22Log.LOG_LEVEL = {}
FS22Log.LOG_LEVEL.ERROR = 1
FS22Log.LOG_LEVEL.WARNING = 2
FS22Log.LOG_LEVEL.INFO = 3
FS22Log.LOG_LEVEL.DEVEL = 4
FS22Log.LOG_LEVEL.VERBOSE = 5
FS22Log.SEARCH = {}
FS22Log.SEARCH.NONE = 0
FS22Log.SEARCH.KEYS = 1
FS22Log.SEARCH.VALUES = 2
FS22Log.SEARCH.BOTH = 3
FS22Log.SEARCH.KEYS_AND_VALUES = 3
FS22Log.SEARCH.BAD_TERMS = 4
FS22Log.SEARCH_TEXT = {
[0] = "NONE",
[1] = "KEYS",
[2] = "VALUES",
[3] = "KEYS and VALUES",
[4] = "-ERROR-"
}
FS22Log.LOG_LEVEL_TEXT = {
[1] = "ERROR",
[2] = "WARNING",
[3] = "INFO",
[4] = "DEVEL",
[5] = "VERBOSE"
}
function FS22Log:new(callerName, debugMode, filterOut, filterExclusive)
local self = setmetatable({}, FS22Log_mt)
self.calledName = callerName or "UnKnown Script"
self.debugMode = debugMode or FS22Log.DEBUG_MODE.ERRORS
self.filteredOut = {}
self.filteredIn = {}
self.trackOnce = {}
self.trackChanges = {}
if filterOut ~= nil and type(filterOut) == "table" then
self.filteredOut = filterOut
end
if filterExclusive ~= nil and type(filterExclusive) == "table" then
self.filteredOut = {}
self.filteredIn = filterExclusive
end
return self
end
function FS22Log:makeStringRep(inputTable)
if type(inputTable) ~= "table" then
return tostring(inputTable)
end
local stringRep = ""
for key,value in ipairs(inputTable) do
stringRep = stringRep .. string.format("%s:%s",tostring(key), tostring(value))
end
return stringRep
end
function FS22Log:isNotSameStored(filterName, inputTable)
local stringValue = self:makeStringRep(inputTable)
for seenName, seenValue in pairs(self.trackChanges) do
if seenName == filterName then
if seenValue == stringValue then
print("same")
return false
else
print("re-setting")
self.trackChanges[filterName] = stringValue
return true
end
end
end
print("initial setting")
self.trackChanges[filterName] = stringValue
return true
end
function FS22Log:wasSeen(filterName)
for _, seenName in ipairs(self.trackOnce) do
if seenName == filterName then
return true
end
end
table.insert(self.trackOnce, filterName)
end
function FS22Log:isFiltered(filterOperator)
if self.filteredIn ~= nil and #self.filteredIn > 0 then
if filterOperator == nil then
return true
end
for _, filterMe in ipairs(self.filteredIn) do
if filterOperator == filterMe then
return false
end
end
return true
end
if self.filteredOut == nil or #self.filteredOut < 0 or filterOperator == nil then
return false
end
for _, filterMe in ipairs(self.filteredOut) do
if filterOperator == filterMe then
return true
end
end
return false
end
function FS22Log:cleanLogLevel(logLevel)
local cleanLogLevel
if logLevel ~= nil and type(logLevel) == "number" and logLevel > 0 then
cleanLogLevel = logLevel
else
cleanLogLevel = FS22Log.LOG_LEVEL.DEVEL
end
return cleanLogLevel
end
function FS22Log:processSearchTerms(testTable, searchTerms, logLevel, filter)
local findWords = {}
local findType = FS22Log.SEARCH.NONE
if searchTerms == nil or testTable == nil or type(testTable) ~= "table" then
return false, nil, nil
end
if type(searchTerms) ~= "table" then
findType = FS22Log.SEARCH.KEYS
findWords = { searchTerms }
else
if #searchTerms ~= 2 or type(searchTerms[2]) ~= "number" then
return true, FS22Log.SEARCH.BAD_TERMS, nil
end
findType = searchTerms[2]
if type(searchTerms[1]) ~= "table" then
findWords = { searchTerms[1] }
else
if #searchTerms[1] < 1 then
return false, nil, nil
end
findWords = searchTerms[1]
end
end
return true, findType, findWords
end
function FS22Log:searchTerm(testKey, testValue, findType, findWords)
if findType == FS22Log.SEARCH.BAD_TERMS then
return false
end
if findType == FS22Log.SEARCH.NONE then
return true
end
if findType == FS22Log.SEARCH.KEYS or findType == FS22Log.SEARCH.BOTH then
for _, lookWord in ipairs(findWords) do
if string.find(tostring(testKey), tostring(lookWord)) then
return true
end
end
end
if findType == FS22Log.SEARCH.VALUES or findType == FS22Log.SEARCH.BOTH then
for _, lookWord in ipairs(findWords) do
if string.find(tostring(testValue), tostring(lookWord)) then
return true
end
end
end
return false
end
function FS22Log:print(text, logLevel, filter)
local logLevel = self:cleanLogLevel(logLevel)
if self.debugMode >= logLevel then
if not self:isFiltered(filter) then
local levelText = FS22Log.LOG_LEVEL_TEXT[logLevel] or "UNKNOWN"
local filterText = filter or "--"
local outputText = "~~ " .. self.calledName .. ":" .. levelText .. ":" .. filterText .. " | " .. text
if logLevel == FS22Log.LOG_LEVEL.ERROR then
printError(outputText)
elseif logLevel == FS22Log.LOG_LEVEL.WARNING then
printWarning(outputText)
else
print(outputText)
end
end
end
end
function FS22Log:printVariableIsTable(output, logLevel, filter, depthLimit, prefix, searchTerms, currentDepth)
if output == nil then
logLevel = FS22Log.LOG_LEVEL.ERROR
elseif type(output) ~= "table" then
logLevel = FS22Log.LOG_LEVEL.WARNING
end
self:printVariable(output, logLevel, filter, depthLimit, prefix, searchTerms, currentDepth)
end
function FS22Log:printVariableOnce(output, logLevel, filter, depthLimit, prefix, searchTerms, currentDepth)
local prefix = prefix or filter or "{}"
local logLevel = self:cleanLogLevel(logLevel)
local depthLimit = depthLimit or 2
local currentDepth = currentDepth or 0
if self:isFiltered(filter) then return end
if self.debugMode < logLevel then return end
if self:wasSeen(filter) then return end
self:printVariable(output, logLevel, filter, depthLimit, prefix, searchTerms, currentDepth)
end
function FS22Log:printVariableIfChanged(output, logLevel, filter, depthLimit, prefix, searchTerms, currentDepth)
local prefix = prefix or filter or "{}"
local logLevel = self:cleanLogLevel(logLevel)
local depthLimit = depthLimit or 2
local currentDepth = currentDepth or 0
if self:isFiltered(filter) then return end
if self.debugMode < logLevel then return end
if not self:isNotSameStored(filter, output) then return end
self:printVariable(output, logLevel, filter, depthLimit, prefix, searchTerms, currentDepth)
end
function FS22Log:printVariable(output, logLevel, filter, depthLimit, prefix, searchTerms, currentDepth)
local prefix = prefix or filter or "{}"
local logLevel = self:cleanLogLevel(logLevel)
local depthLimit = depthLimit or 2
local currentDepth = currentDepth or 0
local maxLength = 0
if self:isFiltered(filter) then return end
if self.debugMode < logLevel then return end
if output == nil or type(output) ~= "table" then
self:print(prefix .. " :: " .. tostring(output), logLevel, filter)
return
end
local searchDo, searchType, searchWords = self:processSearchTerms(output, searchTerms, logLevel, filter)
for key, _ in pairs(output) do
local currentLength = string.len(tostring(key))
if currentLength > maxLength then
maxLength = currentLength
end
end
if searchDo == true and currentDepth == 0 and searchType > 0 and searchType < 4 and type(searchWords) == "table" then
self:print(
"Searching for: {" .. table.concat(searchWords, ",") .. "} [" .. FS22Log.SEARCH_TEXT[searchType] .. "]",
logLevel,
filter
)
end
currentDepth = currentDepth + 1
if searchType == FS22Log.SEARCH.BAD_TERMS then
self:print("ERROR: Incorrect search terms, see logger documentation", logLevel, filter)
return
end
for key, value in pairs(output) do
local keyString = tostring(key)
local spacePad = string.rep(" ", maxLength - string.len(keyString))
local depthPad = string.rep("_", currentDepth - 1)
local thisPrefix = prefix .. "." .. keyString
local searchFound = true
if searchDo and searchType ~= FS22Log.SEARCH.BAD_TERMS then
searchFound = self:searchTerm(key, value, searchType, searchWords)
end
if searchFound then
self:print(
depthPad .. thisPrefix .. spacePad .. " :: " .. tostring(value),
logLevel,
filter
)
end
if type(value) == "table" and currentDepth < depthLimit then
self:printVariable(
value,
logLevel,
filter,
depthLimit,
thisPrefix,
searchTerms,
currentDepth
)
end
end
end
function FS22LogFunction (logLevel, originMod, originFunctionName, originFunction)
if logLevel == nil or logLevel <= FS22Log.DEBUG_MODE.WARNINGS then return originFunction end
if originFunction ~= nil then
return function (...)
local argNames = function(...) return arg end
local argValues = argNames(...)
if type(argValues) == "table" then
local argListText = ""
for idx, arggy in ipairs(argValues) do
argListText = argListText .. (idx == 1 and "" or ", ") .. '[' .. tostring(arggy) .. ']'
end
print("~~ " .. originMod .. ":" .. originFunctionName .. " | Called With: " .. argListText)
else
print("~~ " .. originMod .. ":" .. originFunctionName .. " | Called (no arguments)")
end
originFunction(...)
end
else
print("~~ " .. originMod .. ":" .. originFunctionName .. " | Original Function Not Found")
return function (...) print("~~ " .. originMod .. ":" .. originFunctionName .. " | Invalid function call (no original)") end
end
end