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