Builds a Rust CLI that counts log levels with graceful error handling (missing file, usage), as a testable lib + bin. Verified: 7 cargo tests + CLI exit codes 0/1/2.
A small command-line tool that reads a log file and counts how many lines are
INFO, WARN, and ERROR. Built in Rust with cargo.
✅ Verification status: verified 2026-06-17 with Rust 1.96.0 (Homebrew).
cargo test→ 7 passed;cargo run -- sample.log→INFO: 2 / WARN: 1 / ERROR: 1; missing file → exit1; no args → exit2. Full run log:B6_verification.md.
B6/
├── src/
│ ├── main.rs # CLI layer: argv parsing, output, exit codes
│ ├── parser.rs # Parsing layer: text/file -> LogCounts
│ ├── models.rs # Business logic: LogLevel + LogCounts (+ Display)
│ └── lib.rs # Exposes modules so tests/ can use them
├── tests/
│ └── cli.rs # Integration tests (cargo test)
├── sample.log # Example input
├── Cargo.toml
└── README.md
Layering: CLI (main.rs) → Parsing (parser.rs) → Business logic (models.rs).
Requires the Rust toolchain. Install via rustup:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
# then restart your shell, or: source "$HOME/.cargo/env"
cd B6
cargo build --release # binary at target/release/logcount
cargo run -- sample.log
# or, after build:
./target/release/logcount sample.log
cargo test
sample.log)INFO Application started
WARN Cache miss
INFO Request received
ERROR Database unavailable
INFO: 2
WARN: 1
ERROR: 1
cargo run -- missing.log
Error:
File not found: missing.log
Exits with a non-zero status code (1) and does not panic.
Each line is classified by its first whitespace-separated token. If that token is
INFO, WARN, or ERROR, the corresponding counter increments; any other line
(blank lines, DEBUG, free text) is ignored.