util/logger.lua

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

mercurial