tools/ejabberd2prosody: Fixed private storage export
[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
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         elseif type(logging_config) == "string" and logging_config:match("^%*(.+)") == sink_type then
90                 -- Log all levels (debug+) to this sink
91                 add_rule({ levels = { min = "debug" }, to = sink_type });
92         end
93 end
94
95
96
97 --- Helper function to get a set of levels given a "criteria" table
98 function get_levels(criteria, set)
99         set = set or {};
100         if type(criteria) == "string" then
101                 set[criteria] = true;
102                 return set;
103         end
104         local min, max = criteria.min, criteria.max;
105         if min or max then
106                 local in_range;
107                 for _, level in ipairs(logging_levels) do
108                         if min == level then
109                                 set[level] = true;
110                                 in_range = true;
111                         elseif max == level then
112                                 set[level] = true;
113                                 return set;
114                         elseif in_range then
115                                 set[level] = true;
116                         end     
117                 end
118         end
119         
120         for _, level in ipairs(criteria) do
121                 set[level] = true;
122         end
123         return set;
124 end
125
126 --- Definition of built-in logging sinks ---
127
128 -- Null sink, must enter log_sink_types *first*
129 function log_sink_types.nowhere()
130         return function () return false; end;
131 end
132
133 -- Column width for "source" (used by stdout and console)
134 local sourcewidth = 20;
135
136 function log_sink_types.stdout()
137         local timestamps = config.timestamps;
138         
139         if timestamps == true then
140                 timestamps = default_timestamp; -- Default format
141         end
142         
143         return function (name, level, message, ...)
144                 sourcewidth = math_max(#name+2, sourcewidth);
145                 local namelen = #name;
146                 if timestamps then
147                         io_write(os_date(timestamps), " ");
148                 end
149                 if ... then 
150                         io_write(name, rep(" ", sourcewidth-namelen), level, "\t", format(message, ...), "\n");
151                 else
152                         io_write(name, rep(" ", sourcewidth-namelen), level, "\t", message, "\n");
153                 end
154         end     
155 end
156
157 do
158         local do_pretty_printing = not os_getenv("WINDIR");
159         
160         local logstyles = {};
161         if do_pretty_printing then
162                 logstyles["info"] = getstyle("bold");
163                 logstyles["warn"] = getstyle("bold", "yellow");
164                 logstyles["error"] = getstyle("bold", "red");
165         end
166         function log_sink_types.console(config)
167                 -- Really if we don't want pretty colours then just use plain stdout
168                 if not do_pretty_printing then
169                         return log_sink_types.stdout(config);
170                 end
171                 
172                 local timestamps = config.timestamps;
173
174                 if timestamps == true then
175                         timestamps = default_timestamp; -- Default format
176                 end
177
178                 return function (name, level, message, ...)
179                         sourcewidth = math_max(#name+2, sourcewidth);
180                         local namelen = #name;
181                         if timestamps then
182                                 io_write(os_date(timestamps), " ");
183                         end
184                         if ... then 
185                                 io_write(name, rep(" ", sourcewidth-namelen), getstring(logstyles[level], level), "\t", format(message, ...), "\n");
186                         else
187                                 io_write(name, rep(" ", sourcewidth-namelen), getstring(logstyles[level], level), "\t", message, "\n");
188                         end
189                 end
190         end
191 end
192
193 local empty_function = function () end;
194 function log_sink_types.file(config)
195         local log = config.filename;
196         local logfile = io_open(log, "a+");
197         if not logfile then
198                 return empty_function;
199         end
200         local write, flush = logfile.write, logfile.flush;
201
202         eventmanager.add_event_hook("reopen-log-files", function ()
203                         if logfile then
204                                 logfile:close();
205                         end
206                         logfile = io_open(log, "a+");
207                         if not logfile then
208                                 write, flush = empty_function, empty_function;
209                         else
210                                 write, flush = logfile.write, logfile.flush;
211                         end
212                 end);
213
214         local timestamps = config.timestamps;
215
216         if timestamps == true then
217                 timestamps = default_timestamp; -- Default format
218         end
219
220         return function (name, level, message, ...)
221                 if timestamps then
222                         write(logfile, os_date(timestamps), " ");
223                 end
224                 if ... then 
225                         write(logfile, name, "\t", level, "\t", format(message, ...), "\n");
226                 else
227                         write(logfile, name, "\t" , level, "\t", message, "\n");
228                 end
229                 flush(logfile);
230         end;
231 end
232
233 function register_sink_type(name, sink_maker)
234         local old_sink_maker = log_sink_types[name];
235         log_sink_types[name] = sink_maker;
236         return old_sink_maker;
237 end
238
239 return _M;