Mercurial > prosody-hg
comparison util/logger.lua @ 437:c1a720db2157
Nice enhancement for logging output
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Thu, 27 Nov 2008 03:06:29 +0000 |
| parents | e918c979ad1a |
| children | cccd610a0ef9 |
comparison
equal
deleted
inserted
replaced
| 436:979072fd3981 | 437:c1a720db2157 |
|---|---|
| 1 | 1 |
| 2 local format = string.format; | 2 local format, rep = string.format, string.rep; |
| 3 local io_write = io.write; | |
| 3 local print = print; | 4 local print = print; |
| 4 local debug = debug; | 5 local debug = debug; |
| 5 local tostring = tostring; | 6 local tostring = tostring; |
| 7 local math_max = math.max; | |
| 6 | 8 |
| 7 local getstyle, getstring = require "util.termcolours".getstyle, require "util.termcolours".getstring; | 9 local getstyle, getstring = require "util.termcolours".getstyle, require "util.termcolours".getstring; |
| 8 local do_pretty_printing = not os.getenv("WINDIR"); | 10 local do_pretty_printing = not os.getenv("WINDIR"); |
| 9 | 11 |
| 10 module "logger" | 12 module "logger" |
| 16 logstyles["info"] = getstyle("bold"); | 18 logstyles["info"] = getstyle("bold"); |
| 17 logstyles["warn"] = getstyle("bold", "yellow"); | 19 logstyles["warn"] = getstyle("bold", "yellow"); |
| 18 logstyles["error"] = getstyle("bold", "red"); | 20 logstyles["error"] = getstyle("bold", "red"); |
| 19 end | 21 end |
| 20 | 22 |
| 23 local sourcewidth = 20; | |
| 24 | |
| 21 function init(name) | 25 function init(name) |
| 22 --name = nil; -- While this line is not commented, will automatically fill in file/line number info | 26 --name = nil; -- While this line is not commented, will automatically fill in file/line number info |
| 27 sourcewidth = math_max(#name+2, sourcewidth); | |
| 28 local namelen = #name; | |
| 23 return function (level, message, ...) | 29 return function (level, message, ...) |
| 24 if not name then | 30 if not name then |
| 25 local inf = debug.getinfo(3, 'Snl'); | 31 local inf = debug.getinfo(3, 'Snl'); |
| 26 level = level .. ","..tostring(inf.short_src):match("[^/]*$")..":"..inf.currentline; | 32 level = level .. ","..tostring(inf.short_src):match("[^/]*$")..":"..inf.currentline; |
| 27 end | 33 end |
| 28 if ... then | 34 if ... then |
| 29 print(name, getstring(logstyles[level], level), format(message, ...)); | 35 io_write(name, rep(" ", sourcewidth-namelen), getstring(logstyles[level], level), "\t", format(message, ...), "\n"); |
| 30 else | 36 else |
| 31 print(name, getstring(logstyles[level], level), message); | 37 io_write(name, rep(" ", sourcewidth-namelen), getstring(logstyles[level], level), "\t", message, "\n"); |
| 32 end | 38 end |
| 33 end | 39 end |
| 34 end | 40 end |
| 35 | 41 |
| 36 return _M; | 42 return _M; |
