local math_max, rep = math.max, string.rep;
local os_date = os.date;
local getstyle, getstring = require "util.termcolours".getstyle, require "util.termcolours".getstring;
-
--- COMPAT: This should no longer be needed since the addition of setvbuf calls
-if os.getenv("__FLUSH_LOG") then
- local io_flush = io.flush;
- local _io_write = io_write;
- io_write = function(...) _io_write(...); io_flush(); end
-end
+local tostring = tostring;
+local select = select;
+local unpack = table.unpack or unpack; --luacheck: ignore 113
local config = require "core.configmanager";
local logger = require "util.logger";
-- The log config used if none specified in the config file (see reload_logging for initialization)
local default_logging;
local default_file_logging;
-local default_timestamp = "%b %d %H:%M:%S";
+local default_timestamp = "%b %d %H:%M:%S ";
-- The actual config loggingmanager is using
local logging_config;
if timestamps == true then
timestamps = default_timestamp; -- Default format
+ elseif timestamps then
+ timestamps = timestamps .. " ";
end
if sink_config.buffer_mode ~= false then
logfile:setvbuf(sink_config.buffer_mode or "line");
end
+ -- Column width for "source" (used by stdout and console)
+ local sourcewidth = sink_config.source_width;
+
return function (name, level, message, ...)
- if timestamps then
- write(logfile, os_date(timestamps), " ");
+ local n = select('#', ...);
+ if n ~= 0 then
+ local arg = { ... };
+ for i = 1, n do
+ arg[i] = tostring(arg[i]);
+ end
+ message = format(message, unpack(arg, 1, n));
end
- if ... then
- write(logfile, name, level, "\t", format(message, ...), "\n");
+
+ if sourcewidth then
+ sourcewidth = math_max(#name+2, sourcewidth);
+ name = name .. rep(" ", sourcewidth-#name);
else
- write(logfile, name, level, "\t", message, "\n");
+ name = name .. "\t";
end
+ write(logfile, timestamps and os_date(timestamps) or "", name, level, "\t", message, "\n");
end
end
log_sink_types.file = log_to_file;
--- Column width for "source" (used by stdout and console)
-local sourcewidth = 20;
-
local function log_to_stdout(sink_config)
if not sink_config.timestamps then
sink_config.timestamps = false;
end
- local logtofile = log_to_file(sink_config, stdout);
- return function (name, level, message, ...)
- sourcewidth = math_max(#name+2, sourcewidth);
- name = name .. rep(" ", sourcewidth-#name);
- return logtofile(name, level, message, ...);
+ if sink_config.source_width == nil then
+ sink_config.source_width = 20;
end
+ return log_to_file(sink_config, stdout);
end
log_sink_types.stdout = log_to_stdout;