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";
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;
94 --- Helper function to get a set of levels given a "criteria" table
95 function get_levels(criteria, set)
97 if type(criteria) == "string" then
101 local min, max = criteria.min, criteria.max;
104 for _, level in ipairs(logging_levels) do
108 elseif max == level then
117 for _, level in ipairs(criteria) do
123 --- Definition of built-in logging sinks ---
125 -- Null sink, must enter log_sink_types *first*
126 function log_sink_types.nowhere()
127 return function () return false; end;
130 -- Column width for "source" (used by stdout and console)
131 local sourcewidth = 20;
133 function log_sink_types.stdout()
134 local timestamps = config.timestamps;
136 if timestamps == true then
137 timestamps = default_timestamp; -- Default format
140 return function (name, level, message, ...)
141 sourcewidth = math_max(#name+2, sourcewidth);
142 local namelen = #name;
144 io_write(os_date(timestamps), " ");
147 io_write(name, rep(" ", sourcewidth-namelen), level, "\t", format(message, ...), "\n");
149 io_write(name, rep(" ", sourcewidth-namelen), level, "\t", message, "\n");
155 local do_pretty_printing = not os_getenv("WINDIR");
157 local logstyles = {};
158 if do_pretty_printing then
159 logstyles["info"] = getstyle("bold");
160 logstyles["warn"] = getstyle("bold", "yellow");
161 logstyles["error"] = getstyle("bold", "red");
163 function log_sink_types.console(config)
164 -- Really if we don't want pretty colours then just use plain stdout
165 if not do_pretty_printing then
166 return log_sink_types.stdout(config);
169 local timestamps = config.timestamps;
171 if timestamps == true then
172 timestamps = default_timestamp; -- Default format
175 return function (name, level, message, ...)
176 sourcewidth = math_max(#name+2, sourcewidth);
177 local namelen = #name;
179 io_write(os_date(timestamps), " ");
182 io_write(name, rep(" ", sourcewidth-namelen), getstring(logstyles[level], level), "\t", format(message, ...), "\n");
184 io_write(name, rep(" ", sourcewidth-namelen), getstring(logstyles[level], level), "\t", message, "\n");
190 function log_sink_types.file(config)
191 local log = config.filename;
192 local logfile = io_open(log, "a+");
194 return function () end
197 local timestamps = config.timestamps;
199 if timestamps == true then
200 timestamps = default_timestamp; -- Default format
203 local write, format, flush = logfile.write, format, logfile.flush;
204 return function (name, level, message, ...)
206 write(logfile, os_date(timestamps), " ");
209 write(logfile, name, "\t", level, "\t", format(message, ...), "\n");
211 write(logfile, name, "\t" , level, "\t", message, "\n");
217 function register_sink_type(name, sink_maker)
218 local old_sink_maker = log_sink_types[name];
219 log_sink_types[name] = sink_maker;
220 return old_sink_maker;