Skip to content

Commit 279184a

Browse files
committed
Add tr:to_map/1 to convert trace records to maps
1 parent 71757a4 commit 279184a

2 files changed

Lines changed: 29 additions & 2 deletions

File tree

src/tr.erl

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@
4040
app_modules/1,
4141
mfarity/1,
4242
mfargs/2,
43-
ts/1]).
43+
ts/1,
44+
to_map/1]).
4445

4546
%% gen_server callbacks
4647
-export([init/1,
@@ -689,6 +690,12 @@ mfargs({M, F, Arity}, Args) when length(Args) =:= Arity -> {M, F, Args}.
689690
-spec ts(tr()) -> string().
690691
ts(#tr{ts = TS}) -> calendar:system_time_to_rfc3339(TS, [{unit, microsecond}]).
691692

693+
%% @doc Convert trace records to maps.
694+
-spec to_map(tr()) -> map().
695+
to_map(#tr{} = Tr) ->
696+
IndexedFields = lists:enumerate([record | record_info(fields, tr)]),
697+
maps:from_list(lists:map(fun ({Index, Field}) -> {Field, element(Index, Tr)} end, IndexedFields)).
698+
692699
%% gen_server callbacks
693700

694701
%% @private

test/tr_SUITE.erl

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ groups() ->
6464
match,
6565
do,
6666
next,
67-
prev]},
67+
prev,
68+
to_map]},
6869
{call_stat, [simple_total,
6970
tree_total,
7071
simple_total_with_messages,
@@ -360,6 +361,25 @@ prev(_Config) ->
360361
?assertEqual(T1, tr:prev(4, #{pred => Pred})),
361362
?assertError(not_found, tr:prev(T1)).
362363

364+
to_map(_Config) ->
365+
Pid = erlang:list_to_pid("<0.115.0>"),
366+
Trace = #tr{index = 5,
367+
pid = Pid,
368+
event = call,
369+
mfa = {lists,keyfind,3},
370+
data = [syntax_colors,1,[]],
371+
ts = 1766078114348924,
372+
info = no_info},
373+
?assertEqual(#{record => tr,
374+
index => 5,
375+
pid => Pid,
376+
event => call,
377+
mfa => {lists,keyfind,3},
378+
data => [syntax_colors,1,[]],
379+
ts => 1766078114348924,
380+
info => no_info},
381+
tr:to_map(Trace)).
382+
363383
single_tb(_Config) ->
364384
tr:trace([{?MODULE, fib, 1}]),
365385
?MODULE:fib(4),

0 commit comments

Comments
 (0)