util.logger: Friendlier string.format to automatically tostring() arguments

Sat, 28 Nov 2009 22:24:20 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Sat, 28 Nov 2009 22:24:20 +0000
changeset 5
93970910d064
parent 4
0ef21511c7ff
child 6
f8e0ab90d84e

util.logger: Friendlier string.format to automatically tostring() arguments

libs/logger.lua file | annotate | diff | comparison | revisions
--- a/libs/logger.lua	Sat Nov 28 22:22:51 2009 +0000
+++ b/libs/logger.lua	Sat Nov 28 22:24:20 2009 +0000
@@ -1,9 +1,21 @@
 local print = print
+local select, tostring = select, tostring;
 module "logger"
 
+local function format(format, ...)
+	local n, maxn = 0, #arg;
+	return (format:gsub("%%(.)", function (c) if c ~= "%" and n <= maxn then n = n + 1; return tostring(arg[n]); end end));
+end
+
+local function format(format, ...)
+	local n, maxn = 0, select('#', ...);
+	local arg = { ... };
+	return (format:gsub("%%(.)", function (c) if n <= maxn then n = n + 1; return tostring(arg[n]); end end));
+end
+
 function init(name)
-	return function (level, message)
-		print(level, message);
+	return function (level, message, ...)
+		print(level, format(message, ...));
 	end
 end
 

mercurial