From c42bcb2f4f7453657a90024e11681f658597c6a5 Mon Sep 17 00:00:00 2001 From: fufsob Date: Tue, 29 Sep 2026 09:30:54 +0000 Subject: [PATCH] feat: follow logs is scrollable --- README.md | 3 +- docs/usage/control.md | 5 +++- pkgs/proxy-ctl.nix | 2 ++ pkgs/proxy-ctl/proxy_ctl.py | 51 ++++++++++++++++++++++++++++---- pkgs/proxy-ctl/test_proxy_ctl.py | 21 +++++++++++++ 5 files changed, 74 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index c938caf..a3aba40 100644 --- a/README.md +++ b/README.md @@ -138,7 +138,8 @@ Changes and secrets need root or the userControl group. status [--json] services and routing mode (--tray: deprecated) restart restart running services - logs [unit] follow logs (default: all proxy-suite units) + logs [unit...] follow logs (default: all proxy-suite units); in a + terminal, in less: Ctrl-C scrolls back, F follows where how a domain is routed right now proxy [status|on|off] local proxy diff --git a/docs/usage/control.md b/docs/usage/control.md index 5ebbb0b..7eb98c4 100644 --- a/docs/usage/control.md +++ b/docs/usage/control.md @@ -71,10 +71,13 @@ becomes a global profile, or an outbound when pasted onto the Outbounds tab. See ```sh proxy-ctl status # everything at a glance proxy-ctl status --json # the same, for scripts -proxy-ctl logs # follow the logs of every proxy-suite service +proxy-ctl logs # follow the logs of every proxy-suite service, with the last 1000 lines proxy-ctl restart # restart what is running ``` +In a terminal, `proxy-ctl logs` follows in `less`: Ctrl-C stops following so you can scroll +back, `F` follows again and `q` quits. + ## See also - [`userControl.scopes`](../options/userControl.md#services-proxy-suite-usercontrol-scopes) diff --git a/pkgs/proxy-ctl.nix b/pkgs/proxy-ctl.nix index 46febb0..601721c 100644 --- a/pkgs/proxy-ctl.nix +++ b/pkgs/proxy-ctl.nix @@ -184,6 +184,8 @@ let pkgs.bash pkgs.curl pkgs.fzf + # `proxy-ctl logs` follows in less. + pkgs.less pkgs.proxychains-ng pkgs.qrencode pkgs.systemd diff --git a/pkgs/proxy-ctl/proxy_ctl.py b/pkgs/proxy-ctl/proxy_ctl.py index 424a966..d0fce6f 100644 --- a/pkgs/proxy-ctl/proxy_ctl.py +++ b/pkgs/proxy-ctl/proxy_ctl.py @@ -12,6 +12,7 @@ import ipaddress import json import os import re +import shutil import signal import socket import subprocess @@ -57,7 +58,8 @@ Changes and secrets need root or the userControl group. status [--json] services and routing mode (--tray: deprecated) restart restart running services - logs [unit] follow logs (default: all proxy-suite units) + logs [unit...] follow logs (default: all proxy-suite units); in a + terminal, in less: Ctrl-C scrolls back, F follows where how a domain is routed right now proxy [status|on|off] local proxy @@ -3930,12 +3932,49 @@ ALIASES = { } +LOGS_BACKLOG = 1000 + + def cmd_logs(*units): - if units: - # One -u per unit: a bare second name would be taken as a journal match. - _exec(_manager_argv("journalctl", ["-f", *(f"--unit={u}" for u in units)])) - # journalctl takes unit globs, so the default needs no unit list of its own. - _exec(_manager_argv("journalctl", ["-f", "-u", "proxy-suite-*"])) + # One --unit per unit: a bare second name would be taken as a journal match. journalctl + # takes unit globs, so the default needs no unit list of its own. + argv = _manager_argv( + "journalctl", + ["-f", "-n", str(LOGS_BACKLOG), *(f"--unit={u}" for u in units or ["proxy-suite-*"])], + ) + pager = shutil.which("less") + if not (pager and sys.stdin.isatty() and sys.stdout.isatty()): + _exec(argv) + sys.exit(_follow_in_pager(argv, pager)) + + +def _follow_in_pager(argv, pager): + """argv's output in less, following it: Ctrl-C stops to scroll back, F follows again, q quits. + + argv runs in a session of its own, so Ctrl-C reaches less alone. + """ + sys.stdout.flush() + old = signal.signal(signal.SIGINT, signal.SIG_IGN) + try: + source = subprocess.Popen( + argv, + stdin=subprocess.DEVNULL, + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT, + start_new_session=True, + # journalctl colors only a terminal; less -R shows them. + env={**os.environ, "SYSTEMD_COLORS": "1"}, + ) + except OSError as e: + signal.signal(signal.SIGINT, old) + die(f"proxy-ctl: {argv[0]}: {e.strerror}", 127) + try: + return subprocess.call([pager, "-R", "-S", "-M", "+F"], stdin=source.stdout) + finally: + source.stdout.close() + source.terminate() + source.wait() + signal.signal(signal.SIGINT, old) COMMANDS = { diff --git a/pkgs/proxy-ctl/test_proxy_ctl.py b/pkgs/proxy-ctl/test_proxy_ctl.py index a9136c1..23fb9f6 100644 --- a/pkgs/proxy-ctl/test_proxy_ctl.py +++ b/pkgs/proxy-ctl/test_proxy_ctl.py @@ -353,6 +353,27 @@ class ServiceManagerTest(EnvTest): self.assertEqual(seen, [["systemctl", "--user", "start", "anchor.service"], ["systemctl", "start", "proxy-suite-socks"]]) self.assertEqual(ctl.journal_hint("proxy-suite-autoproxy-learn", 20), "journalctl -u proxy-suite-autoproxy-learn -n 20") + def test_logs(self): + ran = [] + + def exec_(argv): + ran.append(("exec", argv)) + raise SystemExit(0) + + self.patch("_exec", exec_) + self.patch("_follow_in_pager", lambda argv, pager: ran.append(("less", argv)) or 0) + self.patch("shutil", mock.Mock(which=lambda name: f"/bin/{name}")) + # Piped (run() captures stdout): journalctl itself, with a backlog. + run(ctl.cmd_logs) + self.assertEqual(ran[-1], ("exec", ["journalctl", "-f", "-n", "1000", "--unit=proxy-suite-*"])) + # In a terminal: through less, one --unit per named unit. + terminal = mock.Mock(isatty=lambda: True) + with mock.patch.object(ctl.sys, "stdin", terminal), mock.patch.object(ctl.sys, "stdout", terminal): + with self.assertRaises(SystemExit) as done: + ctl.cmd_logs("a", "b") + self.assertEqual(done.exception.code, 0) + self.assertEqual(ran[-1], ("less", ["journalctl", "-f", "-n", "1000", "--unit=a", "--unit=b"])) + def test_kill_switch_lifts_only_on_purpose(self): seen = self.calls() stops = lambda: [argv[2] for argv in seen if argv[1] == "stop"]