1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2019 the original author or authors.
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  ////////////////////////////////////////////////////////////////////////////////
19  
20  package com.puppycrawl.tools.checkstyle;
21  
22  import java.util.Locale;
23  
24  import com.puppycrawl.tools.checkstyle.api.AuditEvent;
25  import com.puppycrawl.tools.checkstyle.api.SeverityLevel;
26  
27  /**
28   * Represents the default formatter for log message.
29   * Default log message format is:
30   * [SEVERITY LEVEL] filePath:lineNo:columnNo: message. [CheckName]
31   * When the module id of the message has been set, the format is:
32   * [SEVERITY LEVEL] filePath:lineNo:columnNo: message. [ModuleId]
33   */
34  public class AuditEventDefaultFormatter implements AuditEventFormatter {
35  
36      /** Length of all separators. */
37      private static final int LENGTH_OF_ALL_SEPARATORS = 10;
38  
39      /** Suffix of module names like XXXXCheck. */
40      private static final String SUFFIX = "Check";
41  
42      @Override
43      public String format(AuditEvent event) {
44          final String fileName = event.getFileName();
45          final String message = event.getMessage();
46  
47          final SeverityLevel severityLevel = event.getSeverityLevel();
48          final String severityLevelName;
49          if (severityLevel == SeverityLevel.WARNING) {
50              // We change the name of severity level intentionally
51              // to shorten the length of the log message.
52              severityLevelName = "WARN";
53          }
54          else {
55              severityLevelName = severityLevel.getName().toUpperCase(Locale.US);
56          }
57  
58          // Avoid StringBuffer.expandCapacity
59          final int bufLen = calculateBufferLength(event, severityLevelName.length());
60          final StringBuilder sb = new StringBuilder(bufLen);
61  
62          sb.append('[').append(severityLevelName).append("] ")
63              .append(fileName).append(':').append(event.getLine());
64          if (event.getColumn() > 0) {
65              sb.append(':').append(event.getColumn());
66          }
67          sb.append(": ").append(message).append(" [");
68          if (event.getModuleId() == null) {
69              final String checkShortName = getCheckShortName(event);
70              sb.append(checkShortName);
71          }
72          else {
73              sb.append(event.getModuleId());
74          }
75          sb.append(']');
76  
77          return sb.toString();
78      }
79  
80      /**
81       * Returns the length of the buffer for StringBuilder.
82       * bufferLength = fileNameLength + messageLength + lengthOfAllSeparators +
83       * + severityNameLength + checkNameLength.
84       * @param event audit event.
85       * @param severityLevelNameLength length of severity level name.
86       * @return the length of the buffer for StringBuilder.
87       */
88      private static int calculateBufferLength(AuditEvent event, int severityLevelNameLength) {
89          return LENGTH_OF_ALL_SEPARATORS + event.getFileName().length()
90              + event.getMessage().length() + severityLevelNameLength
91              + getCheckShortName(event).length();
92      }
93  
94      /**
95       * Returns check name without 'Check' suffix.
96       * @param event audit event.
97       * @return check name without 'Check' suffix.
98       */
99      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);
118             }
119         }
120         return checkShortName;
121     }
122 
123 }