1 17 package org.apache.log.util; 18 19 import java.io.EOFException ; 20 import java.io.IOException ; 21 import java.io.OutputStream ; 22 import org.apache.log.Logger; 23 import org.apache.log.Priority; 24 25 41 public class LoggerOutputStream 42 extends OutputStream 43 { 44 private final Logger m_logger; 46 47 private final Priority m_priority; 49 50 private final StringBuffer m_output = new StringBuffer (); 52 53 private boolean m_closed; 55 56 62 public LoggerOutputStream( final Logger logger, 63 final Priority priority ) 64 { 65 m_logger = logger; 66 m_priority = priority; 67 } 68 69 73 public void close() 74 throws IOException 75 { 76 flush(); 77 super.close(); 78 m_closed = true; 79 } 80 81 87 public void write( final int data ) 88 throws IOException 89 { 90 checkValid(); 91 92 m_output.append( (char)data ); 94 95 if( '\n' == data ) 96 { 97 flush(); 98 } 99 } 100 101 106 public synchronized void flush() 107 throws IOException 108 { 109 checkValid(); 110 111 m_logger.log( m_priority, m_output.toString() ); 112 m_output.setLength( 0 ); 113 } 114 115 120 private void checkValid() 121 throws IOException 122 { 123 if( true == m_closed ) 124 { 125 throw new EOFException ( "OutputStreamLogger closed" ); 126 } 127 } 128 } 129 | Popular Tags |