Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
DefaultLogger |
|
| 1.6666666666666667;1.667 |
1 | //////////////////////////////////////////////////////////////////////////////// | |
2 | // checkstyle: Checks Java source code for adherence to a set of rules. | |
3 | // Copyright (C) 2001-2014 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; | |
22 | import java.io.PrintWriter; | |
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 mInfoWriter; | |
48 | /** close info stream after use */ | |
49 | private final boolean mCloseInfo; | |
50 | ||
51 | /** where to write error messages **/ | |
52 | private final PrintWriter 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 aOS, boolean aCloseStreamsAfterUse) | |
62 | { | |
63 | // no need to close aOS twice | |
64 | 583 | this(aOS, aCloseStreamsAfterUse, aOS, false); |
65 | 583 | } |
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 aInfoStream, | |
76 | boolean aCloseInfoAfterUse, | |
77 | OutputStream aErrorStream, | |
78 | boolean aCloseErrorAfterUse) | |
79 | 583 | { |
80 | 583 | mCloseInfo = aCloseInfoAfterUse; |
81 | 583 | mCloseError = aCloseErrorAfterUse; |
82 | 583 | mInfoWriter = new PrintWriter(aInfoStream); |
83 | 583 | mErrorWriter = (aInfoStream == aErrorStream) |
84 | ? mInfoWriter | |
85 | : new PrintWriter(aErrorStream); | |
86 | 583 | } |
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 | 3384 | final SeverityLevel severityLevel = aEvt.getSeverityLevel(); |
97 | 3384 | if (!SeverityLevel.IGNORE.equals(severityLevel)) { |
98 | ||
99 | 3381 | final String fileName = aEvt.getFileName(); |
100 | 3381 | final String message = aEvt.getMessage(); |
101 | ||
102 | // avoid StringBuffer.expandCapacity | |
103 | 3381 | final int bufLen = fileName.length() + message.length() |
104 | + BUFFER_CUSHION; | |
105 | 3381 | final StringBuffer sb = new StringBuffer(bufLen); |
106 | ||
107 | 3381 | sb.append(fileName); |
108 | 3381 | sb.append(':').append(aEvt.getLine()); |
109 | 3381 | if (aEvt.getColumn() > 0) { |
110 | 2619 | sb.append(':').append(aEvt.getColumn()); |
111 | } | |
112 | 3381 | if (SeverityLevel.WARNING.equals(severityLevel)) { |
113 | 1 | sb.append(": warning"); |
114 | } | |
115 | 3381 | sb.append(": ").append(message); |
116 | 3381 | mErrorWriter.println(sb.toString()); |
117 | } | |
118 | 3384 | } |
119 | ||
120 | /** {@inheritDoc} */ | |
121 | public void addException(AuditEvent aEvt, Throwable aThrowable) | |
122 | { | |
123 | 0 | synchronized (mErrorWriter) { |
124 | 0 | mErrorWriter.println("Error auditing " + aEvt.getFileName()); |
125 | 0 | aThrowable.printStackTrace(mErrorWriter); |
126 | 0 | } |
127 | 0 | } |
128 | ||
129 | /** {@inheritDoc} */ | |
130 | public void auditStarted(AuditEvent aEvt) | |
131 | { | |
132 | 0 | mInfoWriter.println("Starting audit..."); |
133 | 0 | } |
134 | ||
135 | /** {@inheritDoc} */ | |
136 | public void fileFinished(AuditEvent aEvt) | |
137 | { | |
138 | 0 | } |
139 | ||
140 | /** {@inheritDoc} */ | |
141 | public void fileStarted(AuditEvent aEvt) | |
142 | { | |
143 | 0 | } |
144 | ||
145 | /** {@inheritDoc} */ | |
146 | public void auditFinished(AuditEvent aEvt) | |
147 | { | |
148 | 583 | mInfoWriter.println("Audit done."); |
149 | 583 | closeStreams(); |
150 | 583 | } |
151 | ||
152 | /** | |
153 | * Flushes the output streams and closes them if needed. | |
154 | */ | |
155 | protected void closeStreams() | |
156 | { | |
157 | 583 | mInfoWriter.flush(); |
158 | 583 | if (mCloseInfo) { |
159 | 583 | mInfoWriter.close(); |
160 | } | |
161 | ||
162 | 583 | mErrorWriter.flush(); |
163 | 583 | if (mCloseError) { |
164 | 0 | mErrorWriter.close(); |
165 | } | |
166 | 583 | } |
167 | } |