Changeset

5:93970910d064

util.logger: Friendlier string.format to automatically tostring() arguments
author Matthew Wild <mwild1@gmail.com>
date Sat, 28 Nov 2009 22:24:20 +0000
parents 4:0ef21511c7ff
children 6:f8e0ab90d84e
files libs/logger.lua
diffstat 1 files changed, 14 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- 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