Software /
code /
prosody
Annotate
util/logger.lua @ 3555:6938b6153890
mod_posix: Remove redundant import of logger.setwriter()
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Thu, 04 Nov 2010 08:41:24 +0000 |
parent | 3554:1770f8aaf04a |
child | 3556:d00fc909296e |
rev | line source |
---|---|
1523
841d61be198f
Remove version number from copyright headers
Matthew Wild <mwild1@gmail.com>
parents:
1020
diff
changeset
|
1 -- Prosody IM |
2923
b7049746bd29
Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
2 -- Copyright (C) 2008-2010 Matthew Wild |
b7049746bd29
Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
3 -- Copyright (C) 2008-2010 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 |
582
8eb45a8099c4
Make it possible to set custom output handler for logger
Matthew Wild <mwild1@gmail.com>
parents:
519
diff
changeset
|
9 local pcall = pcall; |
262 | 10 |
884
5758c39285ab
util.logger: Small code tidying :)
Matthew Wild <mwild1@gmail.com>
parents:
883
diff
changeset
|
11 local find = string.find; |
1015
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
12 local ipairs, pairs, setmetatable = ipairs, pairs, setmetatable; |
262 | 13 |
30 | 14 module "logger" |
15 | |
1015
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
16 local name_sinks, level_sinks = {}, {}; |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
17 local name_patterns = {}; |
262 | 18 |
1015
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
19 local make_logger; |
582
8eb45a8099c4
Make it possible to set custom output handler for logger
Matthew Wild <mwild1@gmail.com>
parents:
519
diff
changeset
|
20 local outfunction = nil; |
8eb45a8099c4
Make it possible to set custom output handler for logger
Matthew Wild <mwild1@gmail.com>
parents:
519
diff
changeset
|
21 |
30 | 22 function init(name) |
1015
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
23 local log_debug = make_logger(name, "debug"); |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
24 local log_info = make_logger(name, "info"); |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
25 local log_warn = make_logger(name, "warn"); |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
26 local log_error = make_logger(name, "error"); |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
27 |
147 | 28 --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
|
29 local namelen = #name; |
1015
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
30 return function (level, message, ...) |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
31 if outfunction then return outfunction(name, level, message, ...); end |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
32 |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
33 if level == "debug" then |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
34 return log_debug(message, ...); |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
35 elseif level == "info" then |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
36 return log_info(message, ...); |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
37 elseif level == "warn" then |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
38 return log_warn(message, ...); |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
39 elseif level == "error" then |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
40 return log_error(message, ...); |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
41 end |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
42 end |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
43 end |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
44 |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
45 function make_logger(source_name, level) |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
46 local level_handlers = level_sinks[level]; |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
47 if not level_handlers then |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
48 level_handlers = {}; |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
49 level_sinks[level] = level_handlers; |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
50 end |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
51 |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
52 local source_handlers = name_sinks[source_name]; |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
53 |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
54 local logger = function (message, ...) |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
55 if source_handlers then |
3554
1770f8aaf04a
util.logger: Remove my precious premature optimisation :(
Matthew Wild <mwild1@gmail.com>
parents:
3540
diff
changeset
|
56 for i = 1,#source_handlers do |
1020
8bf71f8bd0d1
util.logger: Fix for attempting to call a table
Matthew Wild <mwild1@gmail.com>
parents:
1015
diff
changeset
|
57 if source_handlers[i](source_name, level, message, ...) == false then |
1015
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
58 return; |
30 | 59 end |
60 end | |
1015
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
61 end |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
62 |
3554
1770f8aaf04a
util.logger: Remove my precious premature optimisation :(
Matthew Wild <mwild1@gmail.com>
parents:
3540
diff
changeset
|
63 for i = 1,#level_handlers do |
1015
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
64 level_handlers[i](source_name, level, message, ...); |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
65 end |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
66 end |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
67 |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
68 return logger; |
30 | 69 end |
70 | |
582
8eb45a8099c4
Make it possible to set custom output handler for logger
Matthew Wild <mwild1@gmail.com>
parents:
519
diff
changeset
|
71 function setwriter(f) |
716
d61eabc678a6
util/logger: setwriter now returns the old writer on success
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
72 local old_func = outfunction; |
d61eabc678a6
util/logger: setwriter now returns the old writer on success
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
73 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
|
74 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
|
75 if ok then |
8eb45a8099c4
Make it possible to set custom output handler for logger
Matthew Wild <mwild1@gmail.com>
parents:
519
diff
changeset
|
76 outfunction = f; |
716
d61eabc678a6
util/logger: setwriter now returns the old writer on success
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
77 ret = old_func; |
582
8eb45a8099c4
Make it possible to set custom output handler for logger
Matthew Wild <mwild1@gmail.com>
parents:
519
diff
changeset
|
78 end |
8eb45a8099c4
Make it possible to set custom output handler for logger
Matthew Wild <mwild1@gmail.com>
parents:
519
diff
changeset
|
79 return ok, ret; |
8eb45a8099c4
Make it possible to set custom output handler for logger
Matthew Wild <mwild1@gmail.com>
parents:
519
diff
changeset
|
80 end |
8eb45a8099c4
Make it possible to set custom output handler for logger
Matthew Wild <mwild1@gmail.com>
parents:
519
diff
changeset
|
81 |
3357
1dd83dd2b832
util.logger: Add reset() method to clear all sinks
Matthew Wild <mwild1@gmail.com>
parents:
2923
diff
changeset
|
82 function reset() |
1dd83dd2b832
util.logger: Add reset() method to clear all sinks
Matthew Wild <mwild1@gmail.com>
parents:
2923
diff
changeset
|
83 for k in pairs(name_sinks) do name_sinks[k] = nil; end |
1dd83dd2b832
util.logger: Add reset() method to clear all sinks
Matthew Wild <mwild1@gmail.com>
parents:
2923
diff
changeset
|
84 for level, handler_list in pairs(level_sinks) do |
1dd83dd2b832
util.logger: Add reset() method to clear all sinks
Matthew Wild <mwild1@gmail.com>
parents:
2923
diff
changeset
|
85 -- Clear all handlers for this level |
1dd83dd2b832
util.logger: Add reset() method to clear all sinks
Matthew Wild <mwild1@gmail.com>
parents:
2923
diff
changeset
|
86 for i = 1, #handler_list do |
1dd83dd2b832
util.logger: Add reset() method to clear all sinks
Matthew Wild <mwild1@gmail.com>
parents:
2923
diff
changeset
|
87 handler_list[i] = nil; |
1dd83dd2b832
util.logger: Add reset() method to clear all sinks
Matthew Wild <mwild1@gmail.com>
parents:
2923
diff
changeset
|
88 end |
1dd83dd2b832
util.logger: Add reset() method to clear all sinks
Matthew Wild <mwild1@gmail.com>
parents:
2923
diff
changeset
|
89 end |
1dd83dd2b832
util.logger: Add reset() method to clear all sinks
Matthew Wild <mwild1@gmail.com>
parents:
2923
diff
changeset
|
90 for k in pairs(name_patterns) do name_patterns[k] = nil; end |
1dd83dd2b832
util.logger: Add reset() method to clear all sinks
Matthew Wild <mwild1@gmail.com>
parents:
2923
diff
changeset
|
91 end |
1dd83dd2b832
util.logger: Add reset() method to clear all sinks
Matthew Wild <mwild1@gmail.com>
parents:
2923
diff
changeset
|
92 |
1015
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
93 function add_level_sink(level, sink_function) |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
94 if not level_sinks[level] then |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
95 level_sinks[level] = { sink_function }; |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
96 else |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
97 level_sinks[level][#level_sinks[level] + 1 ] = sink_function; |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
98 end |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
99 end |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
100 |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
101 function add_name_sink(name, sink_function, exclusive) |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
102 if not name_sinks[name] then |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
103 name_sinks[name] = { sink_function }; |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
104 else |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
105 name_sinks[name][#name_sinks[name] + 1] = sink_function; |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
106 end |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
107 end |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
108 |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
109 function add_name_pattern_sink(name_pattern, sink_function, exclusive) |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
110 if not name_patterns[name_pattern] then |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
111 name_patterns[name_pattern] = { sink_function }; |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
112 else |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
113 name_patterns[name_pattern][#name_patterns[name_pattern] + 1] = sink_function; |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
114 end |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
115 end |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
116 |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
117 _M.new = make_logger; |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
118 |
360
e918c979ad1a
Remove or comment useless prints, or change them to log()
Matthew Wild <mwild1@gmail.com>
parents:
262
diff
changeset
|
119 return _M; |