1 17 package org.apache.log.output; 18 19 import java.util.LinkedList ; 20 import org.apache.log.ErrorAware; 21 import org.apache.log.ErrorHandler; 22 import org.apache.log.LogEvent; 23 import org.apache.log.LogTarget; 24 25 43 public class AsyncLogTarget 44 extends AbstractWrappingTarget 45 implements Runnable 46 { 47 private final LinkedList m_list; 48 private final int m_queueSize; 49 50 54 public AsyncLogTarget( final LogTarget logTarget ) 55 { 56 this( logTarget, 15 ); 57 } 58 59 64 public AsyncLogTarget( final LogTarget logTarget, final int queueSize ) 65 { 66 this( logTarget, queueSize, false ); 67 } 68 69 75 public AsyncLogTarget( final LogTarget logTarget, final boolean closeTarget ) 76 { 77 this( logTarget, 15, closeTarget ); 78 } 79 80 87 public AsyncLogTarget( final LogTarget logTarget, final int queueSize, final boolean closeTarget ) 88 { 89 super( logTarget, closeTarget ); 90 m_list = new LinkedList (); 91 m_queueSize = queueSize; 92 open(); 93 } 94 95 100 public synchronized void setErrorHandler( final ErrorHandler errorHandler ) 101 { 102 super.setErrorHandler( errorHandler ); 103 104 if( this.getLogTarget() instanceof ErrorAware ) 105 { 106 ( (ErrorAware)this.getLogTarget() ).setErrorHandler( errorHandler ); 107 } 108 } 109 110 115 public void doProcessEvent( final LogEvent event ) 116 { 117 synchronized( m_list ) 118 { 119 int size = m_list.size(); 120 while( m_queueSize <= size ) 121 { 122 try 123 { 124 m_list.wait(); 125 } 126 catch( final InterruptedException ie ) 127 { 128 } 132 size = m_list.size(); 133 } 134 135 m_list.addFirst( event ); 136 137 if( size == 0 ) 138 { 139 m_list.notify(); 142 } 143 } 144 } 145 146 149 public void run() 150 { 151 boolean interupted = false; 154 155 while( true ) 156 { 157 LogEvent event = null; 158 159 synchronized( m_list ) 160 { 161 while( null == event ) 162 { 163 final int size = m_list.size(); 164 165 if( size > 0 ) 166 { 167 event = (LogEvent)m_list.removeLast(); 168 169 if( size == m_queueSize ) 170 { 171 m_list.notify(); 174 } 175 176 } 177 else if( interupted || Thread.interrupted() ) 178 { 179 return; 182 } 183 else 184 { 185 try 186 { 187 m_list.wait(); 188 } 189 catch( final InterruptedException ie ) 190 { 191 interupted = true; 194 } 195 } 196 } 197 } 198 199 try 200 { 201 this.getLogTarget().processEvent( event ); 203 } 204 catch( final Throwable throwable ) 205 { 206 getErrorHandler().error( "Unknown error writing event.", throwable, event ); 207 } 208 } 209 } 210 } 211 | Popular Tags |