diff options
author | Matthew Wild <mwild1@gmail.com> | 2009-04-22 20:14:11 +0100 |
---|---|---|
committer | Matthew Wild <mwild1@gmail.com> | 2009-04-22 20:14:11 +0100 |
commit | 77195f9fb9f2092fc2ac3fc379185a9210de2a57 (patch) | |
tree | e57cf40c3f3d1cac021d60e36ddcf5d1a95f8133 | |
parent | 84bdf9494c891172c03f81186e0e8ff1a39364d7 (diff) | |
parent | fc2a4159849cf5fcb2c53d4fb0c0f1a7b101d69a (diff) | |
download | prosody-77195f9fb9f2092fc2ac3fc379185a9210de2a57.tar.gz prosody-77195f9fb9f2092fc2ac3fc379185a9210de2a57.zip |
Merge with 0.4.1
-rw-r--r-- | core/loggingmanager.lua | 190 | ||||
-rw-r--r-- | plugins/mod_posix.lua | 68 | ||||
-rwxr-xr-x | prosody | 5 | ||||
-rw-r--r-- | util/array.lua | 15 | ||||
-rw-r--r-- | util/set.lua | 66 |
5 files changed, 213 insertions, 131 deletions
diff --git a/core/loggingmanager.lua b/core/loggingmanager.lua index 53ebb99b..36f83861 100644 --- a/core/loggingmanager.lua +++ b/core/loggingmanager.lua @@ -1,80 +1,121 @@ 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 tostring, setmetatable, rawset, pairs, ipairs, type = + tostring, setmetatable, rawset, pairs, ipairs, type; +local io_open, io_write = io.open, io.write; +local math_max, rep = math.max, string.rep; +local os_getenv = os.getenv; +local getstyle, getstring = require "util.termcolours".getstyle, require "util.termcolours".getstring; + +local config = require "core.configmanager"; local logger = require "util.logger"; +module "loggingmanager" + +-- The log config used if none specified in the config file local default_logging = { { to = "console" } }; --- Global log function, because some people are too --- lazy to get their own... -_G.log = logger.init("general"); +-- The actual config loggingmanager is using +local logging_config = config.get("*", "core", "log") or default_logging; -local log_sink_types = {}; +local apply_sink_rules; +local log_sink_types = setmetatable({}, { __newindex = function (t, k, v) rawset(t, k, v); apply_sink_rules(k); end; }); local get_levels; local logging_levels = { "debug", "info", "warn", "error", "critical" } ---- Main function to read config, create the appropriate sinks and tell logger module -function setup_logging(log) - log = log or config.get("*", "core", "log") or default_logging; - -- Set default logger - if type(log) == "string" then - if not log:match("^%*") then - end - elseif type(log) == "table" then - -- Advanced configuration chosen - for i, sink_config in ipairs(log) do - local sink_maker = log_sink_types[sink_config.to]; - if sink_maker then - if sink_config.levels and not sink_config.source then - -- Create sink - local sink = sink_maker(sink_config); - - -- Set sink for all chosen levels - for level in pairs(get_levels(sink_config.levels)) do - logger.add_level_sink(level, sink); - end - elseif sink_config.source and not sink_config.levels then - logger.add_name_sink(sink_config.source, sink_maker(sink_config)); - elseif sink_config.source and sink_config.levels then - local levels = get_levels(sink_config.levels); - local sink = sink_maker(sink_config); - logger.add_name_sink(sink_config.source, - function (name, level, ...) - if levels[level] then - return sink(name, level, ...); - end - end); - else - -- All sources - -- Create sink - local sink = sink_maker(sink_config); - - -- Set sink for all levels - for _, level in pairs(logging_levels) do - logger.add_level_sink(level, sink); +local function add_rule(sink_config) + local sink_maker = log_sink_types[sink_config.to]; + if sink_maker then + if sink_config.levels and not sink_config.source then + -- Create sink + local sink = sink_maker(sink_config); + + -- Set sink for all chosen levels + for level in pairs(get_levels(sink_config.levels)) do + logger.add_level_sink(level, sink); + end + elseif sink_config.source and not sink_config.levels then + logger.add_name_sink(sink_config.source, sink_maker(sink_config)); + elseif sink_config.source and sink_config.levels then + local levels = get_levels(sink_config.levels); + local sink = sink_maker(sink_config); + logger.add_name_sink(sink_config.source, + function (name, level, ...) + if levels[level] then + return sink(name, level, ...); end - end - else - -- No such sink type + end); + else + -- All sources + -- Create sink + local sink = sink_maker(sink_config); + + -- Set sink for all levels + for _, level in pairs(logging_levels) do + logger.add_level_sink(level, sink); end end + else + -- No such sink type end end ---- Definition of built-in logging sinks --- -local math_max, rep = math.max, string.rep; +-- Search for all rules using a particular sink type, +-- and apply them +function apply_sink_rules(sink_type) + if type(logging_config) == "table" then + for _, sink_config in pairs(logging_config) do + if sink_config.to == sink_type then + add_rule(sink_config); + end + end + elseif type(logging_config) == "string" and sink_type == "file" then + -- User specified simply a filename, and the "file" sink type + -- was just added + end +end --- Column width for "source" (used by stdout and console) + + +--- Helper function to get a set of levels given a "criteria" table +function get_levels(criteria, set) + set = set or {}; + if type(criteria) == "string" then + set[criteria] = true; + return set; + end + local min, max = criteria.min, criteria.max; + if min or max then + local in_range; + for _, level in ipairs(logging_levels) do + if min == level then + set[level] = true; + in_range = true; + elseif max == level then + set[level] = true; + return set; + elseif in_range then + set[level] = true; + end + end + end + + for _, level in ipairs(criteria) do + set[level] = true; + end + return set; +end + +--- Definition of built-in logging sinks --- function log_sink_types.nowhere() return function () return false; end; end +-- Column width for "source" (used by stdout and console) local sourcewidth = 20; function log_sink_types.stdout() @@ -90,8 +131,7 @@ function log_sink_types.stdout() end do - local getstyle, getstring = require "util.termcolours".getstyle, require "util.termcolours".getstring; - local do_pretty_printing = not os.getenv("WINDIR"); + local do_pretty_printing = not os_getenv("WINDIR"); local logstyles = {}; if do_pretty_printing then @@ -119,12 +159,12 @@ end function log_sink_types.file(config) local log = config.filename; - local logfile = io.open(log, "a+"); + local logfile = io_open(log, "a+"); if not logfile then return function () end end - local write, format, flush = logfile.write, string.format, logfile.flush; + local write, format, flush = logfile.write, format, logfile.flush; return function (name, level, message, ...) if ... then write(logfile, name, "\t", level, "\t", format(message, ...), "\n"); @@ -135,38 +175,10 @@ function log_sink_types.file(config) end; end -function log_sink_types.syslog() +function register_sink_type(name, sink_maker) + local old_sink_maker = log_sink_types[name]; + log_sink_types[name] = sink_maker; + return old_sink_maker; end ---- Helper function to get a set of levels given a "criteria" table -function get_levels(criteria, set) - set = set or {}; - if type(criteria) == "string" then - set[criteria] = true; - return set; - end - local min, max = criteria.min, criteria.max; - if min or max then - local in_range; - for _, level in ipairs(logging_levels) do - if min == level then - set[level] = true; - in_range = true; - elseif max == level then - set[level] = true; - return set; - elseif in_range then - set[level] = true; - end - end - end - - for _, level in ipairs(criteria) do - set[level] = true; - end - return set; -end - ---- Set up logging -setup_logging(); - +return _M; diff --git a/plugins/mod_posix.lua b/plugins/mod_posix.lua index 2c56f694..f8fda7cd 100644 --- a/plugins/mod_posix.lua +++ b/plugins/mod_posix.lua @@ -14,7 +14,19 @@ local logger_set = require "util.logger".setwriter; module.host = "*"; -- we're a global module +local pidfile_written; + +local function remove_pidfile() + if pidfile_written then + os.remove(pidfile); + pidfile_written = nil; + end +end + local function write_pidfile() + if pidfile_written then + remove_pidfile(); + end local pidfile = config.get("*", "core", "pidfile"); if pidfile then local pf, err = io.open(pidfile, "w+"); @@ -23,51 +35,31 @@ local function write_pidfile() else pf:write(tostring(pposix.getpid())); pf:close(); + pidfile_written = pidfile; end end end -local logfilename = config_get("*", "core", "log"); -if logfilename == "syslog" then - pposix.syslog_open("prosody"); - pposix.syslog_setminlevel(config.get("*", "core", "minimum_log_level") or "info"); - local syslog, format = pposix.syslog_log, string.format; - logwriter = function (name, level, message, ...) - if ... then - syslog(level, format(message, ...)); - else - syslog(level, message); - end - end; -elseif logfilename then - local logfile = io.open(logfilename, "a+"); - if logfile then - local write, format, flush = logfile.write, string.format, logfile.flush; - logwriter = function (name, level, message, ...) - if ... then - write(logfile, name, "\t", level, "\t", format(message, ...), "\n"); - else - write(logfile, name, "\t" , level, "\t", message, "\n"); - end - flush(logfile); - end; - end -else - log("debug", "No logging specified, will continue with default"); -end - -if logwriter then - local ok, ret = logger_set(logwriter); - if not ok then - log("error", "Couldn't set new log output: %s", ret); +local syslog_opened +function syslog_sink_maker(config) + if not syslog_opened then + print("OPENING SYSLOOOOOOOOOG"); + pposix.syslog_open("prosody"); + syslog_opened = true; end + local syslog, format = pposix.syslog_log, string.format; + return function (name, level, message, ...) + if ... then + syslog(level, format(message, ...)); + else + syslog(level, message); + end + end; end +require "core.loggingmanager".register_sink_type("syslog", syslog_sink_maker); if not config_get("*", "core", "no_daemonize") then local function daemonize_server() - local logwriter; - - local ok, ret = pposix.daemonize(); if not ok then log("error", "Failed to daemonize: %s", ret); @@ -80,10 +72,12 @@ if not config_get("*", "core", "no_daemonize") then end module:add_event_hook("server-starting", daemonize_server); else + -- Not going to daemonize, so write the pid of this process write_pidfile(); - -- Not going to daemonize, but let's write the pidfile anyway end +module:add_event_hook("server-stopped", remove_pidfile); + -- Set signal handler if signal.signal then signal.signal("SIGTERM", function () @@ -160,6 +160,7 @@ end -- Global function to initiate prosody shutdown function prosody_shutdown(reason) log("info", "Shutting down: %s", reason or "unknown reason"); + eventmanager.fire_event("server-stopping", { reason = reason }); server.setquitting(true); end @@ -198,6 +199,8 @@ while select(2, xpcall(server.loop, catch_uncaught_error)) ~= "quitting" do socket.sleep(0.2); end +eventmanager.fire_event("server-cleanup"); + -- Ok, we're quitting I know, but we -- need to do some tidying before we go :) server.setquitting(false); @@ -224,3 +227,5 @@ for hostname, host in pairs(hosts) do end server.closeall(); + +eventmanager.fire_event("server-stopped"); diff --git a/util/array.lua b/util/array.lua index 9e99a3ec..072b24a7 100644 --- a/util/array.lua +++ b/util/array.lua @@ -52,4 +52,17 @@ function array:reverse() end end -_G.array = array +function array.collect(f, s, var) + local t, var = {}; + while true do + var = f(s, var); + if var == nil then break; end + table.insert(t, var); + end + return setmetatable(t, array_mt); +end + +_G.array = array; +module("array"); + +return array; diff --git a/util/set.lua b/util/set.lua index f550a721..84bfe8c4 100644 --- a/util/set.lua +++ b/util/set.lua @@ -1,10 +1,60 @@ -local ipairs, pairs = - ipairs, pairs; +local ipairs, pairs, setmetatable, next, tostring = + ipairs, pairs, setmetatable, next, tostring; +local t_concat = table.concat; module "set" +local set_mt = {}; +function set_mt.__call(set, _, k) + return next(set._items, k); +end +function set_mt.__add(set1, set2) + return _M.union(set1, set2); +end +function set_mt.__sub(set1, set2) + return _M.difference(set1, set2); +end +function set_mt.__div(set, func) + local new_set, new_items = _M.new(); + local items, new_items = set._items, new_set._items; + for item in pairs(items) do + if func(item) then + new_items[item] = true; + end + end + return new_set; +end +function set_mt.__eq(set1, set2) + local set1, set2 = set1._items, set2._items; + for item in pairs(set1) do + if not set2[item] then + return false; + end + end + + for item in pairs(set2) do + if not set1[item] then + return false; + end + end + + return true; +end +function set_mt.__tostring(set) + local s, items = { }, set._items; + for item in pairs(items) do + s[#s+1] = tostring(item); + end + return t_concat(s, ", "); +end + +local items_mt = {}; +function items_mt.__call(items, _, k) + return next(items, k); +end + function new(list) - local items = {}; + local items = setmetatable({}, items_mt); local set = { _items = items }; function set:add(item) @@ -41,11 +91,15 @@ function new(list) end end + function set:empty() + return not next(items); + end + if list then set:add_list(list); end - return set; + return setmetatable(set, set_mt); end function union(set1, set2) @@ -87,4 +141,8 @@ function intersection(set1, set2) return set; end +function xor(set1, set2) + return union(set1, set2) - intersection(set1, set2); +end + return _M; |