KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mortbay > log > LogStream


1 //========================================================================
2
//$Id: LogStream.java,v 1.3 2005/08/13 00:01:27 gregwilkins Exp $
3
//Copyright 2004 Mort Bay Consulting Pty. Ltd.
4
//------------------------------------------------------------------------
5
//Licensed under the Apache License, Version 2.0 (the "License");
6
//you may not use this file except in compliance with the License.
7
//You may obtain a copy of the License at
8
//http://www.apache.org/licenses/LICENSE-2.0
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 implied.
12
//See the License for the specific language governing permissions and
13
//limitations under the License.
14
//========================================================================
15

16 package org.mortbay.log;
17
18 import java.io.PrintStream JavaDoc;
19
20 import org.apache.commons.logging.Log;
21 import org.mortbay.log.LogFactory;
22 import org.mortbay.util.ByteArrayOutputStream2;
23
24 /**
25  * Divert a PrintStream to commons logging.
26  * The stderr and stdout streams can be diverted to logs named "stderr" and "stdout" using this
27  * class.
28  *
29  */

30 public class LogStream extends PrintStream JavaDoc
31 {
32     public static class STDERR extends LogStream
33     {STDERR() {super("STDERR ",LogFactory.getLog("stderr"));}}
34
35     public static class STDOUT extends LogStream
36     {STDOUT() {super("STDOUT ",LogFactory.getLog("stdout"));}}
37
38     /*-------------------------------------------------------------------*/
39     final static PrintStream JavaDoc STDERR_STREAM=System.err;
40     final static PrintStream JavaDoc STDOUT_STREAM=System.out;
41     
42     /* ------------------------------------------------------------ */
43     /** Log standard error stream.
44      * If set to true, output to stderr will be directed to an instance
45      * of LogStream and logged. Beware of log loops from logs that write to stderr.
46      */

47     public static void setLogStdErr(boolean log)
48     {
49         if (log)
50         {
51             if (!(System.err instanceof LogStream))
52                 System.setErr(new LogStream.STDERR());
53         }
54         else
55             System.setErr(STDERR_STREAM);
56     }
57
58     /* ------------------------------------------------------------ */
59     public static boolean getLogStdErr()
60     {
61         return System.err instanceof LogStream;
62     }
63     
64     /* ------------------------------------------------------------ */
65     /** Log standard output stream.
66      * If set to true, output to stdout will be directed to an instance
67      * of LogStream and logged. Beware of log loops from logs that write to stdout.
68      */

69     public static void setLogStdOut(boolean log)
70     {
71         if (log)
72         {
73             if (!(System.out instanceof LogStream))
74                 System.setOut(new LogStream.STDOUT());
75         }
76         else
77             System.setOut(STDOUT_STREAM);
78     }
79
80     /* ------------------------------------------------------------ */
81     public static boolean getLogStdOut()
82     {
83         return System.out instanceof LogStream;
84     }
85
86     /* ------------------------------------------------------------ */
87     private String JavaDoc tag;
88     private Log log;
89     private ByteArrayOutputStream2 bout;
90     
91     /*
92      */

93     public void flush()
94     {
95         super.flush();
96         if (bout.size()>0)
97         {
98             String JavaDoc s=new String JavaDoc(bout.getBuf(),0,bout.size()).trim();
99             if (s.length()>0 && log!=null)
100                 log.info(tag+": "+s);
101         }
102         bout.reset();
103     }
104  
105     /**
106      * @param out
107      * @param autoflush
108      */

109     public LogStream(String JavaDoc tag, Log log)
110     {
111         super(new ByteArrayOutputStream2(128), true);
112         bout=(ByteArrayOutputStream2)this.out;
113         this.tag=tag;
114         this.log=log;
115     }
116     
117     public void close()
118     {
119         flush();
120         super.close();
121     }
122     public void println()
123     {
124         super.println();
125         flush();
126     }
127     public void println(boolean arg0)
128     {
129         super.println(arg0);
130         flush();
131     }
132     public void println(char arg0)
133     {
134         super.println(arg0);
135         flush();
136     }
137     public void println(char[] arg0)
138     {
139         super.println(arg0);
140         flush();
141     }
142     public void println(double arg0)
143     {
144         super.println(arg0);
145         flush();
146     }
147     public void println(float arg0)
148     {
149         super.println(arg0);
150         flush();
151     }
152     public void println(int arg0)
153     {
154         super.println(arg0);
155         flush();
156     }
157     public void println(long arg0)
158     {
159         super.println(arg0);
160         flush();
161     }
162     public void println(Object JavaDoc arg0)
163     {
164         super.println(arg0);
165         flush();
166     }
167     public void println(String JavaDoc arg0)
168     {
169         super.println(arg0);
170         flush();
171     }
172     public void write(byte[] arg0, int arg1, int arg2)
173     {
174         super.write(arg0, arg1, arg2);
175         flush();
176     }
177
178 }
179
Popular Tags