1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.puppycrawl.tools.checkstyle;
21
22 import java.io.OutputStream;
23 import java.io.OutputStreamWriter;
24 import java.io.PrintWriter;
25 import java.io.Writer;
26 import java.nio.charset.StandardCharsets;
27
28 import com.puppycrawl.tools.checkstyle.api.AuditEvent;
29 import com.puppycrawl.tools.checkstyle.api.AuditListener;
30 import com.puppycrawl.tools.checkstyle.api.AutomaticBean;
31 import com.puppycrawl.tools.checkstyle.api.LocalizedMessage;
32 import com.puppycrawl.tools.checkstyle.api.SeverityLevel;
33
34
35
36
37
38
39
40
41
42
43
44 public class DefaultLogger extends AutomaticBean implements AuditListener {
45
46
47
48
49
50 public static final String ADD_EXCEPTION_MESSAGE = "DefaultLogger.addException";
51
52
53
54
55 public static final String AUDIT_STARTED_MESSAGE = "DefaultLogger.auditStarted";
56
57
58
59
60 public static final String AUDIT_FINISHED_MESSAGE = "DefaultLogger.auditFinished";
61
62
63 private final PrintWriter infoWriter;
64
65 private final boolean closeInfo;
66
67
68 private final PrintWriter errorWriter;
69
70 private final boolean closeError;
71
72
73 private final AuditEventFormatter formatter;
74
75
76
77
78
79
80
81
82 @Deprecated
83 public DefaultLogger(OutputStream outputStream, boolean closeStreamsAfterUse) {
84
85 this(outputStream, closeStreamsAfterUse, outputStream, false);
86 }
87
88
89
90
91
92
93
94
95
96
97 @Deprecated
98 public DefaultLogger(OutputStream infoStream,
99 boolean closeInfoAfterUse,
100 OutputStream errorStream,
101 boolean closeErrorAfterUse) {
102 this(infoStream, closeInfoAfterUse, errorStream, closeErrorAfterUse,
103 new AuditEventDefaultFormatter());
104 }
105
106
107
108
109
110
111
112
113
114
115
116
117 @Deprecated
118 public DefaultLogger(OutputStream infoStream,
119 boolean closeInfoAfterUse,
120 OutputStream errorStream,
121 boolean closeErrorAfterUse,
122 AuditEventFormatter messageFormatter) {
123 closeInfo = closeInfoAfterUse;
124 closeError = closeErrorAfterUse;
125 final Writer infoStreamWriter = new OutputStreamWriter(infoStream, StandardCharsets.UTF_8);
126 infoWriter = new PrintWriter(infoStreamWriter);
127
128 if (infoStream == errorStream) {
129 errorWriter = infoWriter;
130 }
131 else {
132 final Writer errorStreamWriter = new OutputStreamWriter(errorStream,
133 StandardCharsets.UTF_8);
134 errorWriter = new PrintWriter(errorStreamWriter);
135 }
136 formatter = messageFormatter;
137 }
138
139
140
141
142
143
144 public DefaultLogger(OutputStream outputStream, OutputStreamOptions outputStreamOptions) {
145
146 this(outputStream, outputStreamOptions, outputStream, OutputStreamOptions.NONE);
147 }
148
149
150
151
152
153
154
155
156 public DefaultLogger(OutputStream infoStream,
157 OutputStreamOptions infoStreamOptions,
158 OutputStream errorStream,
159 OutputStreamOptions errorStreamOptions) {
160 this(infoStream, infoStreamOptions, errorStream, errorStreamOptions,
161 new AuditEventDefaultFormatter());
162 }
163
164
165
166
167
168
169
170
171
172
173
174 public DefaultLogger(OutputStream infoStream,
175 OutputStreamOptions infoStreamOptions,
176 OutputStream errorStream,
177 OutputStreamOptions errorStreamOptions,
178 AuditEventFormatter messageFormatter) {
179 if (infoStreamOptions == null) {
180 throw new IllegalArgumentException("Parameter infoStreamOptions can not be null");
181 }
182 closeInfo = infoStreamOptions == OutputStreamOptions.CLOSE;
183 if (errorStreamOptions == null) {
184 throw new IllegalArgumentException("Parameter errorStreamOptions can not be null");
185 }
186 closeError = errorStreamOptions == OutputStreamOptions.CLOSE;
187 final Writer infoStreamWriter = new OutputStreamWriter(infoStream, StandardCharsets.UTF_8);
188 infoWriter = new PrintWriter(infoStreamWriter);
189
190 if (infoStream == errorStream) {
191 errorWriter = infoWriter;
192 }
193 else {
194 final Writer errorStreamWriter = new OutputStreamWriter(errorStream,
195 StandardCharsets.UTF_8);
196 errorWriter = new PrintWriter(errorStreamWriter);
197 }
198 formatter = messageFormatter;
199 }
200
201 @Override
202 protected void finishLocalSetup() {
203
204 }
205
206
207
208
209
210
211 @Override
212 public void addError(AuditEvent event) {
213 final SeverityLevel severityLevel = event.getSeverityLevel();
214 if (severityLevel != SeverityLevel.IGNORE) {
215 final String errorMessage = formatter.format(event);
216 errorWriter.println(errorMessage);
217 }
218 }
219
220 @Override
221 public void addException(AuditEvent event, Throwable throwable) {
222 synchronized (errorWriter) {
223 final LocalizedMessage addExceptionMessage = new LocalizedMessage(1,
224 Definitions.CHECKSTYLE_BUNDLE, ADD_EXCEPTION_MESSAGE,
225 new String[] {event.getFileName()}, null,
226 LocalizedMessage.class, null);
227 errorWriter.println(addExceptionMessage.getMessage());
228 throwable.printStackTrace(errorWriter);
229 }
230 }
231
232 @Override
233 public void auditStarted(AuditEvent event) {
234 final LocalizedMessage auditStartMessage = new LocalizedMessage(1,
235 Definitions.CHECKSTYLE_BUNDLE, AUDIT_STARTED_MESSAGE, null, null,
236 LocalizedMessage.class, null);
237 infoWriter.println(auditStartMessage.getMessage());
238 infoWriter.flush();
239 }
240
241 @Override
242 public void auditFinished(AuditEvent event) {
243 final LocalizedMessage auditFinishMessage = new LocalizedMessage(1,
244 Definitions.CHECKSTYLE_BUNDLE, AUDIT_FINISHED_MESSAGE, null, null,
245 LocalizedMessage.class, null);
246 infoWriter.println(auditFinishMessage.getMessage());
247 closeStreams();
248 }
249
250 @Override
251 public void fileStarted(AuditEvent event) {
252
253 }
254
255 @Override
256 public void fileFinished(AuditEvent event) {
257 infoWriter.flush();
258 }
259
260
261
262
263 private void closeStreams() {
264 infoWriter.flush();
265 if (closeInfo) {
266 infoWriter.close();
267 }
268
269 errorWriter.flush();
270 if (closeError) {
271 errorWriter.close();
272 }
273 }
274
275 }