KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.log.util;
18
19 import java.io.EOFException JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.OutputStream JavaDoc;
22 import org.apache.log.Logger;
23 import org.apache.log.Priority;
24
25 /**
26  * Redirect an output stream to a logger.
27  * This class is useful to redirect standard output or
28  * standard error to a Logger. An example use is
29  *
30  * <pre>
31  * final LoggerOutputStream outputStream =
32  * new LoggerOutputStream( logger, Priority.DEBUG );
33  * final PrintStream output = new PrintStream( outputStream, true );
34  *
35  * System.setOut( output );
36  * </pre>
37  *
38  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
39  * @author Peter Donald
40  */

41 public class LoggerOutputStream
42     extends OutputStream JavaDoc
43 {
44     ///Logger that we log to
45
private final Logger m_logger;
46
47     ///Log level we log to
48
private final Priority m_priority;
49
50     ///The buffered output so far
51
private final StringBuffer JavaDoc m_output = new StringBuffer JavaDoc();
52
53     ///Flag set to true once stream closed
54
private boolean m_closed;
55
56     /**
57      * Construct OutputStreamLogger to write to a particular logger at a particular priority.
58      *
59      * @param logger the logger to write to
60      * @param priority the priority at which to log
61      */

62     public LoggerOutputStream( final Logger logger,
63                                final Priority priority )
64     {
65         m_logger = logger;
66         m_priority = priority;
67     }
68
69     /**
70      * Shutdown stream.
71      * @exception IOException if an error occurs while closing the stream
72      */

73     public void close()
74         throws IOException JavaDoc
75     {
76         flush();
77         super.close();
78         m_closed = true;
79     }
80
81     /**
82      * Write a single byte of data to output stream.
83      *
84      * @param data the byte of data
85      * @exception IOException if an error occurs
86      */

87     public void write( final int data )
88         throws IOException JavaDoc
89     {
90         checkValid();
91
92         //Should we properly convert char using locales etc??
93
m_output.append( (char)data );
94
95         if( '\n' == data )
96         {
97             flush();
98         }
99     }
100
101     /**
102      * Flush data to underlying logger.
103      *
104      * @exception IOException if an error occurs
105      */

106     public synchronized void flush()
107         throws IOException JavaDoc
108     {
109         checkValid();
110
111         m_logger.log( m_priority, m_output.toString() );
112         m_output.setLength( 0 );
113     }
114
115     /**
116      * Make sure stream is valid.
117      *
118      * @exception IOException if an error occurs
119      */

120     private void checkValid()
121         throws IOException JavaDoc
122     {
123         if( true == m_closed )
124         {
125             throw new EOFException JavaDoc( "OutputStreamLogger closed" );
126         }
127     }
128 }
129
Popular Tags