Let Google Hangouts contacts appear offline
[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                 for _, level in ipairs(logging_levels) do
92                         if type(logging_config[level]) == "string" then
93                                 local value = logging_config[level];
94                                 if sink_type == "file" then
95                                         add_rule({
96                                                 to = sink_type;
97                                                 filename = value;
98                                                 timestamps = true;
99                                                 levels = { min = level };
100                                         });
101                                 elseif value == "*"..sink_type then
102                                         add_rule({
103                                                 to = sink_type;
104                                                 levels = { min = level };
105                                         });
106                                 end
107                         end
108                 end
109                 
110                 for _, sink_config in ipairs(logging_config) do
111                         if (type(sink_config) == "table" and sink_config.to == sink_type) then
112                                 add_rule(sink_config);
113                         elseif (type(sink_config) == "string" and sink_config:match("^%*(.+)") == sink_type) then
114                                 add_rule({ levels = { min = "debug" }, to = sink_type });
115                         end
116                 end
117         elseif type(logging_config) == "string" and (not logging_config:match("^%*")) and sink_type == "file" then
118                 -- User specified simply a filename, and the "file" sink type
119                 -- was just added
120                 for _, sink_config in pairs(default_file_logging) do
121                         sink_config.filename = logging_config;
122                         add_rule(sink_config);
123                         sink_config.filename = nil;
124                 end
125         elseif type(logging_config) == "string" and logging_config:match("^%*(.+)") == sink_type then
126                 -- Log all levels (debug+) to this sink
127                 add_rule({ levels = { min = "debug" }, to = sink_type });
128         end
129 end
130
131
132
133 --- Helper function to get a set of levels given a "criteria" table
134 function get_levels(criteria, set)
135         set = set or {};
136         if type(criteria) == "string" then
137                 set[criteria] = true;
138                 return set;
139         end
140         local min, max = criteria.min, criteria.max;
141         if min or max then
142                 local in_range;
143                 for _, level in ipairs(logging_levels) do
144                         if min == level then
145                                 set[level] = true;
146                                 in_range = true;
147                         elseif max == level then
148                                 set[level] = true;
149                                 return set;
150                         elseif in_range then
151                                 set[level] = true;
152                         end
153                 end
154         end
155         
156         for _, level in ipairs(criteria) do
157                 set[level] = true;
158         end
159         return set;
160 end
161
162 -- Initialize config, etc. --
163 function reload_logging()
164         local old_sink_types = {};
165         
166         for name, sink_maker in pairs(log_sink_types) do
167                 old_sink_types[name] = sink_maker;
168                 log_sink_types[name] = nil;
169         end
170         
171         logger.reset();
172
173         local debug_mode = config.get("*", "core", "debug");
174
175         default_logging = { { to = "console" , levels = { min = (debug_mode and "debug") or "info" } } };
176         default_file_logging = {
177                 { to = "file", levels = { min = (debug_mode and "debug") or "info" }, timestamps = true }
178         };
179         default_timestamp = "%b %d %H:%M:%S";
180
181         logging_config = config.get("*", "core", "log") or default_logging;
182         
183         
184         for name, sink_maker in pairs(old_sink_types) do
185                 log_sink_types[name] = sink_maker;
186         end
187         
188         prosody.events.fire_event("logging-reloaded");
189 end
190
191 reload_logging();
192 prosody.events.add_handler("config-reloaded", reload_logging);
193
194 --- Definition of built-in logging sinks ---
195
196 -- Null sink, must enter log_sink_types *first*
197 function log_sink_types.nowhere()
198         return function () return false; end;
199 end
200
201 -- Column width for "source" (used by stdout and console)
202 local sourcewidth = 20;
203
204 function log_sink_types.stdout(config)
205         local timestamps = config.timestamps;
206         
207         if timestamps == true then
208                 timestamps = default_timestamp; -- Default format
209         end
210         
211         return function (name, level, message, ...)
212                 sourcewidth = math_max(#name+2, sourcewidth);
213                 local namelen = #name;
214                 if timestamps then
215                         io_write(os_date(timestamps), " ");
216                 end
217                 if ... then
218                         io_write(name, rep(" ", sourcewidth-namelen), level, "\t", format(message, ...), "\n");
219                 else
220                         io_write(name, rep(" ", sourcewidth-namelen), level, "\t", message, "\n");
221                 end
222         end
223 end
224
225 do
226         local do_pretty_printing = true;
227         
228         local logstyles = {};
229         if do_pretty_printing then
230                 logstyles["info"] = getstyle("bold");
231                 logstyles["warn"] = getstyle("bold", "yellow");
232                 logstyles["error"] = getstyle("bold", "red");
233         end
234         function log_sink_types.console(config)
235                 -- Really if we don't want pretty colours then just use plain stdout
236                 if not do_pretty_printing then
237                         return log_sink_types.stdout(config);
238                 end
239                 
240                 local timestamps = config.timestamps;
241
242                 if timestamps == true then
243                         timestamps = default_timestamp; -- Default format
244                 end
245
246                 return function (name, level, message, ...)
247                         sourcewidth = math_max(#name+2, sourcewidth);
248                         local namelen = #name;
249                         
250                         if timestamps then
251                                 io_write(os_date(timestamps), " ");
252                         end
253                         io_write(name, rep(" ", sourcewidth-namelen));
254                         setstyle(logstyles[level]);
255                         io_write(level);
256                         setstyle();
257                         if ... then
258                                 io_write("\t", format(message, ...), "\n");
259                         else
260                                 io_write("\t", message, "\n");
261                         end
262                 end
263         end
264 end
265
266 local empty_function = function () end;
267 function log_sink_types.file(config)
268         local log = config.filename;
269         local logfile = io_open(log, "a+");
270         if not logfile then
271                 return empty_function;
272         end
273         local write, flush = logfile.write, logfile.flush;
274
275         local timestamps = config.timestamps;
276
277         if timestamps == nil or timestamps == true then
278                 timestamps = default_timestamp; -- Default format
279         end
280
281         return function (name, level, message, ...)
282                 if timestamps then
283                         write(logfile, os_date(timestamps), " ");
284                 end
285                 if ... then
286                         write(logfile, name, "\t", level, "\t", format(message, ...), "\n");
287                 else
288                         write(logfile, name, "\t" , level, "\t", message, "\n");
289                 end
290                 flush(logfile);
291         end;
292 end
293
294 function register_sink_type(name, sink_maker)
295         local old_sink_maker = log_sink_types[name];
296         log_sink_types[name] = sink_maker;
297         return old_sink_maker;
298 end
299
300 return _M;