Merge
[prosody.git] / util / logger.lua
1 -- Prosody IM v0.4
2 -- Copyright (C) 2008-2009 Matthew Wild
3 -- Copyright (C) 2008-2009 Waqas Hussain
4 -- 
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
7 --
8
9 local format, rep = string.format, string.rep;
10 local io_write = io.write;
11 local pcall = pcall;
12 local debug = debug;
13 local tostring = tostring;
14 local math_max = math.max;
15
16 local config = require "core.configmanager";
17 local log_sources = config.get("*", "core", "log_sources");
18
19 local getstyle, getstring = require "util.termcolours".getstyle, require "util.termcolours".getstring;
20 local do_pretty_printing = not os.getenv("WINDIR");
21 local find = string.find;
22 local ipairs = ipairs;
23
24 module "logger"
25
26 local logstyles = {};
27
28 --TODO: This should be done in config, but we don't have proper config yet
29 if do_pretty_printing then
30         logstyles["info"] = getstyle("bold");
31         logstyles["warn"] = getstyle("bold", "yellow");
32         logstyles["error"] = getstyle("bold", "red");
33 end
34
35 local sourcewidth = 20;
36
37 local outfunction = nil;
38
39 function init(name)
40         if log_sources then
41                 local log_this = false;
42                 for _, source in ipairs(log_sources) do
43                         if find(name, source) then 
44                                 log_this = true;
45                                 break;
46                         end
47                 end
48                 
49                 if not log_this then return function () end end
50         end
51         
52         if name == "modulemanager" or name:match("^c2s") or name == "datamanager" then return function () end; end
53         
54         --name = nil; -- While this line is not commented, will automatically fill in file/line number info
55         local namelen = #name;
56         return  function (level, message, ...)
57                                 if outfunction then return outfunction(name, level, message, ...); end
58                                 
59                                 sourcewidth = math_max(#name+2, sourcewidth);
60                                 if ... then 
61                                         io_write(name, rep(" ", sourcewidth-namelen), getstring(logstyles[level], level), "\t", format(message, ...), "\n");
62                                 else
63                                         io_write(name, rep(" ", sourcewidth-namelen), getstring(logstyles[level], level), "\t", message, "\n");
64                                 end
65                         end
66 end
67
68 function setwriter(f)
69         local old_func = outfunction;
70         if not f then outfunction = nil; return true, old_func; end
71         local ok, ret = pcall(f, "logger", "info", "Switched logging output successfully");
72         if ok then
73                 outfunction = f;
74                 ret = old_func;
75         end
76         return ok, ret;
77 end
78
79 return _M;