Liking cljdoc? Tell your friends :D

Debug macro (dbg) design

Intent

Puzzle solvers want to see intermediate values while working out a solution, without rewriting code to do it and without the debug output affecting timings on a normal run. Elf owns the macro so it controls the formatting (pretty printing, labels, size limits) and can make the calls disappear entirely when debugging is off.

Goals

  • (dbg form) prints a labelled value and returns the value unchanged, so it can wrap any expression, including a step in -> or ->> pipelines
  • bb aoc solve --debug and bb aoc test --debug print debug output. Without --debug, dbg calls have no run-time cost: they are removed when the solution file is loaded
  • Data structures are pretty-printed by default; --no-pretty prints each value on one line
  • Large values are cut off so they don't flood the terminal

Non-goals

  • Other utility macros (later, separately)
  • Per-call options (labels, per-call pretty toggle). dbg takes exactly one argument so it works in both -> and ->>
  • Live toggling in a REPL without reloading the namespace

Anti-goals

  • Any measurable slowdown of a non-debug solve
  • Changing the value or laziness semantics of the wrapped code (beyond the realisation the printer needs when debugging is on)

Approaches

ApproachTrade-off
A (chosen)Decide at load time: the macro reads a setting when it expands; when off it expands to the bare formZero cost when off. The setting must be chosen before the solution is loaded, so toggling needs a reload (fine for the CLI, which loads fresh each run)
BRuntime check per callLive toggling, but a check on every call. Babashka interprets code, so that check costs something in hot loops, which conflicts with the zero-cost goal
Ctap>Standard, but a per-call cost, and the formatting is left to whatever is listening, while the goal is for elf to own it

Chosen with the user: A.

Design

New namespace aoc-clj-elf.debug

  • settings: an atom {:enabled? true :pretty? true :print-length 100 :print-level 10}
    • :enabled? defaults to true, so dbg works in a REPL, where the runner isn't involved ("if you typed dbg, you want to see it")
  • (dbg form) macro:
    • when :enabled? is false at expansion time it expands to form
    • otherwise it expands to code that evaluates form once, prints a label and the value, and returns the value
  • Label: [<file>:<line>] <form> =>, dimmed with bling.
    • <file> is the path relative to the working directory (e.g. src/y2026/d4.clj)
    • <line> comes from &form metadata, falling back to form's metadata (threading macros drop &form's metadata); the :<line> part is left out if neither has one
    • <form> is the form as written after threading expansion
  • Value: printed with *print-length*/*print-level* bound from settings, using clojure.pprint/pprint when :pretty? is on, else prn
  • Output goes to *out*, the same stream solution output already uses

CLI

  • solve and test accept --debug (boolean, default false) and --pretty/--no-pretty (boolean, default true)
  • Before requiring the solution namespace, the runner sets :enabled? (from --debug) and :pretty? in settings
  • Other puzzle commands (new, get) ignore these options and don't list them in help

Template

resources/solution.template adds [aoc-clj-elf.debug :refer [dbg]] to the ns requires.

Docs and release

  • README: a "Debugging" section with an example, --debug and --no-pretty, the REPL default, and a note that debug output is included in timings when on
  • Changelog entry for 0.4.0

Verified with a throwaway experiment

In babashka: load-time elision works (expanded to the bare form, no output); &form has no line inside ->> but the wrapped form does; *print-length* is respected by pprint.

Left to the builder

  • Exact dim styling via bling
  • How the debug and pretty options are added to the option spec for solve and test only
  • Test strategy (macroexpansion when off; captured output when on)

Can you improve this documentation?Edit on GitHub

cljdoc builds & hosts documentation for Clojure/Script libraries

Keyboard shortcuts
Ctrl+kJump to recent docs
←Move to previous article
→Move to next article
Ctrl+/Jump to the search field
× close