Software /
code /
prosody
Comparison
util/logger.lua @ 582:8eb45a8099c4
Make it possible to set custom output handler for logger
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Sat, 06 Dec 2008 23:13:38 +0000 |
parent | 519:cccd610a0ef9 |
child | 615:4ae3e81513f3 |
comparison
equal
deleted
inserted
replaced
581:23b9cd1206ba | 582:8eb45a8099c4 |
---|---|
15 -- You should have received a copy of the GNU General Public License | 15 -- You should have received a copy of the GNU General Public License |
16 -- along with this program; if not, write to the Free Software | 16 -- along with this program; if not, write to the Free Software |
17 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | 17 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
18 -- | 18 -- |
19 | 19 |
20 | |
21 | |
22 local format, rep = string.format, string.rep; | 20 local format, rep = string.format, string.rep; |
23 local io_write = io.write; | 21 local io_write = io.write; |
24 local print = print; | 22 local pcall = pcall; |
25 local debug = debug; | 23 local debug = debug; |
26 local tostring = tostring; | 24 local tostring = tostring; |
27 local math_max = math.max; | 25 local math_max = math.max; |
28 | 26 |
29 local getstyle, getstring = require "util.termcolours".getstyle, require "util.termcolours".getstring; | 27 local getstyle, getstring = require "util.termcolours".getstyle, require "util.termcolours".getstring; |
40 logstyles["error"] = getstyle("bold", "red"); | 38 logstyles["error"] = getstyle("bold", "red"); |
41 end | 39 end |
42 | 40 |
43 local sourcewidth = 20; | 41 local sourcewidth = 20; |
44 | 42 |
43 local outfunction = nil; | |
44 | |
45 function init(name) | 45 function init(name) |
46 --name = nil; -- While this line is not commented, will automatically fill in file/line number info | 46 --name = nil; -- While this line is not commented, will automatically fill in file/line number info |
47 sourcewidth = math_max(#name+2, sourcewidth); | 47 sourcewidth = math_max(#name+2, sourcewidth); |
48 local namelen = #name; | 48 local namelen = #name; |
49 return function (level, message, ...) | 49 return function (level, message, ...) |
50 if not name then | 50 if not name then |
51 local inf = debug.getinfo(3, 'Snl'); | 51 local inf = debug.getinfo(3, 'Snl'); |
52 level = level .. ","..tostring(inf.short_src):match("[^/]*$")..":"..inf.currentline; | 52 level = level .. ","..tostring(inf.short_src):match("[^/]*$")..":"..inf.currentline; |
53 end | 53 end |
54 | |
55 if outfunction then return outfunction(name, level, message, ...); end | |
56 | |
54 if ... then | 57 if ... then |
55 io_write(name, rep(" ", sourcewidth-namelen), getstring(logstyles[level], level), "\t", format(message, ...), "\n"); | 58 io_write(name, rep(" ", sourcewidth-namelen), getstring(logstyles[level], level), "\t", format(message, ...), "\n"); |
56 else | 59 else |
57 io_write(name, rep(" ", sourcewidth-namelen), getstring(logstyles[level], level), "\t", message, "\n"); | 60 io_write(name, rep(" ", sourcewidth-namelen), getstring(logstyles[level], level), "\t", message, "\n"); |
58 end | 61 end |
59 end | 62 end |
60 end | 63 end |
61 | 64 |
65 function setwriter(f) | |
66 if not f then outfunction = nil; return true, nil; end | |
67 local ok, ret = pcall(f, "logger", "info", "Switched logging output successfully"); | |
68 if ok then | |
69 outfunction = f; | |
70 end | |
71 return ok, ret; | |
72 end | |
73 | |
62 return _M; | 74 return _M; |