Merge 0.9->0.10
[prosody.git] / tests / util / logger.lua
1 -- Prosody IM
2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 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 = string.format;
10 local print = print;
11 local debug = debug;
12 local tostring = tostring;
13
14 local getstyle, getstring = require "util.termcolours".getstyle, require "util.termcolours".getstring;
15 local do_pretty_printing = not os.getenv("WINDIR");
16
17 module "logger"
18
19 local logstyles = {};
20
21 --TODO: This should be done in config, but we don't have proper config yet
22 if do_pretty_printing then
23         logstyles["info"] = getstyle("bold");
24         logstyles["warn"] = getstyle("bold", "yellow");
25         logstyles["error"] = getstyle("bold", "red");
26 end
27
28 function init(name)
29         --name = nil; -- While this line is not commented, will automatically fill in file/line number info
30         return  function (level, message, ...)
31                                 if level == "debug" or level == "info" then return; end
32                                 if not name then
33                                         local inf = debug.getinfo(3, 'Snl');
34                                         level = level .. ","..tostring(inf.short_src):match("[^/]*$")..":"..inf.currentline;
35                                 end
36                                 if ... then
37                                         print(name, getstring(logstyles[level], level), format(message, ...));
38                                 else
39                                         print(name, getstring(logstyles[level], level), message);
40                                 end
41                         end
42 end
43
44 return _M;