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