KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > util > log > util > LoggerOutputStream


1 /*
2  * Copyright (C) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE file.
7  */

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 JavaDoc;
13 import java.io.IOException JavaDoc;
14 import java.io.OutputStream JavaDoc;
15
16 /**
17  * Redirect an output stream to a logger.
18  * This class is useful to redirect standard output or
19  * standard error to a Logger. An example use is
20  * <p/>
21  * <pre>
22  * final LoggerOutputStream outputStream =
23  * new LoggerOutputStream( logger, Priority.DEBUG );
24  * final PrintStream output = new PrintStream( outputStream, true );
25  * <p/>
26  * System.setOut( output );
27  * </pre>
28  *
29  * @author <a HREF="mailto:peter@apache.org">Peter Donald</a>
30  */

31 public class LoggerOutputStream
32         extends OutputStream JavaDoc {
33     ///Logger that we log to
34
private final Logger m_logger;
35
36     ///Log level we log to
37
private final Priority m_priority;
38
39     ///The buffered output so far
40
private final StringBuffer JavaDoc m_output = new StringBuffer JavaDoc();
41
42     ///Flag set to true once stream closed
43
private boolean m_closed;
44
45     /**
46      * Construct OutputStreamLogger to send to a particular logger at a particular priority.
47      *
48      * @param logger the logger to send to
49      * @param priority the priority at which to log
50      */

51     public LoggerOutputStream(final Logger logger,
52                               final Priority priority) {
53         m_logger = logger;
54         m_priority = priority;
55     }
56
57     /**
58      * Shutdown stream.
59      */

60     public void close()
61             throws IOException JavaDoc {
62         flush();
63         super.close();
64         m_closed = true;
65     }
66
67     /**
68      * Write a single byte of data to output stream.
69      *
70      * @param data the byte of data
71      * @throws IOException if an error occurs
72      */

73     public void write(final int data)
74             throws IOException JavaDoc {
75         checkValid();
76
77         //Should we properly convert char using locales etc??
78
m_output.append((char)data);
79
80         if ('\n' == data) {
81             flush();
82         }
83     }
84
85     /**
86      * Flush data to underlying logger.
87      *
88      * @throws IOException if an error occurs
89      */

90     public synchronized void flush()
91             throws IOException JavaDoc {
92         checkValid();
93
94         m_logger.log(m_priority, m_output.toString());
95         m_output.setLength(0);
96     }
97
98     /**
99      * Make sure stream is valid.
100      *
101      * @throws IOException if an error occurs
102      */

103     private void checkValid()
104             throws IOException JavaDoc {
105         if (true == m_closed) {
106             throw new EOFException JavaDoc("OutputStreamLogger closed");
107         }
108     }
109 }
110
Popular Tags