Annotate

util/logger.lua @ 972:144d0ad17389

prosody: Don't look to use SSL when LuaSec not available (thanks Florob)
author Matthew Wild <mwild1@gmail.com>
date Thu, 09 Apr 2009 18:48:28 +0100
parent 896:2c0b9e3c11c3
child 974:82f7261c0482
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
896
2c0b9e3c11c3 0.3->0.4
Matthew Wild <mwild1@gmail.com>
parents: 884
diff changeset
1 -- Prosody IM v0.4
760
90ce865eebd8 Update copyright notices for 2009
Matthew Wild <mwild1@gmail.com>
parents: 759
diff changeset
2 -- Copyright (C) 2008-2009 Matthew Wild
90ce865eebd8 Update copyright notices for 2009
Matthew Wild <mwild1@gmail.com>
parents: 759
diff changeset
3 -- Copyright (C) 2008-2009 Waqas Hussain
519
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
883
0112ae30f399 Support to filter logging by source via pattern matching from config file.
Tobias Markmann <tm@ayena.de>
parents: 805
diff changeset
16 local config = require "core.configmanager";
0112ae30f399 Support to filter logging by source via pattern matching from config file.
Tobias Markmann <tm@ayena.de>
parents: 805
diff changeset
17 local log_sources = config.get("*", "core", "log_sources");
0112ae30f399 Support to filter logging by source via pattern matching from config file.
Tobias Markmann <tm@ayena.de>
parents: 805
diff changeset
18
262
8c73fb2ff4a2 A treat for Linux users ;)
Matthew Wild <mwild1@gmail.com>
parents: 259
diff changeset
19 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
20 local do_pretty_printing = not os.getenv("WINDIR");
884
5758c39285ab util.logger: Small code tidying :)
Matthew Wild <mwild1@gmail.com>
parents: 883
diff changeset
21 local find = string.find;
883
0112ae30f399 Support to filter logging by source via pattern matching from config file.
Tobias Markmann <tm@ayena.de>
parents: 805
diff changeset
22 local ipairs = ipairs;
262
8c73fb2ff4a2 A treat for Linux users ;)
Matthew Wild <mwild1@gmail.com>
parents: 259
diff changeset
23
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 module "logger"
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25
262
8c73fb2ff4a2 A treat for Linux users ;)
Matthew Wild <mwild1@gmail.com>
parents: 259
diff changeset
26 local logstyles = {};
8c73fb2ff4a2 A treat for Linux users ;)
Matthew Wild <mwild1@gmail.com>
parents: 259
diff changeset
27
8c73fb2ff4a2 A treat for Linux users ;)
Matthew Wild <mwild1@gmail.com>
parents: 259
diff changeset
28 --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
29 if do_pretty_printing then
8c73fb2ff4a2 A treat for Linux users ;)
Matthew Wild <mwild1@gmail.com>
parents: 259
diff changeset
30 logstyles["info"] = getstyle("bold");
8c73fb2ff4a2 A treat for Linux users ;)
Matthew Wild <mwild1@gmail.com>
parents: 259
diff changeset
31 logstyles["warn"] = getstyle("bold", "yellow");
8c73fb2ff4a2 A treat for Linux users ;)
Matthew Wild <mwild1@gmail.com>
parents: 259
diff changeset
32 logstyles["error"] = getstyle("bold", "red");
8c73fb2ff4a2 A treat for Linux users ;)
Matthew Wild <mwild1@gmail.com>
parents: 259
diff changeset
33 end
8c73fb2ff4a2 A treat for Linux users ;)
Matthew Wild <mwild1@gmail.com>
parents: 259
diff changeset
34
437
c1a720db2157 Nice enhancement for logging output
Matthew Wild <mwild1@gmail.com>
parents: 360
diff changeset
35 local sourcewidth = 20;
c1a720db2157 Nice enhancement for logging output
Matthew Wild <mwild1@gmail.com>
parents: 360
diff changeset
36
582
8eb45a8099c4 Make it possible to set custom output handler for logger
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
37 local outfunction = nil;
8eb45a8099c4 Make it possible to set custom output handler for logger
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
38
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 function init(name)
883
0112ae30f399 Support to filter logging by source via pattern matching from config file.
Tobias Markmann <tm@ayena.de>
parents: 805
diff changeset
40 if log_sources then
0112ae30f399 Support to filter logging by source via pattern matching from config file.
Tobias Markmann <tm@ayena.de>
parents: 805
diff changeset
41 local log_this = false;
0112ae30f399 Support to filter logging by source via pattern matching from config file.
Tobias Markmann <tm@ayena.de>
parents: 805
diff changeset
42 for _, source in ipairs(log_sources) do
0112ae30f399 Support to filter logging by source via pattern matching from config file.
Tobias Markmann <tm@ayena.de>
parents: 805
diff changeset
43 if find(name, source) then
884
5758c39285ab util.logger: Small code tidying :)
Matthew Wild <mwild1@gmail.com>
parents: 883
diff changeset
44 log_this = true;
5758c39285ab util.logger: Small code tidying :)
Matthew Wild <mwild1@gmail.com>
parents: 883
diff changeset
45 break;
883
0112ae30f399 Support to filter logging by source via pattern matching from config file.
Tobias Markmann <tm@ayena.de>
parents: 805
diff changeset
46 end
0112ae30f399 Support to filter logging by source via pattern matching from config file.
Tobias Markmann <tm@ayena.de>
parents: 805
diff changeset
47 end
0112ae30f399 Support to filter logging by source via pattern matching from config file.
Tobias Markmann <tm@ayena.de>
parents: 805
diff changeset
48
0112ae30f399 Support to filter logging by source via pattern matching from config file.
Tobias Markmann <tm@ayena.de>
parents: 805
diff changeset
49 if not log_this then return function () end end
0112ae30f399 Support to filter logging by source via pattern matching from config file.
Tobias Markmann <tm@ayena.de>
parents: 805
diff changeset
50 end
0112ae30f399 Support to filter logging by source via pattern matching from config file.
Tobias Markmann <tm@ayena.de>
parents: 805
diff changeset
51
147
ccebb2720741 working incoming s2s \o/
Matthew Wild <mwild1@gmail.com>
parents: 53
diff changeset
52 --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
53 local namelen = #name;
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 return function (level, message, ...)
582
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
805
5fbbf34ef1c4 Logging format improvement
Waqas Hussain <waqas20@gmail.com>
parents: 760
diff changeset
57 sourcewidth = math_max(#name+2, sourcewidth);
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 if ... then
437
c1a720db2157 Nice enhancement for logging output
Matthew Wild <mwild1@gmail.com>
parents: 360
diff changeset
59 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
60 else
437
c1a720db2157 Nice enhancement for logging output
Matthew Wild <mwild1@gmail.com>
parents: 360
diff changeset
61 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
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 end
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65
582
8eb45a8099c4 Make it possible to set custom output handler for logger
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
66 function setwriter(f)
716
d61eabc678a6 util/logger: setwriter now returns the old writer on success
Waqas Hussain <waqas20@gmail.com>
parents: 615
diff changeset
67 local old_func = outfunction;
d61eabc678a6 util/logger: setwriter now returns the old writer on success
Waqas Hussain <waqas20@gmail.com>
parents: 615
diff changeset
68 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
69 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
70 if ok then
8eb45a8099c4 Make it possible to set custom output handler for logger
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
71 outfunction = f;
716
d61eabc678a6 util/logger: setwriter now returns the old writer on success
Waqas Hussain <waqas20@gmail.com>
parents: 615
diff changeset
72 ret = old_func;
582
8eb45a8099c4 Make it possible to set custom output handler for logger
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
73 end
8eb45a8099c4 Make it possible to set custom output handler for logger
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
74 return ok, ret;
8eb45a8099c4 Make it possible to set custom output handler for logger
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
75 end
8eb45a8099c4 Make it possible to set custom output handler for logger
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
76
360
e918c979ad1a Remove or comment useless prints, or change them to log()
Matthew Wild <mwild1@gmail.com>
parents: 262
diff changeset
77 return _M;