libs/logger.lua

Sat, 02 Jan 2010 05:46:52 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Sat, 02 Jan 2010 05:46:52 +0000
changeset 0
6e60da4625db
permissions
-rw-r--r--

Initial commit

0
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 local print;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 if os.getenv("LAHTTP_DEBUG") then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 print = _G.print;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 else
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 print = function () end;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 local select, tostring = select, tostring;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 module "logger"
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 local function format(format, ...)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 local n, maxn = 0, #arg;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 return (format:gsub("%%(.)", function (c) if c ~= "%" and n <= maxn then n = n + 1; return tostring(arg[n]); end end));
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 local function format(format, ...)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 local n, maxn = 0, select('#', ...);
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 local arg = { ... };
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 return (format:gsub("%%(.)", function (c) if n <= maxn then n = n + 1; return tostring(arg[n]); end end));
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 function init(name)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 return function (level, message, ...)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 print(level, format(message, ...));
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 return _M;

mercurial