feat: follow logs is scrollable
This commit is contained in:
5 files changed
+74
-8
No files matched your search
@@ -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 <domain> how a domain is routed right now
|
||||
|
||||
proxy [status|on|off] local proxy
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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 <domain> 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 = {
|
||||
|
||||
@@ -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"]
|
||||
|
||||
Reference in new issue
Block a user