SoftNet-Consult Java Utility Library

com.softnetConsult.utils.log
Class ScreenFileLogger

java.lang.Object
  extended by com.softnetConsult.utils.log.ScreenFileLogger

public class ScreenFileLogger
extends java.lang.Object

This class provides a convenient way to direct text output to the screen (std-out) and to a file (file stream) at the same time, in addition it provides an easy way to make sure that all output is indented according to a specifiable level; this makes this class ideal for simple logging and tracking tasks.

This product includes software developed by the SoftNet-Consult Java Utility Library project and its contributors.
(http://java-tools.sourceforge.net)
Copyright (c) 2007-2008 SoftNet-Consult.
Copyright (c) 2007-2008 G. Paperin.
All rights reserved.

File: ScreenFileLogger.java
Library API version: "2.02"
Java compliance version: "1.5"

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following terms and conditions are met:

1. Redistributions of source code must retain the above acknowledgement of the SoftNet-Consult Java Utility Library project, the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above acknowledgement of the SoftNet-Consult Java Utility Library project, the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
3. All advertising materials mentioning features or use of this software or any derived software must display the following acknowledgement:
This product includes software developed by the SoftNet-Consult Java Utility Library project and its contributors.

THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Version:
"2.02"
Author:
Greg Paperin (http://www.paperin.org)

Field Summary
private  boolean fileAppend
          Whether to append or overwrite when output file is (re)opened.
private  java.io.FileOutputStream fileStream
          Output file stream.
private  java.lang.String indentation
          Indentation string (one copy per level).
private  int indentLevel
          Current indentation level.
private  boolean indentNext
          Whether to indent next output.
private  java.io.File logFile
          Output file.
static int MAX_INDENT_LEVEL
          Maximum indentation level permitted.
private  com.softnetConsult.utils.streams.MultiPrintStream outs
          Aggregating output stream.
 
Constructor Summary
ScreenFileLogger(boolean logToScreen, boolean logToFile, java.io.File logFile, boolean append, java.lang.String indentation)
          Creates a logger that can write all logged output to screen (or whatever is the current std-out) and/or to a file.
ScreenFileLogger(boolean logToScreen, boolean logToFile, java.lang.String logFileName, boolean append, java.lang.String indentation)
          Creates a logger that can write all logged output to screen (or whatever is the current std-out) and/or to a file.
 
Method Summary
 void close()
          Closes this logger and a possibly open file stream.
 void decIndentLevel()
          Decrements the current indentation level.
private  void doIndentation()
          Used internally to generate a correct indentation string.
protected  void finalize()
          Ensures that all associated streams are closed when this logger is finalised.
 java.io.File getFile()
          Gets the file associated with this logger (regardless of whether this logger currently logs to a file).
 int getIndentLevel()
          Gets the current indentation level.
 boolean getLogToFile()
          Checks whether this logger currently writes to a file.
 boolean getLogToScreen()
          Checks whether this logger currently writes to the screen (std-out).
 void incIndentLevel()
          Increments the current indentation level.
 void print(boolean val)
          Prints the specified value to std-out and/or file.
 void print(char val)
          Prints the specified value to std-out and/or file.
 void print(char[] s)
          Prints a string to std-out and/or file.
 void print(double val)
          Prints the specified value to std-out and/or file.
 void print(float val)
          Prints the specified value to std-out and/or file.
 void print(int val)
          Prints the specified value to std-out and/or file.
 void print(long val)
          Prints the specified value to std-out and/or file.
 void print(java.lang.Object o)
          Prints an object to std-out and/or file.
 void print(java.lang.String s)
          Prints a string to std-out and/or file.
 void print(java.lang.Throwable t)
          Prints extended information about a Thowable (including stack trace) to std-out and/or file.
 void printf(java.lang.String format, java.lang.Object... args)
          Logs a formated string to std-out and/or file following the format specification defined for String.format(String, Object[]).
 void println()
          Initiates a new line by emitting a new-line character to std-out and/or file.
 void println(boolean val)
          Prints the specified value followed by a new line to std-out and/or file.
 void println(char val)
          Prints the specified value followed by a new line to std-out and/or file.
 void println(char[] s)
          Prints a string followed by a new line to std-out and/or file.
 void println(double val)
          Prints the specified value followed by a new line to std-out and/or file.
 void println(float val)
          Prints the specified value followed by a new line to std-out and/or file.
 void println(int val)
          Prints the specified value followed by a new line to std-out and/or file.
 void println(long val)
          Prints the specified value followed by a new line to std-out and/or file.
 void println(java.lang.Object o)
          Prints an object followed by a new line to std-out and/or file.
 void println(java.lang.String s)
          Prints a string followed by a new line to std-out and/or file.
 void println(java.lang.Throwable t)
          Prints extended information about a Thowable (including stack trace) followed by a new line to std-out and/or file.
 void setIndentLevel(int indentLevel)
          Sets the indentation level to the specicied value.
 void setLogToFile(boolean logToFile)
          Sets whether this logger will write to a for the subsequent print-actions.
 void setLogToScreen(boolean logToScreen)
          Sets whether this logger will write to the screen (std-out) for the subsequent print-actions.
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MAX_INDENT_LEVEL

public static final int MAX_INDENT_LEVEL
Maximum indentation level permitted.

See Also:
Constant Field Values

indentation

private java.lang.String indentation
Indentation string (one copy per level).


indentNext

private boolean indentNext
Whether to indent next output.


indentLevel

private int indentLevel
Current indentation level.


logFile

private java.io.File logFile
Output file.


fileStream

private java.io.FileOutputStream fileStream
Output file stream.


fileAppend

private boolean fileAppend
Whether to append or overwrite when output file is (re)opened.


outs

private com.softnetConsult.utils.streams.MultiPrintStream outs
Aggregating output stream.

Constructor Detail

ScreenFileLogger

public ScreenFileLogger(boolean logToScreen,
                        boolean logToFile,
                        java.lang.String logFileName,
                        boolean append,
                        java.lang.String indentation)
                 throws java.io.FileNotFoundException
Creates a logger that can write all logged output to screen (or whatever is the current std-out) and/or to a file.

Parameters:
logToScreen - Whether to start logging to std-out (can be changed later).
logToFile - Whether to start logging to a file (can be changed later if file is specified).
logFileName - File name to write to.
append - Append or overwrite the file.
indentation - Indentation string (one copy per indentation level).
Throws:
java.io.FileNotFoundException - If there is a problem when opening the specified file for writing.

ScreenFileLogger

public ScreenFileLogger(boolean logToScreen,
                        boolean logToFile,
                        java.io.File logFile,
                        boolean append,
                        java.lang.String indentation)
                 throws java.io.FileNotFoundException
Creates a logger that can write all logged output to screen (or whatever is the current std-out) and/or to a file.

Parameters:
logToScreen - Whether to start logging to std-out (can be changed later).
logToFile - Whether to start logging to a file (can be changed later if file is specified).
logFile - File to write to.
append - Append or overwrite the file.
indentation - Indentation string (one copy per indentation level).
Throws:
java.io.FileNotFoundException - If there is a problem when opening the specified file for writing.
Method Detail

getFile

public java.io.File getFile()
Gets the file associated with this logger (regardless of whether this logger currently logs to a file).

Returns:
File associated with this logger.

getLogToScreen

public boolean getLogToScreen()
Checks whether this logger currently writes to the screen (std-out).

Returns:
Whether this logger currently writes to the screen (std-out).

setLogToScreen

public void setLogToScreen(boolean logToScreen)
Sets whether this logger will write to the screen (std-out) for the subsequent print-actions.

Parameters:
logToScreen - Whther to write to System.out or not.

getLogToFile

public boolean getLogToFile()
Checks whether this logger currently writes to a file.

Returns:
Whether this logger currently writes to a file.

setLogToFile

public void setLogToFile(boolean logToFile)
                  throws java.io.FileNotFoundException
Sets whether this logger will write to a for the subsequent print-actions. The file must be specified in the constructor, if it was set to null, an attempt to turn file-logging on will raise an IllegalArgumentException.
If file-writing is being turned on and this logger has not previously written to a file, the data will be overwritten or appended according to the append parameter in the constructor.
If the file-writing was on and is then turned off, the file is closed.
If the file-writing is subsequently turned on again, the file is re-opened and data is appended at the end.

Parameters:
logToFile - Whether to log to a file or not.
Throws:
java.io.FileNotFoundException - If there is a problem when opening the file.

close

public void close()
Closes this logger and a possibly open file stream.


finalize

protected void finalize()
                 throws java.lang.Throwable
Ensures that all associated streams are closed when this logger is finalised.

Overrides:
finalize in class java.lang.Object
Throws:
java.lang.Throwable

setIndentLevel

public void setIndentLevel(int indentLevel)
Sets the indentation level to the specicied value. Each line following a new-line will begin with indentLevel copies of the indentation-string.

Parameters:
indentLevel - The new indentation level.

getIndentLevel

public int getIndentLevel()
Gets the current indentation level.

Returns:
The current indentation level.

incIndentLevel

public void incIndentLevel()
Increments the current indentation level. Each line following a new-line will begin with indentLevel copies of the indentation-string.


decIndentLevel

public void decIndentLevel()
Decrements the current indentation level. Each line following a new-line will begin with indentLevel copies of the indentation-string.


doIndentation

private void doIndentation()
Used internally to generate a correct indentation string.


printf

public void printf(java.lang.String format,
                   java.lang.Object... args)
Logs a formated string to std-out and/or file following the format specification defined for String.format(String, Object[]).

Parameters:
format - The format string.
args - The value arguments.

print

public void print(java.lang.Object o)
Prints an object to std-out and/or file.

Parameters:
o - Object to print.

print

public void print(java.lang.Throwable t)
Prints extended information about a Thowable (including stack trace) to std-out and/or file.

Parameters:
t - Object to print.

print

public void print(java.lang.String s)
Prints a string to std-out and/or file.

Parameters:
s - String to print.

print

public void print(char[] s)
Prints a string to std-out and/or file.

Parameters:
s - String to print.

print

public void print(boolean val)
Prints the specified value to std-out and/or file.

Parameters:
val - The value to print.

print

public void print(char val)
Prints the specified value to std-out and/or file.

Parameters:
val - The value to print.

print

public void print(int val)
Prints the specified value to std-out and/or file.

Parameters:
val - The value to print.

print

public void print(long val)
Prints the specified value to std-out and/or file.

Parameters:
val - The value to print.

print

public void print(float val)
Prints the specified value to std-out and/or file.

Parameters:
val - The value to print.

print

public void print(double val)
Prints the specified value to std-out and/or file.

Parameters:
val - The value to print.

println

public void println()
Initiates a new line by emitting a new-line character to std-out and/or file. Next output will be indented accoring to the current indentation level.


println

public void println(java.lang.Object o)
Prints an object followed by a new line to std-out and/or file. Next output will be indented accoring to the current indentation level.

Parameters:
o - Object to print.

println

public void println(java.lang.Throwable t)
Prints extended information about a Thowable (including stack trace) followed by a new line to std-out and/or file. Next output will be indented accoring to the current indentation level.

Parameters:
t - Object to print.

println

public void println(java.lang.String s)
Prints a string followed by a new line to std-out and/or file. Next output will be indented accoring to the current indentation level.

Parameters:
s - Object to print.

println

public void println(char[] s)
Prints a string followed by a new line to std-out and/or file. Next output will be indented accoring to the current indentation level.

Parameters:
s - Object to print.

println

public void println(boolean val)
Prints the specified value followed by a new line to std-out and/or file. Next output will be indented accoring to the current indentation level.

Parameters:
val - Value to print.

println

public void println(char val)
Prints the specified value followed by a new line to std-out and/or file. Next output will be indented accoring to the current indentation level.

Parameters:
val - Value to print.

println

public void println(int val)
Prints the specified value followed by a new line to std-out and/or file. Next output will be indented accoring to the current indentation level.

Parameters:
val - Value to print.

println

public void println(long val)
Prints the specified value followed by a new line to std-out and/or file. Next output will be indented accoring to the current indentation level.

Parameters:
val - Value to print.

println

public void println(float val)
Prints the specified value followed by a new line to std-out and/or file. Next output will be indented accoring to the current indentation level.

Parameters:
val - Value to print.

println

public void println(double val)
Prints the specified value followed by a new line to std-out and/or file. Next output will be indented accoring to the current indentation level.

Parameters:
val - Value to print.

SoftNet-Consult Java Utility Library is a member of SourceForge.net