libs/logger.lua

changeset 0
6e60da4625db
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libs/logger.lua	Sat Jan 02 05:46:52 2010 +0000
@@ -0,0 +1,28 @@
+local print;
+if os.getenv("LAHTTP_DEBUG") then
+	print = _G.print;
+else
+	print = function () end;
+end
+
+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, format(message, ...));
+	end
+end
+
+return _M;

mercurial