com.bluemarsh.jswat
Class Log

java.lang.Object
  |
  +--com.bluemarsh.jswat.Log
All Implemented Interfaces:
java.lang.Runnable

public class Log
extends java.lang.Object
implements java.lang.Runnable

Class for providing a general logging facility. An instance of this class be attached to multiple OutputStreams or Writers for the log messages to be sent to. This object is synchronized in various places to ensure only reasonable output is generated.

The Log class can automatically flush the buffer contents to the attach streams and writers if its start() method is called. This will create a flusher thread that flushes the buffer whenever something is written to the Log. Alternatively, the buffer can be flushed manually by calling flush().

Author:
Nathan Fiedler

Field Summary
protected  java.lang.Object bufferLock
          Object used to synchronize access to the logBuffer reference.
protected static int DEFAULT_BUFFER_LEN
          Default length for the log buffer.
protected  java.lang.Object flushLock
          Object used to synchronize flushing the buffer.
protected  java.lang.StringBuffer logBuffer
          The messages pending writing to the writers and output streams.
protected  java.util.List oslist
          List of all the OutputStream objects.
protected  java.lang.Thread runningThread
          Thread that handles the flushing of the buffer.
protected  java.util.List wlist
          List of all the Writer objects.
 
Constructor Summary
Log()
          Constructs a Log object.
 
Method Summary
 void attach(java.io.OutputStream os)
          Attach to an output stream in order to write log messages to it.
 void attach(java.io.Writer w)
          Attach to a writer in order to write log messages to it.
 void detach(java.io.OutputStream os)
          Detach an output stream from this log.
 void detach(java.io.Writer w)
          Remove a writer from this log.
 boolean flush()
          Takes the log buffer and writes its contents to the writers.
 void run()
          Run the buffer flusher.
 void start()
          Start the Log flusher thread, using normal thread priority.
 void start(int priority)
          Start the Log flusher thread with the given thread priority.
 void stop()
          Stop the flusher thread synchronously.
 void write(java.lang.String s)
          Write a string to the log (without a linefeed).
 void writeln(java.lang.String s)
          Write a string to the log (with a linefeed).
 void writeStackTrace(java.lang.Throwable t)
          Write the given throwable's stack trace to the Log.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_BUFFER_LEN

protected static final int DEFAULT_BUFFER_LEN
Default length for the log buffer.

oslist

protected java.util.List oslist
List of all the OutputStream objects.

wlist

protected java.util.List wlist
List of all the Writer objects.

logBuffer

protected java.lang.StringBuffer logBuffer
The messages pending writing to the writers and output streams.

bufferLock

protected java.lang.Object bufferLock
Object used to synchronize access to the logBuffer reference. Must acquire this lock before accessing the logBuffer _reference_ itself. The log buffer gets reallocated and reassigned to the field variable over and over, so this lock is used to make sure methods are not using a stale reference to the buffer.

flushLock

protected java.lang.Object flushLock
Object used to synchronize flushing the buffer.

runningThread

protected java.lang.Thread runningThread
Thread that handles the flushing of the buffer.
Constructor Detail

Log

public Log()
Constructs a Log object. Note that the Log will not flush the buffer contents automatically until the start() method is called.
Method Detail

attach

public void attach(java.io.OutputStream os)
Attach to an output stream in order to write log messages to it.
Parameters:
os - output stream to write to

attach

public void attach(java.io.Writer w)
Attach to a writer in order to write log messages to it.
Parameters:
w - writer to write to

detach

public void detach(java.io.OutputStream os)
Detach an output stream from this log. No more log messages will go to this stream.
Parameters:
os - output stream to detach from

detach

public void detach(java.io.Writer w)
Remove a writer from this log. No more log messages will go to this writer.
Parameters:
w - writer to detach from

flush

public boolean flush()
Takes the log buffer and writes its contents to the writers. This is multi-thread safe in that it makes a copy of the buffer before writing to the writers and output streams.
Returns:
true if something was flushed, false if buffer was empty.

run

public void run()
Run the buffer flusher. It is important that this run continuously in its own thread, in order to avoid problems with those blasted piped readers and writers.
Specified by:
run in interface java.lang.Runnable

start

public void start(int priority)
Start the Log flusher thread with the given thread priority.
Parameters:
priority - Thread priority.
See Also:
stop()

start

public void start()
Start the Log flusher thread, using normal thread priority.
See Also:
stop()

stop

public void stop()
Stop the flusher thread synchronously. This method waits until the flusher thread has actually stopped.

write

public void write(java.lang.String s)
Write a string to the log (without a linefeed).
Parameters:
s - string to write to log

writeln

public void writeln(java.lang.String s)
Write a string to the log (with a linefeed).
Parameters:
s - string to write to log

writeStackTrace

public void writeStackTrace(java.lang.Throwable t)
Write the given throwable's stack trace to the Log.
Parameters:
t - throwable to print stack for.