Skip to content

Commit

Permalink
feat(console): feature-flag tracing-journald dependency (#250)
Browse files Browse the repository at this point in the history
`tracing-journald` depends on relatively recent libc APIs that may not
be available on all Linux distros (see #248). Journald support is
primarily used for internal debugging of the `tokio-console` API, so
many users may not need journald support, even if they are on a
compatible Linux distro. Therefore, this commit makes it an
off-by-default optional dependency, so that it can be enabled only when
needed.
  • Loading branch information
hawkw committed Jan 6, 2022
1 parent 931001a commit 24f25db
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
2 changes: 1 addition & 1 deletion console/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ futures = "0.3"
tui = { version = "0.16.0", default-features = false, features = ["crossterm"] }
tracing = "0.1"
tracing-subscriber = { version = "0.3.0", features = ["env-filter"] }
tracing-journald = "0.2"
tracing-journald = { version = "0.2", optional = true }
prost-types = "0.9"
crossterm = { version = "0.20", features = ["event-stream"] }
color-eyre = { version = "0.5", features = ["issue-url"] }
Expand Down
22 changes: 15 additions & 7 deletions console/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,17 @@ impl Config {

// If we're on a Linux distro with journald, try logging to the system
// journal so we don't interfere with text output.
let journald = tracing_journald::layer().ok();
#[cfg(all(feature = "tracing-journald", target_os = "linux"))]
let (journald, should_fmt) = {
let journald = tracing_journald::layer().ok();
(journald, journald.is_some())
};

#[cfg(not(all(feature = "tracing-journald", target_os = "linux")))]
let should_fmt = true;

// Otherwise, log to stderr and rely on the user redirecting output.
let fmt = if journald.is_none() {
let fmt = if should_fmt {
Some(
tracing_subscriber::fmt::layer()
.with_writer(std::io::stderr)
Expand All @@ -140,11 +147,12 @@ impl Config {
None
};

tracing_subscriber::registry()
.with(journald)
.with(fmt)
.with(filter)
.try_init()?;
let registry = tracing_subscriber::registry().with(fmt).with(filter);

#[cfg(all(feature = "tracing-journald", target_os = "linux"))]
let registry = registry.with(journald);

registry.try_init()?;

Ok(())
}
Expand Down

0 comments on commit 24f25db

Please sign in to comment.