io_lib_pretty
. It hasn't got a manpage, but there are some docs if you less `locate io_lib_pretty.erl`
.io_lib_pretty
is the module used by the shell to print records in a nicely formatted way. This isn't possible using plain io:format
but can make program output a lot nicer.Take for example a logging program that deals with records that look like this:
5> L = #logMessage{actor=23507, server_ip = <<123,234,123,234>>}.
#logMessage{actor = 23507,
server_ip = <<"{\352{\352">>,
timestamp = undefined,
level = undefined,
log_filename = undefined,
message = undefined}
If you just try to print it out, you get:
7> io:format("Logged: ~p", [L]).
Logged: {logMessage,23507,<<"{\352{\352">>,undefined,undefined,undefined,undefined}ok
Pretty useless output.
Using
io_lib_pretty
you can get:
9> io:format(io_lib_pretty:print(L, fun(logMessage, 6) -> [actor, server_ip, timestamp, level, log_filename, message] end)).
#logMessage{actor = 23507,
server_ip = <<"{\352{\352">>,
timestamp = undefined,
level = undefined,
log_filename = undefined,
message = undefined}ok
Just like the shell. I listed the record information manually in the function above, but you can easily use the
record_info
macro to accomplish the same without code duplication. Or even easier, use the exprecs parse transform (pretty printing example available there).Next time: how to load record definitions dynamically at runtime.
No comments:
Post a Comment