View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 the original author or authors.
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  
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   * Simple logger for metadata generator util.
34   */
35  public class MetadataGeneratorLogger extends AbstractAutomaticBean implements AuditListener {
36  
37      /**
38       * Where to write error messages.
39       */
40      private final PrintWriter errorWriter;
41  
42      /**
43       * Formatter for the log message.
44       */
45      private final AuditEventFormatter formatter;
46  
47      /**
48       * Close output stream in audit finished.
49       */
50      private final boolean closeErrorWriter;
51  
52      /**
53       * Creates a new MetadataGeneratorLogger instance.
54       *
55       * @param outputStream where to log audit events
56       * @param outputStreamOptions if {@code CLOSE} error should be closed in auditFinished()
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          // No code by default.
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         // No code by default.
109     }
110 }