cli: log startup and shutdown, don't clear service logs on restart

Fixes #183696
This commit is contained in:
Connor Peet
2023-06-16 12:31:04 -07:00
parent 8a74ad8ff5
commit 95e90d22ec
2 changed files with 24 additions and 2 deletions

View File

@@ -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::<Vec<_>>().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,
}
}

View File

@@ -159,9 +159,21 @@ pub struct FileLogSink {
file: Arc<std::sync::Mutex<std::fs::File>>,
}
const FILE_LOG_SIZE_LIMIT: u64 = 1024 * 1024 * 10; // 10MB
impl FileLogSink {
pub fn new(level: Level, path: &Path) -> std::io::Result<Self> {
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)),