Annotate

util/logger.lua @ 632:8f3db0bd2bb4

Merge from waqas
author Matthew Wild <mwild1@gmail.com>
date Mon, 22 Dec 2008 13:29:59 +0000
parent 615:4ae3e81513f3
child 716:d61eabc678a6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
615
4ae3e81513f3 0.1 -> 0.2
Matthew Wild <mwild1@gmail.com>
parents: 582
diff changeset
1 -- Prosody IM v0.2
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 --
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 437
diff changeset
5 -- This program is free software; you can redistribute it and/or
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 437
diff changeset
6 -- modify it under the terms of the GNU General Public License
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 437
diff changeset
7 -- as published by the Free Software Foundation; either version 2
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 437
diff changeset
8 -- of the License, or (at your option) any later version.
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 437
diff changeset
9 --
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 437
diff changeset
10 -- This program is distributed in the hope that it will be useful,
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 437
diff changeset
11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 437
diff changeset
12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 437
diff changeset
13 -- GNU General Public License for more details.
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 437
diff changeset
14 --
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 437
diff changeset
15 -- You should have received a copy of the GNU General Public License
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 437
diff changeset
16 -- along with this program; if not, write to the Free Software
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 437
diff changeset
17 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 437
diff changeset
18 --
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 437
diff changeset
19
437
c1a720db2157 Nice enhancement for logging output
Matthew Wild <mwild1@gmail.com>
parents: 360
diff changeset
20 local format, rep = string.format, string.rep;
c1a720db2157 Nice enhancement for logging output
Matthew Wild <mwild1@gmail.com>
parents: 360
diff changeset
21 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
22 local pcall = pcall;
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 local debug = debug;
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 local tostring = tostring;
437
c1a720db2157 Nice enhancement for logging output
Matthew Wild <mwild1@gmail.com>
parents: 360
diff changeset
25 local math_max = math.max;
262
8c73fb2ff4a2 A treat for Linux users ;)
Matthew Wild <mwild1@gmail.com>
parents: 259
diff changeset
26
8c73fb2ff4a2 A treat for Linux users ;)
Matthew Wild <mwild1@gmail.com>
parents: 259
diff changeset
27 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
28 local do_pretty_printing = not os.getenv("WINDIR");
8c73fb2ff4a2 A treat for Linux users ;)
Matthew Wild <mwild1@gmail.com>
parents: 259
diff changeset
29
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 module "logger"
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31
262
8c73fb2ff4a2 A treat for Linux users ;)
Matthew Wild <mwild1@gmail.com>
parents: 259
diff changeset
32 local logstyles = {};
8c73fb2ff4a2 A treat for Linux users ;)
Matthew Wild <mwild1@gmail.com>
parents: 259
diff changeset
33
8c73fb2ff4a2 A treat for Linux users ;)
Matthew Wild <mwild1@gmail.com>
parents: 259
diff changeset
34 --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
35 if do_pretty_printing then
8c73fb2ff4a2 A treat for Linux users ;)
Matthew Wild <mwild1@gmail.com>
parents: 259
diff changeset
36 logstyles["info"] = getstyle("bold");
8c73fb2ff4a2 A treat for Linux users ;)
Matthew Wild <mwild1@gmail.com>
parents: 259
diff changeset
37 logstyles["warn"] = getstyle("bold", "yellow");
8c73fb2ff4a2 A treat for Linux users ;)
Matthew Wild <mwild1@gmail.com>
parents: 259
diff changeset
38 logstyles["error"] = getstyle("bold", "red");
8c73fb2ff4a2 A treat for Linux users ;)
Matthew Wild <mwild1@gmail.com>
parents: 259
diff changeset
39 end
8c73fb2ff4a2 A treat for Linux users ;)
Matthew Wild <mwild1@gmail.com>
parents: 259
diff changeset
40
437
c1a720db2157 Nice enhancement for logging output
Matthew Wild <mwild1@gmail.com>
parents: 360
diff changeset
41 local sourcewidth = 20;
c1a720db2157 Nice enhancement for logging output
Matthew Wild <mwild1@gmail.com>
parents: 360
diff changeset
42
582
8eb45a8099c4 Make it possible to set custom output handler for logger
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
43 local outfunction = nil;
8eb45a8099c4 Make it possible to set custom output handler for logger
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
44
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 function init(name)
147
ccebb2720741 working incoming s2s \o/
Matthew Wild <mwild1@gmail.com>
parents: 53
diff changeset
46 --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
47 sourcewidth = math_max(#name+2, sourcewidth);
c1a720db2157 Nice enhancement for logging output
Matthew Wild <mwild1@gmail.com>
parents: 360
diff changeset
48 local namelen = #name;
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 return function (level, message, ...)
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 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
51 local inf = debug.getinfo(3, 'Snl');
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 level = level .. ","..tostring(inf.short_src):match("[^/]*$")..":"..inf.currentline;
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 end
582
8eb45a8099c4 Make it possible to set custom output handler for logger
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
54
8eb45a8099c4 Make it possible to set custom output handler for logger
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
55 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
56
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 if ... then
437
c1a720db2157 Nice enhancement for logging output
Matthew Wild <mwild1@gmail.com>
parents: 360
diff changeset
58 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
59 else
437
c1a720db2157 Nice enhancement for logging output
Matthew Wild <mwild1@gmail.com>
parents: 360
diff changeset
60 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
61 end
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 end
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 end
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64
582
8eb45a8099c4 Make it possible to set custom output handler for logger
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
65 function setwriter(f)
8eb45a8099c4 Make it possible to set custom output handler for logger
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
66 if not f then outfunction = nil; return true, nil; end
8eb45a8099c4 Make it possible to set custom output handler for logger
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
67 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
68 if ok then
8eb45a8099c4 Make it possible to set custom output handler for logger
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
69 outfunction = f;
8eb45a8099c4 Make it possible to set custom output handler for logger
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
70 end
8eb45a8099c4 Make it possible to set custom output handler for logger
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
71 return ok, ret;
8eb45a8099c4 Make it possible to set custom output handler for logger
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
72 end
8eb45a8099c4 Make it possible to set custom output handler for logger
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
73
360
e918c979ad1a Remove or comment useless prints, or change them to log()
Matthew Wild <mwild1@gmail.com>
parents: 262
diff changeset
74 return _M;