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>
This commit is contained in:
1 parent
a839d48e02
commit
23c1687118
5 files changed
+176
-1
No files matched your search
Generated
+1
@@ -4982,6 +4982,7 @@ version = "0.1.0-pre.2"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"clap",
|
||||
"libc",
|
||||
"ps-core",
|
||||
"ps-ipc",
|
||||
"rand 0.9.5",
|
||||
|
||||
@@ -0,0 +1,152 @@
|
||||
//! 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)))
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
//! notification.
|
||||
|
||||
mod dpi;
|
||||
mod logs;
|
||||
mod outbounds;
|
||||
mod overview;
|
||||
mod telegram;
|
||||
@@ -60,6 +61,8 @@ pub struct AppView {
|
||||
dpi: Entity<dpi::DpiPage>,
|
||||
telegram: Entity<telegram::TelegramPage>,
|
||||
outbounds: Entity<outbounds::OutboundsPage>,
|
||||
/// Made when first shown: following the logs starts then.
|
||||
logs: Option<Entity<logs::LogsPage>>,
|
||||
_subscriptions: Vec<Subscription>,
|
||||
}
|
||||
|
||||
@@ -78,7 +81,7 @@ impl AppView {
|
||||
let dpi = cx.new(|cx| dpi::DpiPage::new(model.clone(), window, cx));
|
||||
let telegram = cx.new(|cx| telegram::TelegramPage::new(model.clone(), window, cx));
|
||||
let outbounds = cx.new(|cx| outbounds::OutboundsPage::new(model.clone(), window, cx));
|
||||
Self { model, page: Page::Overview, dpi, telegram, outbounds, _subscriptions: subscriptions }
|
||||
Self { model, page: Page::Overview, dpi, telegram, outbounds, logs: None, _subscriptions: subscriptions }
|
||||
}
|
||||
|
||||
fn sidebar(&self, cx: &mut Context<Self>) -> impl IntoElement {
|
||||
@@ -99,6 +102,10 @@ impl Render for AppView {
|
||||
Page::Dpi => self.dpi.clone().into_any_element(),
|
||||
Page::Telegram => self.telegram.clone().into_any_element(),
|
||||
Page::Outbounds => self.outbounds.clone().into_any_element(),
|
||||
Page::Logs => {
|
||||
let model = self.model.clone();
|
||||
self.logs.get_or_insert_with(|| cx.new(|cx| logs::LogsPage::new(model, cx))).clone().into_any_element()
|
||||
}
|
||||
other => widgets::page(other.title(), cx).child(div().text_color(cx.theme().muted_foreground).child("Not here yet.")).into_any_element(),
|
||||
};
|
||||
div()
|
||||
|
||||
@@ -37,3 +37,6 @@ windows-sys = { workspace = true, features = [
|
||||
"Win32_UI_WindowsAndMessaging",
|
||||
"Win32_System_Threading",
|
||||
] }
|
||||
|
||||
[target.'cfg(target_os = "linux")'.dependencies]
|
||||
libc = "0.2"
|
||||
@@ -280,6 +280,18 @@ async fn run(ctx: RunCtx, mut stop: watch::Receiver<bool>) {
|
||||
// No console window for the children.
|
||||
cmd.creation_flags(0x0800_0000);
|
||||
}
|
||||
// On Linux (development, tests) the children end with the service, as the Job Object
|
||||
// ends them on Windows, even when the service is killed outright. The signal comes when
|
||||
// the spawning thread ends: a runtime worker, which lives as long as the service.
|
||||
#[cfg(target_os = "linux")]
|
||||
unsafe {
|
||||
cmd.pre_exec(|| {
|
||||
if libc::prctl(libc::PR_SET_PDEATHSIG, libc::SIGKILL) != 0 {
|
||||
return Err(std::io::Error::last_os_error());
|
||||
}
|
||||
Ok(())
|
||||
});
|
||||
}
|
||||
let mut child = match cmd.spawn() {
|
||||
Ok(c) => c,
|
||||
Err(e) => {
|
||||
|
||||
Reference in new issue
Block a user