Software /
code /
verse
Annotate
libs/logger.lua @ 201:1fce24cb2c41
plugins.smacks: Remove some debugging code from resumption success handling, and fire a "resumed" event instead
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Fri, 18 Mar 2011 21:42:49 +0000 |
parent | 127:8f831f259cea |
rev | line source |
---|---|
5
93970910d064
util.logger: Friendlier string.format to automatically tostring() arguments
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
1 local select, tostring = select, tostring; |
127
8f831f259cea
libs.logger: Use io.write instead of print
Matthew Wild <mwild1@gmail.com>
parents:
5
diff
changeset
|
2 local io_write = io.write; |
0 | 3 module "logger" |
4 | |
5
93970910d064
util.logger: Friendlier string.format to automatically tostring() arguments
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
5 local function format(format, ...) |
93970910d064
util.logger: Friendlier string.format to automatically tostring() arguments
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
6 local n, maxn = 0, #arg; |
93970910d064
util.logger: Friendlier string.format to automatically tostring() arguments
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
7 return (format:gsub("%%(.)", function (c) if c ~= "%" and n <= maxn then n = n + 1; return tostring(arg[n]); end end)); |
93970910d064
util.logger: Friendlier string.format to automatically tostring() arguments
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
8 end |
93970910d064
util.logger: Friendlier string.format to automatically tostring() arguments
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
9 |
93970910d064
util.logger: Friendlier string.format to automatically tostring() arguments
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
10 local function format(format, ...) |
93970910d064
util.logger: Friendlier string.format to automatically tostring() arguments
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
11 local n, maxn = 0, select('#', ...); |
93970910d064
util.logger: Friendlier string.format to automatically tostring() arguments
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
12 local arg = { ... }; |
93970910d064
util.logger: Friendlier string.format to automatically tostring() arguments
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
13 return (format:gsub("%%(.)", function (c) if n <= maxn then n = n + 1; return tostring(arg[n]); end end)); |
93970910d064
util.logger: Friendlier string.format to automatically tostring() arguments
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
14 end |
93970910d064
util.logger: Friendlier string.format to automatically tostring() arguments
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
15 |
0 | 16 function init(name) |
5
93970910d064
util.logger: Friendlier string.format to automatically tostring() arguments
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
17 return function (level, message, ...) |
127
8f831f259cea
libs.logger: Use io.write instead of print
Matthew Wild <mwild1@gmail.com>
parents:
5
diff
changeset
|
18 io_write(level, "\t", format(message, ...), "\n"); |
0 | 19 end |
20 end | |
21 | |
22 return _M; |