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