2 -- Copyright (C) 2008 Matthew Wild
3 -- Copyright (C) 2008 Waqas Hussain
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
9 local format, rep = string.format, string.rep;
10 local io_write = io.write;
13 local tostring = tostring;
14 local math_max = math.max;
16 local getstyle, getstring = require "util.termcolours".getstyle, require "util.termcolours".getstring;
17 local do_pretty_printing = not os.getenv("WINDIR");
23 --TODO: This should be done in config, but we don't have proper config yet
24 if do_pretty_printing then
25 logstyles["info"] = getstyle("bold");
26 logstyles["warn"] = getstyle("bold", "yellow");
27 logstyles["error"] = getstyle("bold", "red");
30 local sourcewidth = 20;
32 local outfunction = nil;
35 --name = nil; -- While this line is not commented, will automatically fill in file/line number info
36 sourcewidth = math_max(#name+2, sourcewidth);
37 local namelen = #name;
38 return function (level, message, ...)
40 local inf = debug.getinfo(3, 'Snl');
41 level = level .. ","..tostring(inf.short_src):match("[^/]*$")..":"..inf.currentline;
44 if outfunction then return outfunction(name, level, message, ...); end
47 io_write(name, rep(" ", sourcewidth-namelen), getstring(logstyles[level], level), "\t", format(message, ...), "\n");
49 io_write(name, rep(" ", sourcewidth-namelen), getstring(logstyles[level], level), "\t", message, "\n");
55 local old_func = outfunction;
56 if not f then outfunction = nil; return true, old_func; end
57 local ok, ret = pcall(f, "logger", "info", "Switched logging output successfully");