lager/io:format and sets

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

lager/io:format and sets

Oleg Tsarev
Hello,

I am logging some record with set.
    State = #state{client_id  = ClientId,
                   session_id = SessionId,
                   user_id    = UserId,
                   channel_id_set = sets:new(),
                   mailbox = []},
    lager:debug("args ~p state ~p", [Args, lager:pr(State, ?MODULE)]),

As result I received:
*** User 2014-11-11 02:54:02.166 *** 02:54:02.166 ct@x200 debug qlp_client:init:89 args {0,"sid0",0} state #state{client_id=0,session_id="sid0",user_id=0,channel_id_set={set,0,16,16,8,80,48,{[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]},{{[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]}}},mailbox=[]}

Same with io:format
10> io:format("test ~p", [sets:new()]).       
test {set,0,16,16,8,80,48,
          {[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]},
          {{[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]}}}

How I can fix the output of the set in lager and io:format?

Thank you very much,
Oleg

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: lager/io:format and sets

Cian Synnott-2
On Tue, Nov 11, 2014 at 12:02 AM, Oleg Tsarev <[hidden email]> wrote:
> How I can fix the output of the set in lager and io:format?
>
I don't see a "built in" way to fix this. To roll your own, you could
"patch over" the record with sets:to_list/1 to make a printable
version. E.g. something like

printable(#state{channel_id_set=Set}=State) ->
  State#state{channel_id_set=sets:to_list(Set)}.

lager:debug("args ~p, state ~p", [Args, lager:pr(printable(State), ?MODULE)),

It's not super. You might need to change the record's type annotations
to avoid upsetting Dialyzer.

Cian
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: lager/io:format and sets

Ola Bäckström
In reply to this post by Oleg Tsarev
I would not recommend to change the internal representation just for making pretty printing simpler.

I'd either make a private conversion function, or if this concerns an otp genserver implement the gen_server optional behaviour format_status/2.
You'd have to do the conversion of the State yourself, and then call the function every time you want to log the state.
The advantage of making the conversion in the format_status/2 function is that also crash reports will contain converted state.
Perhaps also in other tools too, like observer, I haven't tried.

/Ola


-----Original Message-----
From: [hidden email] [mailto:[hidden email]] On Behalf Of Cian Synnott
Sent: den 11 november 2014 02:14
To: Oleg Tsarev
Cc: [hidden email]
Subject: Re: [erlang-questions] lager/io:format and sets

>On Tue, Nov 11, 2014 at 12:02 AM, Oleg Tsarev <[hidden email]> wrote:
>> How I can fix the output of the set in lager and io:format?
>>
>I don't see a "built in" way to fix this. To roll your own, you could "patch over" the record with sets:to_list/1 to make a printable version. E.g. something like
>
>printable(#state{channel_id_set=Set}=State) ->
>  State#state{channel_id_set=sets:to_list(Set)}.
>
>lager:debug("args ~p, state ~p", [Args, lager:pr(printable(State), ?MODULE)),
>
>It's not super. You might need to change the record's type annotations to avoid upsetting Dialyzer.
>
>Cian
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions