2 -- Copyright (C) 2008-2009 Matthew Wild
3 -- Copyright (C) 2008-2009 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 config = require "core.configmanager";
17 local log_sources = config.get("*", "core", "log_sources");
19 local getstyle, getstring = require "util.termcolours".getstyle, require "util.termcolours".getstring;
20 local do_pretty_printing = not os.getenv("WINDIR");
21 local find = string.find;
22 local ipairs = ipairs;
28 --TODO: This should be done in config, but we don't have proper config yet
29 if do_pretty_printing then
30 logstyles["info"] = getstyle("bold");
31 logstyles["warn"] = getstyle("bold", "yellow");
32 logstyles["error"] = getstyle("bold", "red");
35 local sourcewidth = 20;
37 local outfunction = nil;
41 local log_this = false;
42 for _, source in ipairs(log_sources) do
43 if find(name, source) then
49 if not log_this then return function () end end
52 --name = nil; -- While this line is not commented, will automatically fill in file/line number info
53 local namelen = #name;
54 return function (level, message, ...)
55 if outfunction then return outfunction(name, level, message, ...); end
57 sourcewidth = math_max(#name+2, sourcewidth);
59 io_write(name, rep(" ", sourcewidth-namelen), getstring(logstyles[level], level), "\t", format(message, ...), "\n");
61 io_write(name, rep(" ", sourcewidth-namelen), getstring(logstyles[level], level), "\t", message, "\n");
67 local old_func = outfunction;
68 if not f then outfunction = nil; return true, old_func; end
69 local ok, ret = pcall(f, "logger", "info", "Switched logging output successfully");