Comparison

util/logger.lua @ 3554:1770f8aaf04a

util.logger: Remove my precious premature optimisation :(
author Matthew Wild <mwild1@gmail.com>
date Thu, 04 Nov 2010 08:37:39 +0000
parent 3540:bc139431830b
child 3556:d00fc909296e
comparison
equal deleted inserted replaced
3553:1f0af8572f15 3554:1770f8aaf04a
13 13
14 module "logger" 14 module "logger"
15 15
16 local name_sinks, level_sinks = {}, {}; 16 local name_sinks, level_sinks = {}, {};
17 local name_patterns = {}; 17 local name_patterns = {};
18
19 -- Weak-keyed so that loggers are collected
20 local modify_hooks = setmetatable({}, { __mode = "k" });
21 18
22 local make_logger; 19 local make_logger;
23 local outfunction = nil; 20 local outfunction = nil;
24 21
25 function init(name) 22 function init(name)
52 level_sinks[level] = level_handlers; 49 level_sinks[level] = level_handlers;
53 end 50 end
54 51
55 local source_handlers = name_sinks[source_name]; 52 local source_handlers = name_sinks[source_name];
56 53
57 -- All your premature optimisation is belong to me!
58 local num_level_handlers, num_source_handlers = #level_handlers, source_handlers and #source_handlers;
59
60 local logger = function (message, ...) 54 local logger = function (message, ...)
61 if source_handlers then 55 if source_handlers then
62 for i = 1,num_source_handlers do 56 for i = 1,#source_handlers do
63 if source_handlers[i](source_name, level, message, ...) == false then 57 if source_handlers[i](source_name, level, message, ...) == false then
64 return; 58 return;
65 end 59 end
66 end 60 end
67 end 61 end
68 62
69 for i = 1,num_level_handlers do 63 for i = 1,#level_handlers do
70 level_handlers[i](source_name, level, message, ...); 64 level_handlers[i](source_name, level, message, ...);
71 end 65 end
72 end 66 end
73 67
74 -- To make sure our cached lengths stay in sync with reality
75 modify_hooks[logger] = function () num_level_handlers, num_source_handlers = #level_handlers, source_handlers and #source_handlers; end;
76
77 return logger; 68 return logger;
78 end 69 end
79 70
80 function setwriter(f) 71 function setwriter(f)
81 local old_func = outfunction; 72 local old_func = outfunction;
95 for i = 1, #handler_list do 86 for i = 1, #handler_list do
96 handler_list[i] = nil; 87 handler_list[i] = nil;
97 end 88 end
98 end 89 end
99 for k in pairs(name_patterns) do name_patterns[k] = nil; end 90 for k in pairs(name_patterns) do name_patterns[k] = nil; end
100
101 for _, modify_hook in pairs(modify_hooks) do
102 modify_hook();
103 end
104 end 91 end
105 92
106 function add_level_sink(level, sink_function) 93 function add_level_sink(level, sink_function)
107 if not level_sinks[level] then 94 if not level_sinks[level] then
108 level_sinks[level] = { sink_function }; 95 level_sinks[level] = { sink_function };
109 else 96 else
110 level_sinks[level][#level_sinks[level] + 1 ] = sink_function; 97 level_sinks[level][#level_sinks[level] + 1 ] = sink_function;
111 end 98 end
112
113 for _, modify_hook in pairs(modify_hooks) do
114 modify_hook();
115 end
116 end 99 end
117 100
118 function add_name_sink(name, sink_function, exclusive) 101 function add_name_sink(name, sink_function, exclusive)
119 if not name_sinks[name] then 102 if not name_sinks[name] then
120 name_sinks[name] = { sink_function }; 103 name_sinks[name] = { sink_function };
121 else 104 else
122 name_sinks[name][#name_sinks[name] + 1] = sink_function; 105 name_sinks[name][#name_sinks[name] + 1] = sink_function;
123 end
124
125 for _, modify_hook in pairs(modify_hooks) do
126 modify_hook();
127 end 106 end
128 end 107 end
129 108
130 function add_name_pattern_sink(name_pattern, sink_function, exclusive) 109 function add_name_pattern_sink(name_pattern, sink_function, exclusive)
131 if not name_patterns[name_pattern] then 110 if not name_patterns[name_pattern] then