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 */
034public class AuditEventDefaultFormatter implements AuditEventFormatter {
035
036    /** Length of all separators. */
037    private static final int LENGTH_OF_ALL_SEPARATORS = 10;
038
039    /** Suffix of module names like XXXXCheck. */
040    private static final String SUFFIX = "Check";
041
042    @Override
043    public String format(AuditEvent event) {
044        final String fileName = event.getFileName();
045        final String message = event.getMessage();
046
047        final SeverityLevel severityLevel = event.getSeverityLevel();
048        final String severityLevelName;
049        if (severityLevel == SeverityLevel.WARNING) {
050            // We change the name of severity level intentionally
051            // to shorten the length of the log message.
052            severityLevelName = "WARN";
053        }
054        else {
055            severityLevelName = severityLevel.getName().toUpperCase(Locale.US);
056        }
057
058        // Avoid StringBuffer.expandCapacity
059        final int bufLen = calculateBufferLength(event, severityLevelName.length());
060        final StringBuilder sb = new StringBuilder(bufLen);
061
062        sb.append('[').append(severityLevelName).append("] ")
063            .append(fileName).append(':').append(event.getLine());
064        if (event.getColumn() > 0) {
065            sb.append(':').append(event.getColumn());
066        }
067        sb.append(": ").append(message).append(" [");
068        if (event.getModuleId() == null) {
069            final String checkShortName = getCheckShortName(event);
070            sb.append(checkShortName);
071        }
072        else {
073            sb.append(event.getModuleId());
074        }
075        sb.append(']');
076
077        return sb.toString();
078    }
079
080    /**
081     * Returns the length of the buffer for StringBuilder.
082     * bufferLength = fileNameLength + messageLength + lengthOfAllSeparators +
083     * + severityNameLength + checkNameLength.
084     * @param event audit event.
085     * @param severityLevelNameLength length of severity level name.
086     * @return the length of the buffer for StringBuilder.
087     */
088    private static int calculateBufferLength(AuditEvent event, int severityLevelNameLength) {
089        return LENGTH_OF_ALL_SEPARATORS + event.getFileName().length()
090            + event.getMessage().length() + severityLevelNameLength
091            + getCheckShortName(event).length();
092    }
093
094    /**
095     * Returns check name without 'Check' suffix.
096     * @param event audit event.
097     * @return check name without 'Check' suffix.
098     */
099    private static String getCheckShortName(AuditEvent event) {
100        final String checkFullName = event.getSourceName();
101        final String checkShortName;
102        final int lastDotIndex = checkFullName.lastIndexOf('.');
103        if (lastDotIndex == -1) {
104            if (checkFullName.endsWith(SUFFIX)) {
105                checkShortName = checkFullName.substring(0, checkFullName.lastIndexOf(SUFFIX));
106            }
107            else {
108                checkShortName = checkFullName;
109            }
110        }
111        else {
112            if (checkFullName.endsWith(SUFFIX)) {
113                checkShortName = checkFullName.substring(lastDotIndex + 1,
114                    checkFullName.lastIndexOf(SUFFIX));
115            }
116            else {
117                checkShortName = checkFullName.substring(lastDotIndex + 1, checkFullName.length());
118            }
119        }
120        return checkShortName;
121    }
122
123}