From 6e2d9f4706ad314e5926cf4b347a90241fb7cd14 Mon Sep 17 00:00:00 2001 From: Victor Seva Date: Tue, 5 Mar 2024 10:11:23 +0100 Subject: [PATCH] MT#59550 utils.KSR_log helper to generate file log when using KSR.log Change-Id: I965799a082f1047bcfc3a53427a6e9a420d9b3e1 --- debian/control | 1 + ngcp/dlgcnt.lua | 20 ++++++++++++---- ngcp/redis.lua | 5 ++-- ngcp/utils.lua | 53 +++++++++++++++++++++++++++++++++++++++++-- tests/ngcp_dlgcnt.lua | 13 +++++++++++ tests/utils_ksr.lua | 50 ++++++++++++++++++++++++++++++++++++++++ 6 files changed, 133 insertions(+), 9 deletions(-) create mode 100644 tests/utils_ksr.lua diff --git a/debian/control b/debian/control index 98748c2..ebaf8fa 100644 --- a/debian/control +++ b/debian/control @@ -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, diff --git a/ngcp/dlgcnt.lua b/ngcp/dlgcnt.lua index f0629f5..901ad88 100644 --- a/ngcp/dlgcnt.lua +++ b/ngcp/dlgcnt.lua @@ -1,5 +1,5 @@ -- --- Copyright 2014-2022 SipWise Team +-- Copyright 2014-2024 SipWise Team -- -- 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) diff --git a/ngcp/redis.lua b/ngcp/redis.lua index 087d2ed..1296828 100644 --- a/ngcp/redis.lua +++ b/ngcp/redis.lua @@ -1,5 +1,5 @@ -- --- Copyright 2022 SipWise Team +-- Copyright 2022-2024 SipWise Team -- -- 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 diff --git a/ngcp/utils.lua b/ngcp/utils.lua index 1345e46..392f36a 100644 --- a/ngcp/utils.lua +++ b/ngcp/utils.lua @@ -1,5 +1,5 @@ -- --- Copyright 2013-2020 SipWise Team +-- Copyright 2013-2024 SipWise Team -- -- 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 diff --git a/tests/ngcp_dlgcnt.lua b/tests/ngcp_dlgcnt.lua index 98dd4de..bcf640f 100644 --- a/tests/ngcp_dlgcnt.lua +++ b/tests/ngcp_dlgcnt.lua @@ -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 diff --git a/tests/utils_ksr.lua b/tests/utils_ksr.lua new file mode 100644 index 0000000..668ee99 --- /dev/null +++ b/tests/utils_ksr.lua @@ -0,0 +1,50 @@ +-- +-- Copyright 2024 SipWise Team +-- +-- 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 . +-- . +-- 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