使用`dbg`跟踪函数时,只记录调用跟踪中的特定参数?

时间:2016-07-26 17:40:47

标签: erlang

我正在尝试将所有调用记录到dbg的函数进行调试(感谢this回答)。这是代码:

-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).

问题是其中一个参数是真正的大二进制文件,上面的代码会在每次调用时打印完整的二进制文件:

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

是否可以(不修改trace_me/3):

  1. 只打印每个电话的前2个参数?

  2. 打印前两个参数+第三个参数的前几个字节,或者在打印前通过自定义函数传递第三个参数?

1 个答案:

答案 0 :(得分:2)

我不知道dbg,但你可以使用非常好又小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).

查看上面链接中的(非常简洁的)redbug文档,了解更多选项以及如何传递自己的打印机/格式化程序功能。

另请注意,与dbg相反,redbug可以安全地在生产系统上使用。请查看redbug作者的演示文稿:Taking the printf out of printf Debugging