cdx.scorebot.util.io
Class FlexLogger

java.lang.Object
  extended by java.io.OutputStream
      extended by java.io.FilterOutputStream
          extended by java.io.PrintStream
              extended by cdx.scorebot.util.io.FlexLogger
All Implemented Interfaces:
java.io.Closeable, java.io.Flushable, java.lang.Appendable

public class FlexLogger
extends java.io.PrintStream

Provide easy means of outputting to both the terminal and file(s) at the same time

Change Log:

  • v1.03, 25-MAY-2006: fixed duplicate stream prevention; added method getStreamCount which returns the number of streams currently being logged to.
  • v1.02, 24-MAY-2006: made FlexLogger a PrintStream itself so it could be passed wherever streams can be passed and print to all of its log files.
  • v1.01, 16-MAY-2006: first release

    Author:
    David Underhill

    Field Summary
    static int APPEND
              if file exists, append to the old file
    static boolean DEFAULT_MULTIPLES_ALLOWED
              whether or not multiple instances of the same stream is permitted by default (only blocks the streams for which OutputStream.equals returns true).
    static int DEFAULT_NUMBERING_LEN
              default number of digits to append to filenames being reused by default when renaming files
    static boolean DONT_LOG_TO_CONSOLE
              do not log to the console
    static boolean LOG_TO_CONSOLE
              log to the console
    static int OVERWRITE
              if file exists, overwrite the old file
    static int RENAME
              If the file already exists, the old file will be renamed as oldname.### where ### is the earliest number which constitutes an unused filename; ### is a n-digit number specified by the getNumberingLen (defaults to DEFAULT_NUMBERING_LEN.
     
    Fields inherited from class java.io.FilterOutputStream
    out
     
    Constructor Summary
    FlexLogger()
              Creates a FlexLogger which will log only to the System.out PrintStream
    FlexLogger(boolean sysOut)
              Creates a FlexLogger which will log to the console as specified.
    FlexLogger(java.lang.String filename)
              Creates a FlexLogger which is initialized with the one specified stream to log to.
    FlexLogger(java.lang.String filename, boolean sysOut)
              Creates a FlexLogger which is initialized with the one specified stream to log to with an option to log to the console too.
    FlexLogger(java.lang.String filename, boolean sysOut, int alreadyExistsHandling)
              Creates a FlexLogger which is initialized with the one specified stream to log to with an option to log to the console too; if the file already exists, the alreadyExistsHandling argument species how to handle this.
    FlexLogger(java.lang.String filename, int numDigits, boolean sysOut)
              Creates a FlexLogger which is initialized with the one specified stream to log to with an option to log to the console too; if the file already exists, the old file will be renamed; numDigits indicates how many digits to use when renaming any old file.
     
    Method Summary
     boolean addOutputSource(java.io.PrintStream ps)
              Adds the specified PrintStream to the list of streams to log to
     java.io.PrintStream addOutputSource(java.lang.String filename)
              Opens the specified filename and adds it to the list of streams to log to
     java.io.PrintStream addOutputSource(java.lang.String filename, int alreadyExistsHandling)
              Opens the specified filename and adds it to the list of streams to log to
     void close()
              Close all streams except System.out/err (if either System.out or err is one of the streams in this).
     int getNumberingLen()
              get how many digits will be (at a minimum) in the filename
     int getNumStreams()
              returns the number of active streams
     boolean isAllowDuplicateStreams()
              get whether duplicate streams are allowed
     boolean isPrefixingTimestamp()
              gets whether or not a timestamp will be prefixed to string output
    static boolean isValidFileExistsFlag(int alreadyExistsHandling)
              determine whether or not the flag passed is a valid file exists flag operation
    static void main(java.lang.String[] args)
              See if we can output two places at once
     void prefix()
              Write the current timestamp followed by ': ' to all streams
     void print(boolean b)
              Print a boolean value.
     void print(char c)
              Print a character.
     void print(char[] s)
              Print an array of characters.
     void print(double d)
              Print a double-precision floating-point number.
     void print(float f)
              Print a floating-point number.
     void print(int i)
              Print an integer.
     void print(long l)
              Print a long integer.
     void print(java.lang.Object obj)
              Print an object.
     void print(java.lang.String s)
              Print a string.
     void println()
              Terminate the current line by writing the line separator string.
     void println(boolean x)
              Print a boolean and then terminate the line.
     void println(char x)
              Print a character and then terminate the line.
     void println(char[] x)
              Print an array of characters and then terminate the line.
     void println(double x)
              Print a double and then terminate the line.
     void println(float x)
              Print a float and then terminate the line.
     void println(int x)
              Print an integer and then terminate the line.
     void println(long x)
              Print a long and then terminate the line.
     void println(java.lang.Object x)
              Print an Object and then terminate the line.
     void println(java.lang.String x)
              Print a String and then terminate the line.
     void removeOutputSource(java.io.PrintStream ps)
              Removes all instances of the specified PrintStream from the list of streams to log to
     void removeOutputSource(java.lang.String filename)
              Stops logging to the specified filename (only affects logging streams which were added by filename; if they were added by PrintStream then it must be removed by removeOutputSource(PrintStream))
     void setAllowDuplicateStreams(boolean allowDuplicateStreams)
              set whether duplicate streams are allowed (will only apply to streams added in the future)
     void setNumberingLen(int numberingLen)
              set how many digits to have at a minimum in the filename
     void setPrefixTimestamp(boolean prefixTimestamp)
              sets whether or not a timestamp will be prefixed to string output
     java.lang.String toString()
              Names of the files which this is outputting to
     void write(byte[] buf, int off, int len)
              Write len bytes from the specified byte array starting at offset off to these streams.
     void write(int b)
              Write the specified byte to these streams.
     
    Methods inherited from class java.io.PrintStream
    append, append, append, checkError, clearError, flush, format, format, printf, printf, setError
     
    Methods inherited from class java.io.FilterOutputStream
    write
     
    Methods inherited from class java.lang.Object
    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
     

    Field Detail

    LOG_TO_CONSOLE

    public static final boolean LOG_TO_CONSOLE
    log to the console

    See Also:
    Constant Field Values

    DONT_LOG_TO_CONSOLE

    public static final boolean DONT_LOG_TO_CONSOLE
    do not log to the console

    See Also:
    Constant Field Values

    DEFAULT_NUMBERING_LEN

    public static final int DEFAULT_NUMBERING_LEN
    default number of digits to append to filenames being reused by default when renaming files

    See Also:
    Constant Field Values

    DEFAULT_MULTIPLES_ALLOWED

    public static final boolean DEFAULT_MULTIPLES_ALLOWED
    whether or not multiple instances of the same stream is permitted by default (only blocks the streams for which OutputStream.equals returns true).

    See Also:
    Constant Field Values

    RENAME

    public static final int RENAME
    If the file already exists, the old file will be renamed as oldname.### where ### is the earliest number which constitutes an unused filename; ### is a n-digit number specified by the getNumberingLen (defaults to DEFAULT_NUMBERING_LEN.

    See Also:
    Constant Field Values

    APPEND

    public static final int APPEND
    if file exists, append to the old file

    See Also:
    Constant Field Values

    OVERWRITE

    public static final int OVERWRITE
    if file exists, overwrite the old file

    See Also:
    Constant Field Values
    Constructor Detail

    FlexLogger

    public FlexLogger()
    Creates a FlexLogger which will log only to the System.out PrintStream


    FlexLogger

    public FlexLogger(boolean sysOut)
    Creates a FlexLogger which will log to the console as specified.

    Parameters:
    sysOut - whether or not to log to System.out initially

    FlexLogger

    public FlexLogger(java.lang.String filename)
               throws java.io.FileNotFoundException
    Creates a FlexLogger which is initialized with the one specified stream to log to.

    Parameters:
    filename - The name of the file to log. If the file already exists, the old file will be renamed.
    Throws:
    java.io.FileNotFoundException - filename does not exist or cannot be opened for write
    See Also:
    RENAME

    FlexLogger

    public FlexLogger(java.lang.String filename,
                      boolean sysOut)
               throws java.io.FileNotFoundException
    Creates a FlexLogger which is initialized with the one specified stream to log to with an option to log to the console too.

    Parameters:
    filename - The name of the file to log. If the file already exists, the old file will be renamed.
    sysOut - whether or not to log to System.out initially
    Throws:
    java.io.FileNotFoundException - filename does not exist or cannot be opened for write
    See Also:
    RENAME

    FlexLogger

    public FlexLogger(java.lang.String filename,
                      boolean sysOut,
                      int alreadyExistsHandling)
               throws java.io.FileNotFoundException
    Creates a FlexLogger which is initialized with the one specified stream to log to with an option to log to the console too; if the file already exists, the alreadyExistsHandling argument species how to handle this.

    Parameters:
    filename - The name of the file to log. If the file already exists, the old file will be renamed.
    sysOut - whether or not to log to System.out initially
    alreadyExistsHandling - how to handle the file if it already exists. There are three distinct ways to handle this:
    1) FlexLogger.APPEND
    2) FlexLogger.OVERWRITE
    3) FlexLogger.RENAME
    Throws:
    java.io.FileNotFoundException - filename does not exist or cannot be opened for write

    FlexLogger

    public FlexLogger(java.lang.String filename,
                      int numDigits,
                      boolean sysOut)
               throws java.io.FileNotFoundException
    Creates a FlexLogger which is initialized with the one specified stream to log to with an option to log to the console too; if the file already exists, the old file will be renamed; numDigits indicates how many digits to use when renaming any old file.

    Parameters:
    filename - The name of the file to log. If the file already exists, the old file will be renamed.
    numDigits - how many digits to have at a minimum in the filename
    sysOut - whether or not to log to System.out initially
    Throws:
    java.io.FileNotFoundException - filename does not exist or cannot be opened for write
    See Also:
    APPEND, OVERWRITE, RENAME
    Method Detail

    isValidFileExistsFlag

    public static boolean isValidFileExistsFlag(int alreadyExistsHandling)
    determine whether or not the flag passed is a valid file exists flag operation

    Parameters:
    alreadyExistsHandling - how to handle the file if it already exists. There are three distinct ways to handle this:
    1) FlexLogger.APPEND
    2) FlexLogger.OVERWRITE
    3) FlexLogger.RENAME
    Returns:
    returns true if the flag is valid

    main

    public static void main(java.lang.String[] args)
                     throws java.io.FileNotFoundException
    See if we can output two places at once

    Parameters:
    args - command line arguments
    Throws:
    java.io.FileNotFoundException - filename does not exist or cannot be opened for write

    addOutputSource

    public java.io.PrintStream addOutputSource(java.lang.String filename)
                                        throws java.io.FileNotFoundException
    Opens the specified filename and adds it to the list of streams to log to

    Parameters:
    filename - The name of the file to log. If the file already exists, the old file will be renamed.
    Returns:
    the PrintStream created for the specified filename, or null if not added
    Throws:
    java.io.FileNotFoundException - filename does not exist or cannot be opened for write
    See Also:
    RENAME

    addOutputSource

    public java.io.PrintStream addOutputSource(java.lang.String filename,
                                               int alreadyExistsHandling)
                                        throws java.io.FileNotFoundException
    Opens the specified filename and adds it to the list of streams to log to

    Parameters:
    filename - The name of the file to log
    alreadyExistsHandling - how to handle the file if it already exists.
    Returns:
    the PrintStream created for the specified filename, or null if not added
    Throws:
    java.io.FileNotFoundException - filename does not exist or cannot be opened for write
    See Also:
    APPEND, OVERWRITE, RENAME

    addOutputSource

    public boolean addOutputSource(java.io.PrintStream ps)
    Adds the specified PrintStream to the list of streams to log to

    Parameters:
    ps - the PrintStream to log
    Returns:
    true if and only if the PrintStream passed in and added to the list of streams to be logged to
    See Also:
    isAllowDuplicateStreams()

    removeOutputSource

    public void removeOutputSource(java.lang.String filename)
    Stops logging to the specified filename (only affects logging streams which were added by filename; if they were added by PrintStream then it must be removed by removeOutputSource(PrintStream))

    Parameters:
    filename - the filename of the file to stop logging to (case-sensitive)

    removeOutputSource

    public void removeOutputSource(java.io.PrintStream ps)
    Removes all instances of the specified PrintStream from the list of streams to log to

    Parameters:
    ps - the PrintStream to remove from logging

    isAllowDuplicateStreams

    public boolean isAllowDuplicateStreams()
    get whether duplicate streams are allowed

    Returns:
    true if duplicates allowed

    setAllowDuplicateStreams

    public void setAllowDuplicateStreams(boolean allowDuplicateStreams)
    set whether duplicate streams are allowed (will only apply to streams added in the future)

    Parameters:
    allowDuplicateStreams - true if duplicates allowed

    getNumberingLen

    public int getNumberingLen()
    get how many digits will be (at a minimum) in the filename

    Returns:
    number of digits

    setNumberingLen

    public void setNumberingLen(int numberingLen)
    set how many digits to have at a minimum in the filename

    Parameters:
    numberingLen - number of digits

    getNumStreams

    public int getNumStreams()
    returns the number of active streams

    Returns:
    count of the PrintStream streams being logged to

    isPrefixingTimestamp

    public boolean isPrefixingTimestamp()
    gets whether or not a timestamp will be prefixed to string output

    Returns:
    true if timestamps are to be prefixed

    setPrefixTimestamp

    public void setPrefixTimestamp(boolean prefixTimestamp)
    sets whether or not a timestamp will be prefixed to string output

    Parameters:
    prefixTimestamp - true if timestamps are to be prefixed

    prefix

    public void prefix()
    Write the current timestamp followed by ': ' to all streams


    close

    public void close()
    Close all streams except System.out/err (if either System.out or err is one of the streams in this). This is done by flushing the streams and then closing the underlying output streams.

    Specified by:
    close in interface java.io.Closeable
    Overrides:
    close in class java.io.PrintStream
    See Also:
    OutputStream.close()

    write

    public void write(int b)
    Write the specified byte to these streams. If the byte is a newline and automatic flushing is enabled then the flush method will be invoked.

    Note that the byte is written as given; to write a character that will be translated according to the platform's default character encoding, use the print(char) or println(char) methods.

    Overrides:
    write in class java.io.PrintStream
    Parameters:
    b - The byte to be written
    See Also:
    print(char), println(char)

    write

    public void write(byte[] buf,
                      int off,
                      int len)
    Write len bytes from the specified byte array starting at offset off to these streams. If automatic flushing is enabled then the flush method will be invoked.

    Note that the bytes will be written as given; to write characters that will be translated according to the platform's default character encoding, use the print(char) or println(char) methods.

    Overrides:
    write in class java.io.PrintStream
    Parameters:
    buf - A byte array
    off - Offset from which to start taking bytes
    len - Number of bytes to write

    print

    public void print(boolean b)
    Print a boolean value. The string produced by String.valueOf(boolean) is translated into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the write(int) method.

    Overrides:
    print in class java.io.PrintStream
    Parameters:
    b - The boolean to be printed

    print

    public void print(char c)
    Print a character. The character is translated into one or more bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the write(int) method.

    Overrides:
    print in class java.io.PrintStream
    Parameters:
    c - The char to be printed

    print

    public void print(int i)
    Print an integer. The string produced by String.valueOf(int) is translated into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the write(int) method.

    Overrides:
    print in class java.io.PrintStream
    Parameters:
    i - The int to be printed
    See Also:
    Integer.toString(int)

    print

    public void print(long l)
    Print a long integer. The string produced by String.valueOf(long) is translated into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the write(int) method.

    Overrides:
    print in class java.io.PrintStream
    Parameters:
    l - The long to be printed
    See Also:
    Long.toString(long)

    print

    public void print(float f)
    Print a floating-point number. The string produced by String.valueOf(float) is translated into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the write(int) method.

    Overrides:
    print in class java.io.PrintStream
    Parameters:
    f - The float to be printed
    See Also:
    Float.toString(float)

    print

    public void print(double d)
    Print a double-precision floating-point number. The string produced by String.valueOf(double) is translated into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the write(int) method.

    Overrides:
    print in class java.io.PrintStream
    Parameters:
    d - The double to be printed
    See Also:
    Double.toString(double)

    print

    public void print(char[] s)
    Print an array of characters. The characters are converted into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the write(int) method.

    Overrides:
    print in class java.io.PrintStream
    Parameters:
    s - The array of chars to be printed

    print

    public void print(java.lang.String s)
    Print a string. If the argument is null then the string "null" is printed. Otherwise, the string's characters are converted into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the write(int) method.

    Overrides:
    print in class java.io.PrintStream
    Parameters:
    s - The String to be printed

    print

    public void print(java.lang.Object obj)
    Print an object. The string produced by the String.valueOf(Object) method is translated into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the write(int) method.

    Overrides:
    print in class java.io.PrintStream
    Parameters:
    obj - The Object to be printed
    See Also:
    Object.toString()

    println

    public void println()
    Terminate the current line by writing the line separator string. The line separator string is defined by the system property line.separator, and is not necessarily a single newline character ('\n').

    Overrides:
    println in class java.io.PrintStream

    println

    public void println(boolean x)
    Print a boolean and then terminate the line. This method behaves as though it invokes print(boolean) and then println().

    Overrides:
    println in class java.io.PrintStream
    Parameters:
    x - The boolean to be printed

    println

    public void println(char x)
    Print a character and then terminate the line. This method behaves as though it invokes print(char) and then println().

    Overrides:
    println in class java.io.PrintStream
    Parameters:
    x - The char to be printed.

    println

    public void println(int x)
    Print an integer and then terminate the line. This method behaves as though it invokes print(int) and then println().

    Overrides:
    println in class java.io.PrintStream
    Parameters:
    x - The int to be printed.

    println

    public void println(long x)
    Print a long and then terminate the line. This method behaves as though it invokes print(long) and then println().

    Overrides:
    println in class java.io.PrintStream
    Parameters:
    x - a The long to be printed.

    println

    public void println(float x)
    Print a float and then terminate the line. This method behaves as though it invokes print(float) and then println().

    Overrides:
    println in class java.io.PrintStream
    Parameters:
    x - The float to be printed.

    println

    public void println(double x)
    Print a double and then terminate the line. This method behaves as though it invokes print(double) and then println().

    Overrides:
    println in class java.io.PrintStream
    Parameters:
    x - The double to be printed.

    println

    public void println(char[] x)
    Print an array of characters and then terminate the line. This method behaves as though it invokes print(char[]) and then println().

    Overrides:
    println in class java.io.PrintStream
    Parameters:
    x - an array of chars to print.

    println

    public void println(java.lang.String x)
    Print a String and then terminate the line. This method behaves as though it invokes print(String) and then println().

    Overrides:
    println in class java.io.PrintStream
    Parameters:
    x - The String to be printed.

    println

    public void println(java.lang.Object x)
    Print an Object and then terminate the line. This method behaves as though it invokes print(Object) and then println().

    Overrides:
    println in class java.io.PrintStream
    Parameters:
    x - The Object to be printed.

    toString

    public java.lang.String toString()
    Names of the files which this is outputting to

    Overrides:
    toString in class java.lang.Object
    Returns:
    output file list