author | Matthew Wild <mwild1@gmail.com> |
Fri, 10 Apr 2009 10:31:38 +0100 | |
changeset 974 | 82f7261c0482 |
parent 896 | 2c0b9e3c11c3 |
child 977 | 6f0bdf9e4dfb |
permissions | -rw-r--r-- |
896 | 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 | 5 |
-- This project is MIT/X11 licensed. Please see the |
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 | 12 |
local debug = debug; |
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 | 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 | 19 |
local getstyle, getstring = require "util.termcolours".getstyle, require "util.termcolours".getstring; |
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 | 23 |
|
30 | 24 |
module "logger" |
25 |
||
262 | 26 |
local logstyles = {}; |
27 |
||
28 |
--TODO: This should be done in config, but we don't have proper config yet |
|
29 |
if do_pretty_printing then |
|
30 |
logstyles["info"] = getstyle("bold"); |
|
31 |
logstyles["warn"] = getstyle("bold", "yellow"); |
|
32 |
logstyles["error"] = getstyle("bold", "red"); |
|
33 |
end |
|
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 | 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 |
|
974 | 52 |
if name == "modulemanager" or name:match("^c2s") or name == "datamanager" then return function () end; end |
53 |
||
147 | 54 |
--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
|
55 |
local namelen = #name; |
30 | 56 |
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
|
57 |
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
|
58 |
|
805
5fbbf34ef1c4
Logging format improvement
Waqas Hussain <waqas20@gmail.com>
parents:
760
diff
changeset
|
59 |
sourcewidth = math_max(#name+2, sourcewidth); |
30 | 60 |
if ... then |
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", format(message, ...), "\n"); |
30 | 62 |
else |
437
c1a720db2157
Nice enhancement for logging output
Matthew Wild <mwild1@gmail.com>
parents:
360
diff
changeset
|
63 |
io_write(name, rep(" ", sourcewidth-namelen), getstring(logstyles[level], level), "\t", message, "\n"); |
30 | 64 |
end |
65 |
end |
|
66 |
end |
|
67 |
||
582
8eb45a8099c4
Make it possible to set custom output handler for logger
Matthew Wild <mwild1@gmail.com>
parents:
519
diff
changeset
|
68 |
function setwriter(f) |
716
d61eabc678a6
util/logger: setwriter now returns the old writer on success
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
69 |
local old_func = outfunction; |
d61eabc678a6
util/logger: setwriter now returns the old writer on success
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
70 |
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
|
71 |
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
|
72 |
if ok then |
8eb45a8099c4
Make it possible to set custom output handler for logger
Matthew Wild <mwild1@gmail.com>
parents:
519
diff
changeset
|
73 |
outfunction = f; |
716
d61eabc678a6
util/logger: setwriter now returns the old writer on success
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
74 |
ret = old_func; |
582
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 |
return ok, ret; |
8eb45a8099c4
Make it possible to set custom output handler for logger
Matthew Wild <mwild1@gmail.com>
parents:
519
diff
changeset
|
77 |
end |
8eb45a8099c4
Make it possible to set custom output handler for logger
Matthew Wild <mwild1@gmail.com>
parents:
519
diff
changeset
|
78 |
|
360
e918c979ad1a
Remove or comment useless prints, or change them to log()
Matthew Wild <mwild1@gmail.com>
parents:
262
diff
changeset
|
79 |
return _M; |