-
Notifications
You must be signed in to change notification settings - Fork 0
/
Note.lua
400 lines (320 loc) · 11.1 KB
/
Note.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
-- Note.lua by Rouneq
-- Initially based on Write.lua by Knightly
-- Provides logging capabilities to both the MQ console and an external file
---@type OsTime
local time = require('OsTime')
--- The logging severity suppoted by Note
---@alias severityType
---| 'trace'
---| 'debug'
---| 'info'
---| 'warn'
---| 'error'
---| 'fatal'
local _newline = "\r\n"
--- Note Lua Binding
---@class Note
Note = {}
local _version = '1.0'
local logLevels = {
['trace'] = { level = 1, color = '\27[36m', mqcolor = '\at', abbreviation = 'TRACE', },
['debug'] = { level = 2, color = '\27[95m', mqcolor = '\am', abbreviation = 'DEBUG', },
['info'] = { level = 3, color = '\27[92m', mqcolor = '\ag', abbreviation = 'INFO' , },
['warn'] = { level = 4, color = '\27[93m', mqcolor = '\ay', abbreviation = 'WARN' , },
['error'] = { level = 5, color = '\27[31m', mqcolor = '\ao', abbreviation = 'ERROR', },
['fatal'] = { level = 6, color = '\27[91m', mqcolor = '\ar', abbreviation = 'FATAL', },
['help'] = { level = 7, color = '\27[97m', mqcolor = '\aw', abbreviation = 'HELP' , },
}
local luaReservedWords = {
['and'] = true,
['break'] = true,
['do'] = true,
['else'] = true,
['elseif'] = true,
['end'] = true,
['false'] = true,
['for'] = true,
['function'] = true,
['if'] = true,
['in'] = true,
['local'] = true,
['nil'] = true,
['not'] = true,
['or'] = true,
['repeat'] = true,
['return'] = true,
['then'] = true,
['true'] = true,
['until'] = true,
['while'] = true,
}
--- Indicates if log levels should be use colors
---@type boolean
Note.useColors = true
--- Indicates if a timestamp should be added to console output
---@type boolean
Note.useTimestampConsole = false
--- Indicates if a timestamp should be added to log output
---@type boolean
Note.useTimestampLog = true
--- The minimum level at which output is written to the console/log
---@type severityType
Note.logLevel = 'info'
--- A specific prefix added to each output
---@type string|function
Note.prefix = ''
--- Indicates if the output should be written to a log file
---@type boolean
Note.useOutfile = false
--- The path/name of the log file; if one is not defined it will be written to the default logs path for MQ with the name <server>_<character>.log
---@type string
Note.outfile = nil
--- Indicates if the log output should be appended to an existing file (if it exists) or a new file created
---@type boolean
Note.appendToOutfile = true
--- The minimum level at which caller info is included with the output
---@type severityType
Note.callerReportingLevel = 'trace'
local function terminate()
local mq = package.loaded['mq']
if mq then
mq.exit()
end
os.exit()
end
local function getPath(filePath)
if (not filePath or filePath == '') then
return nil
end
return filePath:match("(.*[/\\])")
end
local function checkLogFile()
local mq = package.loaded['mq']
if (not Note.outfile or Note.outfile == '') then
if (mq) then
Note.outfile = string.format("%s\\%s_%s.log", mq.TLO.MacroQuest.Path('logs'), mq.TLO.EverQuest.Server(), mq.TLO.Me.CleanName())
else
Note.outfile = 'output.log'
end
else
local path = getPath(Note.outfile)
if (not path and mq) then
Note.outfile = string.format("%s\\%s", mq.TLO.MacroQuest.Path('logs'), Note.outfile)
end
end
end
local function writeLog(data)
local mode = "a"
if (not Note.appendToOutfile) then
mode = "w"
end
local file, err = io.open(Note.outfile, mode)
if (file == nil) then
Note.Raw('Error opening outfile: %s', err)
return
end
file:write(data, "\n")
file:close()
end
local function getColorStart(logLevel)
if Note.useColors then
if package.loaded['mq'] then return logLevels[logLevel].mqcolor end
return logLevels[logLevel].color
end
return ''
end
local function getColorEnd()
if Note.useColors then
if package.loaded['mq'] then
return '\ax'
end
return '\27[0m'
end
return ''
end
local function getCallerName()
if (logLevels[Note.logLevel:lower()].level > logLevels[Note.callerReportingLevel].level) then
return ''
end
local callName = 'unknown'
local callerInfo = debug.getinfo(4,'Sl')
if callerInfo and callerInfo.short_src ~= nil and callerInfo.short_src ~= '=[C]' then
callName = string.format('%s::%s', callerInfo.short_src:match("[^\\^/]*.lua$"), callerInfo.currentline)
end
return string.format('(%s) ', callName)
end
local function exportstring(s)
return string.format("%q", s)
end
local function tablePrint(data, indent)
if not indent then indent = 0 end
local output = "{" .. _newline
indent = indent + 2
for k, v in pairs(data) do
output = output .. string.rep(" ", indent)
if (type(k) == "number" or type(k) == "boolean") then
output = output .. "[" .. tostring(k) .. "] = "
elseif (type(k) == "string") then
if (luaReservedWords[k] or tonumber(k)) then
k = "["..exportstring(k).."]"
end
output = output .. k .. " = "
end
if (type(v) == "number" or type(v) == "boolean") then
output = output .. tostring(v)
elseif (type(v) == "string") then
output = output .. "\"" .. v .. "\""
elseif (type(v) == "table") then
output = output .. tablePrint(v, indent + 2)
else
output = output .. "\"" .. tostring(v) .. "\""
end
output = output .. "," .. _newline
end
output = output .. string.rep(" ", indent - 2) .. "}"
return output
end
local function normalizeValue(v)
local output
if (v == nil) then
output = 'nil'
---@diagnostic disable-next-line: undefined-global
elseif (v == NULL) then
output = 'NULL'
elseif (type(v) == "number" or type(v) == "string") then
output = v
elseif (type(v) == "table") then
output = tablePrint(v)
else
output = tostring(v)
end
return output
end
local function normalizeArgs(...)
local normalizedArgs = {}
for i = 1, select('#', ...) do
table.insert(normalizedArgs, normalizeValue((select(i, ...))))
end
return unpack(normalizedArgs)
end
local timestampPattern = "<%04d-%02d-%02d %02d:%02d:%02d.%03d> "
---@param useTimestamp boolean
local function getTimestamp(useTimestamp)
if (useTimestamp) then
local lt = time.GetLocalTime()
return string.format(timestampPattern, lt.Year, lt.Month, lt.Day, lt.Hour, lt.Minute, lt.Second, lt.Milliseconds)
end
return ""
end
---@param prependNewline boolean
---@vararg ...
---@return string
local function formatVarargs(prependNewline, ...)
local output = ''
if (select('#', ...) > 0) then
if (prependNewline) then
output = _newline
else
output = ' '
end
output = output .. table.concat({...}, ' ')
end
return output
end
local function formatOutput(message, ...)
local output
if (type(message) == "string") then
output = string.format(message, normalizeArgs(...))
elseif (type(message) == "table") then
output = tablePrint(message) .. formatVarargs(true, ...)
else
output = tostring(message) .. formatVarargs(false, ...)
end
return output
end
---@return string
local function formatHeader()
local header = Note.prefix
if (type(Note.prefix) == 'function') then
header = Note.prefix()
end
return header --[[@as string]]
end
local function printf(format, ...)
print(string.format(format, ...))
end
local function logf(format, ...)
checkLogFile()
local log = string.format(format, ...)
writeLog(log)
end
local function writeOutput(logLevel, message, ...)
if logLevels[Note.logLevel:lower()].level <= logLevels[logLevel].level then
local output = formatOutput(message, ...)
local header = formatHeader()
local caller = getCallerName()
printf('%s%s%s%s[%s]%s :: %s', getTimestamp(Note.useTimestampConsole), header, caller, getColorStart(logLevel), logLevels[logLevel].abbreviation, getColorEnd(), output)
if (Note.useOutfile) then
logf('%s%s%s[%s] :: %s', getTimestamp(Note.useTimestampLog), header, caller, logLevels[logLevel].abbreviation, output)
end
end
end
---Raw console output
---@param message any A message to display to the console (only); if a string, can contain formatting directives for further parameters
---@vararg any
function Note.Raw(message, ...)
printf('%s', formatOutput(message, ...))
end
---Raw log output
---@param message any A message to display to the log (only); if a string, can contain formatting directives for further parameters
---@vararg any
function Note.RawLog(message, ...)
logf('%s', formatOutput(message, ...))
end
---Trace-level output
---@param message any A message to display to the console (and log if configured) at a trace level; if a string, can contain formatting directives for further parameters
---@vararg any
function Note.Trace(message, ...)
writeOutput('trace', message, ...)
end
---Debug-level output
---@param message any A message to display to the console (and log if configured) at a debug level; if a string, can contain formatting directives for further parameters
---@vararg any
function Note.Debug(message, ...)
writeOutput('debug', message, ...)
end
---Information-level output
---@param message any A message to display to the console (and log if configured) at an informational level; if a string, can contain formatting directives for further parameters
---@vararg any
function Note.Info(message, ...)
writeOutput('info', message, ...)
end
---Warning-level output
---@param message any A message to display to the console (and log if configured) at an warning level; if a string, can contain formatting directives for further parameters
---@vararg any
function Note.Warn(message, ...)
writeOutput('warn', message, ...)
end
---Error-level output
---@param message any A message to display to the console (and log if configured) at an error level; if a string, can contain formatting directives for further parameters
---@vararg any
function Note.Error(message, ...)
writeOutput('error', message, ...)
end
---Fatal-level output
---Script will terminate after writing the message
---@param message any A message to display to the console (and log if configured) at a fatal level; if a string, can contain formatting directives for further parameters
---@vararg any
function Note.Fatal(message, ...)
writeOutput('fatal', message, ...)
terminate()
end
---Log output
---@param message any A message to display to the log (only); if a string, can contain formatting directives for further parameters
---@vararg any
function Note.Log(message, ...)
local header = formatHeader()
local caller = getCallerName()
logf('%s%s%s[%s] :: %s', getTimestamp(Note.useTimestampLog), header, caller, logLevels[Note.logLevel].abbreviation, formatOutput(message, ...))
end
return Note