refactor: Add more debugging facilities into examples

This commit is contained in:
Jill 2022-05-13 17:52:30 +02:00
parent 1b3147d493
commit 54aa382f8b
No known key found for this signature in database
GPG key ID: 09A5A2688F13FAC1
2 changed files with 29 additions and 5 deletions

View file

@ -23,3 +23,4 @@ tower = { version = "0.4", features = ["full"] }
[dev-dependencies]
eyre = "0.6"
tracing-subscriber = "0.3"
console-subscriber = "0.1"

View file

@ -14,15 +14,18 @@ async fn handle_req(req: Request) -> eyre::Result<Response> {
#[tokio::main]
async fn main() -> eyre::Result<()> {
tracing_subscriber::fmt()
.with_max_level(tracing::Level::TRACE)
.init();
setup_logging();
let incoming = AddrIncoming::new("127.0.0.1:4567").await?;
let make_service = tower::service_fn(|addr: &AddrStream| {
tracing::debug!("Accept: {} -> {}", addr.remote_addr, addr.local_addr);
futures::future::ok::<_, std::convert::Infallible>(tower::service_fn(handle_req))
tracing::debug!(remote_addr = %addr.remote_addr, local_addr = %addr.local_addr, "accept");
let service = tower::ServiceBuilder::new()
.buffer(16)
.service_fn(handle_req);
futures::future::ok::<_, std::convert::Infallible>(service)
});
let server = Server::new(incoming).serve(make_service);
@ -30,3 +33,23 @@ async fn main() -> eyre::Result<()> {
Ok(())
}
// Don't mind this, this is just for debugging.
fn setup_logging() {
use tracing_subscriber::prelude::*;
tracing_subscriber::registry()
.with(console_subscriber::spawn())
.with(
tracing_subscriber::fmt::layer().with_filter(
tracing_subscriber::filter::Targets::new()
.with_default(tracing::Level::DEBUG)
.with_target("boitalettres", tracing::Level::TRACE)
.with_target("simple", tracing::Level::TRACE)
.with_target("tower", tracing::Level::TRACE)
.with_target("tokio_tower", tracing::Level::TRACE)
.with_target("mio", tracing::Level::TRACE),
),
)
.init();
}