libs/logger.lua

Thu, 09 Sep 2010 19:30:49 +0100

author
Matthew Wild <mwild1@gmail.com>
date
Thu, 09 Sep 2010 19:30:49 +0100
changeset 122
e2600454fd54
parent 5
93970910d064
child 127
8f831f259cea
permissions
-rw-r--r--

plugins.adhoc: Support for querying for and executing commands

0
caf260adc453 Beginning of new verse
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 local print = print
5
93970910d064 util.logger: Friendlier string.format to automatically tostring() arguments
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
2 local select, tostring = select, tostring;
0
caf260adc453 Beginning of new verse
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 module "logger"
caf260adc453 Beginning of new verse
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
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
caf260adc453 Beginning of new verse
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
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, ...)
93970910d064 util.logger: Friendlier string.format to automatically tostring() arguments
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
18 print(level, format(message, ...));
0
caf260adc453 Beginning of new verse
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 end
caf260adc453 Beginning of new verse
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 end
caf260adc453 Beginning of new verse
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21
caf260adc453 Beginning of new verse
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 return _M;

mercurial