Comparison

util/logger.lua @ 12802:4a8740e01813

Merge 0.12->trunk
author Kim Alvefur <zash@zash.se>
date Mon, 12 Dec 2022 07:10:54 +0100
parent 12395:1e34b910b73a
child 12975:d10957394a3c
comparison
equal deleted inserted replaced
12801:ebd6b4d8bf04 12802:4a8740e01813
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 = {};
76 return simple_sink_function(name, level, format(msg, ...)); 77 return simple_sink_function(name, level, format(msg, ...));
77 end 78 end
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
82 return sink_function;
83 end
84
85 local function remove_sink(sink_function)
86 local removed;
87 for level, sinks in pairs(level_sinks) do
88 for i = #sinks, 1, -1 do
89 if sinks[i] == sink_function then
90 t_remove(sinks, i);
91 removed = true;
92 end
93 end
94 end
95 return removed;
81 end 96 end
82 97
83 return { 98 return {
84 init = init; 99 init = init;
85 make_logger = make_logger; 100 make_logger = make_logger;
86 reset = reset; 101 reset = reset;
87 add_level_sink = add_level_sink; 102 add_level_sink = add_level_sink;
88 add_simple_sink = add_simple_sink; 103 add_simple_sink = add_simple_sink;
89 new = make_logger; 104 new = make_logger;
105 remove_sink = remove_sink;
90 }; 106 };