001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2018 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.util.Locale;
023
024import com.puppycrawl.tools.checkstyle.api.AuditEvent;
025import com.puppycrawl.tools.checkstyle.api.SeverityLevel;
026
027/**
028 * Represents the default formatter for log message.
029 * Default log message format is:
030 * [SEVERITY LEVEL] filePath:lineNo:columnNo: message. [CheckName]
031 * When the module id of the message has been set, the format is:
032 * [SEVERITY LEVEL] filePath:lineNo:columnNo: message. [ModuleId]
033 * @author Andrei Selkin
034 */
035public class AuditEventDefaultFormatter implements AuditEventFormatter {
036
037    /** Length of all separators. */
038    private static final int LENGTH_OF_ALL_SEPARATORS = 10;
039
040    /** Suffix of module names like XXXXCheck. */
041    private static final String SUFFIX = "Check";
042
043    @Override
044    public String format(AuditEvent event) {
045        final String fileName = event.getFileName();
046        final String message = event.getMessage();
047
048        final SeverityLevel severityLevel = event.getSeverityLevel();
049        final String severityLevelName;
050        if (severityLevel == SeverityLevel.WARNING) {
051            // We change the name of severity level intentionally
052            // to shorten the length of the log message.
053            severityLevelName = "WARN";
054        }
055        else {
056            severityLevelName = severityLevel.getName().toUpperCase(Locale.US);
057        }
058
059        // Avoid StringBuffer.expandCapacity
060        final int bufLen = calculateBufferLength(event, severityLevelName.length());
061        final StringBuilder sb = new StringBuilder(bufLen);
062
063        sb.append('[').append(severityLevelName).append("] ")
064            .append(fileName).append(':').append(event.getLine());
065        if (event.getColumn() > 0) {
066            sb.append(':').append(event.getColumn());
067        }
068        sb.append(": ").append(message).append(" [");
069        if (event.getModuleId() == null) {
070            final String checkShortName = getCheckShortName(event);
071            sb.append(checkShortName);
072        }
073        else {
074            sb.append(event.getModuleId());
075        }
076        sb.append(']');
077
078        return sb.toString();
079    }
080
081    /**
082     * Returns the length of the buffer for StringBuilder.
083     * bufferLength = fileNameLength + messageLength + lengthOfAllSeparators +
084     * + severityNameLength + checkNameLength.
085     * @param event audit event.
086     * @param severityLevelNameLength length of severity level name.
087     * @return the length of the buffer for StringBuilder.
088     */
089    private static int calculateBufferLength(AuditEvent event, int severityLevelNameLength) {
090        return LENGTH_OF_ALL_SEPARATORS + event.getFileName().length()
091            + event.getMessage().length() + severityLevelNameLength
092            + getCheckShortName(event).length();
093    }
094
095    /**
096     * Returns check name without 'Check' suffix.
097     * @param event audit event.
098     * @return check name without 'Check' suffix.
099     */
100    private static String getCheckShortName(AuditEvent event) {
101        final String checkFullName = event.getSourceName();
102        final String checkShortName;
103        final int lastDotIndex = checkFullName.lastIndexOf('.');
104        if (lastDotIndex == -1) {
105            if (checkFullName.endsWith(SUFFIX)) {
106                checkShortName = checkFullName.substring(0, checkFullName.lastIndexOf(SUFFIX));
107            }
108            else {
109                checkShortName = checkFullName.substring(0, checkFullName.length());
110            }
111        }
112        else {
113            if (checkFullName.endsWith(SUFFIX)) {
114                checkShortName = checkFullName.substring(lastDotIndex + 1,
115                    checkFullName.lastIndexOf(SUFFIX));
116            }
117            else {
118                checkShortName = checkFullName.substring(lastDotIndex + 1, checkFullName.length());
119            }
120        }
121        return checkShortName;
122    }
123
124}