KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > puppycrawl > tools > checkstyle > DefaultLogger


1 ////////////////////////////////////////////////////////////////////////////////
2
// checkstyle: Checks Java source code for adherence to a set of rules.
3
// Copyright (C) 2001-2005 Oliver Burn
4
//
5
// This library is free software; you can redistribute it and/or
6
// modify it under the terms of the GNU Lesser General Public
7
// License as published by the Free Software Foundation; either
8
// version 2.1 of the License, or (at your option) any later version.
9
//
10
// This library is distributed in the hope that it will be useful,
11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
// Lesser General Public License for more details.
14
//
15
// You should have received a copy of the GNU Lesser General Public
16
// License along with this library; if not, write to the Free Software
17
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
////////////////////////////////////////////////////////////////////////////////
19
package com.puppycrawl.tools.checkstyle;
20
21 import java.io.OutputStream JavaDoc;
22 import java.io.PrintWriter JavaDoc;
23
24 import com.puppycrawl.tools.checkstyle.api.AuditEvent;
25 import com.puppycrawl.tools.checkstyle.api.AuditListener;
26 import com.puppycrawl.tools.checkstyle.api.AutomaticBean;
27 import com.puppycrawl.tools.checkstyle.api.SeverityLevel;
28
29 /**
30  * Simple plain logger for text output.
31  * This is maybe not very suitable for a text output into a file since it
32  * does not need all 'audit finished' and so on stuff, but it looks good on
33  * stdout anyway. If there is really a problem this is what XMLLogger is for.
34  * It gives structure.
35  *
36  * @author <a HREF="mailto:stephane.bailliez@wanadoo.fr">Stephane Bailliez</a>
37  * @see XMLLogger
38  */

39 public class DefaultLogger
40     extends AutomaticBean
41     implements AuditListener
42 {
43     /** cushion for avoiding StringBuffer.expandCapacity */
44     private static final int BUFFER_CUSHION = 12;
45
46     /** where to write info messages **/
47     private final PrintWriter JavaDoc mInfoWriter;
48     /** close info stream after use */
49     private final boolean mCloseInfo;
50
51     /** where to write error messages **/
52     private final PrintWriter JavaDoc mErrorWriter;
53     /** close error stream after use */
54     private final boolean mCloseError;
55
56     /**
57      * Creates a new <code>DefaultLogger</code> instance.
58      * @param aOS where to log infos and errors
59      * @param aCloseStreamsAfterUse if aOS should be closed in auditFinished()
60      */

61     public DefaultLogger(OutputStream JavaDoc aOS, boolean aCloseStreamsAfterUse)
62     {
63         // no need to close aOS twice
64
this(aOS, aCloseStreamsAfterUse, aOS, false);
65     }
66
67     /**
68      * Creates a new <code>DefaultLogger</code> instance.
69      *
70      * @param aInfoStream the <code>OutputStream</code> for info messages
71      * @param aCloseInfoAfterUse auditFinished should close aInfoStream
72      * @param aErrorStream the <code>OutputStream</code> for error messages
73      * @param aCloseErrorAfterUse auditFinished should close aErrorStream
74      */

75     public DefaultLogger(OutputStream JavaDoc aInfoStream,
76                          boolean aCloseInfoAfterUse,
77                          OutputStream JavaDoc aErrorStream,
78                          boolean aCloseErrorAfterUse)
79     {
80         mCloseInfo = aCloseInfoAfterUse;
81         mCloseError = aCloseErrorAfterUse;
82         mInfoWriter = new PrintWriter JavaDoc(aInfoStream);
83         mErrorWriter = (aInfoStream == aErrorStream)
84             ? mInfoWriter
85             : new PrintWriter JavaDoc(aErrorStream);
86     }
87
88     /**
89      * Print an Emacs compliant line on the error stream.
90      * If the column number is non zero, then also display it.
91      * @param aEvt {@inheritDoc}
92      * @see AuditListener
93      **/

94     public void addError(AuditEvent aEvt)
95     {
96         final SeverityLevel severityLevel = aEvt.getSeverityLevel();
97         if (!SeverityLevel.IGNORE.equals(severityLevel)) {
98
99             final String JavaDoc fileName = aEvt.getFileName();
100             final String JavaDoc message = aEvt.getMessage();
101
102             // avoid StringBuffer.expandCapacity
103
final int bufLen = fileName.length() + message.length()
104                 + BUFFER_CUSHION;
105             final StringBuffer JavaDoc sb = new StringBuffer JavaDoc(bufLen);
106
107             sb.append(fileName);
108             sb.append(':').append(aEvt.getLine());
109             if (aEvt.getColumn() > 0) {
110                 sb.append(':').append(aEvt.getColumn());
111             }
112             if (SeverityLevel.WARNING.equals(severityLevel)) {
113                 sb.append(": warning");
114             }
115             sb.append(": ").append(message);
116             mErrorWriter.println(sb.toString());
117         }
118     }
119
120     /** {@inheritDoc} */
121     public void addException(AuditEvent aEvt, Throwable JavaDoc aThrowable)
122     {
123         synchronized (mErrorWriter) {
124             mErrorWriter.println("Error auditing " + aEvt.getFileName());
125             aThrowable.printStackTrace(mErrorWriter);
126         }
127     }
128
129     /** {@inheritDoc} */
130     public void auditStarted(AuditEvent aEvt)
131     {
132         mInfoWriter.println("Starting audit...");
133     }
134
135     /** {@inheritDoc} */
136     public void fileFinished(AuditEvent aEvt)
137     {
138     }
139
140     /** {@inheritDoc} */
141     public void fileStarted(AuditEvent aEvt)
142     {
143     }
144
145     /** {@inheritDoc} */
146     public void auditFinished(AuditEvent aEvt)
147     {
148         mInfoWriter.println("Audit done.");
149         closeStreams();
150     }
151
152     /**
153      * Flushes the output streams and closes them if needed.
154      */

155     protected void closeStreams()
156     {
157         mInfoWriter.flush();
158         if (mCloseInfo) {
159             mInfoWriter.close();
160         }
161
162         mErrorWriter.flush();
163         if (mCloseError) {
164             mErrorWriter.close();
165         }
166     }
167 }
168
Popular Tags