debugging 如何在erlang trace中定制return_trace的输出?

7hiiyaii  于 2022-11-30  发布在  Erlang
关注(0)|答案(1)|浏览(169)

我知道Erlang trace中return_trace的函数:

To get trace messages containing return values from functions,
use the {return_trace} match specification action instead.

但是,如果结果太大,我如何自定义输出?
我希望它显示返回值的前100个字节。

mlmc2os5

mlmc2os51#

receive
    {trace, Pid, return_from, {Module, Function, Arity}, ReturnValue} ->
        StringReturnValue = io_lib:format("~w", [ReturnValue]),
        First100 = lists:sublist(lists:flatten(StringReturnValue), 1, 100)
        io:format("~p:~p/~p returned: ~p~n", [Module, Function, Arity, First100)

如果你知道返回值是一个字符串(这和Erlang中的整数列表是一样的),那么你可以写:

receive
    {trace, Pid, return_from, {Module, Function, Arity}, ReturnValue} ->
        First100 = lists:sublist(ReturnValue, 1, 100)
        io:format("~p:~p/~p returned: ~p~n", [Module, Function, Arity, First100)

相关问题