package net.xoetrope.optional.log;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import net.xoetrope.debug.XLogWriter;
/**
* A background log writer using a queue to write to the output stream
*/
public class XQueueLogWriter implements XLogWriter, Runnable
{
protected BlockingQueue msqQ;
protected boolean useErrorStream;
/**
* Construct a new writer
*/
public XQueueLogWriter()
{
msqQ = new LinkedBlockingQueue();
new Thread( this ).start();
}
/**
* Set the use of the error stream
*/
public void setStream( boolean isError )
{
useErrorStream = isError;
}
/**
* Write a line to the log
* @param message the line to write
*/
public void writeLine( String message )
{
try {
msqQ.put( message );
}
catch ( InterruptedException ex ) {
ex.printStackTrace();
}
}
public void run()
{
try {
while ( true ) {
if ( useErrorStream )
System.err.println( msqQ.take());
else
System.out.println( msqQ.take());
}
}
catch ( InterruptedException e ) {
e.printStackTrace();
}
}
}