[erlang-questions] Pretty printing records

Vance Shipley <>
Thu Nov 20 21:25:32 CET 2008


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



More information about the erlang-questions mailing list