Diff

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
line wrap: on
line diff
--- a/util/logger.lua	Mon Mar 07 00:35:29 2022 +0100
+++ b/util/logger.lua	Thu Mar 17 10:19:35 2022 +0000
@@ -10,6 +10,7 @@
 local pairs = pairs;
 local ipairs = ipairs;
 local require = require;
+local t_remove = table.remove;
 
 local _ENV = nil;
 -- luacheck: std none
@@ -80,6 +81,19 @@
 	end
 end
 
+local function remove_sink(sink_function)
+	local removed;
+	for level, sinks in pairs(level_sinks) do
+		for i = #sinks, 1, -1 do
+			if sinks[i] == sink_function then
+				t_remove(sinks, i);
+				removed = true;
+			end
+		end
+	end
+	return removed;
+end
+
 return {
 	init = init;
 	make_logger = make_logger;
@@ -87,4 +101,5 @@
 	add_level_sink = add_level_sink;
 	add_simple_sink = add_simple_sink;
 	new = make_logger;
+	remove_sink = remove_sink;
 };