Class PrettyPrinter


  • public class PrettyPrinter
    extends java.lang.Object
    Prints information in a pretty box
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected java.lang.String kvFormat  
      protected int kvKeyWidth
      Key/value key width
      protected int width
      Box with (adapts to contents)
      protected int wrapWidth
      Wrap width used when an explicit wrap width is not specified
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      PrettyPrinter add()
      Adds a blank line to the output
      PrettyPrinter add​(java.lang.Object object)
      Adds the specified object to the output
      PrettyPrinter add​(java.lang.Object[] array)
      Add elements of the array to the output, one per line
      PrettyPrinter add​(java.lang.Object[] array, java.lang.String format)
      Add elements of the array to the output, one per line
      PrettyPrinter add​(java.lang.Object object, int indent)
      Adds the specified object to the output
      PrettyPrinter add​(java.lang.StackTraceElement[] stackTrace, int indent)
      Print a formatted representation of the specified stack trace with the specified indent
      PrettyPrinter add​(java.lang.String string)
      Adds a string line to the output
      PrettyPrinter add​(java.lang.String format, java.lang.Object... args)
      Adds a formatted line to the output
      PrettyPrinter add​(java.lang.Throwable th)
      Print a formatted representation of the specified throwable with the default indent (4)
      PrettyPrinter add​(java.lang.Throwable th, int indent)
      Print a formatted representation of the specified throwable with the specified indent
      PrettyPrinter add​(java.util.Map<?,​?> map)
      Add all values of the specified map to this printer as key/value pairs
      PrettyPrinter add​(PrettyPrinter.IPrettyPrintable printable)
      Adds a pretty-printable object to the output, the object is responsible for adding its own representation to this printer
      PrettyPrinter addIndexed​(java.lang.Object[] array)
      Add elements of the array to the output, one per line, with array indices
      PrettyPrinter addWithIndices​(java.util.Collection<?> c)
      Add elements of the collection to the output, one per line, with indices
      PrettyPrinter addWrapped​(int width, java.lang.String format, java.lang.Object... args)
      Adds a formatted line to the output, and attempts to wrap the line content to the specified width
      PrettyPrinter addWrapped​(java.lang.String format, java.lang.Object... args)
      Adds a formatted line to the output, and attempts to wrap the line content to the current wrap width
      PrettyPrinter centre()
      Centre the last line added
      static void dumpStack()
      Convenience method, alternative to using Thread.dumpStack which prints to stderr in pretty-printed format.
      PrettyPrinter hr()
      Adds a horizontal rule to the output
      PrettyPrinter hr​(char ruleChar)
      Adds a horizontal rule of the specified char to the output
      PrettyPrinter kv​(java.lang.String key, java.lang.Object value)
      Add a key/value pair to the output
      PrettyPrinter kv​(java.lang.String key, java.lang.String format, java.lang.Object... args)
      Add a formatted key/value pair to the output
      PrettyPrinter kvWidth​(int width)
      Set the minimum key display width
      PrettyPrinter log​(ILogger logger)
      Write this printer to the specified logger at Level.INFO
      PrettyPrinter log​(ILogger logger, Level level)
      Write this printer to the specified logger
      PrettyPrinter log​(Level level)
      Write this printer to the specified logger at Level.INFO
      PrettyPrinter print()
      Print this printer to stderr
      PrettyPrinter print​(java.io.PrintStream stream)
      Print this printer to the specified output
      static void print​(java.lang.Throwable th)
      Convenience methods, pretty-prints the specified throwable to stderr
      PrettyPrinter spacing​(int spacing)
      Set the column spacing for the current table.
      PrettyPrinter table()
      Begin a new table with no header and adaptive column widths
      PrettyPrinter table​(java.lang.Object... format)
      Begin a new table with the specified format.
      PrettyPrinter table​(java.lang.String... titles)
      Begin a new table with the specified headers and adaptive column widths
      PrettyPrinter th()
      Print the current table header.
      PrettyPrinter tr​(java.lang.Object... args)
      Print a table row with the specified values.
      PrettyPrinter trace()
      Outputs this printer to stderr and to a logger decorated with the calling class name with level Level.DEBUG
      PrettyPrinter trace​(java.io.PrintStream stream)
      Outputs this printer to the specified stream and to a logger decorated with the calling class name with level Level.DEBUG
      PrettyPrinter trace​(java.io.PrintStream stream, java.lang.String logger)
      Outputs this printer to the specified stream and to a logger with the specified name with level Level.DEBUG
      PrettyPrinter trace​(java.io.PrintStream stream, java.lang.String logger, Level level)
      Outputs this printer to the specified stream and to a logger with the specified name at the specified level
      PrettyPrinter trace​(java.io.PrintStream stream, ILogger logger)
      Outputs this printer to the specified stream and to the supplied logger with level Level.DEBUG
      PrettyPrinter trace​(java.io.PrintStream stream, ILogger logger, Level level)
      Outputs this printer to the specified stream and to the supplied logger with at the specified level
      PrettyPrinter trace​(java.io.PrintStream stream, Level level)
      Outputs this printer to the specified stream and to a logger decorated with the calling class name with the specified level
      PrettyPrinter trace​(java.lang.String logger)
      Outputs this printer to stderr and to a logger decorated with specified name with level Level.DEBUG
      PrettyPrinter trace​(java.lang.String logger, Level level)
      Outputs this printer to stderr and to a logger decorated with specified name with the specified level
      PrettyPrinter trace​(ILogger logger)
      Outputs this printer to stderr and to the supplied logger with level Level.DEBUG
      PrettyPrinter trace​(ILogger logger, Level level)
      Outputs this printer to stderr and to the supplied logger with the specified level
      PrettyPrinter trace​(Level level)
      Outputs this printer to stderr and to a logger decorated with the calling class name at the specified level
      int wrapTo()
      Get the current wrap width
      PrettyPrinter wrapTo​(int wrapWidth)
      Set the wrap width (default 80 columns)
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • width

        protected int width
        Box with (adapts to contents)
      • wrapWidth

        protected int wrapWidth
        Wrap width used when an explicit wrap width is not specified
      • kvKeyWidth

        protected int kvKeyWidth
        Key/value key width
      • kvFormat

        protected java.lang.String kvFormat
    • Constructor Detail

      • PrettyPrinter

        public PrettyPrinter()
      • PrettyPrinter

        public PrettyPrinter​(int width)
    • Method Detail

      • wrapTo

        public PrettyPrinter wrapTo​(int wrapWidth)
        Set the wrap width (default 80 columns)
        Parameters:
        wrapWidth - new width (in characters) to wrap to
        Returns:
        fluent interface
      • wrapTo

        public int wrapTo()
        Get the current wrap width
        Returns:
        the current wrap width
      • table

        public PrettyPrinter table()
        Begin a new table with no header and adaptive column widths
        Returns:
        fluent interface
      • table

        public PrettyPrinter table​(java.lang.String... titles)
        Begin a new table with the specified headers and adaptive column widths
        Parameters:
        titles - Column titles
        Returns:
        fluent interface
      • table

        public PrettyPrinter table​(java.lang.Object... format)
        Begin a new table with the specified format. The format is specified as a sequence of values with Strings defining column titles, Integers defining column widths, and PrettyPrinter.Alignments defining column alignments. Widths and alignment specifiers should follow the relevant column title. Specify a negative value to specify the maximum width for a column (values will be truncated).

        For example, to specify a table with two columns of width 10:

        printer.table("Column 1", 10, "Column 2", 10);

        A table with a column 30 characters wide and a right-aligned column 20 characters wide:

        printer.table("Column 1", 30, "Column 2", 20, Alignment.RIGHT);
        Parameters:
        format - format string, see description
        Returns:
        fluent interface
      • spacing

        public PrettyPrinter spacing​(int spacing)
        Set the column spacing for the current table. Default = 2
        Parameters:
        spacing - Column spacing in characters
        Returns:
        fluent interface
      • th

        public PrettyPrinter th()
        Print the current table header. The table header is automatically printed before the first row if not explicitly specified by calling this method.
        Returns:
        fluent interface
      • tr

        public PrettyPrinter tr​(java.lang.Object... args)
        Print a table row with the specified values. If more columns are specified than exist in the table, then the table is automatically expanded.
        Parameters:
        args - column values
        Returns:
        fluent interface
      • add

        public PrettyPrinter add()
        Adds a blank line to the output
        Returns:
        fluent interface
      • add

        public PrettyPrinter add​(java.lang.String string)
        Adds a string line to the output
        Parameters:
        string - format string
        Returns:
        fluent interface
      • add

        public PrettyPrinter add​(java.lang.String format,
                                 java.lang.Object... args)
        Adds a formatted line to the output
        Parameters:
        format - format string
        args - arguments
        Returns:
        fluent interface
      • add

        public PrettyPrinter add​(java.lang.Object[] array)
        Add elements of the array to the output, one per line
        Parameters:
        array - Array of objects to print
        Returns:
        fluent interface
      • add

        public PrettyPrinter add​(java.lang.Object[] array,
                                 java.lang.String format)
        Add elements of the array to the output, one per line
        Parameters:
        array - Array of objects to print
        format - Format for each row
        Returns:
        fluent interface
      • addIndexed

        public PrettyPrinter addIndexed​(java.lang.Object[] array)
        Add elements of the array to the output, one per line, with array indices
        Parameters:
        array - Array of objects to print
        Returns:
        fluent interface
      • addWithIndices

        public PrettyPrinter addWithIndices​(java.util.Collection<?> c)
        Add elements of the collection to the output, one per line, with indices
        Parameters:
        c - Collection of objects to print
        Returns:
        fluent interface
      • add

        public PrettyPrinter add​(PrettyPrinter.IPrettyPrintable printable)
        Adds a pretty-printable object to the output, the object is responsible for adding its own representation to this printer
        Parameters:
        printable - object to add
        Returns:
        fluent interface
      • add

        public PrettyPrinter add​(java.lang.Throwable th)
        Print a formatted representation of the specified throwable with the default indent (4)
        Parameters:
        th - Throwable to print
        Returns:
        fluent interface
      • add

        public PrettyPrinter add​(java.lang.Throwable th,
                                 int indent)
        Print a formatted representation of the specified throwable with the specified indent
        Parameters:
        th - Throwable to print
        indent - Indent size for stacktrace lines
        Returns:
        fluent interface
      • add

        public PrettyPrinter add​(java.lang.StackTraceElement[] stackTrace,
                                 int indent)
        Print a formatted representation of the specified stack trace with the specified indent
        Parameters:
        stackTrace - stack trace to print
        indent - Indent size for stacktrace lines
        Returns:
        fluent interface
      • add

        public PrettyPrinter add​(java.lang.Object object)
        Adds the specified object to the output
        Parameters:
        object - object to add
        Returns:
        fluent interface
      • add

        public PrettyPrinter add​(java.lang.Object object,
                                 int indent)
        Adds the specified object to the output
        Parameters:
        object - object to add
        indent - indent amount
        Returns:
        fluent interface
      • addWrapped

        public PrettyPrinter addWrapped​(java.lang.String format,
                                        java.lang.Object... args)
        Adds a formatted line to the output, and attempts to wrap the line content to the current wrap width
        Parameters:
        format - format string
        args - arguments
        Returns:
        fluent interface
      • addWrapped

        public PrettyPrinter addWrapped​(int width,
                                        java.lang.String format,
                                        java.lang.Object... args)
        Adds a formatted line to the output, and attempts to wrap the line content to the specified width
        Parameters:
        width - wrap width to use for this content
        format - format string
        args - arguments
        Returns:
        fluent interface
      • kv

        public PrettyPrinter kv​(java.lang.String key,
                                java.lang.String format,
                                java.lang.Object... args)
        Add a formatted key/value pair to the output
        Parameters:
        key - Key
        format - Value format
        args - Value args
        Returns:
        fluent interface
      • kv

        public PrettyPrinter kv​(java.lang.String key,
                                java.lang.Object value)
        Add a key/value pair to the output
        Parameters:
        key - Key
        value - Value
        Returns:
        fluent interface
      • kvWidth

        public PrettyPrinter kvWidth​(int width)
        Set the minimum key display width
        Parameters:
        width - width to set
        Returns:
        fluent
      • add

        public PrettyPrinter add​(java.util.Map<?,​?> map)
        Add all values of the specified map to this printer as key/value pairs
        Parameters:
        map - Map with entries to add
        Returns:
        fluent
      • hr

        public PrettyPrinter hr()
        Adds a horizontal rule to the output
        Returns:
        fluent interface
      • hr

        public PrettyPrinter hr​(char ruleChar)
        Adds a horizontal rule of the specified char to the output
        Parameters:
        ruleChar - character to use for the horizontal rule
        Returns:
        fluent interface
      • centre

        public PrettyPrinter centre()
        Centre the last line added
        Returns:
        fluent interface
      • trace

        public PrettyPrinter trace()
        Outputs this printer to stderr and to a logger decorated with the calling class name with level Level.DEBUG
        Returns:
        fluent interface
      • trace

        public PrettyPrinter trace​(Level level)
        Outputs this printer to stderr and to a logger decorated with the calling class name at the specified level
        Parameters:
        level - Log level to write messages
        Returns:
        fluent interface
      • trace

        public PrettyPrinter trace​(java.lang.String logger)
        Outputs this printer to stderr and to a logger decorated with specified name with level Level.DEBUG
        Parameters:
        logger - Logger name to write to
        Returns:
        fluent interface
      • trace

        public PrettyPrinter trace​(java.lang.String logger,
                                   Level level)
        Outputs this printer to stderr and to a logger decorated with specified name with the specified level
        Parameters:
        logger - Logger name to write to
        level - Log level to write messages
        Returns:
        fluent interface
      • trace

        public PrettyPrinter trace​(ILogger logger)
        Outputs this printer to stderr and to the supplied logger with level Level.DEBUG
        Parameters:
        logger - Logger to write to
        Returns:
        fluent interface
      • trace

        public PrettyPrinter trace​(ILogger logger,
                                   Level level)
        Outputs this printer to stderr and to the supplied logger with the specified level
        Parameters:
        logger - Logger to write to
        level - Log level to write messages
        Returns:
        fluent interface
      • trace

        public PrettyPrinter trace​(java.io.PrintStream stream)
        Outputs this printer to the specified stream and to a logger decorated with the calling class name with level Level.DEBUG
        Parameters:
        stream - Output stream to print to
        Returns:
        fluent interface
      • trace

        public PrettyPrinter trace​(java.io.PrintStream stream,
                                   Level level)
        Outputs this printer to the specified stream and to a logger decorated with the calling class name with the specified level
        Parameters:
        stream - Output stream to print to
        level - Log level to write messages
        Returns:
        fluent interface
      • trace

        public PrettyPrinter trace​(java.io.PrintStream stream,
                                   java.lang.String logger)
        Outputs this printer to the specified stream and to a logger with the specified name with level Level.DEBUG
        Parameters:
        stream - Output stream to print to
        logger - Logger name to write to
        Returns:
        fluent interface
      • trace

        public PrettyPrinter trace​(java.io.PrintStream stream,
                                   java.lang.String logger,
                                   Level level)
        Outputs this printer to the specified stream and to a logger with the specified name at the specified level
        Parameters:
        stream - Output stream to print to
        logger - Logger name to write to
        level - Log level to write messages
        Returns:
        fluent interface
      • trace

        public PrettyPrinter trace​(java.io.PrintStream stream,
                                   ILogger logger)
        Outputs this printer to the specified stream and to the supplied logger with level Level.DEBUG
        Parameters:
        stream - Output stream to print to
        logger - Logger to write to
        Returns:
        fluent interface
      • trace

        public PrettyPrinter trace​(java.io.PrintStream stream,
                                   ILogger logger,
                                   Level level)
        Outputs this printer to the specified stream and to the supplied logger with at the specified level
        Parameters:
        stream - Output stream to print to
        logger - Logger to write to
        level - Log level to write messages
        Returns:
        fluent interface
      • print

        public PrettyPrinter print()
        Print this printer to stderr
        Returns:
        fluent interface
      • print

        public PrettyPrinter print​(java.io.PrintStream stream)
        Print this printer to the specified output
        Parameters:
        stream - stream to print to
        Returns:
        fluent interface
      • log

        public PrettyPrinter log​(ILogger logger)
        Write this printer to the specified logger at Level.INFO
        Parameters:
        logger - logger to log to
        Returns:
        fluent interface
      • log

        public PrettyPrinter log​(Level level)
        Write this printer to the specified logger at Level.INFO
        Parameters:
        level - log level
        Returns:
        fluent interface
      • log

        public PrettyPrinter log​(ILogger logger,
                                 Level level)
        Write this printer to the specified logger
        Parameters:
        logger - logger to log to
        level - log level
        Returns:
        fluent interface
      • dumpStack

        public static void dumpStack()
        Convenience method, alternative to using Thread.dumpStack which prints to stderr in pretty-printed format.
      • print

        public static void print​(java.lang.Throwable th)
        Convenience methods, pretty-prints the specified throwable to stderr
        Parameters:
        th - Throwable to log