Annotate

util/logger.lua @ 484:af499a5ee32f

Make ejabberd2prosody.lua eecutable
author Matthew Wild <mwild1@gmail.com>
date Sat, 29 Nov 2008 19:14:54 +0000
parent 437:c1a720db2157
child 519:cccd610a0ef9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1
437
c1a720db2157 Nice enhancement for logging output
Matthew Wild <mwild1@gmail.com>
parents: 360
diff changeset
2 local format, rep = string.format, string.rep;
c1a720db2157 Nice enhancement for logging output
Matthew Wild <mwild1@gmail.com>
parents: 360
diff changeset
3 local io_write = io.write;
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 local print = print;
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 local debug = debug;
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 local tostring = tostring;
437
c1a720db2157 Nice enhancement for logging output
Matthew Wild <mwild1@gmail.com>
parents: 360
diff changeset
7 local math_max = math.max;
262
8c73fb2ff4a2 A treat for Linux users ;)
Matthew Wild <mwild1@gmail.com>
parents: 259
diff changeset
8
8c73fb2ff4a2 A treat for Linux users ;)
Matthew Wild <mwild1@gmail.com>
parents: 259
diff changeset
9 local getstyle, getstring = require "util.termcolours".getstyle, require "util.termcolours".getstring;
8c73fb2ff4a2 A treat for Linux users ;)
Matthew Wild <mwild1@gmail.com>
parents: 259
diff changeset
10 local do_pretty_printing = not os.getenv("WINDIR");
8c73fb2ff4a2 A treat for Linux users ;)
Matthew Wild <mwild1@gmail.com>
parents: 259
diff changeset
11
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 module "logger"
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13
262
8c73fb2ff4a2 A treat for Linux users ;)
Matthew Wild <mwild1@gmail.com>
parents: 259
diff changeset
14 local logstyles = {};
8c73fb2ff4a2 A treat for Linux users ;)
Matthew Wild <mwild1@gmail.com>
parents: 259
diff changeset
15
8c73fb2ff4a2 A treat for Linux users ;)
Matthew Wild <mwild1@gmail.com>
parents: 259
diff changeset
16 --TODO: This should be done in config, but we don't have proper config yet
8c73fb2ff4a2 A treat for Linux users ;)
Matthew Wild <mwild1@gmail.com>
parents: 259
diff changeset
17 if do_pretty_printing then
8c73fb2ff4a2 A treat for Linux users ;)
Matthew Wild <mwild1@gmail.com>
parents: 259
diff changeset
18 logstyles["info"] = getstyle("bold");
8c73fb2ff4a2 A treat for Linux users ;)
Matthew Wild <mwild1@gmail.com>
parents: 259
diff changeset
19 logstyles["warn"] = getstyle("bold", "yellow");
8c73fb2ff4a2 A treat for Linux users ;)
Matthew Wild <mwild1@gmail.com>
parents: 259
diff changeset
20 logstyles["error"] = getstyle("bold", "red");
8c73fb2ff4a2 A treat for Linux users ;)
Matthew Wild <mwild1@gmail.com>
parents: 259
diff changeset
21 end
8c73fb2ff4a2 A treat for Linux users ;)
Matthew Wild <mwild1@gmail.com>
parents: 259
diff changeset
22
437
c1a720db2157 Nice enhancement for logging output
Matthew Wild <mwild1@gmail.com>
parents: 360
diff changeset
23 local sourcewidth = 20;
c1a720db2157 Nice enhancement for logging output
Matthew Wild <mwild1@gmail.com>
parents: 360
diff changeset
24
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 function init(name)
147
ccebb2720741 working incoming s2s \o/
Matthew Wild <mwild1@gmail.com>
parents: 53
diff changeset
26 --name = nil; -- While this line is not commented, will automatically fill in file/line number info
437
c1a720db2157 Nice enhancement for logging output
Matthew Wild <mwild1@gmail.com>
parents: 360
diff changeset
27 sourcewidth = math_max(#name+2, sourcewidth);
c1a720db2157 Nice enhancement for logging output
Matthew Wild <mwild1@gmail.com>
parents: 360
diff changeset
28 local namelen = #name;
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 return function (level, message, ...)
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 if not name then
53
14ea0fe6ca86 Session destruction fixes, some debugging code while we fix the rest. Also change logger to be more useful.
Matthew Wild <mwild1@gmail.com>
parents: 30
diff changeset
31 local inf = debug.getinfo(3, 'Snl');
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 level = level .. ","..tostring(inf.short_src):match("[^/]*$")..":"..inf.currentline;
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 end
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 if ... then
437
c1a720db2157 Nice enhancement for logging output
Matthew Wild <mwild1@gmail.com>
parents: 360
diff changeset
35 io_write(name, rep(" ", sourcewidth-namelen), getstring(logstyles[level], level), "\t", format(message, ...), "\n");
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 else
437
c1a720db2157 Nice enhancement for logging output
Matthew Wild <mwild1@gmail.com>
parents: 360
diff changeset
37 io_write(name, rep(" ", sourcewidth-namelen), getstring(logstyles[level], level), "\t", message, "\n");
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 end
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 end
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 end
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41
360
e918c979ad1a Remove or comment useless prints, or change them to log()
Matthew Wild <mwild1@gmail.com>
parents: 262
diff changeset
42 return _M;