001/////////////////////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code and other text files for adherence to a set of rules. 003// Copyright (C) 2001-2024 the original author or authors. 004// 005// This library is free software; you can redistribute it and/or 006// modify it under the terms of the GNU Lesser General Public 007// License as published by the Free Software Foundation; either 008// version 2.1 of the License, or (at your option) any later version. 009// 010// This library is distributed in the hope that it will be useful, 011// but WITHOUT ANY WARRANTY; without even the implied warranty of 012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013// Lesser General Public License for more details. 014// 015// You should have received a copy of the GNU Lesser General Public 016// License along with this library; if not, write to the Free Software 017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 018/////////////////////////////////////////////////////////////////////////////////////////////// 019 020package com.puppycrawl.tools.checkstyle; 021 022import java.io.OutputStream; 023import java.io.OutputStreamWriter; 024import java.io.PrintWriter; 025import java.io.Writer; 026import java.nio.charset.StandardCharsets; 027 028import com.puppycrawl.tools.checkstyle.api.AuditEvent; 029import com.puppycrawl.tools.checkstyle.api.AuditListener; 030import com.puppycrawl.tools.checkstyle.api.SeverityLevel; 031 032/** 033 * Simple logger for metadata generator util. 034 */ 035public class MetadataGeneratorLogger extends AbstractAutomaticBean implements AuditListener { 036 037 /** 038 * Where to write error messages. 039 */ 040 private final PrintWriter errorWriter; 041 042 /** 043 * Formatter for the log message. 044 */ 045 private final AuditEventFormatter formatter; 046 047 /** 048 * Close output stream in audit finished. 049 */ 050 private final boolean closeErrorWriter; 051 052 /** 053 * Creates a new MetadataGeneratorLogger instance. 054 * 055 * @param outputStream where to log audit events 056 * @param outputStreamOptions if {@code CLOSE} error should be closed in auditFinished() 057 */ 058 public MetadataGeneratorLogger(OutputStream outputStream, 059 OutputStreamOptions outputStreamOptions) { 060 final Writer errorStreamWriter = new OutputStreamWriter(outputStream, 061 StandardCharsets.UTF_8); 062 errorWriter = new PrintWriter(errorStreamWriter); 063 formatter = new AuditEventDefaultFormatter(); 064 closeErrorWriter = outputStreamOptions == OutputStreamOptions.CLOSE; 065 } 066 067 @Override 068 public void auditStarted(AuditEvent event) { 069 errorWriter.flush(); 070 } 071 072 @Override 073 public void auditFinished(AuditEvent event) { 074 errorWriter.flush(); 075 if (closeErrorWriter) { 076 errorWriter.close(); 077 } 078 } 079 080 @Override 081 public void fileStarted(AuditEvent event) { 082 // No code by default. 083 } 084 085 @Override 086 public void fileFinished(AuditEvent event) { 087 errorWriter.flush(); 088 } 089 090 @Override 091 public void addError(AuditEvent event) { 092 final SeverityLevel severityLevel = event.getSeverityLevel(); 093 if (severityLevel != SeverityLevel.IGNORE) { 094 final String errorMessage = formatter.format(event); 095 errorWriter.println(errorMessage); 096 } 097 } 098 099 @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}