Merge from waqas
[prosody.git] / util / logger.lua
1 -- Prosody IM v0.1
2 -- Copyright (C) 2008 Matthew Wild
3 -- Copyright (C) 2008 Waqas Hussain
4 -- 
5 -- This program is free software; you can redistribute it and/or
6 -- modify it under the terms of the GNU General Public License
7 -- as published by the Free Software Foundation; either version 2
8 -- of the License, or (at your option) any later version.
9 -- 
10 -- This program is distributed in the hope that it will be useful,
11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 -- GNU General Public License for more details.
14 -- 
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
17 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18 --
19
20 local format, rep = string.format, string.rep;
21 local io_write = io.write;
22 local pcall = pcall;
23 local debug = debug;
24 local tostring = tostring;
25 local math_max = math.max;
26
27 local getstyle, getstring = require "util.termcolours".getstyle, require "util.termcolours".getstring;
28 local do_pretty_printing = not os.getenv("WINDIR");
29
30 module "logger"
31
32 local logstyles = {};
33
34 --TODO: This should be done in config, but we don't have proper config yet
35 if do_pretty_printing then
36         logstyles["info"] = getstyle("bold");
37         logstyles["warn"] = getstyle("bold", "yellow");
38         logstyles["error"] = getstyle("bold", "red");
39 end
40
41 local sourcewidth = 20;
42
43 local outfunction = nil;
44
45 function init(name)
46         --name = nil; -- While this line is not commented, will automatically fill in file/line number info
47         sourcewidth = math_max(#name+2, sourcewidth);
48         local namelen = #name;
49         return  function (level, message, ...)
50                                 if not name then
51                                         local inf = debug.getinfo(3, 'Snl');
52                                         level = level .. ","..tostring(inf.short_src):match("[^/]*$")..":"..inf.currentline;
53                                 end
54                                 
55                                 if outfunction then return outfunction(name, level, message, ...); end
56                                 
57                                 if ... then 
58                                         io_write(name, rep(" ", sourcewidth-namelen), getstring(logstyles[level], level), "\t", format(message, ...), "\n");
59                                 else
60                                         io_write(name, rep(" ", sourcewidth-namelen), getstring(logstyles[level], level), "\t", message, "\n");
61                                 end
62                         end
63 end
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
74 return _M;