Skip to content

Commit

Permalink
tracing: Simplify init logic using optional layers
Browse files Browse the repository at this point in the history
The layer creation itself looks a bit more complex, but this is due to
how altering the presence of timestamps changes the type signature of the
formatter. The main improvements are that we now only initialise the
registry in a single location, and the log filter reload handle has a
single type signature (whereas previously its type signature depended on
the configuration).

Requires a minimum of tracing-subscriber 0.2.12, which added this feature.

Part of zcash/zcash#4668.
  • Loading branch information
str4d committed Dec 18, 2020
1 parent c97665f commit 5dce316
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 51 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ zcash_proofs = "0.4"
ed25519-zebra = "2.0.0"

[dependencies.tracing-subscriber]
version = "0.2"
version = "0.2.12"
default-features = false
features = ["ansi", "chrono", "env-filter"]

Expand Down
92 changes: 42 additions & 50 deletions src/rust/src/tracing_ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,73 +77,65 @@ pub extern "C" fn tracing_init(

let log_path = log_path.as_ref().map(Path::new);

let (file_logger, file_guard) = if let Some(log_path) = log_path {
let (file_logger, file_no_timestamps, file_guard) = if let Some(log_path) = log_path {
let file_appender = tracing_appender::rolling::never(
log_path.parent().unwrap(),
log_path.file_name().unwrap(),
);
let (non_blocking, file_guard) = tracing_appender::non_blocking(file_appender);

if log_timestamps {
(
Some(
tracing_subscriber::fmt::layer()
.with_ansi(false)
.with_writer(non_blocking),
),
None,
Some(file_guard),
)
} else {
(
None,
Some(
tracing_subscriber::fmt::layer()
.with_ansi(false)
.with_writer(non_blocking)
.without_time(),
),
Some(file_guard),
)
}
} else {
(None, None, None)
};

let (stdout_logger, stdout_no_timestamps) = if log_timestamps {
(Some(tracing_subscriber::fmt::layer().with_ansi(true)), None)
} else {
(
None,
Some(
tracing_subscriber::fmt::layer()
.with_ansi(false)
.with_writer(non_blocking),
.with_ansi(true)
.without_time(),
),
Some(file_guard),
)
} else {
(None, None)
};
let stdout_logger = tracing_subscriber::fmt::layer().with_ansi(true);
let filter = EnvFilter::from(initial_filter);

let reload_handle = match (file_logger, log_timestamps) {
(None, true) => {
let (filter, reload_handle) = reload::Layer::new(filter);

tracing_subscriber::registry()
.with(stdout_logger)
.with(filter)
.init();

Box::new(reload_handle) as Box<dyn ReloadHandle>
}
(None, false) => {
let (filter, reload_handle) = reload::Layer::new(filter);

tracing_subscriber::registry()
.with(stdout_logger.without_time())
.with(filter)
.init();

Box::new(reload_handle) as Box<dyn ReloadHandle>
}
(Some(file_logger), true) => {
let (filter, reload_handle) = reload::Layer::new(filter);
let (filter, reload_handle) = reload::Layer::new(EnvFilter::from(initial_filter));

tracing_subscriber::registry()
.with(file_logger)
.with(filter)
.init();

Box::new(reload_handle) as Box<dyn ReloadHandle>
}
(Some(file_logger), false) => {
let (filter, reload_handle) = reload::Layer::new(filter);

tracing_subscriber::registry()
.with(file_logger.without_time())
.with(filter)
.init();

Box::new(reload_handle) as Box<dyn ReloadHandle>
}
};
tracing_subscriber::registry()
.with(stdout_logger)
.with(stdout_no_timestamps)
.with(file_logger)
.with(file_no_timestamps)
.with(filter)
.init();

Box::into_raw(Box::new(TracingHandle {
_file_guard: file_guard,
reload_handle,
reload_handle: Box::new(reload_handle),
}))
}

Expand Down

0 comments on commit 5dce316

Please sign in to comment.