2

I'm trying to log all calls to a function with dbg for debugging (thanks to this answer). Here's the code:

-module(a).
-export([main/0]).

trace_me(_, _, _) ->
  ok.

main() ->
  dbg:start(),
  dbg:tracer(),
  dbg:tpl(a, trace_me, 3, []),
  dbg:p(all, c),
  LargeBinary = binary:copy(<<"foo">>, 10000),
  trace_me(foo, bar, LargeBinary).

The problem is that one of the argument is a really large binary, and the code above prints the complete binary with every call:

1> c(a).
{ok,a}
2> a:main().
(<0.57.0>) call a:trace_me(foo,bar,<<"foofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoo...lots of foos omitted...">>)
ok

Is it possible to (without modifying trace_me/3):

  1. Only print the the first 2 arguments for each call?

  2. Print the first 2 arguments + first few bytes of the 3rd argument or just pass the 3rd argument through a custom function before printing?

Community
  • 1
  • 1
Dogbert
  • 212,659
  • 41
  • 396
  • 397
  • have you tried my suggestion with redbug, or do you _really_ need dbg? – marco.m Aug 04 '16 at 08:43
  • @marco.m sorry I didn't get a chance to update you. I really wanted a solution without adding any new dependencies. Thanks to the video you posted I dived into the implementation of redbug and dbg and wrote a small function that uses the tracer API to do what I want. I will post the solution here as soon as I am able to. – Dogbert Aug 07 '16 at 16:21

1 Answers1

2

I don't know with dbg, but you can use the very good and small redbug:

trace_me(_, _, _) ->
    ok.

do_redbug() ->
    % 1. print arguments as dbg:
    % redbug:start("a:trace_me"),

    % 2. print arity only:
    % redbug:start("a:trace_me", [arity]),

    % 3. Specify the formatting depth:
    redbug:start("a:trace_me", [{print_depth, 10}]),

    LargeBinary = binary:copy(<<"foo">>, 100000),
    trace_me(foo, bar, LargeBinary).

Have a look at the (quite terse) redbug documentation in the above link for more options and for how to pass your own printer/formatter function.

Note also that redbug, as opposed to dbg, is safe to use on a production system. Check out this presentation by the redbug author: Taking the printf out of printf Debugging.

marco.m
  • 4,573
  • 2
  • 26
  • 41
  • I ended up using a tiny module I wrote for now: https://gist.github.com/3cc6b76e9a58823c763e3f000ccf4bed. It doesn't handle cases like large binaries inside other things like tuples, but I didn't want to add dependencies to this toy project, and this was good enough. Redbug looks like a more robust solution for debugging production apps. Thank you for the recommendation and also the presentation video link. – Dogbert Aug 08 '16 at 11:08