Does D have a tool like pySnooper?

H. S. Teoh hsteoh at quickfur.ath.cx
Fri Apr 26 16:59:15 UTC 2019


On Fri, Apr 26, 2019 at 02:33:16PM +0000, Taylor Hillegeist via Digitalmars-d-learn wrote:
> On Friday, 26 April 2019 at 10:22:49 UTC, Bastiaan Veelo wrote:
[...]
> > Proofing the concept:
> > ---
> > mixin(snoop(q{
> >     int fun(int a, int b)
> >     {
> >         int c = 3;
> >         foreach (i; 1 .. 5)
> >         {
> >             a += i;
> >         }
> >         int d = a + b + c;
> >         return d;
> >     }
> > }));
> > ---
> > 
> > Output:
> > 2019-Apr-26 10:33:46.0935135 executed line 7:	        int c = 3;
> > 2019-Apr-26 10:33:46.0936716 executed line 10:	            a += i;
> > 2019-Apr-26 10:33:46.0937348 executed line 10:	            a += i;
> > 2019-Apr-26 10:33:46.0937963 executed line 10:	            a += i;
> > 2019-Apr-26 10:33:46.0938583 executed line 10:	            a += i;
> > 2019-Apr-26 10:33:46.0939622 executed line 12:	        int d = a + b +
> > c;

Now *this* is some seriously cool stuff.


[...]
> [...] Tools like this can really help new users, especially when they
> are doing things the o'l fashion way, printf for debugging.

I debug using printf/writeln too. As Nick would say, it gives you a
fully-rewindable log of what actually happened in the code, and is often
useful where a full debugger wouldn't be able to run (e.g., on embedded
platforms with tight memory/CPU constraints).

That, and also that D debugger support is currently rather anemic; in
gdb, for example, many symbols are not recognized and/or their values
are inaccessible.  Stacktraces are supposed to give line numbers, but I
haven't seen those in a while, even with -g.  (At least it now gives at
least the (mangled) function names; it used to be just a bunch of hex
addresses that you have to decipher yourself.)  I would contribute to
debugger support, but unfortunately I don't actually use debuggers often
enough to warrant the effort.


> I doubt it would be too much help for things like ranges, but one must
> pick their battles.
[...]

If you're talking about UFCS chains, there's std.range.tee that lets you
see what's going on in the middle of the chain without changing the type
required for the next step in the pipeline.  It's not as convenient, but
a LOT better than having to split up a long UFCS chain just to insert
debug code.


T

-- 
Life is complex. It consists of real and imaginary parts. -- YHL


More information about the Digitalmars-d-learn mailing list