2c1accd9411b9e45245afc0b93c8f90900381fac
[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 eventmanager = require "core.eventmanager";
28 local logger = require "util.logger";
29 local prosody = prosody;
30
31 local debug_mode = config.get("*", "core", "debug");
32
33 _G.log = logger.init("general");
34
35 module "loggingmanager"
36
37 -- The log config used if none specified in the config file (see reload_logging for initialization)
38 local default_logging;
39 local default_file_logging;
40 local default_timestamp = "%b %d %H:%M:%S";
41 -- The actual config loggingmanager is using
42 local logging_config;
43
44 local apply_sink_rules;
45 local log_sink_types = setmetatable({}, { __newindex = function (t, k, v) rawset(t, k, v); apply_sink_rules(k); end; });
46 local get_levels;
47 local logging_levels = { "debug", "info", "warn", "error", "critical" }
48
49 -- Put a rule into action. Requires that the sink type has already been registered.
50 -- This function is called automatically when a new sink type is added [see apply_sink_rules()]
51 local function add_rule(sink_config)
52         local sink_maker = log_sink_types[sink_config.to];
53         if sink_maker then
54                 if sink_config.levels and not sink_config.source then
55                         -- Create sink
56                         local sink = sink_maker(sink_config);
57                         
58                         -- Set sink for all chosen levels
59                         for level in pairs(get_levels(sink_config.levels)) do
60                                 logger.add_level_sink(level, sink);
61                         end
62                 elseif sink_config.source and not sink_config.levels then
63                         logger.add_name_sink(sink_config.source, sink_maker(sink_config));
64                 elseif sink_config.source and sink_config.levels then
65                         local levels = get_levels(sink_config.levels);
66                         local sink = sink_maker(sink_config);
67                         logger.add_name_sink(sink_config.source,
68                                 function (name, level, ...)
69                                         if levels[level] then
70                                                 return sink(name, level, ...);
71                                         end
72                                 end);
73                 else
74                         -- All sources
75                         -- Create sink
76                         local sink = sink_maker(sink_config);
77                         
78                         -- Set sink for all levels
79                         for _, level in pairs(logging_levels) do
80                                 logger.add_level_sink(level, sink);
81                         end
82                 end
83         else
84                 -- No such sink type
85         end
86 end
87
88 -- Search for all rules using a particular sink type, and apply
89 -- them. Called automatically when a new sink type is added to
90 -- the log_sink_types table.
91 function apply_sink_rules(sink_type)
92         if type(logging_config) == "table" then
93                 for _, sink_config in pairs(logging_config) do
94                         if sink_config.to == sink_type then
95                                 add_rule(sink_config);
96                         end
97                 end
98         elseif type(logging_config) == "string" and (not logging_config:match("^%*")) and sink_type == "file" then
99                 -- User specified simply a filename, and the "file" sink type
100                 -- was just added
101                 for _, sink_config in pairs(default_file_logging) do
102                         sink_config.filename = logging_config;
103                         add_rule(sink_config);
104                         sink_config.filename = nil;
105                 end
106         elseif type(logging_config) == "string" and logging_config:match("^%*(.+)") == sink_type then
107                 -- Log all levels (debug+) to this sink
108                 add_rule({ levels = { min = "debug" }, to = sink_type });
109         end
110 end
111
112
113
114 --- Helper function to get a set of levels given a "criteria" table
115 function get_levels(criteria, set)
116         set = set or {};
117         if type(criteria) == "string" then
118                 set[criteria] = true;
119                 return set;
120         end
121         local min, max = criteria.min, criteria.max;
122         if min or max then
123                 local in_range;
124                 for _, level in ipairs(logging_levels) do
125                         if min == level then
126                                 set[level] = true;
127                                 in_range = true;
128                         elseif max == level then
129                                 set[level] = true;
130                                 return set;
131                         elseif in_range then
132                                 set[level] = true;
133                         end
134                 end
135         end
136         
137         for _, level in ipairs(criteria) do
138                 set[level] = true;
139         end
140         return set;
141 end
142
143 -- Initialize config, etc. --
144 function reload_logging()
145         local old_sink_types = {};
146         
147         for name, sink_maker in pairs(log_sink_types) do
148                 old_sink_types[name] = sink_maker;
149                 log_sink_types[name] = nil;
150         end
151         
152         logger.reset();
153
154         default_logging = { { to = "console" , levels = { min = (debug_mode and "debug") or "info" } } };
155         default_file_logging = { { to = "file", levels = { min = (debug_mode and "debug") or "info" }, timestamps = true } };
156         default_timestamp = "%b %d %H:%M:%S";
157
158         logging_config = config.get("*", "core", "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()
182         local timestamps = 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 = not os_getenv("WINDIR");
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(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(config);
215                 end
216                 
217                 local timestamps = 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                         if ... then
231                                 io_write(name, rep(" ", sourcewidth-namelen), getstring(logstyles[level], level), "\t", format(message, ...), "\n");
232                         else
233                                 io_write(name, rep(" ", sourcewidth-namelen), getstring(logstyles[level], level), "\t", message, "\n");
234                         end
235                 end
236         end
237 end
238
239 local empty_function = function () end;
240 function log_sink_types.file(config)
241         local log = config.filename;
242         local logfile = io_open(log, "a+");
243         if not logfile then
244                 return empty_function;
245         end
246         local write, flush = logfile.write, logfile.flush;
247
248         prosody.events.add_handler("logging-reloading", function ()
249                         if logfile then
250                                 logfile:close();
251                         end
252                 end);
253
254         local timestamps = config.timestamps;
255
256         if timestamps == nil or timestamps == true then
257                 timestamps = default_timestamp; -- Default format
258         end
259
260         return function (name, level, message, ...)
261                 if timestamps then
262                         write(logfile, os_date(timestamps), " ");
263                 end
264                 if ... then
265                         write(logfile, name, "\t", level, "\t", format(message, ...), "\n");
266                 else
267                         write(logfile, name, "\t" , level, "\t", message, "\n");
268                 end
269                 flush(logfile);
270         end;
271 end
272
273 function register_sink_type(name, sink_maker)
274         local old_sink_maker = log_sink_types[name];
275         log_sink_types[name] = sink_maker;
276         return old_sink_maker;
277 end
278
279 return _M;