Changeset

241:931bf313c691

verse: Format messages before passing them to custom log handler
author Matthew Wild <mwild1@gmail.com>
date Sun, 27 Nov 2011 22:59:59 +0000
parents 240:e27bf761bea6
children 242:ab4773b0ef5e
files init.lua
diffstat 1 files changed, 9 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/init.lua	Sun Nov 27 22:50:21 2011 +0000
+++ b/init.lua	Sun Nov 27 22:59:59 2011 +0000
@@ -37,12 +37,20 @@
 verse.new_logger = logger.init;
 verse.log = verse.logger("verse");
 
+local function format(format, ...)
+	local n, arg, maxn = 0, { ... }, select('#', ...);
+	return (format:gsub("%%(.)", function (c) if n <= maxn then n = n + 1; return tostring(arg[n]); end end));
+end
+
 function verse.set_log_handler(log_handler, levels)
 	levels = levels or { "debug", "info", "warn", "error" };
 	logger.reset();
+	local function _log_handler(name, level, message, ...)
+		return log_handler(name, level, format(message, ...));
+	end
 	if log_handler then
 		for i, level in ipairs(levels) do
-			logger.add_level_sink(level, log_handler);
+			logger.add_level_sink(level, _log_handler);
 		end
 	end
 end