ea0f497d0ef753d580d4f88ddc1037038594890a
[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 -- Null sink, must enter log_sink_types *first*
126 function log_sink_types.nowhere()
127         return function () return false; end;
128 end
129
130 -- Column width for "source" (used by stdout and console)
131 local sourcewidth = 20;
132
133 function log_sink_types.stdout()
134         local timestamps = config.timestamps;
135         
136         if timestamps == true then
137                 timestamps = default_timestamp; -- Default format
138         end
139         
140         return function (name, level, message, ...)
141                 sourcewidth = math_max(#name+2, sourcewidth);
142                 local namelen = #name;
143                 if timestamps then
144                         io_write(os_date(timestamps), " ");
145                 end
146                 if ... then 
147                         io_write(name, rep(" ", sourcewidth-namelen), level, "\t", format(message, ...), "\n");
148                 else
149                         io_write(name, rep(" ", sourcewidth-namelen), level, "\t", message, "\n");
150                 end
151         end     
152 end
153
154 do
155         local do_pretty_printing = not os_getenv("WINDIR");
156         
157         local logstyles = {};
158         if do_pretty_printing then
159                 logstyles["info"] = getstyle("bold");
160                 logstyles["warn"] = getstyle("bold", "yellow");
161                 logstyles["error"] = getstyle("bold", "red");
162         end
163         function log_sink_types.console(config)
164                 -- Really if we don't want pretty colours then just use plain stdout
165                 if not do_pretty_printing then
166                         return log_sink_types.stdout(config);
167                 end
168                 
169                 local timestamps = config.timestamps;
170
171                 if timestamps == true then
172                         timestamps = default_timestamp; -- Default format
173                 end
174
175                 return function (name, level, message, ...)
176                         sourcewidth = math_max(#name+2, sourcewidth);
177                         local namelen = #name;
178                         if timestamps then
179                                 io_write(os_date(timestamps), " ");
180                         end
181                         if ... then 
182                                 io_write(name, rep(" ", sourcewidth-namelen), getstring(logstyles[level], level), "\t", format(message, ...), "\n");
183                         else
184                                 io_write(name, rep(" ", sourcewidth-namelen), getstring(logstyles[level], level), "\t", message, "\n");
185                         end
186                 end
187         end
188 end
189
190 function log_sink_types.file(config)
191         local log = config.filename;
192         local logfile = io_open(log, "a+");
193         if not logfile then
194                 return function () end
195         end
196
197         local timestamps = config.timestamps;
198
199         if timestamps == true then
200                 timestamps = default_timestamp; -- Default format
201         end
202
203         local write, format, flush = logfile.write, format, logfile.flush;
204         return function (name, level, message, ...)
205                 if timestamps then
206                         write(logfile, os_date(timestamps), " ");
207                 end
208                 if ... then 
209                         write(logfile, name, "\t", level, "\t", format(message, ...), "\n");
210                 else
211                         write(logfile, name, "\t" , level, "\t", message, "\n");
212                 end
213                 flush(logfile);
214         end;
215 end
216
217 function register_sink_type(name, sink_maker)
218         local old_sink_maker = log_sink_types[name];
219         log_sink_types[name] = sink_maker;
220         return old_sink_maker;
221 end
222
223 return _M;