Skip to content

The all-in-one local AI studio for your desktop: chat, image and video generation and a coding agent in one free, open source app. Windows and Linux. No Docker, no terminal, no cloud required.

fix(logs): the German OS text in lu-app-exit.log turns into errno plus English

purple3fda2a1f7652
diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs
index 2bd2cbed..4a58de05 100644
--- a/src-tauri/src/main.rs
+++ b/src-tauri/src/main.rs
@@ -59,16 +59,53 @@ fn init_tracing() {
if json_mode {
let _ = tracing_subscriber::registry()
.with(filter)
- .with(fmt::layer().json().with_current_span(false).with_span_list(false))
+ .with(
+ fmt::layer()
+ .json()
+ .with_current_span(false)
+ .with_span_list(false)
+ .fmt_fields(EnglishFields(fmt::format::JsonFields::new())),
+ )
.try_init();
} else {
let _ = tracing_subscriber::registry()
.with(filter)
- .with(fmt::layer().compact())
+ .with(fmt::layer().compact().fmt_fields(EnglishFields(fmt::format::DefaultFields::new())))
.try_init();
}
}
+/// A field formatter that runs the finished text through `os_error`.
+///
+/// The house rule is that our messages are English, and `os_error` keeps every
+/// call site of ours to it. A log line written INSIDE a dependency is out of
+/// that reach: hyper-util renders a failed `set_nodelay` itself, and on the
+/// German Windows box that landed in lu-app-exit.log as
+/// `tcp set_nodelay error: Ein ungueltiges Argument wurde angegeben.
+/// (os error 10022)`. We cannot patch the crate, but every event passes
+/// through here on its way out, so this is where the wording gets repaired.
+///
+/// It wraps the real formatter rather than replacing it, so the text and the
+/// JSON mode keep their exact shapes (including the JSON escaping) and only
+/// the operating system's own words are swapped for ours.
+struct EnglishFields<F>(F);
+
+impl<'writer, F> tracing_subscriber::fmt::FormatFields<'writer> for EnglishFields<F>
+where
+ F: for<'a> tracing_subscriber::fmt::FormatFields<'a>,
+{
+ fn format_fields<R: tracing_subscriber::field::RecordFields>(
+ &self,
+ mut writer: tracing_subscriber::fmt::format::Writer<'writer>,
+ fields: R,
+ ) -> std::fmt::Result {
+ let mut buf = String::new();
+ self.0
+ .format_fields(tracing_subscriber::fmt::format::Writer::new(&mut buf), fields)?;
+ writer.write_str(&os_error::sanitize_os_wording(&buf))
+ }
+}
+
fn main() {
#[cfg(target_os = "linux")]
apply_linux_webkit_workarounds();
@@ -520,3 +557,95 @@ mod tests {
cleanup();
}
}
+
+/// The log sink itself, driven end to end.
+///
+/// `os_error`'s own tests prove the rewriting; this proves it is actually
+/// wired into the subscriber, which is the part a refactor drops for free.
+#[cfg(test)]
+mod log_english_tests {
+ use std::sync::{Arc, Mutex};
+
+ /// The connection-refused code of the machine the test runs on. Its
+ /// Display is the operating system's wording, which on a German Windows is
+ /// German and here differs from ours by its capital letter. Either way it
+ /// is text we did not write, and it must not survive.
+ #[cfg(windows)]
+ const REFUSED: i32 = 10061;
+ #[cfg(target_os = "macos")]
+ const REFUSED: i32 = 61;
+ #[cfg(all(unix, not(target_os = "macos")))]
+ const REFUSED: i32 = 111;
+
+ #[derive(Clone)]
+ struct Capture(Arc<Mutex<Vec<u8>>>);
+
+ impl std::io::Write for Capture {
+ fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
+ self.0.lock().unwrap().extend_from_slice(buf);
+ Ok(buf.len())
+ }
+ fn flush(&mut self) -> std::io::Result<()> {
+ Ok(())
+ }
+ }
+
+ impl<'a> tracing_subscriber::fmt::MakeWriter<'a> for Capture {
+ type Writer = Capture;
+ fn make_writer(&'a self) -> Self::Writer {
+ self.clone()
+ }
+ }
+
+ fn logged(f: impl FnOnce()) -> String {
+ let buf = Arc::new(Mutex::new(Vec::new()));
+ let subscriber = tracing_subscriber::fmt()
+ .with_writer(Capture(buf.clone()))
+ .with_ansi(false)
+ .fmt_fields(super::EnglishFields(
+ tracing_subscriber::fmt::format::DefaultFields::new(),
+ ))
+ .finish();
+ tracing::subscriber::with_default(subscriber, f);
+ let out = buf.lock().unwrap().clone();
+ String::from_utf8(out).expect("the log is utf8")
+ }
+
+ #[test]
+ fn a_dependency_that_logs_an_os_error_still_reads_in_our_words() {
+ // Exactly the hyper-util line that put German text into
+ // lu-app-exit.log on the Windows box.
+ let e = std::io::Error::from_raw_os_error(REFUSED);
+ let os_worded = e.to_string();
+ let out = logged(|| tracing::warn!("tcp set_nodelay error: {}", e));
+ assert!(out.contains("tcp set_nodelay error: "), "got: {out}");
+ assert!(out.contains("connection refused"), "got: {out}");
+ assert!(out.contains(&format!("os error {REFUSED}")), "got: {out}");
+ assert!(!out.contains(&os_worded), "the system wording survived: {out}");
+ }
+
+ /// The test above builds its own subscriber, so on its own it would still
+ /// pass if `init_tracing` stopped using the wrapper. This is the other
+ /// half: BOTH log modes have to go through it, or a user on JSON logs
+ /// keeps the German line the text mode no longer has.
+ #[test]
+ fn both_log_modes_are_wired_through_the_wrapper() {
+ const SRC: &str = include_str!("main.rs");
+ let init = &SRC[SRC.find("fn init_tracing()").expect("init_tracing exists")..];
+ let init = &init[..init.find("\n}\n").expect("the function ends")];
+ assert_eq!(
+ init.matches("EnglishFields(").count(),
+ 2,
+ "text mode and json mode must both sanitise:\n{init}"
+ );
+ }
+
+ // Negative control: a line the operating system had no hand in must come
+ // out byte for byte, fields and all.
+ #[test]
+ fn an_ordinary_line_is_logged_unchanged() {
+ let out = logged(|| tracing::info!(version = "2.6.7", "LU starting"));
+ assert!(out.contains("LU starting"), "got: {out}");
+ assert!(out.contains("version=\"2.6.7\""), "got: {out}");
+ }
+}
diff --git a/src-tauri/src/os_error.rs b/src-tauri/src/os_error.rs
index a0c09453..ca5286e7 100644
--- a/src-tauri/src/os_error.rs
+++ b/src-tauri/src/os_error.rs
@@ -63,6 +63,9 @@ fn code_phrase(code: i32) -> Option<&'static str> {
145 => "the directory is not empty",
1224 => "the file is open by another process",
10013 => "the port is blocked, by permissions or by a firewall",
+ // hyper-util logs a failed set_nodelay with this one, and that log
+ // line is what showed German text in lu-app-exit.log on the box.
+ 10022 => "an invalid argument was supplied",
10048 => "the port is already in use",
10049 => "the address is not available on this machine",
10060 => "the connection timed out",
@@ -128,6 +131,55 @@ where
english_dyn(err)
}
+/// Every operating system worded phrase in a line of text, replaced by ours.
+///
+/// `english_dyn` above can only help where we hold the error value. A log line
+/// written INSIDE a dependency is past that point: hyper-util renders a failed
+/// `set_nodelay` with `warn!("tcp set_nodelay error: {}", e)`, and on the
+/// German Windows box that reached lu-app-exit.log as
+/// `tcp set_nodelay error: Ein ungueltiges Argument wurde angegeben.
+/// (os error 10022)`. We cannot patch the crate, but the log sink is ours, so
+/// the wording is repaired on the way out.
+///
+/// No guessing is involved and no phrase is parsed. A `(os error N)` in the
+/// text names the code; asking this machine for that code's own Display gives
+/// back the exact bytes the operating system would have written, and those
+/// exact bytes are what gets replaced. A line that carries no code, or whose
+/// wording is already ours, comes back untouched and unallocated.
+pub fn sanitize_os_wording(text: &str) -> std::borrow::Cow<'_, str> {
+ const MARK: &str = "(os error ";
+ if !text.contains(MARK) {
+ return std::borrow::Cow::Borrowed(text);
+ }
+
+ // Every distinct code the line mentions.
+ let mut codes: Vec<i32> = Vec::new();
+ let mut rest = text;
+ while let Some(at) = rest.find(MARK) {
+ let after = &rest[at + MARK.len()..];
+ let end = after.find(|c: char| !c.is_ascii_digit()).unwrap_or(after.len());
+ if end > 0 && after[end..].starts_with(')') {
+ if let Ok(code) = after[..end].parse::<i32>() {
+ if !codes.contains(&code) {
+ codes.push(code);
+ }
+ }
+ }
+ rest = &after[end..];
+ }
+
+ let mut out = std::borrow::Cow::Borrowed(text);
+ for code in codes {
+ let e = std::io::Error::from_raw_os_error(code);
+ let os_worded = e.to_string();
+ let ours = io_english(&e);
+ if os_worded != ours && out.contains(&os_worded) {
+ out = std::borrow::Cow::Owned(out.replace(&os_worded, &ours));
+ }
+ }
+ out
+}
+
#[cfg(test)]
mod tests {
use super::*;
@@ -160,6 +212,12 @@ mod tests {
#[cfg(all(unix, not(target_os = "macos")))]
const REFUSED: i32 = 111;
+ /// A second, different code, so a line carrying two of them is covered.
+ #[cfg(windows)]
+ const NOT_FOUND: i32 = 2;
+ #[cfg(unix)]
+ const NOT_FOUND: i32 = 2;
+
#[test]
fn an_os_error_is_answered_in_our_words_not_the_systems() {
let e = Error::from_raw_os_error(REFUSED);
@@ -225,6 +283,63 @@ mod tests {
assert!(!ours.contains(&os_worded), "the OS wording survived: {}", ours);
}
+ #[test]
+ fn a_log_line_written_inside_a_dependency_loses_the_system_wording() {
+ // The exact shape from lu-app-exit.log on the box, with this machine's
+ // own code standing in for 10022: hyper-util formats the io::Error
+ // itself, so what lands in the log is the operating system's wording.
+ let e = Error::from_raw_os_error(REFUSED);
+ let line = format!("tcp set_nodelay error: {}", e);
+ let fixed = sanitize_os_wording(&line);
+ assert!(fixed.starts_with("tcp set_nodelay error: "), "got: {}", fixed);
+ // The number is the whole point of keeping anything at all.
+ assert!(fixed.contains(&format!("os error {}", REFUSED)), "got: {}", fixed);
+ assert!(fixed.contains("connection refused"), "got: {}", fixed);
+ // And the wording the system chose is gone. On a German Windows that
+ // string is German; this assertion does not care which language it was.
+ assert!(!fixed.contains(&e.to_string()), "the system wording survived: {}", fixed);
+ }
+
+ #[test]
+ fn two_codes_in_one_line_are_both_repaired() {
+ let a = Error::from_raw_os_error(REFUSED);
+ let b = Error::from_raw_os_error(NOT_FOUND);
+ let line = format!("first: {} | second: {}", a, b);
+ let fixed = sanitize_os_wording(&line);
+ assert!(!fixed.contains(&a.to_string()), "got: {}", fixed);
+ assert!(!fixed.contains(&b.to_string()), "got: {}", fixed);
+ assert!(fixed.contains(&format!("os error {}", REFUSED)), "got: {}", fixed);
+ assert!(fixed.contains(&format!("os error {}", NOT_FOUND)), "got: {}", fixed);
+ assert!(fixed.contains(" | second: "), "the scaffolding was eaten: {}", fixed);
+ }
+
+ // Negative controls: everything that is not the operating system talking
+ // has to come back byte for byte, and without an allocation.
+ #[test]
+ fn an_ordinary_log_line_is_returned_untouched() {
+ for line in [
+ "LU starting version=2.6.7",
+ "engine loaded mlabonne_gemma-3-4b-it-abliterated-Q4_K_M",
+ "the file is in use by another process",
+ // A number that is not a code, and a marker that never closes.
+ "os error is not the same as (os error ) here",
+ "(os error abc)",
+ ] {
+ let out = sanitize_os_wording(line);
+ assert_eq!(out, line);
+ assert!(matches!(out, std::borrow::Cow::Borrowed(_)), "allocated for: {}", line);
+ }
+ }
+
+ #[test]
+ fn a_line_that_already_reads_in_our_words_is_left_exactly_as_it_is() {
+ let ours = io_english(&Error::from_raw_os_error(REFUSED));
+ let line = format!("proxy_localhost: {}", ours);
+ let out = sanitize_os_wording(&line);
+ assert_eq!(out, line);
+ assert!(matches!(out, std::borrow::Cow::Borrowed(_)), "rewrote its own output");
+ }
+
#[test]
fn a_chain_without_an_os_error_is_returned_untouched() {
let inner = Error::new(ErrorKind::InvalidData, "bad json");
@@ -236,7 +351,7 @@ mod tests {
fn every_named_code_reads_as_a_sentence_and_none_is_empty() {
// A guard on the tables: an entry added with an empty or capitalised
// string would read wrong inside "Failed to X: <phrase>".
- for code in [32, 33, 145, 1224, 10013, 10048, 10049, 10060, 10061] {
+ for code in [32, 33, 145, 1224, 10013, 10022, 10048, 10049, 10060, 10061] {
let p = code_phrase(code).expect("table entry vanished");
assert!(!p.is_empty());
assert!(p.chars().next().unwrap().is_lowercase(), "code {} is capitalised", code);