Software /
code /
prosody
Annotate
util/logger.lua @ 4636:41983ec223f0
mod_http: Include handlers of non-global modules.
author | Waqas Hussain <waqas20@gmail.com> |
---|---|
date | Sat, 21 Apr 2012 18:23:44 +0500 |
parent | 4627:b046cafc81a8 |
child | 4937:b3fe52353295 |
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 | |
4627
b046cafc81a8
loggingmanager, util.logger: Remove name sinks and the ability to filter logs by source name (lots of code, hardly used if at all, and possibly broken)
Matthew Wild <mwild1@gmail.com>
parents:
3556
diff
changeset
|
16 local level_sinks = {}; |
262 | 17 |
1015
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
18 local make_logger; |
582
8eb45a8099c4
Make it possible to set custom output handler for logger
Matthew Wild <mwild1@gmail.com>
parents:
519
diff
changeset
|
19 |
30 | 20 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
|
21 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
|
22 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
|
23 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
|
24 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
|
25 |
147 | 26 --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
|
27 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
|
28 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
|
29 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
|
30 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
|
31 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
|
32 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
|
33 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
|
34 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
|
35 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
|
36 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
|
37 end |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
38 end |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
39 end |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
40 |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
41 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
|
42 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
|
43 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
|
44 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
|
45 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
|
46 end |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
47 |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
48 local logger = function (message, ...) |
3554
1770f8aaf04a
util.logger: Remove my precious premature optimisation :(
Matthew Wild <mwild1@gmail.com>
parents:
3540
diff
changeset
|
49 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
|
50 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
|
51 end |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
52 end |
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 return logger; |
30 | 55 end |
56 | |
3357
1dd83dd2b832
util.logger: Add reset() method to clear all sinks
Matthew Wild <mwild1@gmail.com>
parents:
2923
diff
changeset
|
57 function reset() |
1dd83dd2b832
util.logger: Add reset() method to clear all sinks
Matthew Wild <mwild1@gmail.com>
parents:
2923
diff
changeset
|
58 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
|
59 -- 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
|
60 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
|
61 handler_list[i] = nil; |
1dd83dd2b832
util.logger: Add reset() method to clear all sinks
Matthew Wild <mwild1@gmail.com>
parents:
2923
diff
changeset
|
62 end |
1dd83dd2b832
util.logger: Add reset() method to clear all sinks
Matthew Wild <mwild1@gmail.com>
parents:
2923
diff
changeset
|
63 end |
1dd83dd2b832
util.logger: Add reset() method to clear all sinks
Matthew Wild <mwild1@gmail.com>
parents:
2923
diff
changeset
|
64 end |
1dd83dd2b832
util.logger: Add reset() method to clear all sinks
Matthew Wild <mwild1@gmail.com>
parents:
2923
diff
changeset
|
65 |
1015
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
66 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
|
67 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
|
68 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
|
69 else |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
70 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
|
71 end |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
72 end |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
73 |
9e31e9397cff
util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
Matthew Wild <mwild1@gmail.com>
parents:
977
diff
changeset
|
74 _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
|
75 |
360
e918c979ad1a
Remove or comment useless prints, or change them to log()
Matthew Wild <mwild1@gmail.com>
parents:
262
diff
changeset
|
76 return _M; |