From 4690564755e07daae75fb1063f9666048ae31b7d Mon Sep 17 00:00:00 2001 From: CENSOR37 Date: Thu, 13 Mar 2025 14:17:36 +0700 Subject: [PATCH] feat(print): add convar change listener (#728) --- imports/print/shared.lua | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/imports/print/shared.lua b/imports/print/shared.lua index 305c9a0..38d7ff1 100644 --- a/imports/print/shared.lua +++ b/imports/print/shared.lua @@ -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