Class SimpleLog


  • public final class SimpleLog
    extends java.lang.Object
    A logging class with the following features.
    • Can be enabled and disabled (when disabled, all operations are no-ops),
    • Can indent/exdent log output,
    • Writes to a file or to standard output, and
    • Can provide a stack trace.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      boolean enabled
      If false, do no output.
      private @Nullable java.lang.String filename
      The file for logging output.
      private static java.lang.String INDENT_STR_ONE_LEVEL
      Indentation string for one level of indentation.
      private int indentLevel
      The current indentation level.
      private @Nullable java.lang.String indentString
      Cache for the current indentation string, or null if needs to be recomputed.
      private java.util.List<java.lang.String> indentStrings
      Cache of indentation strings that have been computed so far.
      private @MonotonicNonNull java.io.PrintStream logfile
      Where to write logging output.
    • Constructor Summary

      Constructors 
      Constructor Description
      SimpleLog()
      Create a new SimpleLog object with logging to standard out enabled.
      SimpleLog​(boolean enabled)
      Create a new SimpleLog object with logging to standard out.
      SimpleLog​(@Nullable java.lang.String filename)
      Create a new SimpleLog object with logging to a file enabled.
      SimpleLog​(@Nullable java.lang.String filename, boolean enabled)
      Create a new SimpleLog object with logging to a file.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean enabled()
      Return whether logging is enabled.
      void exdent()
      Decreases indentation by one level.
      private java.lang.String getIndentString()
      Return the current indentation string.
      void indent()
      Increases indentation by one level.
      void log​(java.lang.String format, @Nullable java.lang.Object... args)
      Log a message.
      void logStackTrace()
      Print a stack trace to the log.
      void resetIndent()
      Resets indentation to none.
      private void setLogfile()
      Set the private field logfile (if it is not set), based on the private field filename.
      • Methods inherited from class java.lang.Object

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

      • enabled

        public boolean enabled
        If false, do no output.
      • logfile

        private @MonotonicNonNull java.io.PrintStream logfile
        Where to write logging output. Null if nothing has been output yet.
      • filename

        private @Nullable java.lang.String filename
        The file for logging output. If null, System.out is used.
      • indentLevel

        private int indentLevel
        The current indentation level.
      • INDENT_STR_ONE_LEVEL

        private static final java.lang.String INDENT_STR_ONE_LEVEL
        Indentation string for one level of indentation.
        See Also:
        Constant Field Values
      • indentString

        private @Nullable java.lang.String indentString
        Cache for the current indentation string, or null if needs to be recomputed. Never access this directly; always call getIndentString().
      • indentStrings

        private java.util.List<java.lang.String> indentStrings
        Cache of indentation strings that have been computed so far.
    • Constructor Detail

      • SimpleLog

        public SimpleLog()
        Create a new SimpleLog object with logging to standard out enabled.
      • SimpleLog

        public SimpleLog​(boolean enabled)
        Create a new SimpleLog object with logging to standard out.
        Parameters:
        enabled - whether the logger starts out enabled
      • SimpleLog

        public SimpleLog​(@Nullable java.lang.String filename)
        Create a new SimpleLog object with logging to a file enabled.
        Parameters:
        filename - file name, or use "-" or null for System.out
      • SimpleLog

        public SimpleLog​(@Nullable java.lang.String filename,
                         boolean enabled)
        Create a new SimpleLog object with logging to a file.
        Parameters:
        filename - file name, or use "-" or null for System.out
        enabled - whether the logger starts out enabled
    • Method Detail

      • enabled

        public boolean enabled()
        Return whether logging is enabled.
        Returns:
        whether logging is enabled
      • setLogfile

        @EnsuresNonNull("logfile")
        private void setLogfile()
        Set the private field logfile (if it is not set), based on the private field filename.

        This creates the file if it does not exist. This should be called lazily, when output is performed. Otherwise, it would be annoying to create a zero-size logfile if no output is ever written.

      • log

        @FormatMethod
        public void log​(java.lang.String format,
                        @Nullable java.lang.Object... args)
        Log a message. The message is prepended with the current indentation string. The indentation is only applied at the start of the message, not for every line break within the message.
        Parameters:
        format - format string for message
        args - values to be substituted into format
      • logStackTrace

        public void logStackTrace()
        Print a stack trace to the log.
      • getIndentString

        private java.lang.String getIndentString()
        Return the current indentation string.
        Returns:
        the current indentation string
      • indent

        public void indent()
        Increases indentation by one level.
      • exdent

        public void exdent()
        Decreases indentation by one level.
      • resetIndent

        public void resetIndent()
        Resets indentation to none. Has no effect if logging is disabled.