- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNote.lua
More file actions
Latest commit
400 lines (320 loc) · 11.1 KB
/
Copy pathNote.lua
File metadata and controls
400 lines (320 loc) · 11.1 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
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
---@typeOsTime
localtime=require('OsTime')
--- The logging severity suppoted by Note
---@aliasseverityType
---| 'trace'
---| 'debug'
---| 'info'
---| 'warn'
---| 'error'
---| 'fatal'
local_newline="\r\n"
--- Note Lua Binding
---@classNote
Note= {}
local_version='1.0'
locallogLevels= {
['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' , },
}
localluaReservedWords= {
['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
---@typeboolean
Note.useColors=true
--- Indicates if a timestamp should be added to console output
---@typeboolean
Note.useTimestampConsole=false
--- Indicates if a timestamp should be added to log output
---@typeboolean
Note.useTimestampLog=true
--- The minimum level at which output is written to the console/log
---@typeseverityType
Note.logLevel='info'
--- A specific prefix added to each output
---@typestring|function
Note.prefix=''
--- Indicates if the output should be written to a log file
---@typeboolean
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
---@typestring
Note.outfile=nil
--- Indicates if the log output should be appended to an existing file (if it exists) or a new file created
---@typeboolean
Note.appendToOutfile=true
--- The minimum level at which caller info is included with the output
---@typeseverityType
Note.callerReportingLevel='trace'
localfunctionterminate()
localmq=package.loaded['mq']
ifmqthen
mq.exit()
end
os.exit()
end
localfunctiongetPath(filePath)
if (notfilePathorfilePath=='') then
returnnil
end
returnfilePath:match("(.*[/\\])")
end
localfunctioncheckLogFile()
localmq=package.loaded['mq']
if (notNote.outfileorNote.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
localpath=getPath(Note.outfile)
if (notpathandmq) then
Note.outfile=string.format("%s\\%s", mq.TLO.MacroQuest.Path('logs'), Note.outfile)
end
end
end
localfunctionwriteLog(data)
localmode="a"
if (notNote.appendToOutfile) then
mode="w"
end
localfile, 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
localfunctiongetColorStart(logLevel)
ifNote.useColorsthen
ifpackage.loaded['mq'] thenreturnlogLevels[logLevel].mqcolorend
returnlogLevels[logLevel].color
end
return''
end
localfunctiongetColorEnd()
ifNote.useColorsthen
ifpackage.loaded['mq'] then
return'\ax'
end
return'\27[0m'
end
return''
end
localfunctiongetCallerName()
if (logLevels[Note.logLevel:lower()].level>logLevels[Note.callerReportingLevel].level) then
return''
end
localcallName='unknown'
localcallerInfo=debug.getinfo(4,'Sl')
ifcallerInfoandcallerInfo.short_src~=nilandcallerInfo.short_src~='=[C]' then
callName=string.format('%s::%s', callerInfo.short_src:match("[^\\^/]*.lua$"), callerInfo.currentline)
end
returnstring.format('(%s) ', callName)
end
localfunctionexportstring(s)
returnstring.format("%q", s)
end
localfunctiontablePrint(data, indent)
ifnotindentthenindent=0end
localoutput="{" .._newline
indent=indent+2
fork, vinpairs(data) do
output=output..string.rep("", indent)
if (type(k) =="number" ortype(k) =="boolean") then
output=output.."[" ..tostring(k) .."] = "
elseif (type(k) =="string") then
if (luaReservedWords[k] ortonumber(k)) then
k="["..exportstring(k).."]"
end
output=output..k.." = "
end
if (type(v) =="number" ortype(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) .."}"
returnoutput
end
localfunctionnormalizeValue(v)
localoutput
if (v==nil) then
output='nil'
---@diagnosticdisable-next-line:undefined-global
elseif (v==NULL) then
output='NULL'
elseif (type(v) =="number" ortype(v) =="string") then
output=v
elseif (type(v) =="table") then
output=tablePrint(v)
else
output=tostring(v)
end
returnoutput
end
localfunctionnormalizeArgs(...)
localnormalizedArgs= {}
fori=1, select('#', ...) do
table.insert(normalizedArgs, normalizeValue((select(i, ...))))
end
returnunpack(normalizedArgs)
end
localtimestampPattern="<%04d-%02d-%02d %02d:%02d:%02d.%03d> "
---@paramuseTimestampboolean
localfunctiongetTimestamp(useTimestamp)
if (useTimestamp) then
locallt=time.GetLocalTime()
returnstring.format(timestampPattern, lt.Year, lt.Month, lt.Day, lt.Hour, lt.Minute, lt.Second, lt.Milliseconds)
end
return""
end
---@paramprependNewlineboolean
---@vararg...
---@returnstring
localfunctionformatVarargs(prependNewline, ...)
localoutput=''
if (select('#', ...) >0) then
if (prependNewline) then
output=_newline
else
output=''
end
output=output..table.concat({...}, '')
end
returnoutput
end
localfunctionformatOutput(message, ...)
localoutput
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
returnoutput
end
---@returnstring
localfunctionformatHeader()
localheader=Note.prefix
if (type(Note.prefix) =='function') then
header=Note.prefix()
end
returnheader--[[@as string]]
end
localfunctionprintf(format, ...)
print(string.format(format, ...))
end
localfunctionlogf(format, ...)
checkLogFile()
locallog=string.format(format, ...)
writeLog(log)
end
localfunctionwriteOutput(logLevel, message, ...)
iflogLevels[Note.logLevel:lower()].level<=logLevels[logLevel].levelthen
localoutput=formatOutput(message, ...)
localheader=formatHeader()
localcaller=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
---@parammessageany A message to display to the console (only); if a string, can contain formatting directives for further parameters
---@varargany
functionNote.Raw(message, ...)
printf('%s', formatOutput(message, ...))
end
---Raw log output
---@parammessageany A message to display to the log (only); if a string, can contain formatting directives for further parameters
---@varargany
functionNote.RawLog(message, ...)
logf('%s', formatOutput(message, ...))
end
---Trace-level output
---@parammessageany 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
---@varargany
functionNote.Trace(message, ...)
writeOutput('trace', message, ...)
end
---Debug-level output
---@parammessageany 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
---@varargany
functionNote.Debug(message, ...)
writeOutput('debug', message, ...)
end
---Information-level output
---@parammessageany 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
---@varargany
functionNote.Info(message, ...)
writeOutput('info', message, ...)
end
---Warning-level output
---@parammessageany 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
---@varargany
functionNote.Warn(message, ...)
writeOutput('warn', message, ...)
end
---Error-level output
---@parammessageany 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
---@varargany
functionNote.Error(message, ...)
writeOutput('error', message, ...)
end
---Fatal-level output
---Script will terminate after writing the message
---@parammessageany 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
---@varargany
functionNote.Fatal(message, ...)
writeOutput('fatal', message, ...)
terminate()
end
---Log output
---@parammessageany A message to display to the log (only); if a string, can contain formatting directives for further parameters
---@varargany
functionNote.Log(message, ...)
localheader=formatHeader()
localcaller=getCallerName()
logf('%s%s%s[%s] :: %s', getTimestamp(Note.useTimestampLog), header, caller, logLevels[Note.logLevel].abbreviation, formatOutput(message, ...))
end
returnNote