Comparison

core/loggingmanager.lua @ 5776:bd0ff8ae98a8

Remove all trailing whitespace
author Florian Zeitz <florob@babelmonkeys.de>
date Fri, 09 Aug 2013 17:48:21 +0200
parent 5377:898454038524
child 6617:a455dac79f58
comparison
equal deleted inserted replaced
5775:a6c2b8933507 5776:bd0ff8ae98a8
1 -- Prosody IM 1 -- Prosody IM
2 -- Copyright (C) 2008-2010 Matthew Wild 2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain 3 -- Copyright (C) 2008-2010 Waqas Hussain
4 -- 4 --
5 -- This project is MIT/X11 licensed. Please see the 5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information. 6 -- COPYING file in the source package for more information.
7 -- 7 --
8 8
9 9
46 local function add_rule(sink_config) 46 local function add_rule(sink_config)
47 local sink_maker = log_sink_types[sink_config.to]; 47 local sink_maker = log_sink_types[sink_config.to];
48 if sink_maker then 48 if sink_maker then
49 -- Create sink 49 -- Create sink
50 local sink = sink_maker(sink_config); 50 local sink = sink_maker(sink_config);
51 51
52 -- Set sink for all chosen levels 52 -- Set sink for all chosen levels
53 for level in pairs(get_levels(sink_config.levels or logging_levels)) do 53 for level in pairs(get_levels(sink_config.levels or logging_levels)) do
54 logger.add_level_sink(level, sink); 54 logger.add_level_sink(level, sink);
55 end 55 end
56 else 56 else
61 -- Search for all rules using a particular sink type, and apply 61 -- Search for all rules using a particular sink type, and apply
62 -- them. Called automatically when a new sink type is added to 62 -- them. Called automatically when a new sink type is added to
63 -- the log_sink_types table. 63 -- the log_sink_types table.
64 function apply_sink_rules(sink_type) 64 function apply_sink_rules(sink_type)
65 if type(logging_config) == "table" then 65 if type(logging_config) == "table" then
66 66
67 for _, level in ipairs(logging_levels) do 67 for _, level in ipairs(logging_levels) do
68 if type(logging_config[level]) == "string" then 68 if type(logging_config[level]) == "string" then
69 local value = logging_config[level]; 69 local value = logging_config[level];
70 if sink_type == "file" and not value:match("^%*") then 70 if sink_type == "file" and not value:match("^%*") then
71 add_rule({ 71 add_rule({
80 levels = { min = level }; 80 levels = { min = level };
81 }); 81 });
82 end 82 end
83 end 83 end
84 end 84 end
85 85
86 for _, sink_config in ipairs(logging_config) do 86 for _, sink_config in ipairs(logging_config) do
87 if (type(sink_config) == "table" and sink_config.to == sink_type) then 87 if (type(sink_config) == "table" and sink_config.to == sink_type) then
88 add_rule(sink_config); 88 add_rule(sink_config);
89 elseif (type(sink_config) == "string" and sink_config:match("^%*(.+)") == sink_type) then 89 elseif (type(sink_config) == "string" and sink_config:match("^%*(.+)") == sink_type) then
90 add_rule({ levels = { min = "debug" }, to = sink_type }); 90 add_rule({ levels = { min = "debug" }, to = sink_type });
126 elseif in_range then 126 elseif in_range then
127 set[level] = true; 127 set[level] = true;
128 end 128 end
129 end 129 end
130 end 130 end
131 131
132 for _, level in ipairs(criteria) do 132 for _, level in ipairs(criteria) do
133 set[level] = true; 133 set[level] = true;
134 end 134 end
135 return set; 135 return set;
136 end 136 end
137 137
138 -- Initialize config, etc. -- 138 -- Initialize config, etc. --
139 function reload_logging() 139 function reload_logging()
140 local old_sink_types = {}; 140 local old_sink_types = {};
141 141
142 for name, sink_maker in pairs(log_sink_types) do 142 for name, sink_maker in pairs(log_sink_types) do
143 old_sink_types[name] = sink_maker; 143 old_sink_types[name] = sink_maker;
144 log_sink_types[name] = nil; 144 log_sink_types[name] = nil;
145 end 145 end
146 146
147 logger.reset(); 147 logger.reset();
148 148
149 local debug_mode = config.get("*", "debug"); 149 local debug_mode = config.get("*", "debug");
150 150
151 default_logging = { { to = "console" , levels = { min = (debug_mode and "debug") or "info" } } }; 151 default_logging = { { to = "console" , levels = { min = (debug_mode and "debug") or "info" } } };
153 { to = "file", levels = { min = (debug_mode and "debug") or "info" }, timestamps = true } 153 { to = "file", levels = { min = (debug_mode and "debug") or "info" }, timestamps = true }
154 }; 154 };
155 default_timestamp = "%b %d %H:%M:%S"; 155 default_timestamp = "%b %d %H:%M:%S";
156 156
157 logging_config = config.get("*", "log") or default_logging; 157 logging_config = config.get("*", "log") or default_logging;
158 158
159 159
160 for name, sink_maker in pairs(old_sink_types) do 160 for name, sink_maker in pairs(old_sink_types) do
161 log_sink_types[name] = sink_maker; 161 log_sink_types[name] = sink_maker;
162 end 162 end
163 163
164 prosody.events.fire_event("logging-reloaded"); 164 prosody.events.fire_event("logging-reloaded");
165 end 165 end
166 166
167 reload_logging(); 167 reload_logging();
168 prosody.events.add_handler("config-reloaded", reload_logging); 168 prosody.events.add_handler("config-reloaded", reload_logging);
177 -- Column width for "source" (used by stdout and console) 177 -- Column width for "source" (used by stdout and console)
178 local sourcewidth = 20; 178 local sourcewidth = 20;
179 179
180 function log_sink_types.stdout(config) 180 function log_sink_types.stdout(config)
181 local timestamps = config.timestamps; 181 local timestamps = config.timestamps;
182 182
183 if timestamps == true then 183 if timestamps == true then
184 timestamps = default_timestamp; -- Default format 184 timestamps = default_timestamp; -- Default format
185 end 185 end
186 186
187 return function (name, level, message, ...) 187 return function (name, level, message, ...)
188 sourcewidth = math_max(#name+2, sourcewidth); 188 sourcewidth = math_max(#name+2, sourcewidth);
189 local namelen = #name; 189 local namelen = #name;
190 if timestamps then 190 if timestamps then
191 io_write(os_date(timestamps), " "); 191 io_write(os_date(timestamps), " ");
198 end 198 end
199 end 199 end
200 200
201 do 201 do
202 local do_pretty_printing = true; 202 local do_pretty_printing = true;
203 203
204 local logstyles = {}; 204 local logstyles = {};
205 if do_pretty_printing then 205 if do_pretty_printing then
206 logstyles["info"] = getstyle("bold"); 206 logstyles["info"] = getstyle("bold");
207 logstyles["warn"] = getstyle("bold", "yellow"); 207 logstyles["warn"] = getstyle("bold", "yellow");
208 logstyles["error"] = getstyle("bold", "red"); 208 logstyles["error"] = getstyle("bold", "red");
210 function log_sink_types.console(config) 210 function log_sink_types.console(config)
211 -- Really if we don't want pretty colours then just use plain stdout 211 -- Really if we don't want pretty colours then just use plain stdout
212 if not do_pretty_printing then 212 if not do_pretty_printing then
213 return log_sink_types.stdout(config); 213 return log_sink_types.stdout(config);
214 end 214 end
215 215
216 local timestamps = config.timestamps; 216 local timestamps = config.timestamps;
217 217
218 if timestamps == true then 218 if timestamps == true then
219 timestamps = default_timestamp; -- Default format 219 timestamps = default_timestamp; -- Default format
220 end 220 end
221 221
222 return function (name, level, message, ...) 222 return function (name, level, message, ...)
223 sourcewidth = math_max(#name+2, sourcewidth); 223 sourcewidth = math_max(#name+2, sourcewidth);
224 local namelen = #name; 224 local namelen = #name;
225 225
226 if timestamps then 226 if timestamps then
227 io_write(os_date(timestamps), " "); 227 io_write(os_date(timestamps), " ");
228 end 228 end
229 io_write(name, rep(" ", sourcewidth-namelen)); 229 io_write(name, rep(" ", sourcewidth-namelen));
230 setstyle(logstyles[level]); 230 setstyle(logstyles[level]);