Package net.xoetrope.optional.log

Source Code of net.xoetrope.optional.log.XQueueLogWriter

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();
    }
  }
}
TOP

Related Classes of net.xoetrope.optional.log.XQueueLogWriter

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.