MT#59550 utils.KSR_log

helper to generate file log when using KSR.log

Change-Id: I965799a082f1047bcfc3a53427a6e9a420d9b3e1
mr13.0
Victor Seva 2 years ago committed by Alessio Garzi
parent d1cf935a66
commit 6e2d9f4706

1
debian/control vendored

@ -15,6 +15,7 @@ Depends:
lua-argparse,
lua-cjson,
lua-curl,
lua-logging,
lua-redis (>= 2.0.5~git20141117.880dda9-7~),
lua-sql-mysql (>= 2.4.0),
lua5.1,

@ -1,5 +1,5 @@
--
-- Copyright 2014-2022 SipWise Team <development@sipwise.com>
-- Copyright 2014-2024 SipWise Team <development@sipwise.com>
--
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
@ -20,6 +20,8 @@
local NGCPDlgCounters = {
__class__ = 'NGCPDlgCounters'
}
-- luacheck: globals KSR
local KSR = KSR
local NGCPRedis = require 'ngcp.redis';
local utils = require 'ngcp.utils';
local utable = utils.table;
@ -40,6 +42,7 @@ local defaults = {
port = 6379,
db = 4
},
logfile = false,
debug = false,
check_pair_dup = false,
allow_negative = false
@ -53,19 +56,26 @@ NGCPDlgCounters_MT.__tostring = function (t)
utable.tostring(t.config), utable.tostring(t.central),
utable.tostring(t.pair));
end
-- luacheck: globals KSR
function NGCPDlgCounters:new(config)
local t = NGCPDlgCounters.init(utils.merge_defaults(config, defaults))
setmetatable( t, NGCPDlgCounters_MT )
return t
return setmetatable( t, NGCPDlgCounters_MT )
end
function NGCPDlgCounters.init(config)
return {
local t = {
config = config,
central = NGCPRedis:new(config.central),
pair = NGCPRedis:new(config.pair)
}
if config.logfile then
t.KSR = utils.KSR_log(KSR, config.logfile)
if t.KSR and t.KSR._logger then
KSR = t.KSR
KSR.dbg(string.format("logfile %s will be in used", config.logfile))
end
end
return t
end
function NGCPDlgCounters._decr(self, key)

@ -1,5 +1,5 @@
--
-- Copyright 2022 SipWise Team <development@sipwise.com>
-- Copyright 2022-2024 SipWise Team <development@sipwise.com>
--
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
@ -17,11 +17,12 @@
-- On Debian systems, the complete text of the GNU General
-- Public License version 3 can be found in "/usr/share/common-licenses/GPL-3".
--
-- luacheck: globals KSR
local utils = require 'ngcp.utils'
local redis = require 'redis';
local utable = utils.table
local NGCPRedis = utils.inheritsFrom()
-- luacheck: globals KSR
local KSR = KSR
_ENV = NGCPRedis

@ -1,5 +1,5 @@
--
-- Copyright 2013-2020 SipWise Team <development@sipwise.com>
-- Copyright 2013-2024 SipWise Team <development@sipwise.com>
--
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
@ -17,8 +17,9 @@
-- On Debian systems, the complete text of the GNU General
-- Public License version 3 can be found in "/usr/share/common-licenses/GPL-3".
--
local logging = require ('logging')
local log_file = require ('logging.file')
-- Lua utils
local utils = {}
utils.table = {}
utils.string = {}
@ -466,4 +467,52 @@ end
--EOF
utils.Stack = Stack
function utils.KSR_log(KSR, logfile)
-- KSR has already the metatable
if KSR._logger then
KSR._logger = log_file(logfile, "%Y-%m-%d")
return KSR
end
local ksr_MT = { __index = KSR }
local t = {
_log = KSR.log,
_logger = log_file(logfile, "%Y-%m-%d"),
_logger_levels = {
dbg = logging.DEBUG,
info = logging.INFO,
warn = logging.WARN,
err = logging.ERROR,
crit = logging.FATAL
}
}
function t.log(level, message)
if not t._logger_levels[level] then
error(string.format("level %s unknown", tostring(level)))
end
-- same message on both
t._logger:log(t._logger_levels[level], message)
t._log(level, message)
end
function t.dbg(message)
t._logger:log(logging.DEBUG, message)
end
function t.err(message)
t._logger:log(logging.ERROR, message)
end
function t.info(message)
t._logger:log(logging.INFO, message)
end
function t.notice(message)
t._logger:log(logging.INFO, message)
end
function t.warn(message)
t._logger:log(logging.WARN, message)
end
function t.crit(message)
t._logger:log(logging.FATAL, message)
end
return setmetatable(t, ksr_MT)
end
return utils

@ -250,5 +250,18 @@ TestNGCPDlgCnt = {} --class
lu.assertIs(self.dlg.pair.client, self.pair)
end
function TestNGCPDlgCnt:test_logfile()
local KSR_old = KSR
local config = {
logfile = '/dev/null'
}
local NGCPDlg = require 'ngcp.dlgcnt'
dlg = NGCPDlg:new(config)
-- no changes in global
lu.assertIs(KSR, KSR_old)
lu.assertEvalToTrue(dlg.KSR)
lu.assertEvalToTrue(dlg.KSR._logger)
end
-- class TestNGCPDlgCnt
--EOF

@ -0,0 +1,50 @@
--
-- Copyright 2024 SipWise Team <development@sipwise.com>
--
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
--
-- This package is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with this program. If not, see <http://www.gnu.org/licenses/>.
-- .
-- On Debian systems, the complete text of the GNU General
-- Public License version 3 can be found in "/usr/share/common-licenses/GPL-3".
--
local lu = require('luaunit')
local utils = require 'ngcp.utils'
-- luacheck: ignore TestUtilsKSR
TestUtilsKSR = {}
function TestUtilsKSR:setUp()
if os.getenv('RESULTS') then
self.file = os.getenv('RESULTS').."/test_utils_ksr"
end
end
function TestUtilsKSR:test_simple()
local KSR_log = {}
local KSR = { log = KSR_log }
KSR = utils.KSR_log(KSR, self.file)
lu.assertNotIs(KSR.log, KSR_log)
lu.assertIs(KSR._log, KSR_log)
end
function TestUtilsKSR:test_twice()
local KSR = {}
lu.assertNil(KSR.log)
KSR = utils.KSR_log(KSR, self.file)
local KSR_log = KSR.log
local KSR_logger = KSR._logger
lu.assertNotNil(KSR.log)
KSR = utils.KSR_log(KSR, self.file)
lu.assertIs(KSR.log, KSR_log)
lu.assertNotIs(KSR._logger, KSR_logger)
end
Loading…
Cancel
Save