|
| 1 | +use std::path::PathBuf; |
| 2 | + |
1 | 3 | use clap::Parser; |
2 | 4 | use sacp_conductor::ConductorArgs; |
| 5 | +use tracing::Instrument; |
3 | 6 | use tracing_subscriber::{EnvFilter, layer::SubscriberExt, util::SubscriberInitExt}; |
4 | 7 |
|
5 | 8 | #[tokio::main] |
6 | 9 | async fn main() -> anyhow::Result<()> { |
7 | | - // Initialize tracing with env filter support (RUST_LOG=debug, etc.) |
8 | | - // Important: Always write to stderr to avoid interfering with stdio protocols |
9 | | - tracing_subscriber::registry() |
10 | | - .with( |
11 | | - EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("conductor=info")), |
12 | | - ) |
13 | | - .with( |
14 | | - tracing_subscriber::fmt::layer() |
15 | | - .with_target(true) |
16 | | - .with_writer(std::io::stderr), |
17 | | - ) |
18 | | - .init(); |
19 | | - |
20 | | - tracing::info!("Conductor starting"); |
| 10 | + let pid = std::process::id(); |
| 11 | + let cwd = std::env::current_dir() |
| 12 | + .map(|p| p.display().to_string()) |
| 13 | + .unwrap_or_else(|_| "<unknown>".to_string()); |
| 14 | + |
| 15 | + // Check for SYMPOSIUM_LOG environment variable |
| 16 | + if let Ok(log_level) = std::env::var("SYMPOSIUM_LOG") { |
| 17 | + // Set up file logging to ~/.symposium/logs.$DATE |
| 18 | + let home = std::env::var("HOME") |
| 19 | + .map(PathBuf::from) |
| 20 | + .unwrap_or_else(|_| PathBuf::from(".")); |
| 21 | + |
| 22 | + let log_dir = home.join(".symposium"); |
| 23 | + std::fs::create_dir_all(&log_dir)?; |
| 24 | + |
| 25 | + let file_appender = tracing_appender::rolling::daily(log_dir, "logs"); |
| 26 | + |
| 27 | + tracing_subscriber::registry() |
| 28 | + .with(EnvFilter::new(&log_level)) |
| 29 | + .with( |
| 30 | + tracing_subscriber::fmt::layer() |
| 31 | + .with_target(true) |
| 32 | + .with_span_events( |
| 33 | + tracing_subscriber::fmt::format::FmtSpan::NEW |
| 34 | + | tracing_subscriber::fmt::format::FmtSpan::CLOSE, |
| 35 | + ) |
| 36 | + .with_writer(file_appender), |
| 37 | + ) |
| 38 | + .init(); |
| 39 | + |
| 40 | + tracing::info!( |
| 41 | + pid = %pid, |
| 42 | + cwd = %cwd, |
| 43 | + level = %log_level, |
| 44 | + "Conductor starting with file logging" |
| 45 | + ); |
| 46 | + } else { |
| 47 | + // Initialize tracing with env filter support (RUST_LOG=debug, etc.) |
| 48 | + // Important: Always write to stderr to avoid interfering with stdio protocols |
| 49 | + tracing_subscriber::registry() |
| 50 | + .with( |
| 51 | + EnvFilter::try_from_default_env() |
| 52 | + .unwrap_or_else(|_| EnvFilter::new("conductor=info")), |
| 53 | + ) |
| 54 | + .with( |
| 55 | + tracing_subscriber::fmt::layer() |
| 56 | + .with_target(true) |
| 57 | + .with_writer(std::io::stderr), |
| 58 | + ) |
| 59 | + .init(); |
| 60 | + |
| 61 | + tracing::info!(pid = %pid, cwd = %cwd, "Conductor starting"); |
| 62 | + } |
21 | 63 |
|
22 | 64 | ConductorArgs::parse() |
23 | 65 | .run() |
| 66 | + .instrument(tracing::info_span!("conductor", pid = %pid, cwd = %cwd)) |
24 | 67 | .await |
25 | 68 | .map_err(|err| anyhow::anyhow!("{err}")) |
26 | 69 | } |
0 commit comments