Make it possible to set custom output handler for logger

Sat, 06 Dec 2008 23:13:38 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Sat, 06 Dec 2008 23:13:38 +0000
changeset 582
8eb45a8099c4
parent 581
23b9cd1206ba
child 583
5821eaa80baa

Make it possible to set custom output handler for logger

util/logger.lua file | annotate | diff | comparison | revisions
--- a/util/logger.lua	Sat Dec 06 23:12:46 2008 +0000
+++ b/util/logger.lua	Sat Dec 06 23:13:38 2008 +0000
@@ -17,11 +17,9 @@
 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 --
 
-
-
 local format, rep = string.format, string.rep;
 local io_write = io.write;
-local print = print;
+local pcall = pcall;
 local debug = debug;
 local tostring = tostring;
 local math_max = math.max;
@@ -42,6 +40,8 @@
 
 local sourcewidth = 20;
 
+local outfunction = nil;
+
 function init(name)
 	--name = nil; -- While this line is not commented, will automatically fill in file/line number info
 	sourcewidth = math_max(#name+2, sourcewidth);
@@ -51,6 +51,9 @@
 					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
@@ -59,4 +62,13 @@
 			end
 end
 
+function setwriter(f)
+	if not f then outfunction = nil; return true, nil; end
+	local ok, ret = pcall(f, "logger", "info", "Switched logging output successfully");
+	if ok then
+		outfunction = f;
+	end
+	return ok, ret;
+end
+
 return _M;

mercurial