Comparison

core/loggingmanager.lua @ 7137:4a5619a87b44

loggingmanager: Write out timestamps in same write() call as everything else
author Kim Alvefur <zash@zash.se>
date Thu, 04 Feb 2016 17:57:12 +0100
parent 7136:6c3a33e54399
child 7138:ae1d53c2f598
comparison
equal deleted inserted replaced
7136:6c3a33e54399 7137:4a5619a87b44
33 local _ENV = nil; 33 local _ENV = nil;
34 34
35 -- The log config used if none specified in the config file (see reload_logging for initialization) 35 -- The log config used if none specified in the config file (see reload_logging for initialization)
36 local default_logging; 36 local default_logging;
37 local default_file_logging; 37 local default_file_logging;
38 local default_timestamp = "%b %d %H:%M:%S"; 38 local default_timestamp = "%b %d %H:%M:%S ";
39 -- The actual config loggingmanager is using 39 -- The actual config loggingmanager is using
40 local logging_config; 40 local logging_config;
41 41
42 local apply_sink_rules; 42 local apply_sink_rules;
43 local log_sink_types = setmetatable({}, { __newindex = function (t, k, v) rawset(t, k, v); apply_sink_rules(k); end; }); 43 local log_sink_types = setmetatable({}, { __newindex = function (t, k, v) rawset(t, k, v); apply_sink_rules(k); end; });
185 185
186 local timestamps = sink_config.timestamps; 186 local timestamps = sink_config.timestamps;
187 187
188 if timestamps == true then 188 if timestamps == true then
189 timestamps = default_timestamp; -- Default format 189 timestamps = default_timestamp; -- Default format
190 elseif timestamps then
191 timestamps = timestamps .. " ";
190 end 192 end
191 193
192 if sink_config.buffer_mode ~= false then 194 if sink_config.buffer_mode ~= false then
193 logfile:setvbuf(sink_config.buffer_mode or "line"); 195 logfile:setvbuf(sink_config.buffer_mode or "line");
194 end 196 end
195 197
196 -- Column width for "source" (used by stdout and console) 198 -- Column width for "source" (used by stdout and console)
197 local sourcewidth = sink_config.source_width; 199 local sourcewidth = sink_config.source_width;
198 200
199 return function (name, level, message, ...) 201 return function (name, level, message, ...)
200 if timestamps then
201 write(logfile, os_date(timestamps), " ");
202 end
203 if sourcewidth then 202 if sourcewidth then
204 sourcewidth = math_max(#name+2, sourcewidth); 203 sourcewidth = math_max(#name+2, sourcewidth);
205 name = name .. rep(" ", sourcewidth-#name); 204 name = name .. rep(" ", sourcewidth-#name);
206 else 205 else
207 name = name .. "\t"; 206 name = name .. "\t";
208 end 207 end
209 if ... then 208 if ... then
210 write(logfile, name, level, "\t", format(message, ...), "\n"); 209 write(logfile, timestamps and os_date(timestamps) or "", name, level, "\t", format(message, ...), "\n");
211 else 210 else
212 write(logfile, name, level, "\t", message, "\n"); 211 write(logfile, timestamps and os_date(timestamps) or "", name, level, "\t", message, "\n");
213 end 212 end
214 end 213 end
215 end 214 end
216 log_sink_types.file = log_to_file; 215 log_sink_types.file = log_to_file;
217 216