Diff

util/format.lua @ 9693:6ed0d6224d64

util.format: Serialize values for the %q format Improves eg debug logs
author Kim Alvefur <zash@zash.se>
date Fri, 12 Oct 2018 01:29:34 +0200
parent 9687:8c92ef4270c9
child 10034:4fca92d60040
line wrap: on
line diff
--- a/util/format.lua	Sat Dec 08 17:10:51 2018 +0100
+++ b/util/format.lua	Fri Oct 12 01:29:34 2018 +0200
@@ -6,6 +6,7 @@
 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 @@
 			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);