-
-
Notifications
You must be signed in to change notification settings - Fork 444
/
Copy pathshared.lua
51 lines (43 loc) · 1.56 KB
/
shared.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
---@enum PrintLevel
local printLevel = {
error = 1,
warn = 2,
info = 3,
verbose = 4,
debug = 5,
}
local levelPrefixes = {
'^1[ERROR]',
'^3[WARN]',
'^7[INFO]',
'^4[VERBOSE]',
'^6[DEBUG]',
}
local resourcePrintLevel = printLevel[GetConvar('ox:printlevel:' .. cache.resource, GetConvar('ox:printlevel', 'info'))]
local template = ('^5[%s] %%s %%s^7'):format(cache.resource)
local function handleException(reason, value)
if type(value) == 'function' then return tostring(value) end
return reason
end
local jsonOptions = { sort_keys = true, indent = true, exception = handleException }
---Prints to console conditionally based on what ox:printlevel is.
---Any print with a level more severe will also print. If ox:printlevel is info, then warn and error prints will appear as well, but debug prints will not.
---@param level PrintLevel
---@param ... any
local function libPrint(level, ...)
if level > resourcePrintLevel then return end
local args = { ... }
for i = 1, #args do
local arg = args[i]
args[i] = type(arg) == 'table' and json.encode(arg, jsonOptions) or tostring(arg)
end
print(template:format(levelPrefixes[level], table.concat(args, '\t')))
end
lib.print = {
error = function(...) libPrint(printLevel.error, ...) end,
warn = function(...) libPrint(printLevel.warn, ...) end,
info = function(...) libPrint(printLevel.info, ...) end,
verbose = function(...) libPrint(printLevel.verbose, ...) end,
debug = function(...) libPrint(printLevel.debug, ...) end,
}
return lib.print