From bf7d712864d75ef280371ffb9fdca39e51d32405 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Sat, 6 Dec 2008 23:13:38 +0000 Subject: Make it possible to set custom output handler for logger --- util/logger.lua | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) (limited to 'util/logger.lua') diff --git a/util/logger.lua b/util/logger.lua index 82a7e110..f7ea187b 100644 --- a/util/logger.lua +++ b/util/logger.lua @@ -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 @@ end 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 @@ function init(name) 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 @@ function init(name) 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; -- cgit v1.2.3