Comparison

util/logger.lua @ 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)
author Matthew Wild <mwild1@gmail.com>
date Thu, 15 Mar 2012 19:09:24 +0000
parent 3556:d00fc909296e
child 4937:b3fe52353295
comparison
equal deleted inserted replaced
4626:9df9e87d0339 4627:b046cafc81a8
11 local find = string.find; 11 local find = string.find;
12 local ipairs, pairs, setmetatable = ipairs, pairs, setmetatable; 12 local ipairs, pairs, setmetatable = ipairs, pairs, setmetatable;
13 13
14 module "logger" 14 module "logger"
15 15
16 local name_sinks, level_sinks = {}, {}; 16 local level_sinks = {};
17 local name_patterns = {};
18 17
19 local make_logger; 18 local make_logger;
20 19
21 function init(name) 20 function init(name)
22 local log_debug = make_logger(name, "debug"); 21 local log_debug = make_logger(name, "debug");
44 if not level_handlers then 43 if not level_handlers then
45 level_handlers = {}; 44 level_handlers = {};
46 level_sinks[level] = level_handlers; 45 level_sinks[level] = level_handlers;
47 end 46 end
48 47
49 local source_handlers = name_sinks[source_name];
50
51 local logger = function (message, ...) 48 local logger = function (message, ...)
52 if source_handlers then
53 for i = 1,#source_handlers do
54 if source_handlers[i](source_name, level, message, ...) == false then
55 return;
56 end
57 end
58 end
59
60 for i = 1,#level_handlers do 49 for i = 1,#level_handlers do
61 level_handlers[i](source_name, level, message, ...); 50 level_handlers[i](source_name, level, message, ...);
62 end 51 end
63 end 52 end
64 53
65 return logger; 54 return logger;
66 end 55 end
67 56
68 function reset() 57 function reset()
69 for k in pairs(name_sinks) do name_sinks[k] = nil; end
70 for level, handler_list in pairs(level_sinks) do 58 for level, handler_list in pairs(level_sinks) do
71 -- Clear all handlers for this level 59 -- Clear all handlers for this level
72 for i = 1, #handler_list do 60 for i = 1, #handler_list do
73 handler_list[i] = nil; 61 handler_list[i] = nil;
74 end 62 end
75 end 63 end
76 for k in pairs(name_patterns) do name_patterns[k] = nil; end
77 end 64 end
78 65
79 function add_level_sink(level, sink_function) 66 function add_level_sink(level, sink_function)
80 if not level_sinks[level] then 67 if not level_sinks[level] then
81 level_sinks[level] = { sink_function }; 68 level_sinks[level] = { sink_function };
82 else 69 else
83 level_sinks[level][#level_sinks[level] + 1 ] = sink_function; 70 level_sinks[level][#level_sinks[level] + 1 ] = sink_function;
84 end 71 end
85 end 72 end
86 73
87 function add_name_sink(name, sink_function, exclusive)
88 if not name_sinks[name] then
89 name_sinks[name] = { sink_function };
90 else
91 name_sinks[name][#name_sinks[name] + 1] = sink_function;
92 end
93 end
94
95 function add_name_pattern_sink(name_pattern, sink_function, exclusive)
96 if not name_patterns[name_pattern] then
97 name_patterns[name_pattern] = { sink_function };
98 else
99 name_patterns[name_pattern][#name_patterns[name_pattern] + 1] = sink_function;
100 end
101 end
102
103 _M.new = make_logger; 74 _M.new = make_logger;
104 75
105 return _M; 76 return _M;