Comparison

util/logger.lua @ 12394:86d6b069b892

util.logger: Support for removing individual log sinks without a full reset
author Matthew Wild <mwild1@gmail.com>
date Thu, 17 Mar 2022 10:19:35 +0000
parent 8556:5800f0c7afd6
child 12395:1e34b910b73a
comparison
equal deleted inserted replaced
12388:50fcd3879482 12394:86d6b069b892
8 -- luacheck: ignore 213/level 8 -- luacheck: ignore 213/level
9 9
10 local pairs = pairs; 10 local pairs = pairs;
11 local ipairs = ipairs; 11 local ipairs = ipairs;
12 local require = require; 12 local require = require;
13 local t_remove = table.remove;
13 14
14 local _ENV = nil; 15 local _ENV = nil;
15 -- luacheck: std none 16 -- luacheck: std none
16 17
17 local level_sinks = {}; 18 local level_sinks = {};
78 for _, level in ipairs(levels or {"debug", "info", "warn", "error"}) do 79 for _, level in ipairs(levels or {"debug", "info", "warn", "error"}) do
79 add_level_sink(level, sink_function); 80 add_level_sink(level, sink_function);
80 end 81 end
81 end 82 end
82 83
84 local function remove_sink(sink_function)
85 local removed;
86 for level, sinks in pairs(level_sinks) do
87 for i = #sinks, 1, -1 do
88 if sinks[i] == sink_function then
89 t_remove(sinks, i);
90 removed = true;
91 end
92 end
93 end
94 return removed;
95 end
96
83 return { 97 return {
84 init = init; 98 init = init;
85 make_logger = make_logger; 99 make_logger = make_logger;
86 reset = reset; 100 reset = reset;
87 add_level_sink = add_level_sink; 101 add_level_sink = add_level_sink;
88 add_simple_sink = add_simple_sink; 102 add_simple_sink = add_simple_sink;
89 new = make_logger; 103 new = make_logger;
104 remove_sink = remove_sink;
90 }; 105 };