Diff

plugins/mod_admin_shell.lua @ 12399:d99772b739e0

mod_admin_shell: Add watch:log() command to tail logs in realtime
author Matthew Wild <mwild1@gmail.com>
date Thu, 17 Mar 2022 10:24:38 +0000
parent 12398:478fff93ac37
child 12464:f4c59af273a3
line wrap: on
line diff
--- a/plugins/mod_admin_shell.lua	Thu Mar 17 10:23:12 2022 +0000
+++ b/plugins/mod_admin_shell.lua	Thu Mar 17 10:24:38 2022 +0000
@@ -36,6 +36,7 @@
 local serialize_config = serialization.new ({ fatal = false, unquoted = true});
 local time = require "util.time";
 local promise = require "util.promise";
+local logger = require "util.logger";
 
 local t_insert = table.insert;
 local t_concat = table.concat;
@@ -1589,6 +1590,26 @@
 	return true;
 end
 
+def_env.watch = {};
+
+function def_env.watch:log()
+	local writing = false;
+	local sink = logger.add_simple_sink(function (source, level, message)
+		if writing then return; end
+		writing = true;
+		self.session.print(source, level, message);
+		writing = false;
+	end);
+
+	while self.session.is_connected() do
+		async.sleep(3);
+	end
+	if not logger.remove_sink(sink) then
+		module:log("warn", "Unable to remove watch:log() sink");
+	end
+end
+
+
 def_env.debug = {};
 
 function def_env.debug:logevents(host)