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