diff options
author | Kim Alvefur <zash@zash.se> | 2018-10-12 01:29:34 +0200 |
---|---|---|
committer | Kim Alvefur <zash@zash.se> | 2018-10-12 01:29:34 +0200 |
commit | 177420df39c60de47eb47bc5ed574e2ccf082ec4 (patch) | |
tree | 771c72f485c59b84ac9169be1acdee8e4cd360d3 | |
parent | 5a608450d505944cbac268f28e48751c8fa3ee10 (diff) | |
download | prosody-177420df39c60de47eb47bc5ed574e2ccf082ec4.tar.gz prosody-177420df39c60de47eb47bc5ed574e2ccf082ec4.zip |
util.format: Serialize values for the %q format
Improves eg debug logs
-rw-r--r-- | spec/util_format_spec.lua | 1 | ||||
-rw-r--r-- | util/format.lua | 6 |
2 files changed, 6 insertions, 1 deletions
diff --git a/spec/util_format_spec.lua b/spec/util_format_spec.lua index 8a2e9312..b9652d19 100644 --- a/spec/util_format_spec.lua +++ b/spec/util_format_spec.lua @@ -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); diff --git a/util/format.lua b/util/format.lua index 6c46384a..c31f599f 100644 --- a/util/format.lua +++ b/util/format.lua @@ -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); |