Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

subscriber: Add MakeWriter impl for Option #3196

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 97 additions & 0 deletions tracing-subscriber/src/fmt/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1095,6 +1095,27 @@ where
}
}

impl<'a, M> MakeWriter<'a> for Option<M>
where
M: MakeWriter<'a> + 'static,
{
type Writer = OptionalWriter<M::Writer>;

fn make_writer(&'a self) -> Self::Writer {
match self {
Some(inner) => OptionalWriter::some(inner.make_writer()),
None => OptionalWriter::none(),
}
}

fn make_writer_for(&'a self, meta: &Metadata<'_>) -> Self::Writer {
match self {
Some(inner) => OptionalWriter::some(inner.make_writer_for(meta)),
None => OptionalWriter::none(),
}
}
}

// === impl WriteAdaptor ===

#[cfg(any(feature = "json", feature = "time"))]
Expand Down Expand Up @@ -1391,4 +1412,80 @@ mod test {
has_lines(&a_buf, &lines[..]);
has_lines(&b_buf, &lines[..]);
}

#[test]
fn option_some_makewriter() {
let buf = Arc::new(Mutex::new(Vec::new()));
let make_writer = Some(MockMakeWriter::new(buf.clone()));

let lines = &[(Level::INFO, "hello"), (Level::INFO, "world")];
let c = {
#[cfg(feature = "ansi")]
let f = Format::default().without_time().with_ansi(false);
#[cfg(not(feature = "ansi"))]
let f = Format::default().without_time();
Collector::builder()
.event_format(f)
.with_writer(make_writer)
.with_max_level(Level::TRACE)
.finish()
};
let _s = tracing::collect::set_default(c);
info!("hello");
info!("world");
has_lines(&buf, &lines[..]);
}

#[test]
fn option_none_makewriter() {
let make_writer = Option::<MockMakeWriter>::None;

let c = {
#[cfg(feature = "ansi")]
let f = Format::default().without_time().with_ansi(false);
#[cfg(not(feature = "ansi"))]
let f = Format::default().without_time();
Collector::builder()
.event_format(f)
.with_writer(make_writer)
.with_max_level(Level::TRACE)
.finish()
};
let _s = tracing::collect::set_default(c);
info!("hello");
info!("world");
}

#[test]
fn multi_tee() {
let always_buf = Arc::new(Mutex::new(Vec::new()));
let some_buf = Arc::new(Mutex::new(Vec::new()));

let always_make_writer = MockMakeWriter::new(always_buf.clone());
let some_make_writer = Some(MockMakeWriter::new(some_buf.clone()));
let none_make_writer = Option::<MockMakeWriter>::None;

let make_writer = always_make_writer
.and(some_make_writer)
.and(none_make_writer);

let lines = &[(Level::INFO, "hello"), (Level::INFO, "world")];
let c = {
#[cfg(feature = "ansi")]
let f = Format::default().without_time().with_ansi(false);
#[cfg(not(feature = "ansi"))]
let f = Format::default().without_time();
Collector::builder()
.event_format(f)
.with_writer(make_writer)
.with_max_level(Level::TRACE)
.finish()
};
let _s = tracing::collect::set_default(c);
info!("hello");
info!("world");

has_lines(&always_buf, &lines[..]);
has_lines(&some_buf, &lines[..]);
}
}
Loading