Software /
code /
verse
Changeset
238:9b6fca5d9a02
verse, squishy: Port to Prosody's util.logger. Adds new function, verse.set_log_handler(handler, levels).
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Sun, 27 Nov 2011 22:45:26 +0000 |
parents | 237:d158c97556b4 |
children | 239:65a0d3fcbbad |
files | init.lua libs/logger.lua squishy |
diffstat | 3 files changed, 11 insertions(+), 69 deletions(-) [+] |
line wrap: on
line diff
--- a/init.lua Sun Nov 27 22:07:59 2011 +0000 +++ b/init.lua Sun Nov 27 22:45:26 2011 +0000 @@ -22,7 +22,7 @@ function verse.new(logger, base) local t = setmetatable(base or {}, stream); t.id = tostring(t):match("%x*$"); - t:set_logger(logger or verse.log, true); + t.logger = logger or verse.new_logger("stream"..t.id); t.events = events.new(); t.plugins = {}; return t; @@ -30,25 +30,16 @@ verse.add_task = require "util.timer".add_task; -verse.logger = logger.init; -- Deprecated +verse.logger = logger.init; -- COMPAT: Deprecated verse.new_logger = logger.init; verse.log = verse.logger("verse"); -function verse.set_logger(logger) - verse.log = logger; - server.setlogger(logger); -end - -function verse.filter_log(levels, logger) - local level_set = {}; - for _, level in ipairs(levels) do - level_set[level] = true; +function verse.set_log_handler(log_handler, levels) + levels = levels or { "debug", "info", "warn", "error" }; + logger.reset(); + for i, level in ipairs(levels) do + logger.add_level_sink(level, log_handler); end - return function (level, name, ...) - if level_set[level] then - return logger(level, name, ...); - end - end; end local function error_handler(err) @@ -114,42 +105,15 @@ -- Logging functions function stream:debug(...) - if self.logger and self.log.debug then - return self.logger("debug", ...); - end + return self.logger("debug", ...); end function stream:warn(...) - if self.logger and self.log.warn then - return self.logger("warn", ...); - end + return self.logger("warn", ...); end function stream:error(...) - if self.logger and self.log.error then - return self.logger("error", ...); - end -end - -function stream:set_logger(logger, levels) - local old_logger = self.logger; - if logger then - self.logger = logger; - end - if levels then - if levels == true then - levels = { "debug", "info", "warn", "error" }; - end - self.log = {}; - for _, level in ipairs(levels) do - self.log[level] = true; - end - end - return old_logger; -end - -function stream_mt:set_log_levels(levels) - self:set_logger(nil, levels); + return self.logger("error", ...); end -- Event handling
--- a/libs/logger.lua Sun Nov 27 22:07:59 2011 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,22 +0,0 @@ -local select, tostring = select, tostring; -local io_write = io.write; -module "logger" - -local function format(format, ...) - local n, maxn = 0, #arg; - return (format:gsub("%%(.)", function (c) if c ~= "%" and n <= maxn then n = n + 1; return tostring(arg[n]); end end)); -end - -local function format(format, ...) - local n, maxn = 0, select('#', ...); - local arg = { ... }; - return (format:gsub("%%(.)", function (c) if n <= maxn then n = n + 1; return tostring(arg[n]); end end)); -end - -function init(name) - return function (level, message, ...) - io_write(level, "\t", format(message, ...), "\n"); - end -end - -return _M;
--- a/squishy Sun Nov 27 22:07:59 2011 +0000 +++ b/squishy Sun Nov 27 22:45:26 2011 +0000 @@ -3,7 +3,6 @@ -- Verse-specific versions of libraries Module "util.encodings" "libs/encodings.lua" Module "util.hashes" "libs/hashes.lua" -Module "util.logger" "libs/logger.lua" Module "util.sha1" "util/sha1.lua" Module "lib.adhoc" "libs/adhoc.lib.lua" @@ -27,6 +26,7 @@ Module "util.dataforms" "util/dataforms.lua" Module "util.caps" "util/caps.lua" Module "util.vcard" "util/vcard.lua" +Module "util.logger" "util/logger.lua" -- Verse plugins plugins = {