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