> ## Documentation Index
> Fetch the complete documentation index at: https://desme.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Capture API

> Programmatically capture execution sessions from scripts, binaries, and custom workflows.

The capture API lets you instrument any Rust code — not just tests — and collect a debug session from it. Use these helpers when you want to trace a `main` function, a CLI script, a benchmark harness, or any other code that runs outside of `#[dbg_test]`.

## Setup

Add `dbgflow` to your `Cargo.toml`:

```toml theme={null}
[dependencies]
dbgflow = "0.1"
```

Then import the prelude at the top of your file:

```rust theme={null}
use dbgflow::prelude::*;
```

The prelude re-exports `capture`, `capture_to_file`, `capture_and_serve`, and all instrumentation macros (`#[trace]`, `#[ui_debug]`, `#[dbg_test]`).

***

## `dbgflow::capture`

```rust theme={null}
pub fn capture<T>(title: impl Into<String>, run: impl FnOnce() -> T) -> T
```

Runs a closure inside a fresh in-memory session and returns its result. The session stays in memory after the closure completes; use [`dbgflow::save_current_session`](https://desme.mintlify.site/api/session-management#save-current-session) afterward if you want to persist it.

**Parameters**

| Parameter | Type | Description |
| - | - | - |
| `title` | `impl Into<String>` | Display name for the session in the UI |
| `run` | `impl FnOnce() -> T` | Closure containing the code to trace |

**Returns** `T` — the return value of the closure.

### Example

```rust theme={null}
use dbgflow::prelude::*;

#[trace]
fn process(input: &str) -> String {
    input.to_uppercase()
}

fn main() {
    let result = dbgflow::capture("my session", || {
        process("hello world")
    });

    println!("Result: {result}");
    // The session is in memory; save or inspect it as needed.
}
```

> [!NOTE]
>
> `capture` resets any previously active session. If you need to keep an existing session, retrieve it with `dbgflow::current_session()` before calling `capture`.

***

## `dbgflow::capture_to_file`

```rust theme={null}
pub fn capture_to_file<T>(
    title: impl Into<String>,
    path: impl AsRef<Path>,
    run: impl FnOnce() -> T,
) -> std::io::Result<T>
```

Runs a closure, then writes the captured session to a JSON file. This is the most common choice for CI pipelines or build scripts where you want to save a session artifact for later review.

**Parameters**

| Parameter | Type | Description |
| - | - | - |
| `title` | `impl Into<String>` | Display name for the session in the UI |
| `path` | `impl AsRef<Path>` | Destination path for the JSON file |
| `run` | `impl FnOnce() -> T` | Closure containing the code to trace |

**Returns** `std::io::Result<T>` — `Ok(result)` on success, or an `Err` if writing the file fails.

### Example

```rust theme={null}
use dbgflow::prelude::*;

#[trace]
fn run_pipeline(data: &[u32]) -> u32 {
    data.iter().sum()
}

fn main() -> std::io::Result<()> {
    let data = vec![1, 2, 3, 4, 5];

    let total = dbgflow::capture_to_file("pipeline run", "artifacts/session.json", || {
        run_pipeline(&data)
    })?;

    println!("Total: {total}");
    // Inspect artifacts/session.json with: dbgflow serve artifacts/session.json
    Ok(())
}
```

> [!TIP]
>
> After saving, you can open the session in the browser UI with the CLI:
>
> ```bash theme={null}
> dbgflow serve artifacts/session.json
> ```

***

## `dbgflow::capture_and_serve`

```rust theme={null}
pub fn capture_and_serve<T>(
    title: impl Into<String>,
    host: &str,
    port: u16,
    run: impl FnOnce() -> T,
) -> std::io::Result<T>
```

Runs a closure, then immediately serves the captured session over the embedded HTTP server. Your browser can then open the interactive graph UI. This is the fastest way to get a visual of your execution during local development.

**Parameters**

| Parameter | Type | Description |
| - | - | - |
| `title` | `impl Into<String>` | Display name for the session in the UI |
| `host` | `&str` | Host address to bind (e.g. `"127.0.0.1"`) |
| `port` | `u16` | Port to listen on (e.g. `3000`) |
| `run` | `impl FnOnce() -> T` | Closure containing the code to trace |

**Returns** `std::io::Result<T>` — `Ok(result)` on success, or an `Err` if starting the server fails. The call **blocks** until you stop the server.

### Example

```rust theme={null}
use dbgflow::prelude::*;

#[ui_debug]
struct State {
    counter: usize,
}

#[trace]
fn step(state: &mut State) {
    state.counter += 1;
    state.emit_snapshot("after step");
}

fn main() -> std::io::Result<()> {
    capture_and_serve("my session", "127.0.0.1", 3000, || {
        let mut state = State { counter: 0 };
        step(&mut state);
        step(&mut state);
    })?;

    // The browser UI is available at http://127.0.0.1:3000
    // This line is reached only after the server shuts down.
    Ok(())
}
```

> [!NOTE]
>
> `capture_and_serve` blocks the current thread while the server is running. Use it in standalone binaries or scripts, not in library code that should return promptly.

***

## Choosing the right helper

| Goal | Use |
| - | - |
| Inspect a session live in the browser right now | `capture_and_serve` |
| Save a session artifact to disk | `capture_to_file` |
| Keep the session in memory for further processing | `capture` |
| Manage multiple sessions or merge captures manually | [Session Management](https://desme.mintlify.site/api/session-management.md) |


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.