Files
h2/tests/h2-support/src/trace.rs
Eliza Weisman fc7f63f641 start adding tracing spans to internals (#478)
We've adopted `tracing` for diagnostics, but currently, it is just being
used as a drop-in replacement for the `log` crate. Ideally, we would
want to start emitting more structured diagnostics, using `tracing`'s
`Span`s and structured key-value fields.

A lot of the logging in `h2` is already written in a style that imitates
the formatting of structured key-value logs, but as textual log
messages. Migrating the logs to structured `tracing` events therefore is
pretty easy to do. I've also started adding spans, mostly in the read
path.

Finally, I've updated the tests to use `tracing` rather than
`env_logger`. The tracing setup happens in a macro, so that a span for
each test with the test's name can be generated and entered. This will
make the test output easier to read if multiple tests are run
concurrently with `--nocapture`.

Signed-off-by: Eliza Weisman <eliza@buoyant.io>
2020-08-17 17:29:22 -07:00

42 lines
1.0 KiB
Rust

use std::{io, str};
pub use tracing;
pub use tracing_subscriber;
pub fn init() -> tracing::dispatcher::DefaultGuard {
tracing::subscriber::set_default(
tracing_subscriber::fmt()
.with_max_level(tracing::Level::TRACE)
.with_span_events(tracing_subscriber::fmt::format::FmtSpan::CLOSE)
.with_writer(PrintlnWriter { _p: () })
.finish(),
)
}
struct PrintlnWriter {
_p: (),
}
impl tracing_subscriber::fmt::MakeWriter for PrintlnWriter {
type Writer = PrintlnWriter;
fn make_writer(&self) -> Self::Writer {
PrintlnWriter { _p: () }
}
}
impl io::Write for PrintlnWriter {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
let s = str::from_utf8(buf).map_err(|e| io::Error::new(io::ErrorKind::InvalidInput, e))?;
println!("{}", s);
Ok(s.len())
}
fn write_fmt(&mut self, fmt: std::fmt::Arguments<'_>) -> io::Result<()> {
println!("{}", fmt);
Ok(())
}
fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}