Skip to content

feat: use tracing and RUST_LOG for logging - #64

Open
b1scoito wants to merge 1 commit into
mentebinaria:mainfrom
b1scoito:feat/tracing-logging
Open

b1scoito wants to merge 1 commit into
mentebinaria:mainfrom
b1scoito:feat/tracing-logging

Conversation

@b1scoito

@b1scoito b1scoito commented Sep 16, 2026

Copy link
Copy Markdown

Closes #57.

As I said in the issue, I moved the logging to tracing + tracing-subscriber, so RUST_LOG
works like in any other Rust program:

RUST_LOG=debug dz6 file.bin
RUST_LOG=dz6::hex::search=trace dz6 file.bin

The old App::log() pushed a String into app.logs, and goto() called it on every cursor
movement, so the vector grew for the whole session and the window was mostly a wall of
goto: 2a lines. There were no levels either, so a failure looked exactly like a cursor move,
and there was no way to get any of it out of the process.

Now the messages go to two places. The window (Alt+l) reads a ring buffer with the last 2048
of them. They also go to stderr, but only when you redirect it, because the TUI owns the
terminal and writing there would mess up the screen:

dz6 file.bin 2> dz6.log

When stderr is the terminal, the messages stay in the buffer and are printed after the
terminal is restored: warnings and errors only, or everything, if you set RUST_LOG yourself.
So dz6 file.bin stays as quiet as before, but RUST_LOG=debug dz6 file.bin still gives you
the whole log when you quit. It works after a panic too: our hook is installed before
ratatui::init(), so ratatui restores the terminal, the default hook prints the panic, and
then the log comes out.

This is what the window looks like with RUST_LOG=debug, after a search:

┌──────────────────────────────────────────── Log (8) ────────────────────────────────────────────┐
│18:27:54.415  INFO dz6: starting version="0.8.0" file=/tmp/demo.bin readonly=false              │
│18:27:54.422 DEBUG app{load_file}: file type identified kind=""                                 │
│18:27:54.422  INFO app{load_file}: file loaded path=/tmp/demo.bin size=64 read_only=false       │
│18:27:54.422  INFO database{load}: database loaded bookmarks=0 comments=1 blocks=0              │
│18:27:54.422 DEBUG initfile{read}: no init file path=/home/user/.dz6init                        │
│18:27:54.422 DEBUG dz6: page size changed height=22 from=1 to=304                               │
│18:27:56.936 DEBUG hex::search: searching bytes=4 from=16 backward=false                        │
│18:27:56.936 DEBUG hex::search: pattern found offset=20 wrapped=false                           │
└──────────────────────────────────────── RUST_LOG=debug ─────────────────────────────────────────┘

Without RUST_LOG the same window shows three lines (started, file loaded, database loaded),
which is about what it showed before, so I think the default (warn,dz6=info) is a good one.
goto is at trace level now, and it carries page_start and page_end, which is the
information the window used to print as a {:?} of Reader at the top.

The window itself got a bit better while I was there: it shows the time, level, module and the
span of each message, colors the level with the terminal colors (so it works in both themes),
opens on the newest message, and scrolls with j/k, f/b, g/G and h/l. c clears
it. I dropped the line wrapping in exchange for an exact scroll limit (the old one incremented
the offset forever, even past the end) and for building only the lines that are visible instead
of all of them. Long lines scroll sideways now.

I added three tests in src/logging.rs: the buffer dropping the oldest messages, an invalid
RUST_LOG falling back to the default, and an event becoming a record. cargo fmt and
cargo clippy are clean, the two warnings left were already there before this PR.

A little on how it looks:

image

New dependencies: tracing and tracing-subscriber (with the env-filter feature).

@b1scoito b1scoito mentioned this pull request Sep 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improved logging

1 participant