Changeset

1034:2b0aba0bb013

Merge with 0.4.1
author Matthew Wild <mwild1@gmail.com>
date Wed, 22 Apr 2009 20:14:11 +0100
parents 1025:efd71cae24ea (current diff) 1033:4a9f0d482028 (diff)
children 1035:8cc7e4452b2b
files
diffstat 5 files changed, 240 insertions(+), 158 deletions(-) [+]
line wrap: on
line diff
--- a/core/loggingmanager.lua	Tue Apr 21 21:11:12 2009 +0100
+++ b/core/loggingmanager.lua	Wed Apr 22 20:14:11 2009 +0100
@@ -1,142 +1,84 @@
 
 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);
+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
-				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);
-					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;
-
--- Column width for "source" (used by stdout and console)
-
-function log_sink_types.nowhere()
-	return function () return false; end;
-end
-
-local sourcewidth = 20;
-
-function log_sink_types.stdout()
-	return function (name, level, message, ...)
-		sourcewidth = math_max(#name+2, sourcewidth);
-		local namelen = #name;
-		if ... then 
-			io_write(name, rep(" ", sourcewidth-namelen), level, "\t", format(message, ...), "\n");
-		else
-			io_write(name, rep(" ", sourcewidth-namelen), level, "\t", message, "\n");
-		end
-	end	
-end
-
-do
-	local getstyle, getstring = require "util.termcolours".getstyle, require "util.termcolours".getstring;
-	local do_pretty_printing = not os.getenv("WINDIR");
-	
-	local logstyles = {};
-	if do_pretty_printing then
-		logstyles["info"] = getstyle("bold");
-		logstyles["warn"] = getstyle("bold", "yellow");
-		logstyles["error"] = getstyle("bold", "red");
-	end
-	function log_sink_types.console(config)
-		-- Really if we don't want pretty colours then just use plain stdout
-		if not do_pretty_printing then
-			return log_sink_types.stdout(config);
-		end
-		
-		return function (name, level, message, ...)
-			sourcewidth = math_max(#name+2, sourcewidth);
-			local namelen = #name;
-			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");
+-- 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
 
-function log_sink_types.file(config)
-	local log = config.filename;
-	local logfile = io.open(log, "a+");
-	if not logfile then
-		return function () end
-	end
 
-	local write, format, flush = logfile.write, string.format, logfile.flush;
-	return 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
-
-function log_sink_types.syslog()
-end
 
 --- Helper function to get a set of levels given a "criteria" table
 function get_levels(criteria, set)
@@ -167,6 +109,76 @@
 	return set;
 end
 
---- Set up logging
-setup_logging();
+--- 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()
+	return function (name, level, message, ...)
+		sourcewidth = math_max(#name+2, sourcewidth);
+		local namelen = #name;
+		if ... then 
+			io_write(name, rep(" ", sourcewidth-namelen), level, "\t", format(message, ...), "\n");
+		else
+			io_write(name, rep(" ", sourcewidth-namelen), level, "\t", message, "\n");
+		end
+	end	
+end
 
+do
+	local do_pretty_printing = not os_getenv("WINDIR");
+	
+	local logstyles = {};
+	if do_pretty_printing then
+		logstyles["info"] = getstyle("bold");
+		logstyles["warn"] = getstyle("bold", "yellow");
+		logstyles["error"] = getstyle("bold", "red");
+	end
+	function log_sink_types.console(config)
+		-- Really if we don't want pretty colours then just use plain stdout
+		if not do_pretty_printing then
+			return log_sink_types.stdout(config);
+		end
+		
+		return function (name, level, message, ...)
+			sourcewidth = math_max(#name+2, sourcewidth);
+			local namelen = #name;
+			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");
+			end
+		end
+	end
+end
+
+function log_sink_types.file(config)
+	local log = config.filename;
+	local logfile = io_open(log, "a+");
+	if not logfile then
+		return function () end
+	end
+
+	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");
+		else
+			write(logfile, name, "\t" , level, "\t", message, "\n");
+		end
+		flush(logfile);
+	end;
+end
+
+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
+
+return _M;
--- a/plugins/mod_posix.lua	Tue Apr 21 21:11:12 2009 +0100
+++ b/plugins/mod_posix.lua	Wed Apr 22 20:14:11 2009 +0100
@@ -14,7 +14,19 @@
 
 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 @@
 		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;
+local syslog_opened 
+function syslog_sink_maker(config)
+	if not syslog_opened then
+		print("OPENING SYSLOOOOOOOOOG");
+		pposix.syslog_open("prosody");
+		syslog_opened = true;
 	end
-else
-	log("debug", "No logging specified, will continue with default");
+	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
-
-if logwriter then
-	local ok, ret = logger_set(logwriter);
-	if not ok then
-		log("error", "Couldn't set new log output: %s", ret);
-	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 @@
 	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 ()
--- a/prosody	Tue Apr 21 21:11:12 2009 +0100
+++ b/prosody	Wed Apr 22 20:14:11 2009 +0100
@@ -160,6 +160,7 @@
 -- 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 @@
 	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 @@
 end
 
 server.closeall();
+
+eventmanager.fire_event("server-stopped");
--- a/util/array.lua	Tue Apr 21 21:11:12 2009 +0100
+++ b/util/array.lua	Wed Apr 22 20:14:11 2009 +0100
@@ -52,4 +52,17 @@
 	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;
--- a/util/set.lua	Tue Apr 21 21:11:12 2009 +0100
+++ b/util/set.lua	Wed Apr 22 20:14:11 2009 +0100
@@ -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 @@
 		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 @@
 	return set;
 end
 
+function xor(set1, set2)
+	return union(set1, set2) - intersection(set1, set2);
+end
+
 return _M;