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.SeverityLevel;
32
33
34
35
36
37
38
39
40
41
42 public class DefaultLogger extends AbstractAutomaticBean implements AuditListener {
43
44
45
46
47
48 public static final String ADD_EXCEPTION_MESSAGE = "DefaultLogger.addException";
49
50
51
52
53 public static final String AUDIT_STARTED_MESSAGE = "DefaultLogger.auditStarted";
54
55
56
57
58 public static final String AUDIT_FINISHED_MESSAGE = "DefaultLogger.auditFinished";
59
60
61 private final PrintWriter infoWriter;
62
63 private final boolean closeInfo;
64
65
66 private final PrintWriter errorWriter;
67
68 private final boolean closeError;
69
70
71 private final AuditEventFormatter formatter;
72
73
74
75
76
77
78
79
80
81
82 public DefaultLogger(OutputStream outputStream,
83 AutomaticBean.OutputStreamOptions outputStreamOptions) {
84 this(outputStream, OutputStreamOptions.valueOf(outputStreamOptions.name()));
85 }
86
87
88
89
90
91
92
93 public DefaultLogger(OutputStream outputStream, OutputStreamOptions outputStreamOptions) {
94
95 this(outputStream, outputStreamOptions, outputStream, OutputStreamOptions.NONE);
96 }
97
98
99
100
101
102
103
104
105
106 public DefaultLogger(OutputStream infoStream,
107 OutputStreamOptions infoStreamOptions,
108 OutputStream errorStream,
109 OutputStreamOptions errorStreamOptions) {
110 this(infoStream, infoStreamOptions, errorStream, errorStreamOptions,
111 new AuditEventDefaultFormatter());
112 }
113
114
115
116
117
118
119
120
121
122
123
124
125
126 public DefaultLogger(OutputStream infoStream,
127 OutputStreamOptions infoStreamOptions,
128 OutputStream errorStream,
129 OutputStreamOptions errorStreamOptions,
130 AuditEventFormatter messageFormatter) {
131 if (infoStreamOptions == null) {
132 throw new IllegalArgumentException("Parameter infoStreamOptions can not be null");
133 }
134 closeInfo = infoStreamOptions == OutputStreamOptions.CLOSE;
135 if (errorStreamOptions == null) {
136 throw new IllegalArgumentException("Parameter errorStreamOptions can not be null");
137 }
138 closeError = errorStreamOptions == OutputStreamOptions.CLOSE;
139 final Writer infoStreamWriter = new OutputStreamWriter(infoStream, StandardCharsets.UTF_8);
140 infoWriter = new PrintWriter(infoStreamWriter);
141
142 if (infoStream == errorStream) {
143 errorWriter = infoWriter;
144 }
145 else {
146 final Writer errorStreamWriter = new OutputStreamWriter(errorStream,
147 StandardCharsets.UTF_8);
148 errorWriter = new PrintWriter(errorStreamWriter);
149 }
150 formatter = messageFormatter;
151 }
152
153 @Override
154 protected void finishLocalSetup() {
155
156 }
157
158
159
160
161
162
163
164 @Override
165 public void addError(AuditEvent event) {
166 final SeverityLevel severityLevel = event.getSeverityLevel();
167 if (severityLevel != SeverityLevel.IGNORE) {
168 final String errorMessage = formatter.format(event);
169 errorWriter.println(errorMessage);
170 }
171 }
172
173 @Override
174 public void addException(AuditEvent event, Throwable throwable) {
175 synchronized (errorWriter) {
176 final LocalizedMessage exceptionMessage = new LocalizedMessage(
177 Definitions.CHECKSTYLE_BUNDLE, DefaultLogger.class,
178 ADD_EXCEPTION_MESSAGE, event.getFileName());
179 errorWriter.println(exceptionMessage.getMessage());
180 throwable.printStackTrace(errorWriter);
181 }
182 }
183
184 @Override
185 public void auditStarted(AuditEvent event) {
186 final LocalizedMessage auditStartMessage = new LocalizedMessage(
187 Definitions.CHECKSTYLE_BUNDLE, DefaultLogger.class,
188 AUDIT_STARTED_MESSAGE);
189 infoWriter.println(auditStartMessage.getMessage());
190 infoWriter.flush();
191 }
192
193 @Override
194 public void auditFinished(AuditEvent event) {
195 final LocalizedMessage auditFinishMessage = new LocalizedMessage(
196 Definitions.CHECKSTYLE_BUNDLE, DefaultLogger.class,
197 AUDIT_FINISHED_MESSAGE);
198 infoWriter.println(auditFinishMessage.getMessage());
199 closeStreams();
200 }
201
202 @Override
203 public void fileStarted(AuditEvent event) {
204
205 }
206
207 @Override
208 public void fileFinished(AuditEvent event) {
209 infoWriter.flush();
210 }
211
212
213
214
215 private void closeStreams() {
216 infoWriter.flush();
217 if (closeInfo) {
218 infoWriter.close();
219 }
220
221 errorWriter.flush();
222 if (closeError) {
223 errorWriter.close();
224 }
225 }
226 }