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.SeverityLevel;
31
32
33
34
35 public class MetadataGeneratorLogger extends AbstractAutomaticBean implements AuditListener {
36
37
38
39
40 private final PrintWriter errorWriter;
41
42
43
44
45 private final AuditEventFormatter formatter;
46
47
48
49
50 private final boolean closeErrorWriter;
51
52
53
54
55
56
57
58 public MetadataGeneratorLogger(OutputStream outputStream,
59 OutputStreamOptions outputStreamOptions) {
60 final Writer errorStreamWriter = new OutputStreamWriter(outputStream,
61 StandardCharsets.UTF_8);
62 errorWriter = new PrintWriter(errorStreamWriter);
63 formatter = new AuditEventDefaultFormatter();
64 closeErrorWriter = outputStreamOptions == OutputStreamOptions.CLOSE;
65 }
66
67 @Override
68 public void auditStarted(AuditEvent event) {
69 errorWriter.flush();
70 }
71
72 @Override
73 public void auditFinished(AuditEvent event) {
74 errorWriter.flush();
75 if (closeErrorWriter) {
76 errorWriter.close();
77 }
78 }
79
80 @Override
81 public void fileStarted(AuditEvent event) {
82
83 }
84
85 @Override
86 public void fileFinished(AuditEvent event) {
87 errorWriter.flush();
88 }
89
90 @Override
91 public void addError(AuditEvent event) {
92 final SeverityLevel severityLevel = event.getSeverityLevel();
93 if (severityLevel != SeverityLevel.IGNORE) {
94 final String errorMessage = formatter.format(event);
95 errorWriter.println(errorMessage);
96 }
97 }
98
99 @Override
100 public void addException(AuditEvent event, Throwable throwable) {
101 synchronized (errorWriter) {
102 throwable.printStackTrace(errorWriter);
103 }
104 }
105
106 @Override
107 protected void finishLocalSetup() {
108
109 }
110 }