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