001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2019 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.LocalizedMessage;
032import com.puppycrawl.tools.checkstyle.api.SeverityLevel;
033
034/**
035 * Simple plain logger for text output.
036 * This is maybe not very suitable for a text output into a file since it
037 * does not need all 'audit finished' and so on stuff, but it looks good on
038 * stdout anyway. If there is really a problem this is what XMLLogger is for.
039 * It gives structure.
040 *
041 * @see XMLLogger
042 */
043public class DefaultLogger extends AutomaticBean implements AuditListener {
044
045    /**
046     * A key pointing to the add exception
047     * message in the "messages.properties" file.
048     */
049    public static final String ADD_EXCEPTION_MESSAGE = "DefaultLogger.addException";
050    /**
051     * A key pointing to the started audit
052     * message in the "messages.properties" file.
053     */
054    public static final String AUDIT_STARTED_MESSAGE = "DefaultLogger.auditStarted";
055    /**
056     * A key pointing to the finished audit
057     * message in the "messages.properties" file.
058     */
059    public static final String AUDIT_FINISHED_MESSAGE = "DefaultLogger.auditFinished";
060
061    /** Where to write info messages. **/
062    private final PrintWriter infoWriter;
063    /** Close info stream after use. */
064    private final boolean closeInfo;
065
066    /** Where to write error messages. **/
067    private final PrintWriter errorWriter;
068    /** Close error stream after use. */
069    private final boolean closeError;
070
071    /** Formatter for the log message. */
072    private final AuditEventFormatter formatter;
073
074    /**
075     * Creates a new {@code DefaultLogger} instance.
076     * @param outputStream where to log audit events
077     * @param outputStreamOptions if {@code CLOSE} that should be closed in auditFinished()
078     */
079    public DefaultLogger(OutputStream outputStream, OutputStreamOptions outputStreamOptions) {
080        // no need to close oS twice
081        this(outputStream, outputStreamOptions, outputStream, OutputStreamOptions.NONE);
082    }
083
084    /**
085     * Creates a new {@code DefaultLogger} instance.
086     * @param infoStream the {@code OutputStream} for info messages.
087     * @param infoStreamOptions if {@code CLOSE} info should be closed in auditFinished()
088     * @param errorStream the {@code OutputStream} for error messages.
089     * @param errorStreamOptions if {@code CLOSE} error should be closed in auditFinished()
090     */
091    public DefaultLogger(OutputStream infoStream,
092                         OutputStreamOptions infoStreamOptions,
093                         OutputStream errorStream,
094                         OutputStreamOptions errorStreamOptions) {
095        this(infoStream, infoStreamOptions, errorStream, errorStreamOptions,
096                new AuditEventDefaultFormatter());
097    }
098
099    /**
100     * Creates a new {@code DefaultLogger} instance.
101     *
102     * @param infoStream the {@code OutputStream} for info messages
103     * @param infoStreamOptions if {@code CLOSE} info should be closed in auditFinished()
104     * @param errorStream the {@code OutputStream} for error messages
105     * @param errorStreamOptions if {@code CLOSE} error should be closed in auditFinished()
106     * @param messageFormatter formatter for the log message.
107     * @noinspection WeakerAccess
108     */
109    public DefaultLogger(OutputStream infoStream,
110                         OutputStreamOptions infoStreamOptions,
111                         OutputStream errorStream,
112                         OutputStreamOptions errorStreamOptions,
113                         AuditEventFormatter messageFormatter) {
114        if (infoStreamOptions == null) {
115            throw new IllegalArgumentException("Parameter infoStreamOptions can not be null");
116        }
117        closeInfo = infoStreamOptions == OutputStreamOptions.CLOSE;
118        if (errorStreamOptions == null) {
119            throw new IllegalArgumentException("Parameter errorStreamOptions can not be null");
120        }
121        closeError = errorStreamOptions == OutputStreamOptions.CLOSE;
122        final Writer infoStreamWriter = new OutputStreamWriter(infoStream, StandardCharsets.UTF_8);
123        infoWriter = new PrintWriter(infoStreamWriter);
124
125        if (infoStream == errorStream) {
126            errorWriter = infoWriter;
127        }
128        else {
129            final Writer errorStreamWriter = new OutputStreamWriter(errorStream,
130                    StandardCharsets.UTF_8);
131            errorWriter = new PrintWriter(errorStreamWriter);
132        }
133        formatter = messageFormatter;
134    }
135
136    @Override
137    protected void finishLocalSetup() {
138        // No code by default
139    }
140
141    /**
142     * Print an Emacs compliant line on the error stream.
143     * If the column number is non zero, then also display it.
144     * @see AuditListener
145     **/
146    @Override
147    public void addError(AuditEvent event) {
148        final SeverityLevel severityLevel = event.getSeverityLevel();
149        if (severityLevel != SeverityLevel.IGNORE) {
150            final String errorMessage = formatter.format(event);
151            errorWriter.println(errorMessage);
152        }
153    }
154
155    @Override
156    public void addException(AuditEvent event, Throwable throwable) {
157        synchronized (errorWriter) {
158            final LocalizedMessage addExceptionMessage = new LocalizedMessage(1,
159                Definitions.CHECKSTYLE_BUNDLE, ADD_EXCEPTION_MESSAGE,
160                new String[] {event.getFileName()}, null,
161                LocalizedMessage.class, null);
162            errorWriter.println(addExceptionMessage.getMessage());
163            throwable.printStackTrace(errorWriter);
164        }
165    }
166
167    @Override
168    public void auditStarted(AuditEvent event) {
169        final LocalizedMessage auditStartMessage = new LocalizedMessage(1,
170            Definitions.CHECKSTYLE_BUNDLE, AUDIT_STARTED_MESSAGE, null, null,
171            LocalizedMessage.class, null);
172        infoWriter.println(auditStartMessage.getMessage());
173        infoWriter.flush();
174    }
175
176    @Override
177    public void auditFinished(AuditEvent event) {
178        final LocalizedMessage auditFinishMessage = new LocalizedMessage(1,
179            Definitions.CHECKSTYLE_BUNDLE, AUDIT_FINISHED_MESSAGE, null, null,
180            LocalizedMessage.class, null);
181        infoWriter.println(auditFinishMessage.getMessage());
182        closeStreams();
183    }
184
185    @Override
186    public void fileStarted(AuditEvent event) {
187        // No need to implement this method in this class
188    }
189
190    @Override
191    public void fileFinished(AuditEvent event) {
192        infoWriter.flush();
193    }
194
195    /**
196     * Flushes the output streams and closes them if needed.
197     */
198    private void closeStreams() {
199        infoWriter.flush();
200        if (closeInfo) {
201            infoWriter.close();
202        }
203
204        errorWriter.flush();
205        if (closeError) {
206            errorWriter.close();
207        }
208    }
209
210}