KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > logging > util > CategoryStream


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.logging.util;
23
24 import java.io.IOException JavaDoc;
25 import java.io.PrintStream JavaDoc;
26
27 import org.apache.log4j.Category;
28 import org.apache.log4j.Priority;
29
30
31 /**
32  * A subclass of PrintStream that redirects its output to a log4j Category.
33  *
34  * <p>This class is used to map PrintStream/PrintWriter oriented logging onto
35  * the log4j Categories. Examples include capturing System.out/System.err
36  *
37  * @deprecated Use {@link LoggerStream} instead.
38  *
39  * @version <tt>$Revision: 1958 $</tt>
40  * @author <a HREF="mailto:Scott.Stark@jboss.org">Scott Stark</a>.
41  * @author <a HREF="mailto:jason@planet57.com">Jason Dillon</a>
42  */

43 public class CategoryStream
44     extends PrintStream JavaDoc
45 {
46    /**
47     * Default flag to enable/disable tracing println calls.
48     * from the system property <tt>org.jboss.logging.util.CategoryStream.trace</tt>
49     * or if not set defaults to <tt>false</tt>.
50     */

51    public static final boolean TRACE =
52       getBoolean(CategoryStream.class.getName() + ".trace", false);
53
54    /** Helper to get boolean value from system property or use default if not set. */
55    private static boolean getBoolean(String JavaDoc name, boolean defaultValue)
56    {
57       String JavaDoc value = System.getProperty(name, null);
58       if (value == null)
59          return defaultValue;
60       return new Boolean JavaDoc(value).booleanValue();
61    }
62    
63    private Category category;
64    private Priority priority;
65    private boolean issuedWarning;
66    
67    /**
68     * Redirect logging to the indicated category using Priority.INFO
69     */

70    public CategoryStream(final Category category)
71    {
72       this(category, Priority.INFO, System.out);
73    }
74     
75    /**
76     * Redirect logging to the indicated category using the given
77     * priority. The ps is simply passed to super but is not used.
78     */

79    public CategoryStream(final Category category,
80                          final Priority priority,
81                          final PrintStream JavaDoc ps)
82    {
83       super(ps);
84       this.category = category;
85       this.priority = priority;
86    }
87     
88    public void println(String JavaDoc msg)
89    {
90       if( msg == null )
91          msg = "null";
92       byte[] bytes = msg.getBytes();
93       write(bytes, 0, bytes.length);
94    }
95     
96    public void println(Object JavaDoc msg)
97    {
98       if( msg == null )
99          msg = "null";
100       byte[] bytes = msg.toString().getBytes();
101       write(bytes, 0, bytes.length);
102    }
103     
104    public void write(byte b)
105    {
106       byte[] bytes = {b};
107       write(bytes, 0, 1);
108    }
109
110    private ThreadLocal JavaDoc recursiveCheck = new ThreadLocal JavaDoc();
111    public void write(byte[] b, int off, int len)
112    {
113       Boolean JavaDoc recursed = (Boolean JavaDoc)recursiveCheck.get();
114       if (recursed != null && recursed.equals(Boolean.TRUE))
115       {
116          /* There is a configuration error that is causing looping. Most
117             likely there are two console appenders so just return to prevent
118             spinning.
119          */

120          if( issuedWarning == false )
121          {
122             String JavaDoc msg = "ERROR: invalid console appender config detected, console stream is looping";
123             try
124             {
125                out.write(msg.getBytes());
126             }
127             catch(IOException JavaDoc ignore)
128             {
129             }
130             issuedWarning = true;
131          }
132          return;
133       }
134
135       // Remove the end of line chars
136
while( len > 0 && (b[len-1] == '\n' || b[len-1] == '\r') && len > off )
137          len --;
138
139       // HACK, something is logging exceptions line by line (including
140
// blanks), but I can't seem to find it, so for now just ignore
141
// empty lines... they aren't very useful.
142
if (len != 0)
143       {
144          String JavaDoc msg = new String JavaDoc(b, off, len);
145          recursiveCheck.set(Boolean.TRUE);
146          if (TRACE)
147          {
148             category.log(priority, msg, new Throwable JavaDoc());
149          }
150          else
151          {
152             category.log(priority, msg);
153          }
154          recursiveCheck.set(Boolean.FALSE);
155       }
156    }
157 }
158
Popular Tags