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_date, os_getenv = os.date, os.getenv;
10 local getstyle, getstring = require "util.termcolours".getstyle, require "util.termcolours".getstring;
12 local config = require "core.configmanager";
13 local eventmanager = require "core.eventmanager";
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" } };
22 local default_file_logging = { { to = "file", levels = { min = "info" } } };
23 local default_timestamp = "%b %d %T";
24 -- The actual config loggingmanager is using
25 local logging_config = config.get("*", "core", "log") or default_logging;
27 local apply_sink_rules;
28 local log_sink_types = setmetatable({}, { __newindex = function (t, k, v) rawset(t, k, v); apply_sink_rules(k); end; });
30 local logging_levels = { "debug", "info", "warn", "error", "critical" }
32 -- Put a rule into action. Requires that the sink type has already been registered.
33 -- This function is called automatically when a new sink type is added [see apply_sink_rules()]
34 local function add_rule(sink_config)
35 local sink_maker = log_sink_types[sink_config.to];
37 if sink_config.levels and not sink_config.source then
39 local sink = sink_maker(sink_config);
41 -- Set sink for all chosen levels
42 for level in pairs(get_levels(sink_config.levels)) do
43 logger.add_level_sink(level, sink);
45 elseif sink_config.source and not sink_config.levels then
46 logger.add_name_sink(sink_config.source, sink_maker(sink_config));
47 elseif sink_config.source and sink_config.levels then
48 local levels = get_levels(sink_config.levels);
49 local sink = sink_maker(sink_config);
50 logger.add_name_sink(sink_config.source,
51 function (name, level, ...)
53 return sink(name, level, ...);
59 local sink = sink_maker(sink_config);
61 -- Set sink for all levels
62 for _, level in pairs(logging_levels) do
63 logger.add_level_sink(level, sink);
71 -- Search for all rules using a particular sink type, and apply
72 -- them. Called automatically when a new sink type is added to
73 -- the log_sink_types table.
74 function apply_sink_rules(sink_type)
75 if type(logging_config) == "table" then
76 for _, sink_config in pairs(logging_config) do
77 if sink_config.to == sink_type then
78 add_rule(sink_config);
81 elseif type(logging_config) == "string" and (not logging_config:match("^%*")) and sink_type == "file" then
82 -- User specified simply a filename, and the "file" sink type
84 for _, sink_config in pairs(default_file_logging) do
85 sink_config.filename = logging_config;
86 add_rule(sink_config);
87 sink_config.filename = nil;
89 elseif type(logging_config) == "string" and logging_config:match("^%*(.+)") == sink_type then
90 -- Log all levels (debug+) to this sink
91 add_rule({ levels = { min = "debug" }, to = sink_type });
97 --- Helper function to get a set of levels given a "criteria" table
98 function get_levels(criteria, set)
100 if type(criteria) == "string" then
101 set[criteria] = true;
104 local min, max = criteria.min, criteria.max;
107 for _, level in ipairs(logging_levels) do
111 elseif max == level then
120 for _, level in ipairs(criteria) do
126 --- Definition of built-in logging sinks ---
128 -- Null sink, must enter log_sink_types *first*
129 function log_sink_types.nowhere()
130 return function () return false; end;
133 -- Column width for "source" (used by stdout and console)
134 local sourcewidth = 20;
136 function log_sink_types.stdout()
137 local timestamps = config.timestamps;
139 if timestamps == true then
140 timestamps = default_timestamp; -- Default format
143 return function (name, level, message, ...)
144 sourcewidth = math_max(#name+2, sourcewidth);
145 local namelen = #name;
147 io_write(os_date(timestamps), " ");
150 io_write(name, rep(" ", sourcewidth-namelen), level, "\t", format(message, ...), "\n");
152 io_write(name, rep(" ", sourcewidth-namelen), level, "\t", message, "\n");
158 local do_pretty_printing = not os_getenv("WINDIR");
160 local logstyles = {};
161 if do_pretty_printing then
162 logstyles["info"] = getstyle("bold");
163 logstyles["warn"] = getstyle("bold", "yellow");
164 logstyles["error"] = getstyle("bold", "red");
166 function log_sink_types.console(config)
167 -- Really if we don't want pretty colours then just use plain stdout
168 if not do_pretty_printing then
169 return log_sink_types.stdout(config);
172 local timestamps = config.timestamps;
174 if timestamps == true then
175 timestamps = default_timestamp; -- Default format
178 return function (name, level, message, ...)
179 sourcewidth = math_max(#name+2, sourcewidth);
180 local namelen = #name;
182 io_write(os_date(timestamps), " ");
185 io_write(name, rep(" ", sourcewidth-namelen), getstring(logstyles[level], level), "\t", format(message, ...), "\n");
187 io_write(name, rep(" ", sourcewidth-namelen), getstring(logstyles[level], level), "\t", message, "\n");
193 local empty_function = function () end;
194 function log_sink_types.file(config)
195 local log = config.filename;
196 local logfile = io_open(log, "a+");
198 return empty_function;
200 local write, flush = logfile.write, logfile.flush;
202 eventmanager.add_event_hook("reopen-log-files", function ()
206 logfile = io_open(log, "a+");
208 write, flush = empty_function, empty_function;
210 write, flush = logfile.write, logfile.flush;
214 local timestamps = config.timestamps;
216 if timestamps == true then
217 timestamps = default_timestamp; -- Default format
220 return function (name, level, message, ...)
222 write(logfile, os_date(timestamps), " ");
225 write(logfile, name, "\t", level, "\t", format(message, ...), "\n");
227 write(logfile, name, "\t" , level, "\t", message, "\n");
233 function register_sink_type(name, sink_maker)
234 local old_sink_maker = log_sink_types[name];
235 log_sink_types[name] = sink_maker;
236 return old_sink_maker;