Skip to content

Commit 365c4bc

Browse files
authored
Merge pull request #24 from nikomatsakis/main
logging, conductor is a component
2 parents 22e7953 + f286997 commit 365c4bc

File tree

3 files changed

+69
-14
lines changed

3 files changed

+69
-14
lines changed

src/sacp-conductor/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ sacp-proxy = { version = "1.0.0-alpha.4", path = "../sacp-proxy" }
1717
sacp-tokio = { version = "1.0.0-alpha.5", path = "../sacp-tokio" }
1818
agent-client-protocol-schema.workspace = true
1919
anyhow.workspace = true
20+
chrono.workspace = true
2021
clap.workspace = true
2122
futures.workspace = true
2223

@@ -27,6 +28,7 @@ thiserror.workspace = true
2728
tokio.workspace = true
2829
tokio-util.workspace = true
2930
tracing.workspace = true
31+
tracing-appender.workspace = true
3032
tracing-subscriber.workspace = true
3133
uuid.workspace = true
3234

src/sacp-conductor/src/conductor.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ use std::{
120120
use futures::{
121121
SinkExt, StreamExt,
122122
channel::mpsc::{self},
123+
future::BoxFuture,
123124
};
124125
use sacp::{
125126
JrConnectionCx, JrHandlerChain, JrNotification, JrRequest, JrRequestCx, JrResponse,
@@ -222,6 +223,15 @@ impl Conductor {
222223
}
223224
}
224225

226+
impl sacp::Component for Conductor {
227+
fn serve(
228+
self: Box<Self>,
229+
channels: sacp::Channels,
230+
) -> BoxFuture<'static, Result<(), sacp::Error>> {
231+
Box::pin(async move { (*self).run(channels).await })
232+
}
233+
}
234+
225235
pub struct ConductorMessageHandler {
226236
conductor_tx: mpsc::Sender<ConductorMessage>,
227237
}

src/sacp-conductor/src/main.rs

Lines changed: 57 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,69 @@
1+
use std::path::PathBuf;
2+
13
use clap::Parser;
24
use sacp_conductor::ConductorArgs;
5+
use tracing::Instrument;
36
use tracing_subscriber::{EnvFilter, layer::SubscriberExt, util::SubscriberInitExt};
47

58
#[tokio::main]
69
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+
}
2163

2264
ConductorArgs::parse()
2365
.run()
66+
.instrument(tracing::info_span!("conductor", pid = %pid, cwd = %cwd))
2467
.await
2568
.map_err(|err| anyhow::anyhow!("{err}"))
2669
}

0 commit comments

Comments
 (0)