Skip to content

Commit 95e90d2

Browse files
committed
cli: log startup and shutdown, don't clear service logs on restart
Fixes microsoft#183696
1 parent 8a74ad8 commit 95e90d2

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

cli/src/commands/tunnels.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,13 @@ async fn serve_with_csa(
345345
log = log.tee(log_broadcast.clone());
346346
log::install_global_logger(log.clone()); // re-install so that library logs are captured
347347

348+
debug!(
349+
log,
350+
"Starting tunnel with `{} {}`",
351+
APPLICATION_NAME,
352+
std::env::args().collect::<Vec<_>>().join(" ")
353+
);
354+
348355
// Intentionally read before starting the server. If the server updated and
349356
// respawn is requested, the old binary will get renamed, and then
350357
// current_exe will point to the wrong path.
@@ -435,7 +442,10 @@ async fn serve_with_csa(
435442

436443
return Ok(exit.code().unwrap_or(1));
437444
}
438-
Next::Exit => return Ok(0),
445+
Next::Exit => {
446+
debug!(log, "Tunnel shut down");
447+
return Ok(0);
448+
}
439449
Next::Restart => continue,
440450
}
441451
}

cli/src/log.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,21 @@ pub struct FileLogSink {
159159
file: Arc<std::sync::Mutex<std::fs::File>>,
160160
}
161161

162+
const FILE_LOG_SIZE_LIMIT: u64 = 1024 * 1024 * 10; // 10MB
163+
162164
impl FileLogSink {
163165
pub fn new(level: Level, path: &Path) -> std::io::Result<Self> {
164-
let file = std::fs::File::create(path)?;
166+
// Truncate the service log occasionally to avoid growing infinitely
167+
if matches!(path.metadata(), Ok(m) if m.len() > FILE_LOG_SIZE_LIMIT) {
168+
// ignore errors, can happen if another process is writing right now
169+
let _ = std::fs::remove_file(path);
170+
}
171+
172+
let file = std::fs::OpenOptions::new()
173+
.append(true)
174+
.create(true)
175+
.open(path)?;
176+
165177
Ok(Self {
166178
level,
167179
file: Arc::new(std::sync::Mutex::new(file)),

0 commit comments

Comments
 (0)