mirror of
https://github.com/bjc/prosody.git
synced 2025-04-07 07:07:38 +03:00
util.format: Serialize values for the %q format
Improves eg debug logs
This commit is contained in:
parent
5a608450d5
commit
177420df39
2 changed files with 6 additions and 1 deletions
|
@ -11,6 +11,7 @@ describe("util.format", function()
|
|||
assert.equal("true", format("%s", true));
|
||||
assert.equal("[true]", format("%d", true));
|
||||
assert.equal("% [true]", format("%%", true));
|
||||
assert.equal("{ }", format("%q", { }));
|
||||
end);
|
||||
end);
|
||||
end);
|
||||
|
|
|
@ -6,6 +6,7 @@ local tostring = tostring;
|
|||
local unpack = table.unpack or unpack; -- luacheck: ignore 113/unpack
|
||||
local pack = require "util.table".pack; -- TODO table.pack in 5.2+
|
||||
local type = type;
|
||||
local dump = require "util.serialization".new("debug");
|
||||
|
||||
local function format(formatstring, ...)
|
||||
local args = pack(...);
|
||||
|
@ -34,7 +35,10 @@ local function format(formatstring, ...)
|
|||
if arg == nil then
|
||||
args[i] = "nil";
|
||||
spec = "<%s>";
|
||||
elseif option == "q" or option == "s" then -- arg should be string
|
||||
elseif option == "q" then
|
||||
args[i] = dump(arg);
|
||||
spec = "%s";
|
||||
elseif option == "s" then
|
||||
args[i] = tostring(arg);
|
||||
elseif type(arg) ~= "number" then -- arg isn't number as expected?
|
||||
args[i] = tostring(arg);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue