package org.jzonic.jlo.processor;
import org.jzonic.jlo.LogEvent;
import org.jzonic.jlo.LogGenerator;
import org.jzonic.jlo.LogRecord;
import java.util.List;
import java.util.Vector;
/**
* The AsynchronousLogProcessor will process all incoming
* log request inside a separate thread.
*
*@author mecky
*@created 29. Januar 2002
*/
public class AsynchronousLogProcessor extends AbstractLogProcessor implements Runnable {
private static List events = new Vector();
private static long counter = 0;
private static Thread threadCleanerUpper = null;
private boolean running = false;
int milliSecondSleepTime = 500;
/**
* Constructor for the LogHandler object
*/
public AsynchronousLogProcessor() {
running = true;
Thread thread = new Thread(this);
thread.setDaemon(true);
thread.start();
}
public void run() {
LogEvent le;
while ( running ) {
while ( getEvents().iterator().hasNext() ) {
le = getNextRecord();
handlePipes(le);
handle(le);
handleSpecialChannels(le.getLogRecord());
}
// time to sleep.
try {
Thread.sleep(this.milliSecondSleepTime);
}
catch (Exception e) {
// we do nothing here
}
}
}
private synchronized List getEvents() {
return events;
}
/**
* This method adds a logevent to the qeue of the LogHandler.
*
*@param handler the handler for this logevent
*@param formatter the formatter for this logevent (can be null)
*@param lr the LogRecord
*/
public void processEvent(LogGenerator lg,LogRecord lr) {
LogEvent le = new LogEvent(lg.getHandler(), lg.getFormatter(), lr);
if ( lg.getFilter() != null ) {
if ( lg.getFilter().match(lr.getMessage() )) {
getEvents().add(le);
}
}
else {
getEvents().add(le);
}
}
public void flush() {
while ( getEvents().iterator().hasNext() ) {
LogEvent le = getNextRecord();
if (le.getFormatter() != null) {
String msg = le.getFormatter().formatMessage(le.getLogRecord());
le.getHandler().publish(msg);
} else {
le.getHandler().publish(le.getLogRecord());
}
}
}
private synchronized LogEvent getNextRecord() {
LogEvent le = (LogEvent) getEvents().iterator().next();
getEvents().remove(le);
return le;
}
public String getProcessorName() {
return "AsynchronousLogProcessor";
}
}