X-Git-Url: https://git.enpas.org/?a=blobdiff_plain;ds=sidebyside;f=util%2Flogger.lua;h=80b5bcd08416b9f02d0ebebb57f34470d40ef341;hb=d89f50109f97ff133c2ee9270e54851d7939aa69;hp=5115bbdaaeadc545fe2b2ef0a93b4e205911e721;hpb=5466de23f56d2121e384f47fe14f32b4ff5ed587;p=prosody.git diff --git a/util/logger.lua b/util/logger.lua index 5115bbda..80b5bcd0 100644 --- a/util/logger.lua +++ b/util/logger.lua @@ -1,74 +1,140 @@ --- Prosody IM v0.2 --- Copyright (C) 2008 Matthew Wild --- Copyright (C) 2008 Waqas Hussain +-- Prosody IM +-- Copyright (C) 2008-2010 Matthew Wild +-- Copyright (C) 2008-2010 Waqas Hussain -- --- 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 2 --- of the License, or (at your option) any later version. --- --- This program 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, write to the Free Software --- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +-- This project is MIT/X11 licensed. Please see the +-- COPYING file in the source package for more information. -- -local format, rep = string.format, string.rep; -local io_write = io.write; local pcall = pcall; -local debug = debug; -local tostring = tostring; -local math_max = math.max; -local getstyle, getstring = require "util.termcolours".getstyle, require "util.termcolours".getstring; -local do_pretty_printing = not os.getenv("WINDIR"); +local find = string.find; +local ipairs, pairs, setmetatable = ipairs, pairs, setmetatable; module "logger" -local logstyles = {}; - ---TODO: This should be done in config, but we don't have proper config yet -if do_pretty_printing then - logstyles["info"] = getstyle("bold"); - logstyles["warn"] = getstyle("bold", "yellow"); - logstyles["error"] = getstyle("bold", "red"); -end +local name_sinks, level_sinks = {}, {}; +local name_patterns = {}; -local sourcewidth = 20; +-- Weak-keyed so that loggers are collected +local modify_hooks = setmetatable({}, { __mode = "k" }); +local make_logger; local outfunction = nil; function init(name) + local log_debug = make_logger(name, "debug"); + local log_info = make_logger(name, "info"); + local log_warn = make_logger(name, "warn"); + local log_error = make_logger(name, "error"); + --name = nil; -- While this line is not commented, will automatically fill in file/line number info - sourcewidth = math_max(#name+2, sourcewidth); local namelen = #name; - return function (level, message, ...) - if not name then - local inf = debug.getinfo(3, 'Snl'); - level = level .. ","..tostring(inf.short_src):match("[^/]*$")..":"..inf.currentline; - end - - if outfunction then return outfunction(name, level, message, ...); end - - if ... then - io_write(name, rep(" ", sourcewidth-namelen), getstring(logstyles[level], level), "\t", format(message, ...), "\n"); - else - io_write(name, rep(" ", sourcewidth-namelen), getstring(logstyles[level], level), "\t", message, "\n"); + return function (level, message, ...) + if outfunction then return outfunction(name, level, message, ...); end + + if level == "debug" then + return log_debug(message, ...); + elseif level == "info" then + return log_info(message, ...); + elseif level == "warn" then + return log_warn(message, ...); + elseif level == "error" then + return log_error(message, ...); + end + end +end + +function make_logger(source_name, level) + local level_handlers = level_sinks[level]; + if not level_handlers then + level_handlers = {}; + level_sinks[level] = level_handlers; + end + + local source_handlers = name_sinks[source_name]; + + -- All your premature optimisation is belong to me! + local num_level_handlers, num_source_handlers = #level_handlers, source_handlers and #source_handlers; + + local logger = function (message, ...) + if source_handlers then + for i = 1,num_source_handlers do + if source_handlers[i](source_name, level, message, ...) == false then + return; end end + end + + for i = 1,num_level_handlers do + level_handlers[i](source_name, level, message, ...); + end + end + + -- To make sure our cached lengths stay in sync with reality + modify_hooks[logger] = function () num_level_handlers, num_source_handlers = #level_handlers, source_handlers and #source_handlers; end; + + return logger; end function setwriter(f) - if not f then outfunction = nil; return true, nil; end + local old_func = outfunction; + if not f then outfunction = nil; return true, old_func; end local ok, ret = pcall(f, "logger", "info", "Switched logging output successfully"); if ok then outfunction = f; + ret = old_func; end return ok, ret; end +function reset() + for k in pairs(name_sinks) do name_sinks[k] = nil; end + for level, handler_list in pairs(level_sinks) do + -- Clear all handlers for this level + for i = 1, #handler_list do + handler_list[i] = nil; + end + end + for k in pairs(name_patterns) do name_patterns[k] = nil; end + + for _, modify_hook in pairs(modify_hooks) do + modify_hook(); + end +end + +function add_level_sink(level, sink_function) + if not level_sinks[level] then + level_sinks[level] = { sink_function }; + else + level_sinks[level][#level_sinks[level] + 1 ] = sink_function; + end + + for _, modify_hook in pairs(modify_hooks) do + modify_hook(); + end +end + +function add_name_sink(name, sink_function, exclusive) + if not name_sinks[name] then + name_sinks[name] = { sink_function }; + else + name_sinks[name][#name_sinks[name] + 1] = sink_function; + end + + for _, modify_hook in pairs(modify_hooks) do + modify_hook(); + end +end + +function add_name_pattern_sink(name_pattern, sink_function, exclusive) + if not name_patterns[name_pattern] then + name_patterns[name_pattern] = { sink_function }; + else + name_patterns[name_pattern][#name_patterns[name_pattern] + 1] = sink_function; + end +end + +_M.new = make_logger; + return _M;