Skip to content

Commit f418a0e

Browse files
authored
Merge pull request #21 from erszcz/add-to-map
Add tr:to_map/1 to convert trace records to maps
2 parents 0169fe2 + e085b10 commit f418a0e

2 files changed

Lines changed: 32 additions & 2 deletions

File tree

src/tr.erl

Lines changed: 11 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,
@@ -696,6 +697,15 @@ mfargs({M, F, Arity}, Args) when length(Args) =:= Arity -> {M, F, Args}.
696697
-spec ts(tr()) -> string().
697698
ts(#tr{ts = TS}) -> calendar:system_time_to_rfc3339(TS, [{unit, microsecond}]).
698699

700+
%% @doc Convert trace records to maps.
701+
-spec to_map(tr()) -> map().
702+
to_map(#tr{} = Tr) ->
703+
IndexedFields = enumerate([record | record_info(fields, tr)]),
704+
maps:from_list(lists:map(fun ({Index, Field}) -> {Field, element(Index, Tr)} end, IndexedFields)).
705+
706+
enumerate(List) ->
707+
lists:zip(lists:seq(1, length(List)), List).
708+
699709
%% gen_server callbacks
700710

701711
%% @private

test/tr_SUITE.erl

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ groups() ->
6666
match,
6767
do,
6868
next,
69-
prev]},
69+
prev,
70+
to_map]},
7071
{call_stat, [simple_total,
7172
tree_total,
7273
simple_total_with_messages,
@@ -424,6 +425,25 @@ prev(_Config) ->
424425
?assertEqual(T1, tr:prev(4, #{pred => Pred})),
425426
?assertError(not_found, tr:prev(T1)).
426427

428+
to_map(_Config) ->
429+
Pid = erlang:list_to_pid("<0.115.0>"),
430+
Trace = #tr{index = 5,
431+
pid = Pid,
432+
event = call,
433+
mfa = {lists,keyfind,3},
434+
data = [syntax_colors,1,[]],
435+
ts = 1766078114348924,
436+
info = no_info},
437+
?assertEqual(#{record => tr,
438+
index => 5,
439+
pid => Pid,
440+
event => call,
441+
mfa => {lists,keyfind,3},
442+
data => [syntax_colors,1,[]],
443+
ts => 1766078114348924,
444+
info => no_info},
445+
tr:to_map(Trace)).
446+
427447
single_tb(_Config) ->
428448
tr:trace([{?MODULE, fib, 1}]),
429449
?MODULE:fib(4),

0 commit comments

Comments
 (0)