feat(print): add convar change listener (#728)

This commit is contained in:
CENSOR37
2025-03-13 14:17:36 +07:00
committed by GitHub
parent 2a8ce3f4b0
commit 4690564755

View File

@@ -14,8 +14,12 @@ local levelPrefixes = {
'^4[VERBOSE]',
'^6[DEBUG]',
}
local resourcePrintLevel = printLevel[GetConvar('ox:printlevel:' .. cache.resource, GetConvar('ox:printlevel', 'info'))]
local convarGlobal = 'ox:printlevel'
local convarResource = 'ox:printlevel:' .. cache.resource
local function getPrintLevelFromConvar()
return printLevel[GetConvar(convarResource, GetConvar(convarGlobal, 'info'))]
end
local resourcePrintLevel = getPrintLevelFromConvar()
local template = ('^5[%s] %%s %%s^7'):format(cache.resource)
local function handleException(reason, value)
if type(value) == 'function' then return tostring(value) end
@@ -48,4 +52,14 @@ lib.print = {
debug = function(...) libPrint(printLevel.debug, ...) end,
}
-- Update the print level when the convar changes
if (AddConvarChangeListener) then
AddConvarChangeListener('ox:printlevel*', function(convarName, reserved)
if (convarName ~= convarResource and convarName ~= convarGlobal) then return end
resourcePrintLevel = getPrintLevelFromConvar()
end)
else
libPrint(printLevel.verbose, 'Convar change listener not available, print level will not update dynamically.')
end
return lib.print