Pretty printing records

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

Pretty printing records

Vance Shipley
The shell now does a nice job of pretty printing records,
once you've loaded them with rr().  Currently I'm writing
test suites using common_test and would love the web page
reports generated to pretty print the (record) return values.

Is there any easy way to do that?

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

Re: Pretty printing records

Ulf Wiger-2
You can use the same function as the shell, namely:

io_lib_pretty:print(Term, RecDefFun)

where RecDefFun is a fun/2:

fun(Tag, NoOfFields) -> [FieldTag] | no

The RecDefFun can be somewhat generalized through use of
the exprecs parse transform.

This is (almost) described in:

http://forum.trapexit.org/viewtopic.php?p=21790#21790

You could make a module that includes definitions of all
relevant records, and "exports" them using exprecs.
Then you could modify the code in the example so that it
returns 'no' for terms that are not representations of some
exported record.

BR,
Ulf W

2008/11/20 Vance Shipley <[hidden email]>:

> The shell now does a nice job of pretty printing records,
> once you've loaded them with rr().  Currently I'm writing
> test suites using common_test and would love the web page
> reports generated to pretty print the (record) return values.
>
> Is there any easy way to do that?
>
>        -Vance
> _______________________________________________
> erlang-questions mailing list
> [hidden email]
> http://www.erlang.org/mailman/listinfo/erlang-questions
>
_______________________________________________
erlang-questions mailing list
[hidden email]
http://www.erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Pretty printing records

Vance Shipley
On Thu, Nov 20, 2008 at 05:44:26PM +0100, Ulf Wiger wrote:
}  You can use the same function as the shell, namely:
}  
}  io_lib_pretty:print(Term, RecDefFun)
 
That is indeed what I was looking for.  The result is a deep
list which when printed using the '~s' control sequence in
io:fwrite/2 (or in my case ct:log/2) gets output nicely.

     ct:log("Foo = ~s", [pretty_print(Foo)])

}  The RecDefFun can be somewhat generalized through use of
}  the exprecs parse transform.
 
I just used a local fun with the records I was interested in
seeing:

     pretty_print(Record) ->
          io_lib_pretty:print(Record, fun pretty_print/2).
     
     pretty_print(foo, N) ->
          N = record_info(size, foo) - 1,
          record_info(fields, foo);
     pretty_print(bar, N) ->
          N = record_info(size, bar) - 1,
          record_info(fields, bar);
     pretty_print(_, _) ->
          [].

Thanks,

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

Re: Pretty printing records

Ulf Wiger-2
But I believe the catch-all should be:

pretty_print(_, _) ->
    no.


BR,
Ulf W

2008/11/20 Vance Shipley <[hidden email]>:

> On Thu, Nov 20, 2008 at 05:44:26PM +0100, Ulf Wiger wrote:
> }  You can use the same function as the shell, namely:
> }
> }  io_lib_pretty:print(Term, RecDefFun)
>
> That is indeed what I was looking for.  The result is a deep
> list which when printed using the '~s' control sequence in
> io:fwrite/2 (or in my case ct:log/2) gets output nicely.
>
>     ct:log("Foo = ~s", [pretty_print(Foo)])
>
> }  The RecDefFun can be somewhat generalized through use of
> }  the exprecs parse transform.
>
> I just used a local fun with the records I was interested in
> seeing:
>
>     pretty_print(Record) ->
>          io_lib_pretty:print(Record, fun pretty_print/2).
>
>     pretty_print(foo, N) ->
>          N = record_info(size, foo) - 1,
>          record_info(fields, foo);
>     pretty_print(bar, N) ->
>          N = record_info(size, bar) - 1,
>          record_info(fields, bar);
>     pretty_print(_, _) ->
>          [].
>
> Thanks,
>
>        -Vance
>
_______________________________________________
erlang-questions mailing list
[hidden email]
http://www.erlang.org/mailman/listinfo/erlang-questions