Diff

init.lua @ 44:37396504de5f

verse: Multiple changes to allow controlling logging for both verse and streams
author Matthew Wild <mwild1@gmail.com>
date Wed, 06 Jan 2010 17:42:37 +0000
parent 42:6006e6bb1c28
child 45:50a2e4fb0a16
line wrap: on
line diff
--- a/init.lua	Mon Jan 04 15:21:32 2010 +0000
+++ b/init.lua	Wed Jan 06 17:42:37 2010 +0000
@@ -14,12 +14,12 @@
 
 verse.plugins = {};
 
-function verse.new(base)
-	local t = base or {};
+function verse.new(logger, base)
+	local t = setmetatable(base or {}, stream);
 	t.id = tostring(t):match("%x*$");
-	t.logger = logger.init(t.id);
+	t:set_logger(logger, true);
 	t.events = events.new();
-	return setmetatable(t, stream);
+	return t;
 end
 
 verse.add_task = require "util.timer".add_task;
@@ -28,6 +28,12 @@
 	return server.loop();
 end
 
+verse.logger = logger.init;
+
+function verse.set_logger(logger)
+	server.setlogger(logger);
+end
+
 function stream:connect(connect_host, connect_port)
 	connect_host = connect_host or "localhost";
 	connect_port = tonumber(connect_port) or 5222;
@@ -55,15 +61,38 @@
 
 -- Logging functions
 function stream:debug(...)
-	return self.logger("debug", ...);
+	if self.logger and self.log.debug then
+		return self.logger("debug", ...);
+	end
 end
 
 function stream:warn(...)
-	return self.logger("warn", ...);
+	if self.logger and self.log.warn then
+		return self.logger("warn", ...);
+	end
 end
 
 function stream:error(...)
-	return self.logger("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
 
 -- Event handling