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