loggingmanager: Re-read 'debug' option on reload.
[prosody.git] / core / loggingmanager.lua
1 -- Prosody IM
2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
4 -- 
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
7 --
8
9
10 local format, rep = string.format, string.rep;
11 local pcall = pcall;
12 local debug = debug;
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, setstyle = require "util.termcolours".getstyle, require "util.termcolours".setstyle;
19
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
24 end
25
26 local config = require "core.configmanager";
27 local logger = require "util.logger";
28 local prosody = prosody;
29
30 _G.log = logger.init("general");
31
32 module "loggingmanager"
33
34 -- The log config used if none specified in the config file (see reload_logging for initialization)
35 local default_logging;
36 local default_file_logging;
37 local default_timestamp = "%b %d %H:%M:%S";
38 -- The actual config loggingmanager is using
39 local logging_config;
40
41 local apply_sink_rules;
42 local log_sink_types = setmetatable({}, { __newindex = function (t, k, v) rawset(t, k, v); apply_sink_rules(k); end; });
43 local get_levels;
44 local logging_levels = { "debug", "info", "warn", "error", "critical" }
45
46 -- Put a rule into action. Requires that the sink type has already been registered.
47 -- This function is called automatically when a new sink type is added [see apply_sink_rules()]
48 local function add_rule(sink_config)
49         local sink_maker = log_sink_types[sink_config.to];
50         if sink_maker then
51                 if sink_config.levels and not sink_config.source then
52                         -- Create sink
53                         local sink = sink_maker(sink_config);
54                         
55                         -- Set sink for all chosen levels
56                         for level in pairs(get_levels(sink_config.levels)) do
57                                 logger.add_level_sink(level, sink);
58                         end
59                 elseif sink_config.source and not sink_config.levels then
60                         logger.add_name_sink(sink_config.source, sink_maker(sink_config));
61                 elseif sink_config.source and sink_config.levels then
62                         local levels = get_levels(sink_config.levels);
63                         local sink = sink_maker(sink_config);
64                         logger.add_name_sink(sink_config.source,
65                                 function (name, level, ...)
66                                         if levels[level] then
67                                                 return sink(name, level, ...);
68                                         end
69                                 end);
70                 else
71                         -- All sources
72                         -- Create sink
73                         local sink = sink_maker(sink_config);
74                         
75                         -- Set sink for all levels
76                         for _, level in pairs(logging_levels) do
77                                 logger.add_level_sink(level, sink);
78                         end
79                 end
80         else
81                 -- No such sink type
82         end
83 end
84
85 -- Search for all rules using a particular sink type, and apply
86 -- them. Called automatically when a new sink type is added to
87 -- the log_sink_types table.
88 function apply_sink_rules(sink_type)
89         if type(logging_config) == "table" then
90                 
91                 if sink_type == "file" then
92                         for _, level in ipairs(logging_levels) do
93                                 if type(logging_config[level]) == "string" then
94                                         add_rule({
95                                                 to = "file",
96                                                 filename = logging_config[level],
97                                                 timestamps = true,
98                                                 levels = { min = level },
99                                         });
100                                 end
101                         end
102                 end
103                 
104                 for _, sink_config in pairs(logging_config) do
105                         if (type(sink_config) == "table" and sink_config.to == sink_type) then
106                                 add_rule(sink_config);
107                         elseif (type(sink_config) == "string" and sink_config:match("^%*(.+)") == sink_type) then
108                                 add_rule({ levels = { min = "debug" }, to = sink_type });
109                         end
110                 end
111         elseif type(logging_config) == "string" and (not logging_config:match("^%*")) and sink_type == "file" then
112                 -- User specified simply a filename, and the "file" sink type
113                 -- was just added
114                 for _, sink_config in pairs(default_file_logging) do
115                         sink_config.filename = logging_config;
116                         add_rule(sink_config);
117                         sink_config.filename = nil;
118                 end
119         elseif type(logging_config) == "string" and logging_config:match("^%*(.+)") == sink_type then
120                 -- Log all levels (debug+) to this sink
121                 add_rule({ levels = { min = "debug" }, to = sink_type });
122         end
123 end
124
125
126
127 --- Helper function to get a set of levels given a "criteria" table
128 function get_levels(criteria, set)
129         set = set or {};
130         if type(criteria) == "string" then
131                 set[criteria] = true;
132                 return set;
133         end
134         local min, max = criteria.min, criteria.max;
135         if min or max then
136                 local in_range;
137                 for _, level in ipairs(logging_levels) do
138                         if min == level then
139                                 set[level] = true;
140                                 in_range = true;
141                         elseif max == level then
142                                 set[level] = true;
143                                 return set;
144                         elseif in_range then
145                                 set[level] = true;
146                         end
147                 end
148         end
149         
150         for _, level in ipairs(criteria) do
151                 set[level] = true;
152         end
153         return set;
154 end
155
156 -- Initialize config, etc. --
157 function reload_logging()
158         local old_sink_types = {};
159         
160         for name, sink_maker in pairs(log_sink_types) do
161                 old_sink_types[name] = sink_maker;
162                 log_sink_types[name] = nil;
163         end
164         
165         logger.reset();
166
167         local debug_mode = config.get("*", "core", "debug");
168
169         default_logging = { { to = "console" , levels = { min = (debug_mode and "debug") or "info" } } };
170         default_file_logging = {
171                 { to = "file", levels = { min = (debug_mode and "debug") or "info" }, timestamps = true }
172         };
173         default_timestamp = "%b %d %H:%M:%S";
174
175         logging_config = config.get("*", "core", "log") or default_logging;
176         
177         
178         for name, sink_maker in pairs(old_sink_types) do
179                 log_sink_types[name] = sink_maker;
180         end
181         
182         prosody.events.fire_event("logging-reloaded");
183 end
184
185 reload_logging();
186 prosody.events.add_handler("config-reloaded", reload_logging);
187
188 --- Definition of built-in logging sinks ---
189
190 -- Null sink, must enter log_sink_types *first*
191 function log_sink_types.nowhere()
192         return function () return false; end;
193 end
194
195 -- Column width for "source" (used by stdout and console)
196 local sourcewidth = 20;
197
198 function log_sink_types.stdout()
199         local timestamps = config.timestamps;
200         
201         if timestamps == true then
202                 timestamps = default_timestamp; -- Default format
203         end
204         
205         return function (name, level, message, ...)
206                 sourcewidth = math_max(#name+2, sourcewidth);
207                 local namelen = #name;
208                 if timestamps then
209                         io_write(os_date(timestamps), " ");
210                 end
211                 if ... then
212                         io_write(name, rep(" ", sourcewidth-namelen), level, "\t", format(message, ...), "\n");
213                 else
214                         io_write(name, rep(" ", sourcewidth-namelen), level, "\t", message, "\n");
215                 end
216         end
217 end
218
219 do
220         local do_pretty_printing = true;
221         
222         local logstyles = {};
223         if do_pretty_printing then
224                 logstyles["info"] = getstyle("bold");
225                 logstyles["warn"] = getstyle("bold", "yellow");
226                 logstyles["error"] = getstyle("bold", "red");
227         end
228         function log_sink_types.console(config)
229                 -- Really if we don't want pretty colours then just use plain stdout
230                 if not do_pretty_printing then
231                         return log_sink_types.stdout(config);
232                 end
233                 
234                 local timestamps = config.timestamps;
235
236                 if timestamps == true then
237                         timestamps = default_timestamp; -- Default format
238                 end
239
240                 return function (name, level, message, ...)
241                         sourcewidth = math_max(#name+2, sourcewidth);
242                         local namelen = #name;
243                         
244                         if timestamps then
245                                 io_write(os_date(timestamps), " ");
246                         end
247                         io_write(name, rep(" ", sourcewidth-namelen));
248                         setstyle(logstyles[level]);
249                         io_write(level);
250                         setstyle();
251                         if ... then
252                                 io_write("\t", format(message, ...), "\n");
253                         else
254                                 io_write("\t", message, "\n");
255                         end
256                 end
257         end
258 end
259
260 local empty_function = function () end;
261 function log_sink_types.file(config)
262         local log = config.filename;
263         local logfile = io_open(log, "a+");
264         if not logfile then
265                 return empty_function;
266         end
267         local write, flush = logfile.write, logfile.flush;
268
269         local timestamps = config.timestamps;
270
271         if timestamps == nil or timestamps == true then
272                 timestamps = default_timestamp; -- Default format
273         end
274
275         return function (name, level, message, ...)
276                 if timestamps then
277                         write(logfile, os_date(timestamps), " ");
278                 end
279                 if ... then
280                         write(logfile, name, "\t", level, "\t", format(message, ...), "\n");
281                 else
282                         write(logfile, name, "\t" , level, "\t", message, "\n");
283                 end
284                 flush(logfile);
285         end;
286 end
287
288 function register_sink_type(name, sink_maker)
289         local old_sink_maker = log_sink_types[name];
290         log_sink_types[name] = sink_maker;
291         return old_sink_maker;
292 end
293
294 return _M;