Module trunc_io

Description

Module to print out terms for logging. Limits by length rather than depth.

The resulting string may be slightly larger than the limit; the intention is to provide predictable CPU and memory consumption for formatting terms, not produce precise string lengths.

Typical use:

trunc_io:print(Term, 500).

Source license: Erlang Public License. Original author: Matthias Lang, matthias@corelatus.se

Function Index

fprint/2Returns an flattened list containing the ASCII representation of the given term.
print/2Returns {List, Length}.
safe/2Same as print, but never crashes.

Function Details

fprint/2

fprint(T :: term(), Max :: pos_integer()) -> string()

Returns an flattened list containing the ASCII representation of the given term.

print/2

print(Tuple :: term(), Max :: pos_integer()) ->
         {iolist(), pos_integer()}

Returns {List, Length}

safe/2

safe(What :: term(), Len :: pos_integer()) ->
        {string(), pos_integer()} | {string()}

Same as print, but never crashes.

This is a tradeoff. Print might conceivably crash if it's asked to print something it doesn't understand, for example some new data type in a future version of Erlang. If print crashes, we fall back to io_lib to format the term, but then the formatting is depth-limited instead of length limited, so you might run out memory printing it. Out of the frying pan and into the fire.