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