From 8c2ea6e093baf4559d92d6d407fda355fd62807b Mon Sep 17 00:00:00 2001 From: Alexey Melnichuk Date: Wed, 9 Sep 2015 10:45:49 +0400 Subject: [PATCH] Add. Basic log class. --- .../resources/scripts/dialplan/dialplan.lua | 9 +-- .../scripts/resources/functions/log.lua | 70 +++++++++++++++++++ 2 files changed, 75 insertions(+), 4 deletions(-) create mode 100644 resources/install/scripts/resources/functions/log.lua diff --git a/resources/install/scripts/app/xml_handler/resources/scripts/dialplan/dialplan.lua b/resources/install/scripts/app/xml_handler/resources/scripts/dialplan/dialplan.lua index 232612118d..5ccc18f06b 100644 --- a/resources/install/scripts/app/xml_handler/resources/scripts/dialplan/dialplan.lua +++ b/resources/install/scripts/app/xml_handler/resources/scripts/dialplan/dialplan.lua @@ -25,15 +25,16 @@ -- POSSIBILITY OF SUCH DAMAGE. local cache = require"resources.functions.cache" + local log = require"resources.functions.log"["xml_handler"] --get the cache XML_STRING, err = cache.get("dialplan:" .. call_context) if debug['cache'] then if XML_STRING then - freeswitch.consoleLog("notice", "[xml_handler] dialplan:"..call_context.." source: memcache\n"); + log.notice("dialplan:"..call_context.." source: memcache"); elseif err ~= 'NOT FOUND' then - freeswitch.consoleLog("notice", "[xml_handler] error get element form cache: " .. err .. "\n"); + log.notice("error get element form cache: " .. err); end end @@ -105,7 +106,7 @@ sql = sql .. "ELSE 100 END, "; sql = sql .. "s.dialplan_detail_order asc "; if (debug["sql"]) then - freeswitch.consoleLog("notice", "[xml_handler] SQL: " .. sql .. "\n"); + log.notice("SQL: " .. sql); end x = 0; dbh:query(sql, function(row) @@ -306,7 +307,7 @@ --send to the console if (debug["cache"]) then - freeswitch.consoleLog("notice", "[xml_handler] dialplan:"..call_context.." source: database\n"); + log.notice("dialplan:"..call_context.." source: database"); end --close the database connection diff --git a/resources/install/scripts/resources/functions/log.lua b/resources/install/scripts/resources/functions/log.lua new file mode 100644 index 0000000000..b6cb59652b --- /dev/null +++ b/resources/install/scripts/resources/functions/log.lua @@ -0,0 +1,70 @@ +-- @usage local log = require"resources.functions.log"["xml_handler"] +-- log.notice("hello world") +-- log.noticef("%s %s", "hello", "world") +-- -- log if debug.SQL or debug.xml_handler.SQL then +-- log.tracef("SQL", "SQL is %s", sql) + +local function log(name, level, msg) + freeswitch.consoleLog(level, "[" .. name .. "] " .. msg .. "\n") +end + +local function logf(name, level, ...) + return log(name, level, string.format(...)) +end + +local function trace(type, name, ...) + local t = debug[name] + if t and t[type] ~= nil then + if t[type] then + return log(name, ...) + end + end + if debug[type] then + log(name, ...) + end +end + +local function tracef(type, name, level, ...) + local t = debug[name] + if t and t[type] ~= nil then + if t[type] then + return logf(name, ...) + end + end + if debug[type] then + logf(name, ...) + end +end + +local LEVELS = { + 'error', + 'warning', + 'notice', + 'info', +} + +local TRACE_LEVEL = 'notice' + +local function make_log(name) + local logger = {} + for i = 1, #LEVELS do + logger[ LEVELS[i] ] = function(...) return log(name, LEVELS[i], ...) end; + logger[ LEVELS[i] .. "f" ] = function(...) return logf(name, LEVELS[i], ...) end; + end + + logger.trace = function(type, ...) + trace(type, name, TRACE_LEVEL, ...) + end + + logger.tracef = function(type, ...) + tracef(type, name, TRACE_LEVEL, ...) + end + + return logger +end + +return setmetatable({}, {__index = function(self, name) + local logger = make_log(name) + self[name] = logger + return logger +end}) \ No newline at end of file