2 -- Copyright (C) 2008-2009 Matthew Wild
3 -- Copyright (C) 2008-2009 Waqas Hussain
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
10 local format, rep = string.format, string.rep;
13 local tostring, setmetatable, rawset, pairs, ipairs, type =
14 tostring, setmetatable, rawset, pairs, ipairs, type;
15 local io_open, io_write = io.open, io.write;
16 local math_max, rep = math.max, string.rep;
17 local os_date, os_getenv = os.date, os.getenv;
18 local getstyle, getstring = require "util.termcolours".getstyle, require "util.termcolours".getstring;
20 if os.getenv("__FLUSH_LOG") then
21 local io_flush = io.flush;
22 local _io_write = io_write;
23 io_write = function(...) _io_write(...); io_flush(); end
26 local config = require "core.configmanager";
27 local eventmanager = require "core.eventmanager";
28 local logger = require "util.logger";
29 local debug_mode = config.get("*", "core", "debug");
31 _G.log = logger.init("general");
33 module "loggingmanager"
35 -- The log config used if none specified in the config file
36 local default_logging = { { to = "console" } };
37 local default_file_logging = { { to = "file", levels = { min = (debug_mode and "debug") or "info" }, timestamps = true } };
38 local default_timestamp = "%b %d %T";
39 -- The actual config loggingmanager is using
40 local logging_config = config.get("*", "core", "log") or default_logging;
42 local apply_sink_rules;
43 local log_sink_types = setmetatable({}, { __newindex = function (t, k, v) rawset(t, k, v); apply_sink_rules(k); end; });
45 local logging_levels = { "debug", "info", "warn", "error", "critical" }
47 -- Put a rule into action. Requires that the sink type has already been registered.
48 -- This function is called automatically when a new sink type is added [see apply_sink_rules()]
49 local function add_rule(sink_config)
50 local sink_maker = log_sink_types[sink_config.to];
52 if sink_config.levels and not sink_config.source then
54 local sink = sink_maker(sink_config);
56 -- Set sink for all chosen levels
57 for level in pairs(get_levels(sink_config.levels)) do
58 logger.add_level_sink(level, sink);
60 elseif sink_config.source and not sink_config.levels then
61 logger.add_name_sink(sink_config.source, sink_maker(sink_config));
62 elseif sink_config.source and sink_config.levels then
63 local levels = get_levels(sink_config.levels);
64 local sink = sink_maker(sink_config);
65 logger.add_name_sink(sink_config.source,
66 function (name, level, ...)
68 return sink(name, level, ...);
74 local sink = sink_maker(sink_config);
76 -- Set sink for all levels
77 for _, level in pairs(logging_levels) do
78 logger.add_level_sink(level, sink);
86 -- Search for all rules using a particular sink type, and apply
87 -- them. Called automatically when a new sink type is added to
88 -- the log_sink_types table.
89 function apply_sink_rules(sink_type)
90 if type(logging_config) == "table" then
91 for _, sink_config in pairs(logging_config) do
92 if sink_config.to == sink_type then
93 add_rule(sink_config);
96 elseif type(logging_config) == "string" and (not logging_config:match("^%*")) and sink_type == "file" then
97 -- User specified simply a filename, and the "file" sink type
99 for _, sink_config in pairs(default_file_logging) do
100 sink_config.filename = logging_config;
101 add_rule(sink_config);
102 sink_config.filename = nil;
104 elseif type(logging_config) == "string" and logging_config:match("^%*(.+)") == sink_type then
105 -- Log all levels (debug+) to this sink
106 add_rule({ levels = { min = "debug" }, to = sink_type });
112 --- Helper function to get a set of levels given a "criteria" table
113 function get_levels(criteria, set)
115 if type(criteria) == "string" then
116 set[criteria] = true;
119 local min, max = criteria.min, criteria.max;
122 for _, level in ipairs(logging_levels) do
126 elseif max == level then
135 for _, level in ipairs(criteria) do
141 --- Definition of built-in logging sinks ---
143 -- Null sink, must enter log_sink_types *first*
144 function log_sink_types.nowhere()
145 return function () return false; end;
148 -- Column width for "source" (used by stdout and console)
149 local sourcewidth = 20;
151 function log_sink_types.stdout()
152 local timestamps = config.timestamps;
154 if timestamps == true then
155 timestamps = default_timestamp; -- Default format
158 return function (name, level, message, ...)
159 sourcewidth = math_max(#name+2, sourcewidth);
160 local namelen = #name;
162 io_write(os_date(timestamps), " ");
165 io_write(name, rep(" ", sourcewidth-namelen), level, "\t", format(message, ...), "\n");
167 io_write(name, rep(" ", sourcewidth-namelen), level, "\t", message, "\n");
173 local do_pretty_printing = not os_getenv("WINDIR");
175 local logstyles = {};
176 if do_pretty_printing then
177 logstyles["info"] = getstyle("bold");
178 logstyles["warn"] = getstyle("bold", "yellow");
179 logstyles["error"] = getstyle("bold", "red");
181 function log_sink_types.console(config)
182 -- Really if we don't want pretty colours then just use plain stdout
183 if not do_pretty_printing then
184 return log_sink_types.stdout(config);
187 local timestamps = config.timestamps;
189 if timestamps == true then
190 timestamps = default_timestamp; -- Default format
193 return function (name, level, message, ...)
194 sourcewidth = math_max(#name+2, sourcewidth);
195 local namelen = #name;
198 io_write(os_date(timestamps), " ");
201 io_write(name, rep(" ", sourcewidth-namelen), getstring(logstyles[level], level), "\t", format(message, ...), "\n");
203 io_write(name, rep(" ", sourcewidth-namelen), getstring(logstyles[level], level), "\t", message, "\n");
209 local empty_function = function () end;
210 function log_sink_types.file(config)
211 local log = config.filename;
212 local logfile = io_open(log, "a+");
214 return empty_function;
216 local write, flush = logfile.write, logfile.flush;
218 eventmanager.add_event_hook("reopen-log-files", function ()
222 logfile = io_open(log, "a+");
224 write, flush = empty_function, empty_function;
226 write, flush = logfile.write, logfile.flush;
230 local timestamps = config.timestamps;
232 if timestamps == nil or timestamps == true then
233 timestamps = default_timestamp; -- Default format
236 return function (name, level, message, ...)
238 write(logfile, os_date(timestamps), " ");
241 write(logfile, name, "\t", level, "\t", format(message, ...), "\n");
243 write(logfile, name, "\t" , level, "\t", message, "\n");
249 function register_sink_type(name, sink_maker)
250 local old_sink_maker = log_sink_types[name];
251 log_sink_types[name] = sink_maker;
252 return old_sink_maker;