From 95e90d22ec5f0465c63abdc023c88b8b8e31f0b3 Mon Sep 17 00:00:00 2001 From: Connor Peet Date: Fri, 16 Jun 2023 12:31:04 -0700 Subject: [PATCH] cli: log startup and shutdown, don't clear service logs on restart Fixes #183696 --- cli/src/commands/tunnels.rs | 12 +++++++++++- cli/src/log.rs | 14 +++++++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/cli/src/commands/tunnels.rs b/cli/src/commands/tunnels.rs index 3f29ee7020e..d11c15ef1e3 100644 --- a/cli/src/commands/tunnels.rs +++ b/cli/src/commands/tunnels.rs @@ -345,6 +345,13 @@ async fn serve_with_csa( log = log.tee(log_broadcast.clone()); log::install_global_logger(log.clone()); // re-install so that library logs are captured + debug!( + log, + "Starting tunnel with `{} {}`", + APPLICATION_NAME, + std::env::args().collect::>().join(" ") + ); + // Intentionally read before starting the server. If the server updated and // respawn is requested, the old binary will get renamed, and then // current_exe will point to the wrong path. @@ -435,7 +442,10 @@ async fn serve_with_csa( return Ok(exit.code().unwrap_or(1)); } - Next::Exit => return Ok(0), + Next::Exit => { + debug!(log, "Tunnel shut down"); + return Ok(0); + } Next::Restart => continue, } } diff --git a/cli/src/log.rs b/cli/src/log.rs index 1cd908c4b6c..a7561a37f6c 100644 --- a/cli/src/log.rs +++ b/cli/src/log.rs @@ -159,9 +159,21 @@ pub struct FileLogSink { file: Arc>, } +const FILE_LOG_SIZE_LIMIT: u64 = 1024 * 1024 * 10; // 10MB + impl FileLogSink { pub fn new(level: Level, path: &Path) -> std::io::Result { - let file = std::fs::File::create(path)?; + // Truncate the service log occasionally to avoid growing infinitely + if matches!(path.metadata(), Ok(m) if m.len() > FILE_LOG_SIZE_LIMIT) { + // ignore errors, can happen if another process is writing right now + let _ = std::fs::remove_file(path); + } + + let file = std::fs::OpenOptions::new() + .append(true) + .create(true) + .open(path)?; + Ok(Self { level, file: Arc::new(std::sync::Mutex::new(file)),