1 8 package org.jivesoftware.util.log.util; 9 10 import org.jivesoftware.util.log.Logger; 11 import org.jivesoftware.util.log.Priority; 12 import java.io.EOFException ; 13 import java.io.IOException ; 14 import java.io.OutputStream ; 15 16 31 public class LoggerOutputStream 32 extends OutputStream { 33 private final Logger m_logger; 35 36 private final Priority m_priority; 38 39 private final StringBuffer m_output = new StringBuffer (); 41 42 private boolean m_closed; 44 45 51 public LoggerOutputStream(final Logger logger, 52 final Priority priority) { 53 m_logger = logger; 54 m_priority = priority; 55 } 56 57 60 public void close() 61 throws IOException { 62 flush(); 63 super.close(); 64 m_closed = true; 65 } 66 67 73 public void write(final int data) 74 throws IOException { 75 checkValid(); 76 77 m_output.append((char)data); 79 80 if ('\n' == data) { 81 flush(); 82 } 83 } 84 85 90 public synchronized void flush() 91 throws IOException { 92 checkValid(); 93 94 m_logger.log(m_priority, m_output.toString()); 95 m_output.setLength(0); 96 } 97 98 103 private void checkValid() 104 throws IOException { 105 if (true == m_closed) { 106 throw new EOFException ("OutputStreamLogger closed"); 107 } 108 } 109 } 110 | Popular Tags |