-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
DefaultLogger.java
executable file
·167 lines (149 loc) · 5.55 KB
/
DefaultLogger.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
////////////////////////////////////////////////////////////////////////////////
// checkstyle: Checks Java source code for adherence to a set of rules.
// Copyright (C) 2001-2013 Oliver Burn
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
////////////////////////////////////////////////////////////////////////////////
package com.puppycrawl.tools.checkstyle;
import java.io.OutputStream;
import java.io.PrintWriter;
import com.puppycrawl.tools.checkstyle.api.AuditEvent;
import com.puppycrawl.tools.checkstyle.api.AuditListener;
import com.puppycrawl.tools.checkstyle.api.AutomaticBean;
import com.puppycrawl.tools.checkstyle.api.SeverityLevel;
/**
* Simple plain logger for text output.
* This is maybe not very suitable for a text output into a file since it
* does not need all 'audit finished' and so on stuff, but it looks good on
* stdout anyway. If there is really a problem this is what XMLLogger is for.
* It gives structure.
*
* @author <a href="mailto:stephane.bailliez@wanadoo.fr">Stephane Bailliez</a>
* @see XMLLogger
*/
public class DefaultLogger
extends AutomaticBean
implements AuditListener
{
/** cushion for avoiding StringBuffer.expandCapacity */
private static final int BUFFER_CUSHION = 12;
/** where to write info messages **/
private final PrintWriter mInfoWriter;
/** close info stream after use */
private final boolean mCloseInfo;
/** where to write error messages **/
private final PrintWriter mErrorWriter;
/** close error stream after use */
private final boolean mCloseError;
/**
* Creates a new <code>DefaultLogger</code> instance.
* @param aOS where to log infos and errors
* @param aCloseStreamsAfterUse if aOS should be closed in auditFinished()
*/
public DefaultLogger(OutputStream aOS, boolean aCloseStreamsAfterUse)
{
// no need to close aOS twice
this(aOS, aCloseStreamsAfterUse, aOS, false);
}
/**
* Creates a new <code>DefaultLogger</code> instance.
*
* @param aInfoStream the <code>OutputStream</code> for info messages
* @param aCloseInfoAfterUse auditFinished should close aInfoStream
* @param aErrorStream the <code>OutputStream</code> for error messages
* @param aCloseErrorAfterUse auditFinished should close aErrorStream
*/
public DefaultLogger(OutputStream aInfoStream,
boolean aCloseInfoAfterUse,
OutputStream aErrorStream,
boolean aCloseErrorAfterUse)
{
mCloseInfo = aCloseInfoAfterUse;
mCloseError = aCloseErrorAfterUse;
mInfoWriter = new PrintWriter(aInfoStream);
mErrorWriter = (aInfoStream == aErrorStream)
? mInfoWriter
: new PrintWriter(aErrorStream);
}
/**
* Print an Emacs compliant line on the error stream.
* If the column number is non zero, then also display it.
* @param aEvt {@inheritDoc}
* @see AuditListener
**/
public void addError(AuditEvent aEvt)
{
final SeverityLevel severityLevel = aEvt.getSeverityLevel();
if (!SeverityLevel.IGNORE.equals(severityLevel)) {
final String fileName = aEvt.getFileName();
final String message = aEvt.getMessage();
// avoid StringBuffer.expandCapacity
final int bufLen = fileName.length() + message.length()
+ BUFFER_CUSHION;
final StringBuffer sb = new StringBuffer(bufLen);
sb.append(fileName);
sb.append(':').append(aEvt.getLine());
if (aEvt.getColumn() > 0) {
sb.append(':').append(aEvt.getColumn());
}
if (SeverityLevel.WARNING.equals(severityLevel)) {
sb.append(": warning");
}
sb.append(": ").append(message);
mErrorWriter.println(sb.toString());
}
}
/** {@inheritDoc} */
public void addException(AuditEvent aEvt, Throwable aThrowable)
{
synchronized (mErrorWriter) {
mErrorWriter.println("Error auditing " + aEvt.getFileName());
aThrowable.printStackTrace(mErrorWriter);
}
}
/** {@inheritDoc} */
public void auditStarted(AuditEvent aEvt)
{
mInfoWriter.println("Starting audit...");
}
/** {@inheritDoc} */
public void fileFinished(AuditEvent aEvt)
{
}
/** {@inheritDoc} */
public void fileStarted(AuditEvent aEvt)
{
}
/** {@inheritDoc} */
public void auditFinished(AuditEvent aEvt)
{
mInfoWriter.println("Audit done.");
closeStreams();
}
/**
* Flushes the output streams and closes them if needed.
*/
protected void closeStreams()
{
mInfoWriter.flush();
if (mCloseInfo) {
mInfoWriter.close();
}
mErrorWriter.flush();
if (mCloseError) {
mErrorWriter.close();
}
}
}