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 _G.log = logger.init("general");
18 module "loggingmanager"
20 -- The log config used if none specified in the config file
21 local default_logging = { { to = "console" } };
23 -- The actual config loggingmanager is using
24 local logging_config = config.get("*", "core", "log") or default_logging;
26 local apply_sink_rules;
27 local log_sink_types = setmetatable({}, { __newindex = function (t, k, v) rawset(t, k, v); apply_sink_rules(k); end; });
29 local logging_levels = { "debug", "info", "warn", "error", "critical" }
31 local function add_rule(sink_config)
32 local sink_maker = log_sink_types[sink_config.to];
34 if sink_config.levels and not sink_config.source then
36 local sink = sink_maker(sink_config);
38 -- Set sink for all chosen levels
39 for level in pairs(get_levels(sink_config.levels)) do
40 logger.add_level_sink(level, sink);
42 elseif sink_config.source and not sink_config.levels then
43 logger.add_name_sink(sink_config.source, sink_maker(sink_config));
44 elseif sink_config.source and sink_config.levels then
45 local levels = get_levels(sink_config.levels);
46 local sink = sink_maker(sink_config);
47 logger.add_name_sink(sink_config.source,
48 function (name, level, ...)
50 return sink(name, level, ...);
56 local sink = sink_maker(sink_config);
58 -- Set sink for all levels
59 for _, level in pairs(logging_levels) do
60 logger.add_level_sink(level, sink);
68 -- Search for all rules using a particular sink type,
70 function apply_sink_rules(sink_type)
71 if type(logging_config) == "table" then
72 for _, sink_config in pairs(logging_config) do
73 if sink_config.to == sink_type then
74 add_rule(sink_config);
77 elseif type(logging_config) == "string" and sink_type == "file" then
78 -- User specified simply a filename, and the "file" sink type
85 --- Helper function to get a set of levels given a "criteria" table
86 function get_levels(criteria, set)
88 if type(criteria) == "string" then
92 local min, max = criteria.min, criteria.max;
95 for _, level in ipairs(logging_levels) do
99 elseif max == level then
108 for _, level in ipairs(criteria) do
114 --- Definition of built-in logging sinks ---
116 function log_sink_types.nowhere()
117 return function () return false; end;
120 -- Column width for "source" (used by stdout and console)
121 local sourcewidth = 20;
123 function log_sink_types.stdout()
124 return function (name, level, message, ...)
125 sourcewidth = math_max(#name+2, sourcewidth);
126 local namelen = #name;
128 io_write(name, rep(" ", sourcewidth-namelen), level, "\t", format(message, ...), "\n");
130 io_write(name, rep(" ", sourcewidth-namelen), level, "\t", message, "\n");
136 local do_pretty_printing = not os_getenv("WINDIR");
138 local logstyles = {};
139 if do_pretty_printing then
140 logstyles["info"] = getstyle("bold");
141 logstyles["warn"] = getstyle("bold", "yellow");
142 logstyles["error"] = getstyle("bold", "red");
144 function log_sink_types.console(config)
145 -- Really if we don't want pretty colours then just use plain stdout
146 if not do_pretty_printing then
147 return log_sink_types.stdout(config);
150 return function (name, level, message, ...)
151 sourcewidth = math_max(#name+2, sourcewidth);
152 local namelen = #name;
154 io_write(name, rep(" ", sourcewidth-namelen), getstring(logstyles[level], level), "\t", format(message, ...), "\n");
156 io_write(name, rep(" ", sourcewidth-namelen), getstring(logstyles[level], level), "\t", message, "\n");
162 function log_sink_types.file(config)
163 local log = config.filename;
164 local logfile = io_open(log, "a+");
166 return function () end
169 local write, format, flush = logfile.write, format, logfile.flush;
170 return function (name, level, message, ...)
172 write(logfile, name, "\t", level, "\t", format(message, ...), "\n");
174 write(logfile, name, "\t" , level, "\t", message, "\n");
180 function register_sink_type(name, sink_maker)
181 local old_sink_maker = log_sink_types[name];
182 log_sink_types[name] = sink_maker;
183 return old_sink_maker;