sylveos

Toy Operating System
Log | Files | Refs

commit db2e55d8508d663d94f9e5f9860d8756d6f9a157
parent 9fa4ca0b645350e0c52d6c1afad2dcba9922e146
Author: Sylvia Ivory <git@sivory.net>
Date:   Tue, 10 Feb 2026 15:12:46 -0800

Use tracing instead of log

Diffstat:
Mprograms/tcp-echo.zig | 2+-
Mtools/Cargo.lock | 253++++++++++++++++++++++++++++++++++++++++++++++++-------------------------------
Mtools/Cargo.toml | 6+++---
Mtools/src/bootload.rs | 60+++++++++++++++++++++++++++++++-----------------------------
Mtools/src/main.rs | 46+++++++++++++++++++++++++++++++---------------
Mtools/src/tcp.rs | 125+++++++++++++++++++++++++++++++++++++++++++++++--------------------------------
6 files changed, 296 insertions(+), 196 deletions(-)

diff --git a/programs/tcp-echo.zig b/programs/tcp-echo.zig @@ -13,7 +13,7 @@ pub fn main() !void { var fba: std.heap.FixedBufferAllocator = .init(&buffer); var allocator = fba.allocator(); - try tcp.send(w, .{ .Bind = .{ .port = 8080, .ttl = 1000 } }); + try tcp.send(w, .{ .Bind = .{ .port = 8080, .ttl = 0 } }); while (true) { switch (try tcp.receive(r, allocator)) { diff --git a/tools/Cargo.lock b/tools/Cargo.lock @@ -68,6 +68,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5f0e0fee31ef5ed1ba1316088939cea399010ed7731dba877ed44aeb407a75ea" [[package]] +name = "arrayvec" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7c02d123df017efcdfbd739ef81735b36c5ba83ec3c59c80a9d7ecc718f92e50" + +[[package]] name = "bitflags" version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -214,29 +220,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "34aa73646ffb006b8f5147f3dc182bd4bcb190227ce861fc4a4844bf8e3cb2c0" [[package]] -name = "env_filter" -version = "0.1.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1bf3c259d255ca70051b30e2e95b5446cdb8949ac4cd22c0d7fd634d89f568e2" -dependencies = [ - "log", - "regex", -] - -[[package]] -name = "env_logger" -version = "0.11.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "13c863f0904021b108aa8b2f55046443e6b1ebde8fd4a15c399893aae4fa069f" -dependencies = [ - "anstream", - "anstyle", - "env_filter", - "jiff", - "log", -] - -[[package]] name = "errno" version = "0.3.14" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -361,20 +344,11 @@ dependencies = [ "portable-atomic", "unicode-width", "unit-prefix", + "vt100", "web-time", ] [[package]] -name = "indicatif-log-bridge" -version = "0.2.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "63703cf9069b85dbe6fe26e1c5230d013dee99d3559cd3d02ba39e099ef7ab02" -dependencies = [ - "indicatif", - "log", -] - -[[package]] name = "io-kit-sys" version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -391,28 +365,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695" [[package]] -name = "jiff" -version = "0.2.19" +name = "itoa" +version = "1.0.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d89a5b5e10d5a9ad6e5d1f4bd58225f655d6fe9767575a5e8ac5a6fe64e04495" -dependencies = [ - "jiff-static", - "log", - "portable-atomic", - "portable-atomic-util", - "serde_core", -] - -[[package]] -name = "jiff-static" -version = "0.2.19" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ff7a39c8862fc1369215ccf0a8f12dd4598c7f6484704359f0351bd617034dbf" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] +checksum = "92ecc6618181def0457392ccd0ee51198e065e016d1d527a7ac1b6dc7c1f09d2" [[package]] name = "js-sys" @@ -425,6 +381,12 @@ dependencies = [ ] [[package]] +name = "lazy_static" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" + +[[package]] name = "libc" version = "0.2.181" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -455,6 +417,15 @@ dependencies = [ ] [[package]] +name = "matchers" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d1525a2a28c7f4fa0fc98bb91ae755d1e2d1505079e05539e35bc876b5d65ae9" +dependencies = [ + "regex-automata", +] + +[[package]] name = "memchr" version = "2.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -509,6 +480,15 @@ dependencies = [ ] [[package]] +name = "nu-ansi-term" +version = "0.50.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7957b9740744892f114936ab4a57b3f487491bbeafaf8083688b16841a4240e5" +dependencies = [ + "windows-sys 0.61.2", +] + +[[package]] name = "once_cell" version = "1.21.3" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -562,15 +542,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c33a9471896f1c69cecef8d20cbe2f7accd12527ce60845ff44c153bb2a21b49" [[package]] -name = "portable-atomic-util" -version = "0.2.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7a9db96d7fa8782dd8c15ce32ffe8680bbd1e978a43bf51a34d39483540495f5" -dependencies = [ - "portable-atomic", -] - -[[package]] name = "proc-macro2" version = "1.0.106" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -598,18 +569,6 @@ dependencies = [ ] [[package]] -name = "regex" -version = "1.12.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e10754a14b9137dd7b1e3e5b0493cc9171fdd105e0ab477f51b72e7f3ac0e276" -dependencies = [ - "aho-corasick", - "memchr", - "regex-automata", - "regex-syntax", -] - -[[package]] name = "regex-automata" version = "0.4.14" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -639,26 +598,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" [[package]] -name = "serde_core" -version = "1.0.228" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "41d385c7d4ca58e59fc732af25c3983b67ac852c1a25000afe1175de458b67ad" -dependencies = [ - "serde_derive", -] - -[[package]] -name = "serde_derive" -version = "1.0.228" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - -[[package]] name = "serialport" version = "4.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -678,6 +617,15 @@ dependencies = [ ] [[package]] +name = "sharded-slab" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f40ca3c46823713e0d4209592e8d6e826aa57e928f09752619fc696c499637f6" +dependencies = [ + "lazy_static", +] + +[[package]] name = "signal-hook-registry" version = "1.4.8" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -753,6 +701,15 @@ dependencies = [ ] [[package]] +name = "thread_local" +version = "1.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f60246a4944f24f6e018aa17cdeffb7818b76356965d03b07d6a9886e8962185" +dependencies = [ + "cfg-if", +] + +[[package]] name = "tokio" version = "1.49.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -801,13 +758,86 @@ dependencies = [ "anyhow", "clap", "crc-fast", - "env_logger", "indicatif", - "indicatif-log-bridge", - "log", "thiserror", "tokio", "tokio-serial", + "tracing", + "tracing-indicatif", + "tracing-subscriber", +] + +[[package]] +name = "tracing" +version = "0.1.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "63e71662fa4b2a2c3a26f570f037eb95bb1f85397f3cd8076caed2f026a6d100" +dependencies = [ + "pin-project-lite", + "tracing-attributes", + "tracing-core", +] + +[[package]] +name = "tracing-attributes" +version = "0.1.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7490cfa5ec963746568740651ac6781f701c9c5ea257c58e057f3ba8cf69e8da" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tracing-core" +version = "0.1.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db97caf9d906fbde555dd62fa95ddba9eecfd14cb388e4f491a66d74cd5fb79a" +dependencies = [ + "once_cell", + "valuable", +] + +[[package]] +name = "tracing-indicatif" +version = "0.3.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e1ef6990e0438749f0080573248e96631171a0b5ddfddde119aa5ba8c3a9c47e" +dependencies = [ + "indicatif", + "tracing", + "tracing-core", + "tracing-subscriber", +] + +[[package]] +name = "tracing-log" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee855f1f400bd0e5c02d150ae5de3840039a3f54b025156404e34c23c03f47c3" +dependencies = [ + "log", + "once_cell", + "tracing-core", +] + +[[package]] +name = "tracing-subscriber" +version = "0.3.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2f30143827ddab0d256fd843b7a66d164e9f271cfa0dde49142c5ca0ca291f1e" +dependencies = [ + "matchers", + "nu-ansi-term", + "once_cell", + "regex-automata", + "sharded-slab", + "smallvec", + "thread_local", + "tracing", + "tracing-core", + "tracing-log", ] [[package]] @@ -850,12 +880,39 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" [[package]] +name = "valuable" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba73ea9cf16a25df0c8caa16c51acb937d5712a8429db78a3ee29d5dcacd3a65" + +[[package]] name = "version_check" version = "0.9.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" [[package]] +name = "vt100" +version = "0.16.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "054ff75fb8fa83e609e685106df4faeffdf3a735d3c74ebce97ec557d5d36fd9" +dependencies = [ + "itoa", + "unicode-width", + "vte", +] + +[[package]] +name = "vte" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a5924018406ce0063cd67f8e008104968b74b563ee1b85dde3ed1f7cb87d3dbd" +dependencies = [ + "arrayvec", + "memchr", +] + +[[package]] name = "wasi" version = "0.11.1+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" diff --git a/tools/Cargo.toml b/tools/Cargo.toml @@ -7,10 +7,10 @@ edition = "2024" anyhow = "1.0.101" clap = { version = "4.5.57", features = ["derive"] } crc-fast = "1.10.0" -env_logger = "0.11.8" indicatif = "0.18.3" -indicatif-log-bridge = "0.2.3" -log = "0.4.29" thiserror = "2.0.18" tokio = { version = "1.49.0", features = ["full"] } tokio-serial = "5.4.5" +tracing = "0.1.44" +tracing-indicatif = "0.3.14" +tracing-subscriber = { version = "0.3.22", features = ["env-filter"] } diff --git a/tools/src/bootload.rs b/tools/src/bootload.rs @@ -1,13 +1,14 @@ use std::path::Path; use crc_fast::{CrcAlgorithm::Crc32IsoHdlc, checksum}; -use indicatif::{MultiProgress, ProgressBar, ProgressStyle}; -use log::{debug, info}; +use indicatif::ProgressStyle; use tokio::{ fs::File, io::{AsyncReadExt, AsyncWriteExt}, }; use tokio_serial::SerialStream; +use tracing::{debug, instrument, trace}; +use tracing_indicatif::span_ext::IndicatifSpanExt; #[repr(u32)] #[derive(Debug)] @@ -33,27 +34,30 @@ pub enum Error { Io(#[from] std::io::Error), } -pub async fn boot( - multi: &MultiProgress, - port: &mut SerialStream, - bin: &Path, - base_address: u32, -) -> Result<(), Error> { - // Technically blocking but it doesn't matter here +#[instrument(skip_all, fields(indicatif.pb_show))] +pub async fn boot(port: &mut SerialStream, bin: &Path, base_address: u32) -> Result<(), Error> { + let current = tracing::Span::current(); + let mut code = Vec::new(); File::open(bin).await?.read_to_end(&mut code).await?; let checksum = checksum(Crc32IsoHdlc, &code) as u32; - info!("crc32={:x}", checksum); - info!("waiting for a start"); + debug!("boot parameters: crc32={checksum:x}, file={bin:?}, base_address=0x{base_address:X}"); - while port.read_u32_le().await? != (Message::GetProgInfo as u32) { - debug!("got invalid GetProgInfo") - } + current.pb_set_style(&ProgressStyle::default_spinner()); + current.pb_set_message("waiting for GetProgInfo"); - info!("got response"); + loop { + match port.read_u32_le().await? { + v if v == (Message::GetProgInfo as u32) => break, + v => { + trace!("got invalid GetProgInfo: {v:X}"); + continue; + } + } + } port.write_u32_le(Message::PutProgInfo as u32).await?; port.write_u32_le(base_address).await?; @@ -66,7 +70,7 @@ pub async fn boot( v if v == (Message::GetCode as u32) => break, v if v == (Message::BadCodeAddr as u32) => return Err(Error::BadAddress), v => { - debug!("got invalid GetCode: {v}"); + trace!("got invalid GetCode: {v:X}"); continue; } } @@ -79,24 +83,24 @@ pub async fn boot( } port.write_u32_le(Message::PutCode as u32).await?; - let pg = multi.add( - ProgressBar::new(code.len() as u64).with_style( - ProgressStyle::with_template( - "[{elapsed}] [{wide_bar}] {binary_bytes}/{binary_total_bytes} (eta: {eta})", - ) - .unwrap() - .progress_chars("=> "), - ), + + current.pb_set_style( + &ProgressStyle::with_template( + "[{elapsed}] [{wide_bar}] {binary_bytes}/{binary_total_bytes} (eta: {eta})", + ) + .unwrap() + .progress_chars("=> "), ); + current.pb_set_length(code.len() as u64); for b in code.iter() { port.write_u8(*b).await?; - pg.inc(1); + current.pb_inc(1); } port.flush().await?; - pg.finish(); - info!("waiting for response"); + current.pb_set_style(&ProgressStyle::default_spinner()); + current.pb_set_message("waiting for BootSuccess"); loop { match port.read_u32_le().await? { @@ -109,7 +113,5 @@ pub async fn boot( } } - info!("boot successful"); - Ok(()) } diff --git a/tools/src/main.rs b/tools/src/main.rs @@ -1,9 +1,15 @@ use clap::Parser; -use indicatif::MultiProgress; -use indicatif_log_bridge::LogWrapper; -use log::{info, warn}; +use indicatif::ProgressStyle; use tokio::io; use tokio_serial::{DataBits, FlowControl, Parity, SerialPortBuilderExt, SerialStream, StopBits}; +use tracing::{level_filters::LevelFilter, warn}; +use tracing_indicatif::{ + IndicatifLayer, + filter::{IndicatifFilter, hide_indicatif_span_fields}, +}; +use tracing_subscriber::{ + EnvFilter, Layer, fmt::format::DefaultFields, layer::SubscriberExt, util::SubscriberInitExt, +}; use std::{path::PathBuf, process::exit, time::Duration}; @@ -56,13 +62,27 @@ async fn default_action(port: SerialStream) -> anyhow::Result<()> { #[tokio::main] async fn main() -> anyhow::Result<()> { - let logger = - env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info")).build(); - let level = logger.filter(); - let multi = MultiProgress::new(); - - LogWrapper::new(multi.clone(), logger).try_init().unwrap(); - log::set_max_level(level); + let indicatif_layer = IndicatifLayer::new() + .with_span_field_formatter(hide_indicatif_span_fields(DefaultFields::new())) + .with_progress_style(ProgressStyle::with_template( + "{span_child_prefix}{span_name}{{{span_fields}}} {wide_msg} {elapsed}", + )?) + .with_span_child_prefix_symbol("↳ ") + .with_span_child_prefix_indent(" "); + + tracing_subscriber::registry() + .with( + EnvFilter::builder() + .with_default_directive(LevelFilter::INFO.into()) + .from_env_lossy(), + ) + .with( + tracing_subscriber::fmt::layer() + .with_writer(indicatif_layer.get_stderr_writer()) + .with_target(false), + ) + .with(indicatif_layer.with_filter(IndicatifFilter::new(false))) + .init(); let args = Args::parse(); @@ -82,11 +102,7 @@ async fn main() -> anyhow::Result<()> { .timeout(Duration::from_secs_f64(args.timeout)) .open_native_async()?; - info!( - "tty=<{}> baud=<{}> program=<{:?}> base address=<0x{:X}> timeout=<{}s>", - &args.device, args.baud, &args.file, args.arm_base, args.timeout - ); - bootload::boot(&multi, &mut port, &args.file, args.arm_base).await?; + bootload::boot(&mut port, &args.file, args.arm_base).await?; if args.tcp { let (port_read, port_write) = io::split(port); diff --git a/tools/src/tcp.rs b/tools/src/tcp.rs @@ -4,7 +4,6 @@ use std::{ sync::Arc, }; -use log::{debug, error, info}; use tokio::{ io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt, Interest}, net::{ @@ -13,6 +12,7 @@ use tokio::{ }, sync::RwLock, }; +use tracing::{Instrument, Level, debug, error, field, info, instrument, span}; pub type ConnectionId = u16; @@ -63,7 +63,11 @@ pub enum Error { Io(#[from] std::io::Error), } -pub async fn parse_message<R: AsyncRead + Unpin>(r: &mut R) -> Result<Message, Error> { +#[instrument(skip_all)] +pub async fn parse_message<R: AsyncRead + Unpin>( + r: &mut R, + log_err: bool, +) -> Result<Message, Error> { let kind = r.read_u8().await?; match kind { @@ -105,15 +109,17 @@ pub async fn parse_message<R: AsyncRead + Unpin>(r: &mut R) -> Result<Message, E Ok(Message::Print { length, data }) } _ => { - debug!("got invalid kind: {kind}"); + if log_err { + tracing::error!(kind = kind, "invalid message kind"); + } + Err(Error::InvalidKind) } } } +#[instrument(err, skip_all)] pub async fn send_message<W: AsyncWrite + Unpin>(w: &mut W, msg: &Message) -> Result<(), Error> { - debug!("sending message: {msg:#?}"); - w.write_u8(msg.into()).await?; match msg { @@ -140,10 +146,11 @@ pub async fn send_message<W: AsyncWrite + Unpin>(w: &mut W, msg: &Message) -> Re async fn socket_handler<W: AsyncWrite + Send + Sync + Unpin + 'static>( id: ConnectionId, + ip: IpAddr, socket: OwnedReadHalf, w: Arc<RwLock<W>>, ) -> Result<(), Error> { - let ip = match socket.peer_addr()?.ip() { + let ip = match ip { IpAddr::V4(v4) => v4.to_bits(), _ => unreachable!(), }; @@ -153,53 +160,57 @@ async fn socket_handler<W: AsyncWrite + Send + Sync + Unpin + 'static>( send_message(&mut *writer, &Message::Request { id, ip }).await?; } + debug!("ready"); + loop { let ready = socket.ready(Interest::READABLE).await?; - if ready.is_readable() { - debug!("socket is readable"); - let mut data = vec![0; 1024]; + if !ready.is_readable() { + continue; + }; - match socket.try_read(&mut data) { - Ok(0) => { - debug!("socket disconnected: {id}"); - let mut writer = w.write().await; + let mut data = vec![0; 1024]; - if let Err(e) = send_message(&mut *writer, &Message::Disconnect(id)).await { - error!("failed to send message: {e}"); - } + match socket.try_read(&mut data) { + Ok(0) => { + debug!("disconnected"); + let mut writer = w.write().await; - return Ok(()); - } - Ok(n) => { - debug!("got data: n={n}: {:?}", &data[..n]); - let mut writer = w.write().await; - - if let Err(e) = send_message( - &mut *writer, - &Message::Write { - id, - length: n as u16, - data, - }, - ) - .await - { - error!("failed to send message: {e}"); - } - } - Err(ref e) if e.kind() == std::io::ErrorKind::WouldBlock => { - continue; + if let Err(e) = send_message(&mut *writer, &Message::Disconnect(id)).await { + error!("failed to send message: {e}"); } - Err(e) => { - error!("failed to read: {e}"); - return Err(e.into()); + + return Ok(()); + } + Ok(n) => { + debug!("read {n} bytes"); + let mut writer = w.write().await; + + if let Err(e) = send_message( + &mut *writer, + &Message::Write { + id, + length: n as u16, + data, + }, + ) + .await + { + error!("failed to send message: {e}"); } } + Err(ref e) if e.kind() == std::io::ErrorKind::WouldBlock => { + continue; + } + Err(e) => { + error!("failed to read: {e}"); + return Err(e.into()); + } } } } +#[instrument(name = "server", skip_all, fields(indicatif.pb_show))] async fn tcp_handler<W: AsyncWrite + Send + Sync + Unpin + 'static>( server: TcpListener, w: Arc<RwLock<W>>, @@ -208,23 +219,34 @@ async fn tcp_handler<W: AsyncWrite + Send + Sync + Unpin + 'static>( let mut counter: ConnectionId = 0; // TODO; should automatically detect when sockets drop loop { - let (socket, _) = server.accept().await?; + let (socket, peer) = server.accept().await?; let (socket_read, socket_write) = socket.into_split(); map.write().await.insert(counter, socket_write); - tokio::spawn(socket_handler(counter, socket_read, w.clone())); + + let socket_span = tracing::info_span!( + "socket", + id = counter, + ip = peer.ip().to_string(), + indicatif.pb_show = field::Empty, + ); + + tokio::spawn( + socket_handler(counter, peer.ip(), socket_read, w.clone()) + .instrument(socket_span.or_current()), + ); counter += 1; } } +#[instrument(name = "serial", skip_all, fields(ip, indicatif.pb_show))] async fn port_handler<R: AsyncRead + Unpin>( mut r: R, map: Arc<RwLock<HashMap<ConnectionId, OwnedWriteHalf>>>, ) -> Result<(), Error> { loop { - let msg = parse_message(&mut r).await?; - debug!("got message: {msg:#?}"); + let msg = parse_message(&mut r, true).await?; match msg { Message::Write { @@ -253,27 +275,30 @@ async fn port_handler<R: AsyncRead + Unpin>( } } Message::Print { length: _, data } => { - info!("pi: {}", String::from_utf8_lossy(&data).trim()) + let _enter = span!(Level::INFO, "pi").entered(); + info!("{}", String::from_utf8_lossy(&data).trim()) } _ => {} } } } +#[instrument(skip_all)] async fn wait_for_bind<R: AsyncRead + Unpin>(r: &mut R) -> Result<TcpListener, Error> { - info!("waiting for bind"); - loop { - if let Ok(Message::Bind { port, ttl }) = parse_message(r).await { - info!("got response, port={port}, ttl={ttl}"); + if let Ok(Message::Bind { port, ttl }) = parse_message(r, false).await { + debug!(port = port, ttl = ttl, "bind request"); let addr = SocketAddrV4::new(Ipv4Addr::LOCALHOST, port); let listener = TcpListener::bind(addr).await?; - // listener.set_ttl(ttl)?; + if ttl != 0 { + listener.set_ttl(ttl)?; + } return Ok(listener); } } } +#[instrument(name = "tcp", skip_all)] pub async fn serve< R: AsyncRead + Send + Sync + Unpin + 'static, W: AsyncWrite + Send + Sync + Unpin + 'static,