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