Skip to content

Commit

Permalink
Merge pull request #855 from JomerDev/async-defmt-print
Browse files Browse the repository at this point in the history
Make defmt-print async
  • Loading branch information
Urhengulas authored Jun 25, 2024
2 parents 75fb317 + 538470d commit 29f1472
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 14 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- [#845]: `decoder`: fix println!() records being printed with formatting
- [#843]: `defmt`: Sort IDs of log msgs by severity to allow runtime filtering by severity
- [#822]: `CI`: Run `cargo semver-checks` on every PR
- [#855]: `defmt-print`: Now uses tokio to make tcp and stdin reads async (in preparation for a `watch elf` flag)

[#852]: https://github.com/knurling-rs/defmt/pull/852
[#847]: https://github.com/knurling-rs/defmt/pull/847
[#845]: https://github.com/knurling-rs/defmt/pull/845
[#843]: https://github.com/knurling-rs/defmt/pull/843
[#822]: https://github.com/knurling-rs/defmt/pull/822
[#855]: https://github.com/knurling-rs/defmt/pull/855

## [v0.3.8] - 2024-05-17

Expand Down
1 change: 1 addition & 0 deletions print/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ defmt-decoder = { version = "=0.3.11", path = "../decoder", features = [
"unstable",
] }
log = "0.4"
tokio = { version = "1.38", features = ["full"] }
33 changes: 19 additions & 14 deletions print/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use std::{
env, fs,
io::{self, Read, StdinLock},
net::TcpStream,
env,
path::{Path, PathBuf},
};

Expand All @@ -15,6 +13,12 @@ use defmt_decoder::{
DecodeError, Frame, Locations, Table, DEFMT_VERSIONS,
};

use tokio::{
fs,
io::{self, AsyncReadExt, Stdin},
net::TcpStream,
};

/// Prints defmt-encoded logs to stdout
#[derive(Parser)]
#[command(name = "defmt-print")]
Expand Down Expand Up @@ -59,36 +63,37 @@ enum Command {
}

enum Source {
Stdin(StdinLock<'static>),
Stdin(Stdin),
Tcp(TcpStream),
}

impl Source {
fn stdin() -> Self {
Source::Stdin(io::stdin().lock())
Source::Stdin(io::stdin())
}

fn tcp(host: String, port: u16) -> anyhow::Result<Self> {
match TcpStream::connect((host, port)) {
async fn tcp(host: String, port: u16) -> anyhow::Result<Self> {
match TcpStream::connect((host, port)).await {
Ok(stream) => Ok(Source::Tcp(stream)),
Err(e) => Err(anyhow!(e)),
}
}

fn read(&mut self, buf: &mut [u8]) -> anyhow::Result<(usize, bool)> {
async fn read(&mut self, buf: &mut [u8]) -> anyhow::Result<(usize, bool)> {
match self {
Source::Stdin(stdin) => {
let n = stdin.read(buf)?;
let n = stdin.read(buf).await?;
Ok((n, n == 0))
}
Source::Tcp(tcpstream) => Ok((tcpstream.read(buf)?, false)),
Source::Tcp(tcpstream) => Ok((tcpstream.read(buf).await?, false)),
}
}
}

const READ_BUFFER_SIZE: usize = 1024;

fn main() -> anyhow::Result<()> {
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let Opts {
elf,
json,
Expand All @@ -105,7 +110,7 @@ fn main() -> anyhow::Result<()> {
}

// read and parse elf file
let bytes = fs::read(elf.unwrap())?;
let bytes = fs::read(elf.unwrap()).await?;
let table = Table::parse(&bytes)?.ok_or_else(|| anyhow!(".defmt data not found"))?;
let locs = table.get_locations(&bytes)?;

Expand Down Expand Up @@ -159,12 +164,12 @@ fn main() -> anyhow::Result<()> {

let mut source = match command {
None | Some(Command::Stdin) => Source::stdin(),
Some(Command::Tcp { host, port }) => Source::tcp(host, port)?,
Some(Command::Tcp { host, port }) => Source::tcp(host, port).await?,
};

loop {
// read from stdin or tcpstream and push it to the decoder
let (n, eof) = source.read(&mut buf)?;
let (n, eof) = source.read(&mut buf).await?;

// if 0 bytes where read, we reached EOF, so quit
if eof {
Expand Down

0 comments on commit 29f1472

Please sign in to comment.