6041d927485b03f95d9c7bbe788a16645ff3cad1
[yangtools.git] / common / checkstyle-logging / src / main / java / org / opendaylight / yangtools / checkstyle / LogMessageExtractorCheck.java
1 /*
2  * Copyright (c) 2016 Red Hat, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.checkstyle;
9
10 import com.google.common.base.Preconditions;
11 import com.google.common.io.Files;
12 import com.puppycrawl.tools.checkstyle.api.DetailAST;
13 import java.io.File;
14 import java.io.IOException;
15 import java.nio.charset.StandardCharsets;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 /**
20  * Check which extracts the content of Logger messages somewhere (e.g. a file).
21  *
22  * <p>This can be used to create a comprehensive list of all log messages.
23  *
24  * <p>It is a first step towards more formal tracking of all messages
25  * from a system with a unique ID, using e.g. a framework such
26  * as jboss-logging.
27  *
28  * <p>Does not actually Check anything, i.e. never emits any Checkstyle warnings.
29  */
30 public class LogMessageExtractorCheck extends AbstractLogMessageCheck {
31
32     private static final Logger LOG = LoggerFactory.getLogger(LogMessageExtractorCheck.class);
33
34     static final File DEFAULT_REPORT_FILE = new File("target/logger-messages.txt");
35
36     private File logMessagesReportFile = DEFAULT_REPORT_FILE;
37
38     public void setLogMessagesReportFileName(String fileName) {
39         logMessagesReportFile = new File(fileName);
40         logMessagesReportFile.getParentFile().mkdirs();
41     }
42
43     public File getLogMessagesReportFile() {
44         return logMessagesReportFile;
45     }
46
47     @Override
48     protected void visitLogMessage(DetailAST ast, String logMessage) {
49         File file = new File(getFileContents().getFileName());
50         String fileName = FileNameUtil.getPathRelativeToMavenProjectRootIfPossible(file).getPath();
51         int lineNumber = ast.getLineNo();
52         LogMessageOccurence log = new LogMessageOccurence(fileName, lineNumber, logMessage);
53         updateMessagesReportFile(log);
54     }
55
56     protected void updateMessagesReportFile(LogMessageOccurence log) {
57         try {
58             Files.append(log.toString() + "\n", getLogMessagesReportFile(), StandardCharsets.UTF_8);
59         } catch (IOException e) {
60             LOG.error("Failed to append to file: {}", logMessagesReportFile.getPath(), e);
61         }
62     }
63
64     public static class LogMessageOccurence {
65
66         // relative to current project root
67         public final String javaSourceFilePath;
68         public final int lineNumber;
69         public final String message;
70
71         public LogMessageOccurence(String javaSourceFilePath, int lineNumber, String message) {
72             this.javaSourceFilePath = Preconditions.checkNotNull(javaSourceFilePath, "javaSourceFilePath");
73             this.lineNumber = lineNumber;
74             this.message = Preconditions.checkNotNull(message, "message");
75         }
76
77         @Override
78         public String toString() {
79             return javaSourceFilePath + ":" + lineNumber + ":" + message;
80         }
81     }
82 }