philoskim / debux

A trace-based debugging library for Clojure and ClojureScript.
468 stars 19 forks source link

Request to return local vars #19

Closed Futurile closed 4 years ago

Futurile commented 4 years ago

I found a useful function in the Postmortem that provides the local variables of a function. It's really useful for trying to understand the local context of the function. Would you consider adding a feature to debux?

For example, something like this:

(let [i 10]
    (* 2 (dbg (+ i 5) :locals ))

{:ns user, :line 1}
    dbg: (+ 10 5) :locals {:i 10} =>
    |   15
    30

(dbgn :locals (defn stats [fname [height weight] ]
            (let [i 10]
            (println fname "is" height))))

(stats "Bob" [178 68])

| fname =>
|   "Bob"
| height =>
|   178
Bob is 178
| (println fname "is" height) =>
|   nil
| (let [i 10] (debux.common.util/insert-blank-line) (println fname "is"  ... =>
|   nil
nil
| :locals {name "Bob", height 178,  weight 68, i 10}

A new option called :locals provides any local vars.

In Postmortem there's a specific function called debug, see the link for an example. Hash-meta has played with some interesting ideas and its implementation does this:

{:locals {}, :result 23, :form (+ 3 (* 4 5))}

The only way I could figure out how to do this with debux at the moment was to specifically call the vars in a dbg such as (dbg [fname height weight i])

philoskim commented 4 years ago

Your suggestion is reflected in the new 0.6.6 version of debux library. Thanks a lot for the suggestion!

Futurile commented 4 years ago

I wrote a post about a few tools including Debux here: http://www.futurile.net/2020/05/16/clojure-observability-and-debugging-tools/