Annotate

util/logger.lua @ 759:5cccfb5da6cb

0.2->0.3
author Matthew Wild <mwild1@gmail.com>
date Fri, 30 Jan 2009 17:40:25 +0000
parent 758:b1885732e979
child 760:90ce865eebd8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
759
5cccfb5da6cb 0.2->0.3
Matthew Wild <mwild1@gmail.com>
parents: 758
diff changeset
1 -- Prosody IM v0.3
519
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 437
diff changeset
2 -- Copyright (C) 2008 Matthew Wild
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 437
diff changeset
3 -- Copyright (C) 2008 Waqas Hussain
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 437
diff changeset
4 --
758
b1885732e979 GPL->MIT!
Matthew Wild <mwild1@gmail.com>
parents: 716
diff changeset
5 -- This project is MIT/X11 licensed. Please see the
b1885732e979 GPL->MIT!
Matthew Wild <mwild1@gmail.com>
parents: 716
diff changeset
6 -- COPYING file in the source package for more information.
519
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 437
diff changeset
7 --
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 437
diff changeset
8
437
c1a720db2157 Nice enhancement for logging output
Matthew Wild <mwild1@gmail.com>
parents: 360
diff changeset
9 local format, rep = string.format, string.rep;
c1a720db2157 Nice enhancement for logging output
Matthew Wild <mwild1@gmail.com>
parents: 360
diff changeset
10 local io_write = io.write;
582
8eb45a8099c4 Make it possible to set custom output handler for logger
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
11 local pcall = pcall;
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 local debug = debug;
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 local tostring = tostring;
437
c1a720db2157 Nice enhancement for logging output
Matthew Wild <mwild1@gmail.com>
parents: 360
diff changeset
14 local math_max = math.max;
262
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 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
17 local do_pretty_printing = not os.getenv("WINDIR");
8c73fb2ff4a2 A treat for Linux users ;)
Matthew Wild <mwild1@gmail.com>
parents: 259
diff changeset
18
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 module "logger"
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20
262
8c73fb2ff4a2 A treat for Linux users ;)
Matthew Wild <mwild1@gmail.com>
parents: 259
diff changeset
21 local logstyles = {};
8c73fb2ff4a2 A treat for Linux users ;)
Matthew Wild <mwild1@gmail.com>
parents: 259
diff changeset
22
8c73fb2ff4a2 A treat for Linux users ;)
Matthew Wild <mwild1@gmail.com>
parents: 259
diff changeset
23 --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
24 if do_pretty_printing then
8c73fb2ff4a2 A treat for Linux users ;)
Matthew Wild <mwild1@gmail.com>
parents: 259
diff changeset
25 logstyles["info"] = getstyle("bold");
8c73fb2ff4a2 A treat for Linux users ;)
Matthew Wild <mwild1@gmail.com>
parents: 259
diff changeset
26 logstyles["warn"] = getstyle("bold", "yellow");
8c73fb2ff4a2 A treat for Linux users ;)
Matthew Wild <mwild1@gmail.com>
parents: 259
diff changeset
27 logstyles["error"] = getstyle("bold", "red");
8c73fb2ff4a2 A treat for Linux users ;)
Matthew Wild <mwild1@gmail.com>
parents: 259
diff changeset
28 end
8c73fb2ff4a2 A treat for Linux users ;)
Matthew Wild <mwild1@gmail.com>
parents: 259
diff changeset
29
437
c1a720db2157 Nice enhancement for logging output
Matthew Wild <mwild1@gmail.com>
parents: 360
diff changeset
30 local sourcewidth = 20;
c1a720db2157 Nice enhancement for logging output
Matthew Wild <mwild1@gmail.com>
parents: 360
diff changeset
31
582
8eb45a8099c4 Make it possible to set custom output handler for logger
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
32 local outfunction = nil;
8eb45a8099c4 Make it possible to set custom output handler for logger
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
33
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 function init(name)
147
ccebb2720741 working incoming s2s \o/
Matthew Wild <mwild1@gmail.com>
parents: 53
diff changeset
35 --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
36 sourcewidth = math_max(#name+2, sourcewidth);
c1a720db2157 Nice enhancement for logging output
Matthew Wild <mwild1@gmail.com>
parents: 360
diff changeset
37 local namelen = #name;
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 return function (level, message, ...)
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 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
40 local inf = debug.getinfo(3, 'Snl');
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 level = level .. ","..tostring(inf.short_src):match("[^/]*$")..":"..inf.currentline;
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 end
582
8eb45a8099c4 Make it possible to set custom output handler for logger
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
43
8eb45a8099c4 Make it possible to set custom output handler for logger
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
44 if outfunction then return outfunction(name, level, message, ...); end
8eb45a8099c4 Make it possible to set custom output handler for logger
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
45
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 if ... then
437
c1a720db2157 Nice enhancement for logging output
Matthew Wild <mwild1@gmail.com>
parents: 360
diff changeset
47 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
48 else
437
c1a720db2157 Nice enhancement for logging output
Matthew Wild <mwild1@gmail.com>
parents: 360
diff changeset
49 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
50 end
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 end
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 end
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53
582
8eb45a8099c4 Make it possible to set custom output handler for logger
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
54 function setwriter(f)
716
d61eabc678a6 util/logger: setwriter now returns the old writer on success
Waqas Hussain <waqas20@gmail.com>
parents: 615
diff changeset
55 local old_func = outfunction;
d61eabc678a6 util/logger: setwriter now returns the old writer on success
Waqas Hussain <waqas20@gmail.com>
parents: 615
diff changeset
56 if not f then outfunction = nil; return true, old_func; end
582
8eb45a8099c4 Make it possible to set custom output handler for logger
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
57 local ok, ret = pcall(f, "logger", "info", "Switched logging output successfully");
8eb45a8099c4 Make it possible to set custom output handler for logger
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
58 if ok then
8eb45a8099c4 Make it possible to set custom output handler for logger
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
59 outfunction = f;
716
d61eabc678a6 util/logger: setwriter now returns the old writer on success
Waqas Hussain <waqas20@gmail.com>
parents: 615
diff changeset
60 ret = old_func;
582
8eb45a8099c4 Make it possible to set custom output handler for logger
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
61 end
8eb45a8099c4 Make it possible to set custom output handler for logger
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
62 return ok, ret;
8eb45a8099c4 Make it possible to set custom output handler for logger
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
63 end
8eb45a8099c4 Make it possible to set custom output handler for logger
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
64
360
e918c979ad1a Remove or comment useless prints, or change them to log()
Matthew Wild <mwild1@gmail.com>
parents: 262
diff changeset
65 return _M;