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";
15 local debug_mode = config.get("*", "core", "debug");
17 _G.log = logger.init("general");
19 module "loggingmanager"
21 -- The log config used if none specified in the config file
22 local default_logging = { { to = "console" } };
23 local default_file_logging = { { to = "file", levels = { min = (debug_mode and "debug") or "info" }, timestamps = true } };
24 local default_timestamp = "%b %d %T";
25 -- The actual config loggingmanager is using
26 local logging_config = config.get("*", "core", "log") or default_logging;
28 local apply_sink_rules;
29 local log_sink_types = setmetatable({}, { __newindex = function (t, k, v) rawset(t, k, v); apply_sink_rules(k); end; });
31 local logging_levels = { "debug", "info", "warn", "error", "critical" }
33 -- Put a rule into action. Requires that the sink type has already been registered.
34 -- This function is called automatically when a new sink type is added [see apply_sink_rules()]
35 local function add_rule(sink_config)
36 local sink_maker = log_sink_types[sink_config.to];
38 if sink_config.levels and not sink_config.source then
40 local sink = sink_maker(sink_config);
42 -- Set sink for all chosen levels
43 for level in pairs(get_levels(sink_config.levels)) do
44 logger.add_level_sink(level, sink);
46 elseif sink_config.source and not sink_config.levels then
47 logger.add_name_sink(sink_config.source, sink_maker(sink_config));
48 elseif sink_config.source and sink_config.levels then
49 local levels = get_levels(sink_config.levels);
50 local sink = sink_maker(sink_config);
51 logger.add_name_sink(sink_config.source,
52 function (name, level, ...)
54 return sink(name, level, ...);
60 local sink = sink_maker(sink_config);
62 -- Set sink for all levels
63 for _, level in pairs(logging_levels) do
64 logger.add_level_sink(level, sink);
72 -- Search for all rules using a particular sink type, and apply
73 -- them. Called automatically when a new sink type is added to
74 -- the log_sink_types table.
75 function apply_sink_rules(sink_type)
76 if type(logging_config) == "table" then
77 for _, sink_config in pairs(logging_config) do
78 if sink_config.to == sink_type then
79 add_rule(sink_config);
82 elseif type(logging_config) == "string" and (not logging_config:match("^%*")) and sink_type == "file" then
83 -- User specified simply a filename, and the "file" sink type
85 for _, sink_config in pairs(default_file_logging) do
86 sink_config.filename = logging_config;
87 add_rule(sink_config);
88 sink_config.filename = nil;
90 elseif type(logging_config) == "string" and logging_config:match("^%*(.+)") == sink_type then
91 -- Log all levels (debug+) to this sink
92 add_rule({ levels = { min = "debug" }, to = sink_type });
98 --- Helper function to get a set of levels given a "criteria" table
99 function get_levels(criteria, set)
101 if type(criteria) == "string" then
102 set[criteria] = true;
105 local min, max = criteria.min, criteria.max;
108 for _, level in ipairs(logging_levels) do
112 elseif max == level then
121 for _, level in ipairs(criteria) do
127 --- Definition of built-in logging sinks ---
129 -- Null sink, must enter log_sink_types *first*
130 function log_sink_types.nowhere()
131 return function () return false; end;
134 -- Column width for "source" (used by stdout and console)
135 local sourcewidth = 20;
137 function log_sink_types.stdout()
138 local timestamps = config.timestamps;
140 if timestamps == true then
141 timestamps = default_timestamp; -- Default format
144 return function (name, level, message, ...)
145 sourcewidth = math_max(#name+2, sourcewidth);
146 local namelen = #name;
148 io_write(os_date(timestamps), " ");
151 io_write(name, rep(" ", sourcewidth-namelen), level, "\t", format(message, ...), "\n");
153 io_write(name, rep(" ", sourcewidth-namelen), level, "\t", message, "\n");
159 local do_pretty_printing = not os_getenv("WINDIR");
161 local logstyles = {};
162 if do_pretty_printing then
163 logstyles["info"] = getstyle("bold");
164 logstyles["warn"] = getstyle("bold", "yellow");
165 logstyles["error"] = getstyle("bold", "red");
167 function log_sink_types.console(config)
168 -- Really if we don't want pretty colours then just use plain stdout
169 if not do_pretty_printing then
170 return log_sink_types.stdout(config);
173 local timestamps = config.timestamps;
175 if timestamps == true then
176 timestamps = default_timestamp; -- Default format
179 return function (name, level, message, ...)
180 sourcewidth = math_max(#name+2, sourcewidth);
181 local namelen = #name;
183 io_write(os_date(timestamps), " ");
186 io_write(name, rep(" ", sourcewidth-namelen), getstring(logstyles[level], level), "\t", format(message, ...), "\n");
188 io_write(name, rep(" ", sourcewidth-namelen), getstring(logstyles[level], level), "\t", message, "\n");
194 local empty_function = function () end;
195 function log_sink_types.file(config)
196 local log = config.filename;
197 local logfile = io_open(log, "a+");
199 return empty_function;
201 local write, flush = logfile.write, logfile.flush;
203 eventmanager.add_event_hook("reopen-log-files", function ()
207 logfile = io_open(log, "a+");
209 write, flush = empty_function, empty_function;
211 write, flush = logfile.write, logfile.flush;
215 local timestamps = config.timestamps;
217 if timestamps == true then
218 timestamps = default_timestamp; -- Default format
221 return function (name, level, message, ...)
223 write(logfile, os_date(timestamps), " ");
226 write(logfile, name, "\t", level, "\t", format(message, ...), "\n");
228 write(logfile, name, "\t" , level, "\t", message, "\n");
234 function register_sink_type(name, sink_maker)
235 local old_sink_maker = log_sink_types[name];
236 log_sink_types[name] = sink_maker;
237 return old_sink_maker;