Skip to content

Commit

Permalink
hide all tracing-log stuff behind feature
Browse files Browse the repository at this point in the history
  • Loading branch information
GoldsteinE committed Aug 25, 2023
1 parent b885ef6 commit 6af92dd
Showing 1 changed file with 24 additions and 18 deletions.
42 changes: 24 additions & 18 deletions elfo-logger/src/filtering_layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::sync::Arc;

use arc_swap::ArcSwap;
use fxhash::FxHashMap;
#[cfg(feature = "tracing-log")]
use once_cell::sync::OnceCell;
use tracing::{metadata::LevelFilter, subscriber::Interest, Metadata, Subscriber};
use tracing_subscriber::{
Expand All @@ -28,6 +29,7 @@ impl Default for FilteringConfig {

struct Inner {
config: ArcSwap<FilteringConfig>,
#[cfg(feature = "tracing-log")]
log_metadata_name: OnceCell<&'static str>,
}

Expand All @@ -41,6 +43,7 @@ impl FilteringLayer {
Self {
inner: Arc::new(Inner {
config: ArcSwap::new(Arc::new(FilteringConfig::default())),
#[cfg(feature = "tracing-log")]
log_metadata_name: OnceCell::new(),
}),
}
Expand Down Expand Up @@ -80,28 +83,31 @@ impl<S: Subscriber> Layer<S> for FilteringLayer {
// would already eliminate logs that would be filtered by it.
let level = *meta.level();

// `tracing-log` doesn't call `.register_callsite()`, so we have to recheck
// `.targets` in this one case.
let name = meta.name();
if let Some(&log_name) = self.inner.log_metadata_name.get() {
// We've already got logs from `tracing-log`, just compare str
// pointers for performance.
if std::ptr::eq(log_name, name) {
#[cfg(feature = "tracing-log")]
{
// `tracing-log` doesn't call `.register_callsite()`, so we have to recheck
// `.targets` in this one case.
let name = meta.name();
if let Some(&log_name) = self.inner.log_metadata_name.get() {
// We've already got logs from `tracing-log`, just compare str
// pointers for performance.
if std::ptr::eq(log_name, name) {
let config = self.inner.config.load();
if !config.targets.would_enable(meta.target(), meta.level()) {
return false;
}
}
} else if name == "log record" {
let config = self.inner.config.load();
if !config.targets.would_enable(meta.target(), meta.level()) {
return false;
}
}
} else if name == "log record" {
let config = self.inner.config.load();
if !config.targets.would_enable(meta.target(), meta.level()) {
return false;
}
// That's "initialized tracing_log adapter" to set `log_metadata_name`, just
// ignore it. Any actual logs from `elfo_logger` would use `tracing`.
if meta.target() == "elfo_logger" {
self.inner.log_metadata_name.set(name).ok();
return false;
// That's "initialized tracing_log adapter" to set `log_metadata_name`, just
// ignore it. Any actual logs from `elfo_logger` would use `tracing`.
if meta.target() == "elfo_logger" {
self.inner.log_metadata_name.set(name).ok();
return false;
}
}
}

Expand Down

0 comments on commit 6af92dd

Please sign in to comment.