Mercurial > prosody-hg
comparison core/loggingmanager.lua @ 7125:631c47a65519
loggingmanager: Call setvbuf on output files, defaulting to line-buffered, instead of manually calling flush(). Adds 'buffer_mode' option to sink configuration for stdout, console and file sinks.
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Thu, 04 Feb 2016 14:56:49 +0000 |
| parents | 93771dad3e05 |
| children | 3868d231c2c5 |
comparison
equal
deleted
inserted
replaced
| 7122:89c51ee23122 | 7125:631c47a65519 |
|---|---|
| 13 local io_open, io_write = io.open, io.write; | 13 local io_open, io_write = io.open, io.write; |
| 14 local math_max, rep = math.max, string.rep; | 14 local math_max, rep = math.max, string.rep; |
| 15 local os_date = os.date; | 15 local os_date = os.date; |
| 16 local getstyle, setstyle = require "util.termcolours".getstyle, require "util.termcolours".setstyle; | 16 local getstyle, setstyle = require "util.termcolours".getstyle, require "util.termcolours".setstyle; |
| 17 | 17 |
| 18 -- COMPAT: This should no longer be needed since the addition of setvbuf calls | |
| 18 if os.getenv("__FLUSH_LOG") then | 19 if os.getenv("__FLUSH_LOG") then |
| 19 local io_flush = io.flush; | 20 local io_flush = io.flush; |
| 20 local _io_write = io_write; | 21 local _io_write = io_write; |
| 21 io_write = function(...) _io_write(...); io_flush(); end | 22 io_write = function(...) _io_write(...); io_flush(); end |
| 22 end | 23 end |
| 155 }; | 156 }; |
| 156 default_timestamp = "%b %d %H:%M:%S"; | 157 default_timestamp = "%b %d %H:%M:%S"; |
| 157 | 158 |
| 158 logging_config = config.get("*", "log") or default_logging; | 159 logging_config = config.get("*", "log") or default_logging; |
| 159 | 160 |
| 160 | |
| 161 for name, sink_maker in pairs(old_sink_types) do | 161 for name, sink_maker in pairs(old_sink_types) do |
| 162 log_sink_types[name] = sink_maker; | 162 log_sink_types[name] = sink_maker; |
| 163 end | 163 end |
| 164 | 164 |
| 165 prosody.events.fire_event("logging-reloaded"); | 165 prosody.events.fire_event("logging-reloaded"); |
| 181 function log_sink_types.stdout(sink_config) | 181 function log_sink_types.stdout(sink_config) |
| 182 local timestamps = sink_config.timestamps; | 182 local timestamps = sink_config.timestamps; |
| 183 | 183 |
| 184 if timestamps == true then | 184 if timestamps == true then |
| 185 timestamps = default_timestamp; -- Default format | 185 timestamps = default_timestamp; -- Default format |
| 186 end | |
| 187 | |
| 188 if sink_config.buffer_mode ~= false then | |
| 189 io.stdout:setvbuf(sink_config.buffer_mode or "line"); | |
| 186 end | 190 end |
| 187 | 191 |
| 188 return function (name, level, message, ...) | 192 return function (name, level, message, ...) |
| 189 sourcewidth = math_max(#name+2, sourcewidth); | 193 sourcewidth = math_max(#name+2, sourcewidth); |
| 190 local namelen = #name; | 194 local namelen = #name; |
| 216 | 220 |
| 217 local timestamps = sink_config.timestamps; | 221 local timestamps = sink_config.timestamps; |
| 218 | 222 |
| 219 if timestamps == true then | 223 if timestamps == true then |
| 220 timestamps = default_timestamp; -- Default format | 224 timestamps = default_timestamp; -- Default format |
| 225 end | |
| 226 | |
| 227 if sink_config.buffer_mode ~= false then | |
| 228 io.stdout:setvbuf(sink_config.buffer_mode or "line"); | |
| 221 end | 229 end |
| 222 | 230 |
| 223 return function (name, level, message, ...) | 231 return function (name, level, message, ...) |
| 224 sourcewidth = math_max(#name+2, sourcewidth); | 232 sourcewidth = math_max(#name+2, sourcewidth); |
| 225 local namelen = #name; | 233 local namelen = #name; |
| 245 local log = sink_config.filename; | 253 local log = sink_config.filename; |
| 246 local logfile = io_open(log, "a+"); | 254 local logfile = io_open(log, "a+"); |
| 247 if not logfile then | 255 if not logfile then |
| 248 return empty_function; | 256 return empty_function; |
| 249 end | 257 end |
| 250 local write, flush = logfile.write, logfile.flush; | 258 |
| 259 if sink_config.buffer_mode ~= false then | |
| 260 logfile:setvbuf(sink_config.buffer_mode or "line"); | |
| 261 end | |
| 262 | |
| 263 local write = logfile.write; | |
| 251 | 264 |
| 252 local timestamps = sink_config.timestamps; | 265 local timestamps = sink_config.timestamps; |
| 253 | 266 |
| 254 if timestamps == nil or timestamps == true then | 267 if timestamps == nil or timestamps == true then |
| 255 timestamps = default_timestamp; -- Default format | 268 timestamps = default_timestamp; -- Default format |
| 262 if ... then | 275 if ... then |
| 263 write(logfile, name, "\t", level, "\t", format(message, ...), "\n"); | 276 write(logfile, name, "\t", level, "\t", format(message, ...), "\n"); |
| 264 else | 277 else |
| 265 write(logfile, name, "\t" , level, "\t", message, "\n"); | 278 write(logfile, name, "\t" , level, "\t", message, "\n"); |
| 266 end | 279 end |
| 267 flush(logfile); | |
| 268 end; | 280 end; |
| 269 end | 281 end |
| 270 | 282 |
| 271 local function register_sink_type(name, sink_maker) | 283 local function register_sink_type(name, sink_maker) |
| 272 local old_sink_maker = log_sink_types[name]; | 284 local old_sink_maker = log_sink_types[name]; |
