Skip to content

Commit

Permalink
implement rust log_file example
Browse files Browse the repository at this point in the history
  • Loading branch information
teh-cmc committed Feb 29, 2024
1 parent 5592980 commit c4f35d5
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 12 deletions.
36 changes: 24 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions examples/rust/log_file/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "log_file"
version = "0.14.0-alpha.2"
edition = "2021"
rust-version = "1.74"
license = "MIT OR Apache-2.0"
publish = false

[dependencies]
rerun = { path = "../../../crates/rerun", features = ["web_viewer", "clap"] }

anyhow = "1.0"
clap = { version = "4.0", features = ["derive"] }
glam = "0.22"
10 changes: 10 additions & 0 deletions examples/rust/log_file/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!--[metadata]
title = "Log file example"
-->

Demonstrates how to log any file from the SDK using the [`DataLoader`](https://www.rerun.io/docs/howto/open-any-file) machinery.

Usage:
```bash
cargo run -p log_file -- examples/assets
```
59 changes: 59 additions & 0 deletions examples/rust/log_file/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//! Demonstrates how to log any file from the SDK using the `DataLoader` machinery.
//!
//! See <https://www.rerun.io/docs/howto/open-any-file> for more information.
//!
//! Usage:
//! ```
//! cargo run -p log_file -- examples/assets
//! ```

use rerun::external::re_log;

#[derive(Debug, clap::Parser)]
#[clap(author, version, about)]
struct Args {
#[command(flatten)]
rerun: rerun::clap::RerunArgs,

// Log the contents of the file directly (files only -- not supported by external loaders).
#[clap(long, default_value = "false")]
from_contents: bool,

/// The filepaths to be loaded and logged.
filepaths: Vec<std::path::PathBuf>,
}

fn main() -> anyhow::Result<()> {
re_log::setup_logging();

use clap::Parser as _;
let args = Args::parse();

let (rec, _serve_guard) = args.rerun.init("rerun_example_log_file")?;
run(&rec, &args)?;

Ok(())
}

fn run(rec: &rerun::RecordingStream, args: &Args) -> anyhow::Result<()> {
for filepath in &args.filepaths {
let filepath = filepath.as_path();

if !args.from_contents {
// Either log the file using its path…
rec.log_file_from_path(filepath)?;
} else {
// …or using its contents if you already have them loaded for some reason.
if filepath.is_file() {
let contents = std::fs::read(filepath)?;
rec.log_file_from_contents(filepath, std::borrow::Cow::Borrowed(&contents))?;
}
}
}

// TODO(cmc): This is required because the way we handle RecordingStream clones in subtly
// broken. That's outside the scope of this PR, I'll fix this in a follow-up.
rec.disconnect();

Ok(())
}

0 comments on commit c4f35d5

Please sign in to comment.