Files
VoelinClient/crates/voelin-gateway/src/logging.rs
T
Claude 79a12654c8 myTS avatar, badges and User Tag from a saved session; banners; gateway logs and speed
Own account on TeamSpeak 6 servers:
- With only a saved session, the account's avatar and its certificate come
  from myTeamSpeak's avatar service (requestContactsAvatar for the own myTS
  ID), the signed badges from getSignedBadges and the User Tag's token from
  /tschat requestSignedAllowedIdentifierList; no password sign-in needed.
- tsproto::myts::Certificate checks a certificate chain as the server does
  (block types 4-7, leaf type 6, validity, key from the root, cofactorless
  Ed25519); each part is sent with a certificate that verifies it, in
  separate updatemytsdata commands when they differ, and cleared on sign-out.
- client_user_tag is set as the official client sets it
  ({"myts_token","tag","updated"}) on TeamSpeak 6 servers, after a
  self-check of the token; cleared on sign-out or an account change.
- Settings -> My Account shows the User Tag and lets the user choose up to
  three badges to show. The log says what the server published.

Banners:
- The URL (without query) is logged on the first failure; stalls, connect
  failures and missing answers are named; 400/404/410 are retried after
  15 minutes, then hourly; 429/503 honour Retry-After.
- Cached pictures are revalidated (ETag, Last-Modified); at most 6
  downloads per host; a User-Agent with the app version.
- The desktop portal's proxy (Linux) and the system proxy (Windows, macOS)
  are used, SOCKS included; redirects are followed hop by hop.

Gateway, client side:
- Only a refused login stops observing; retries after 1, 2, 5, 10, 20, then
  30 s with jitter, at once when voice connects; a 30 s keepalive.
- Discovery asks DNS and the well-known endpoints at once; observing is not
  restarted when the found list still holds the gateway in use.
- Info logs for discovery, attempts (with the stage that hung), connections,
  losses and the first presence.

tsgw:
- Logging setup (log.level, log.file with size rotation, colour only on a
  terminal) and lifecycle logs: startup summary, the address the server
  sees for queries, WebSocket connections, logins, refusals, slow requests,
  ServerQuery failures, relays and the observer.
- The lookup query connection reconnects after a TeamSpeak restart; query
  connects time out after 10 s; a silent server cannot wedge a connection;
  SIGTERM, startup while TeamSpeak is down and /health are handled.
- TCP_QUICKACK and one write per line: ~45 ms -> <2 ms per SSH command.
- One flood guard shared by all query connections (9 per 3 s, SSH auth
  counted, every connection holds on a 524).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VZBWxxMsxUzp4PSTKVzbZN
2026-10-06 18:28:09 +00:00

203 lines
7.0 KiB
Rust

//! Where the log goes: standard output (journald, `docker logs`) and, with
//! `log.file`, a file as well.
//!
//! What is logged comes from `log.level` (`TSGW_LOG_LEVEL`), else
//! `RUST_LOG`, else [`DEFAULT_FILTER`]. The libraries that log every packet
//! stay at warnings unless the filter names them, so `debug` stays readable.
use std::fs::{File, OpenOptions};
use std::io::{self, IsTerminal, Write};
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex, PoisonError};
use tracing::warn;
use tracing_subscriber::prelude::*;
use tracing_subscriber::{EnvFilter, fmt};
use crate::config::Bootstrap;
/// What is logged when nothing says otherwise.
pub const DEFAULT_FILTER: &str = "info,russh=warn,hyper=warn,h2=warn,tower=warn,tungstenite=warn";
/// Libraries kept at warnings unless a filter names them.
const NOISY: &[&str] = &["russh", "hyper", "h2", "tower", "tungstenite", "tokio_tungstenite"];
/// One log file's limit before it moves aside.
const MAX_BYTES: u64 = 20 << 20;
/// Files kept: the one written and the ones moved aside.
const KEEP: usize = 5;
/// Start logging. Problems with the filter or the file are logged, not
/// fatal. Returns the log file in use.
pub fn init(boot: &Bootstrap) -> Option<PathBuf> {
let rust_log = std::env::var("RUST_LOG").ok();
let (directives, from) = match (&boot.log_level, &rust_log) {
(Some(level), _) => (level.as_str(), "log.level"),
(None, Some(rust_log)) if !rust_log.trim().is_empty() => (rust_log.as_str(), "RUST_LOG"),
_ => (DEFAULT_FILTER, "default"),
};
let (filter, bad_filter) = match EnvFilter::try_new(quiet_noisy(directives)) {
Ok(filter) => (filter, None),
Err(error) => (EnvFilter::new(DEFAULT_FILTER), Some(error)),
};
let ansi = io::stdout().is_terminal() && std::env::var_os("NO_COLOR").is_none();
let console = fmt::layer().with_ansi(ansi);
let file = boot.log_file.as_deref().map(|path| (path, LogFile::open(path, KEEP, MAX_BYTES)));
let to_file = match &file {
Some((_, Ok(log))) => {
let log = log.clone();
Some(fmt::layer().with_ansi(false).with_writer(move || log.clone()))
}
_ => None,
};
tracing_subscriber::registry().with(filter).with(console).with(to_file).init();
if let Some(error) = bad_filter {
warn!(filter = directives, from, %error, "not a log filter; using the default");
}
match file {
Some((path, Ok(_))) => Some(path.to_path_buf()),
Some((path, Err(error))) => {
warn!(
file = %path.display(),
%error,
"cannot write the log file; logging to standard output only"
);
None
}
None => None,
}
}
/// `directives` with the libraries in [`NOISY`] it does not name at
/// warnings.
fn quiet_noisy(directives: &str) -> String {
let named = |lib: &str| {
directives.split(',').any(|d| {
let target = d.trim().split(['=', '[']).next().unwrap_or_default();
target == lib || target.starts_with(&format!("{lib}::"))
})
};
let mut filter = directives.trim().trim_end_matches(',').to_string();
for lib in NOISY.iter().filter(|lib| !named(lib)) {
filter.push_str(&format!(",{lib}=warn"));
}
filter
}
/// A log file written by appending, also across restarts. When it would
/// grow past its limit it moves to `<name>.1.<ext>` (the older ones one
/// further along, the oldest dropped) and a new one starts. Clones write to
/// the same file.
#[derive(Clone)]
pub struct LogFile(Arc<Mutex<Inner>>);
struct Inner {
path: PathBuf,
file: File,
written: u64,
keep: usize,
max: u64,
}
impl LogFile {
pub fn open(path: &Path, keep: usize, max: u64) -> io::Result<Self> {
if let Some(dir) = path.parent().filter(|d| !d.as_os_str().is_empty()) {
std::fs::create_dir_all(dir)?;
}
let file = append(path)?;
let written = file.metadata()?.len();
let inner = Inner { path: path.to_path_buf(), file, written, keep: keep.max(1), max };
Ok(Self(Arc::new(Mutex::new(inner))))
}
}
impl Write for LogFile {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
let mut inner = self.0.lock().unwrap_or_else(PoisonError::into_inner);
if inner.written > 0 && inner.written + buf.len() as u64 > inner.max {
rotate(&inner.path, inner.keep);
// If no new file can be made, the moved one goes on: moving the
// files along on every line would soon drop them all.
if let Ok(file) = append(&inner.path) {
inner.file = file;
}
inner.written = 0;
}
let n = inner.file.write(buf)?;
inner.written += n as u64;
Ok(n)
}
fn flush(&mut self) -> io::Result<()> {
self.0.lock().unwrap_or_else(PoisonError::into_inner).file.flush()
}
}
fn append(path: &Path) -> io::Result<File> {
OpenOptions::new().create(true).append(true).open(path)
}
/// `tsgw.log` as the `n`th one moved aside: `tsgw.1.log`.
fn moved_aside(path: &Path, n: usize) -> PathBuf {
let stem = path.file_stem().unwrap_or_default().to_string_lossy();
let name = match path.extension() {
Some(ext) => format!("{stem}.{n}.{}", ext.to_string_lossy()),
None => format!("{stem}.{n}"),
};
path.with_file_name(name)
}
/// Move the files one along; the oldest goes.
fn rotate(path: &Path, keep: usize) {
if keep <= 1 {
let _ = std::fs::remove_file(path);
return;
}
let _ = std::fs::remove_file(moved_aside(path, keep - 1));
for n in (1..keep - 1).rev() {
let _ = std::fs::rename(moved_aside(path, n), moved_aside(path, n + 1));
}
let _ = std::fs::rename(path, moved_aside(path, 1));
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn noisy_libraries_stay_quiet_unless_named() {
assert_eq!(
quiet_noisy("debug"),
"debug,russh=warn,hyper=warn,h2=warn,tower=warn,tungstenite=warn,tokio_tungstenite=warn"
);
let named = quiet_noisy("info,voelin_query=trace,russh=debug,hyper::proto=info,");
assert!(named.starts_with("info,voelin_query=trace,russh=debug,hyper::proto=info,h2=warn"));
assert!(!named.contains("russh=warn") && !named.contains("hyper=warn"));
assert!(EnvFilter::try_new(quiet_noisy(DEFAULT_FILTER)).is_ok());
assert_eq!(quiet_noisy(DEFAULT_FILTER).matches("russh").count(), 1);
}
#[test]
fn the_file_is_appended_to_and_moved_aside_when_full() {
let dir = std::env::temp_dir().join(format!("tsgw-log-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
let path = dir.join("logs").join("tsgw.log");
let read = |p: PathBuf| std::fs::read_to_string(p).ok();
LogFile::open(&path, 3, 12).unwrap().write_all(b"start 1\n").unwrap();
// A restart goes on in the same file.
let log = LogFile::open(&path, 3, 12).unwrap();
log.clone().write_all(b"two\n").unwrap();
assert_eq!(read(path.clone()).as_deref(), Some("start 1\ntwo\n"));
// Full: moved aside, and the oldest goes.
log.clone().write_all(b"three\n").unwrap();
log.clone().write_all(b"four 4\n").unwrap();
log.clone().write_all(b"five 5\n").unwrap();
assert_eq!(read(path.clone()).as_deref(), Some("five 5\n"));
assert_eq!(read(dir.join("logs/tsgw.1.log")).as_deref(), Some("four 4\n"));
assert_eq!(read(dir.join("logs/tsgw.2.log")).as_deref(), Some("three\n"));
assert_eq!(read(dir.join("logs/tsgw.3.log")), None, "three files in all");
assert_eq!(moved_aside(Path::new("/var/log/tsgw"), 2), Path::new("/var/log/tsgw.2"));
std::fs::remove_dir_all(&dir).unwrap();
}
}