feat(connlib): always symlink to latest log file (#8400)

When debugging Firezone, it is useful to use `tail -f` on the current
logfile to see what `connlib` is doing. This is quite annoying to do
however because the log file rolls over with every restart of the
application. As a small QoL improvement, we always symlink the latest
log file to a link called `latest`. Therefore, all one needs to do is
re-run the latest `tail -f ./latest` command to get the new logs.

Resolves: #8388
This commit is contained in:
Thomas Eizinger
2025-03-11 01:16:58 +11:00
committed by GitHub
parent a9cc428b32
commit 421f8e76d6

View File

@@ -19,11 +19,14 @@ use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::{fs, io};
use anyhow::Context;
use time::OffsetDateTime;
use tracing::Subscriber;
use tracing_appender::non_blocking::{NonBlocking, WorkerGuard};
use tracing_subscriber::Layer;
use crate::unwrap_or_debug;
pub const TIME_FORMAT: &str = "[year]-[month]-[day]-[hour]-[minute]-[second]";
/// How many lines we will at most buffer in the channel with the background thread that writes to disk.
@@ -130,6 +133,7 @@ impl Appender {
let filename = format!("{}.{date}.{}", self.file_base_name, self.file_extension);
let path = self.directory.join(&filename);
let latest = self.directory.join("latest");
let mut open_options = fs::OpenOptions::new();
open_options.append(true).create(true);
@@ -146,6 +150,21 @@ impl Appender {
let file = new_file?;
Self::set_permissions(&file)?;
let _ = std::fs::remove_file(&latest);
#[cfg(unix)]
unwrap_or_debug!(
std::os::unix::fs::symlink(path, latest)
.context("Failed to create `latest` link to log file"),
"{}"
);
#[cfg(windows)]
unwrap_or_debug!(
std::os::windows::fs::symlink_file(path, latest)
.context("Failed to create `latest` link to log file"),
"{}"
);
Ok((file, filename))
}