Files
proxy-suite-flake/windows/crates/ps-gui/src/ui/logs.rs
T
faumarayandClaude Opus 5.5 23c1687118 windows: the app's Logs page; on Linux, units end with the service
- Logs: every unit's as they come (the last 500 first, 5000 kept), one
  unit's on its own, following or held; terminal colours stripped; a
  virtual list, so thousands of lines cost nothing. The follow starts
  when the page is first opened and reconnects by itself.
- Linux (development, tests): the service's children get SIGKILL when it
  dies (PR_SET_PDEATHSIG), as the Job Object does on Windows; a killed
  service no longer leaves sing-box running (the e2e test leaked one per
  run).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 01:41:12 +03:00

153 lines
5.6 KiB
Rust

//! Logs: every unit's, as they come, the last few thousand lines kept; one unit's on its own.
use std::collections::VecDeque;
use gpui_kit::component::button::Button;
use gpui_kit::component::radio::RadioGroup;
use gpui_kit::component::switch::Switch;
use gpui_kit::component::*;
use gpui_kit::*;
use super::widgets::{card, hint, page};
use crate::bridge::LogEvent;
use crate::model::Model;
const KEEP: usize = 5000;
/// The line without its terminal colours (sing-box and XRay write them).
fn plain(line: &str) -> String {
let mut out = String::with_capacity(line.len());
let mut chars = line.chars().peekable();
while let Some(c) = chars.next() {
if c == '\u{1b}' && chars.peek() == Some(&'[') {
chars.next();
for c in chars.by_ref() {
if c.is_ascii_alphabetic() {
break;
}
}
} else {
out.push(c);
}
}
out
}
/// A unit's short name: what follows "proxy-suite-".
fn short(unit: &str) -> &str {
unit.strip_prefix("proxy-suite-").unwrap_or(unit)
}
pub struct LogsPage {
lines: VecDeque<(SharedString, SharedString)>,
units: Vec<SharedString>,
/// Only this unit's lines; all with none.
only: Option<SharedString>,
follow: bool,
lost: Option<SharedString>,
scroll: UniformListScrollHandle,
_drain: Task<()>,
}
impl LogsPage {
pub fn new(model: Entity<Model>, cx: &mut Context<Self>) -> Self {
let events = model.read(cx).bridge().follow_logs(500);
let drain = cx.spawn(async move |this, cx| {
while let Ok(first) = events.recv().await {
// What has piled up comes in one update, not one per line.
let mut batch = vec![first];
while let Ok(more) = events.try_recv() {
batch.push(more);
}
if this.update(cx, |this, cx| this.take(batch, cx)).is_err() {
break;
}
}
});
Self { lines: VecDeque::new(), units: Vec::new(), only: None, follow: true, lost: None, scroll: UniformListScrollHandle::new(), _drain: drain }
}
fn take(&mut self, batch: Vec<LogEvent>, cx: &mut Context<Self>) {
for event in batch {
match event {
LogEvent::Line { unit, line } => {
self.lost = None;
let unit = SharedString::from(unit);
if !self.units.contains(&unit) {
self.units.push(unit.clone());
self.units.sort();
}
self.lines.push_back((unit, plain(&line).into()));
if self.lines.len() > KEEP {
self.lines.pop_front();
}
}
LogEvent::Lost(why) => self.lost = Some(why.into()),
}
}
if self.follow {
let shown = self.shown().len();
if shown > 0 {
self.scroll.scroll_to_item(shown - 1, ScrollStrategy::Bottom);
}
}
cx.notify();
}
fn shown(&self) -> Vec<usize> {
(0..self.lines.len()).filter(|i| self.only.as_ref().is_none_or(|u| &self.lines[*i].0 == u)).collect()
}
}
impl Render for LogsPage {
fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let shown = self.shown();
let mut choices: Vec<SharedString> = vec!["All".into()];
choices.extend(self.units.iter().map(|u| SharedString::from(short(u).to_owned())));
let selected = match &self.only {
None => 0,
Some(u) => self.units.iter().position(|x| x == u).map_or(0, |i| i + 1),
};
let filter = RadioGroup::horizontal("log-unit").children(choices).selected_index(Some(selected)).on_change(cx.listener(|this, ix: &usize, _, cx| {
this.only = if *ix == 0 { None } else { this.units.get(*ix - 1).cloned() };
cx.notify();
}));
let follow = Switch::new("log-follow").label("Follow").checked(self.follow).on_change(cx.listener(|this, on: &bool, _, cx| {
this.follow = *on;
cx.notify();
}));
let clear = Button::new("log-clear").small().label("Clear").on_click(cx.listener(|this, _, _, cx| {
this.lines.clear();
cx.notify();
}));
let lines = uniform_list(
"log-lines",
shown.len(),
cx.processor(move |this, range: std::ops::Range<usize>, _, cx| {
range
.map(|i| {
let (unit, line) = &this.lines[shown[i]];
div()
.h_flex()
.gap_2()
.text_xs()
.font_family("monospace")
.child(div().w(px(110.)).flex_none().truncate().text_color(cx.theme().muted_foreground).child(short(unit).to_owned()))
.child(div().flex_1().min_w_0().truncate().child(line.clone()))
})
.collect()
}),
)
.track_scroll(&self.scroll)
.h(px(460.));
let mut body = card("Logs", Some(div().h_flex().gap_3().items_center().child(follow).child(clear).into_any_element()), cx).child(filter);
if let Some(why) = &self.lost {
body = body.child(hint(format!("Not receiving logs: {why}"), cx));
}
page("Logs", cx).child(body.child(div().p_2().rounded_md().bg(cx.theme().muted).child(lines)))
}
}