Skip to content

Commit

Permalink
Hack to get correct log file (#4873)
Browse files Browse the repository at this point in the history
### Description

Match the logic in `tracing_appender::Rotation::DAILY` to generate the
correct log filename to report in `turbo daemon status`. This is kind of
a hack, but there did not appear to be an easy way to grab this value.

### Testing Instructions

Tested manually

Co-authored-by: Greg Soltis <Greg Soltis>
  • Loading branch information
gsoltis committed May 8, 2023
1 parent eb71778 commit 013c0bb
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 1 deletion.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/turborepo-lib/Cargo.toml
Expand Up @@ -68,6 +68,7 @@ sha2 = "0.10.6"
shared_child = "1.0.0"
sysinfo = "0.27.7"
thiserror = "1.0.38"
time = "0.3.20"
tiny-gradient = { workspace = true }
tokio = { workspace = true, features = ["full", "time"] }
tokio-stream = { version = "0.1.12", features = ["net"] }
Expand Down
14 changes: 13 additions & 1 deletion crates/turborepo-lib/src/commands/daemon.rs
@@ -1,6 +1,7 @@
use std::{path::PathBuf, time::Duration};

use pidlock::PidlockError::AlreadyOwned;
use time::{format_description, OffsetDateTime};
use tracing::{trace, warn};
use turbopath::{AbsoluteSystemPathBuf, RelativeSystemPathBuf};

Expand Down Expand Up @@ -44,9 +45,10 @@ pub async fn daemon_client(command: &DaemonCommand, base: &CommandBase) -> Resul
}
DaemonCommand::Status { json } => {
let status = client.status().await?;
let log_file = log_filename(&status.log_file)?;
let status = DaemonStatus {
uptime_ms: status.uptime_msec,
log_file: status.log_file.into(),
log_file: log_file.into(),
pid_file: client.pid_file().to_owned(),
sock_file: client.sock_file().to_owned(),
};
Expand All @@ -67,6 +69,16 @@ pub async fn daemon_client(command: &DaemonCommand, base: &CommandBase) -> Resul
Ok(())
}

// log_filename matches the algorithm used by tracing_appender::Rotation::DAILY
// to generate the log filename. This is kind of a hack, but there didn't appear
// to be a simple way to grab the generated filename.
fn log_filename(base_filename: &str) -> Result<String, time::Error> {
let now = OffsetDateTime::now_utc();
let format = format_description::parse("[year]-[month]-[day]")?;
let date = now.format(&format)?;
Ok(format!("{}.{}", base_filename, date))
}

#[tracing::instrument(skip(base, logging), fields(repo_root = %base.repo_root))]
pub async fn daemon_server(
base: &CommandBase,
Expand Down
3 changes: 3 additions & 0 deletions crates/turborepo-lib/src/daemon/client.rs
Expand Up @@ -152,6 +152,9 @@ pub enum DaemonError {

#[error("unable to display output: {0}")]
DisplayError(#[from] serde_json::Error),

#[error("unable to construct log file name: {0}")]
InvalidLogFile(#[from] time::Error),
}

impl From<Status> for DaemonError {
Expand Down

0 comments on commit 013c0bb

Please sign in to comment.