Add new logger for tests to use
[prosody.git] / tests / util / logger.lua
1 local format = string.format;
2 local print = print;
3 local debug = debug;
4 local tostring = tostring;
5
6 local getstyle, getstring = require "util.termcolours".getstyle, require "util.termcolours".getstring;
7 local do_pretty_printing = not os.getenv("WINDIR");
8
9 module "logger"
10
11 local logstyles = {};
12
13 --TODO: This should be done in config, but we don't have proper config yet
14 if do_pretty_printing then
15         logstyles["info"] = getstyle("bold");
16         logstyles["warn"] = getstyle("bold", "yellow");
17         logstyles["error"] = getstyle("bold", "red");
18 end
19
20 function init(name)
21         --name = nil; -- While this line is not commented, will automatically fill in file/line number info
22         return  function (level, message, ...)
23                                 if level == "debug" or level == "info" then return; end
24                                 if not name then
25                                         local inf = debug.getinfo(3, 'Snl');
26                                         level = level .. ","..tostring(inf.short_src):match("[^/]*$")..":"..inf.currentline;
27                                 end
28                                 if ... then 
29                                         print(name, getstring(logstyles[level], level), format(message, ...));
30                                 else
31                                         print(name, getstring(logstyles[level], level), message);
32                                 end
33                         end
34 end
35
36 return _M;