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