2 local format, rep = string.format, string.rep;
5 local tostring, setmetatable, rawset, pairs, ipairs, type =
6 tostring, setmetatable, rawset, pairs, ipairs, type;
7 local io_open, io_write = io.open, io.write;
8 local math_max, rep = math.max, string.rep;
9 local os_getenv = os.getenv;
10 local getstyle, getstring = require "util.termcolours".getstyle, require "util.termcolours".getstring;
12 local config = require "core.configmanager";
14 local logger = require "util.logger";
16 module "loggingmanager"
18 -- The log config used if none specified in the config file
19 local default_logging = { { to = "console" } };
21 -- The actual config loggingmanager is using
22 local logging_config = config.get("*", "core", "log") or default_logging;
24 local apply_sink_rules;
25 local log_sink_types = setmetatable({}, { __newindex = function (t, k, v) rawset(t, k, v); apply_sink_rules(k); end; });
27 local logging_levels = { "debug", "info", "warn", "error", "critical" }
29 local function add_rule(sink_config)
30 local sink_maker = log_sink_types[sink_config.to];
32 if sink_config.levels and not sink_config.source then
34 local sink = sink_maker(sink_config);
36 -- Set sink for all chosen levels
37 for level in pairs(get_levels(sink_config.levels)) do
38 logger.add_level_sink(level, sink);
40 elseif sink_config.source and not sink_config.levels then
41 logger.add_name_sink(sink_config.source, sink_maker(sink_config));
42 elseif sink_config.source and sink_config.levels then
43 local levels = get_levels(sink_config.levels);
44 local sink = sink_maker(sink_config);
45 logger.add_name_sink(sink_config.source,
46 function (name, level, ...)
48 return sink(name, level, ...);
54 local sink = sink_maker(sink_config);
56 -- Set sink for all levels
57 for _, level in pairs(logging_levels) do
58 logger.add_level_sink(level, sink);
66 -- Search for all rules using a particular sink type,
68 function apply_sink_rules(sink_type)
69 if type(logging_config) == "table" then
70 for _, sink_config in pairs(logging_config) do
71 if sink_config.to == sink_type then
72 add_rule(sink_config);
75 elseif type(logging_config) == "string" and sink_type == "file" then
76 -- User specified simply a filename, and the "file" sink type
83 --- Helper function to get a set of levels given a "criteria" table
84 function get_levels(criteria, set)
86 if type(criteria) == "string" then
90 local min, max = criteria.min, criteria.max;
93 for _, level in ipairs(logging_levels) do
97 elseif max == level then
106 for _, level in ipairs(criteria) do
112 --- Definition of built-in logging sinks ---
114 function log_sink_types.nowhere()
115 return function () return false; end;
118 -- Column width for "source" (used by stdout and console)
119 local sourcewidth = 20;
121 function log_sink_types.stdout()
122 return function (name, level, message, ...)
123 sourcewidth = math_max(#name+2, sourcewidth);
124 local namelen = #name;
126 io_write(name, rep(" ", sourcewidth-namelen), level, "\t", format(message, ...), "\n");
128 io_write(name, rep(" ", sourcewidth-namelen), level, "\t", message, "\n");
134 local do_pretty_printing = not os_getenv("WINDIR");
136 local logstyles = {};
137 if do_pretty_printing then
138 logstyles["info"] = getstyle("bold");
139 logstyles["warn"] = getstyle("bold", "yellow");
140 logstyles["error"] = getstyle("bold", "red");
142 function log_sink_types.console(config)
143 -- Really if we don't want pretty colours then just use plain stdout
144 if not do_pretty_printing then
145 return log_sink_types.stdout(config);
148 return function (name, level, message, ...)
149 sourcewidth = math_max(#name+2, sourcewidth);
150 local namelen = #name;
152 io_write(name, rep(" ", sourcewidth-namelen), getstring(logstyles[level], level), "\t", format(message, ...), "\n");
154 io_write(name, rep(" ", sourcewidth-namelen), getstring(logstyles[level], level), "\t", message, "\n");
160 function log_sink_types.file(config)
161 local log = config.filename;
162 local logfile = io_open(log, "a+");
164 return function () end
167 local write, format, flush = logfile.write, format, logfile.flush;
168 return function (name, level, message, ...)
170 write(logfile, name, "\t", level, "\t", format(message, ...), "\n");
172 write(logfile, name, "\t" , level, "\t", message, "\n");
178 function register_sink_type(name, sink_maker)
179 local old_sink_maker = log_sink_types[name];
180 log_sink_types[name] = sink_maker;
181 return old_sink_maker;