001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2022 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.AutomaticBean; 031import com.puppycrawl.tools.checkstyle.api.SeverityLevel; 032 033/** 034 * Simple logger for metadata generator util. 035 */ 036public class MetadataGeneratorLogger extends AutomaticBean implements AuditListener { 037 038 /** 039 * Where to write error messages. 040 */ 041 private final PrintWriter errorWriter; 042 043 /** 044 * Formatter for the log message. 045 */ 046 private final AuditEventFormatter formatter; 047 048 /** 049 * Close output stream in audit finished. 050 */ 051 private final boolean closeErrorWriter; 052 053 /** 054 * Creates a new MetadataGeneratorLogger instance. 055 * 056 * @param outputStream where to log audit events 057 * @param outputStreamOptions if {@code CLOSE} error should be closed in auditFinished() 058 */ 059 public MetadataGeneratorLogger(OutputStream outputStream, 060 OutputStreamOptions outputStreamOptions) { 061 final Writer errorStreamWriter = new OutputStreamWriter(outputStream, 062 StandardCharsets.UTF_8); 063 errorWriter = new PrintWriter(errorStreamWriter); 064 formatter = new AuditEventDefaultFormatter(); 065 closeErrorWriter = outputStreamOptions == OutputStreamOptions.CLOSE; 066 } 067 068 @Override 069 public void auditStarted(AuditEvent event) { 070 errorWriter.flush(); 071 } 072 073 @Override 074 public void auditFinished(AuditEvent event) { 075 errorWriter.flush(); 076 if (closeErrorWriter) { 077 errorWriter.close(); 078 } 079 } 080 081 @Override 082 public void fileStarted(AuditEvent event) { 083 // No code by default. 084 } 085 086 @Override 087 public void fileFinished(AuditEvent event) { 088 errorWriter.flush(); 089 } 090 091 @Override 092 public void addError(AuditEvent event) { 093 final SeverityLevel severityLevel = event.getSeverityLevel(); 094 if (severityLevel != SeverityLevel.IGNORE) { 095 final String errorMessage = formatter.format(event); 096 errorWriter.println(errorMessage); 097 } 098 } 099 100 @Override 101 public void addException(AuditEvent event, Throwable throwable) { 102 synchronized (errorWriter) { 103 throwable.printStackTrace(errorWriter); 104 } 105 } 106 107 @Override 108 protected void finishLocalSetup() { 109 // No code by default. 110 } 111}