Source code: com/mycompany/listeners/VerboseListener.java
1 package com.mycompany.listeners;
2
3 import java.io.FileNotFoundException;
4 import java.io.FileOutputStream;
5 import java.io.OutputStream;
6 import java.io.PrintWriter;
7
8 import com.puppycrawl.tools.checkstyle.api.AuditEvent;
9 import com.puppycrawl.tools.checkstyle.api.AuditListener;
10 import com.puppycrawl.tools.checkstyle.api.AutomaticBean;
11 import com.puppycrawl.tools.checkstyle.api.SeverityLevel;
12
13 /**
14 * An AuditListener that reports every event to an output stream.
15 * @author Rick Giles
16 */
17 public class VerboseListener
18 extends AutomaticBean
19 implements AuditListener
20 {
21 /** where to write messages */
22 private PrintWriter mWriter = new PrintWriter(System.out);
23
24 /** close output stream */
25 private boolean mCloseOut = false;
26
27 /** total number of errors and exceptions */
28 private int mTotalErrors;
29
30 /** number of errors and exceptions in the audit of one file */
31 private int mErrors;
32
33 /**
34 * Sets the output stream to a file.
35 * @param aFileName name of the output file.
36 * @throws FileNotFoundException if an error occurs.
37 */
38 public void setFile(String aFileName)
39 throws FileNotFoundException
40 {
41 final OutputStream out = new FileOutputStream(aFileName);
42 mWriter = new PrintWriter(out);
43 mCloseOut = true;
44 }
45
46 /** @see com.puppycrawl.tools.checkstyle.api.AuditListener */
47 public void auditStarted(AuditEvent aEvt)
48 {
49 mTotalErrors = 0;
50 mWriter.println("Audit started.");
51 }
52
53 /** @see com.puppycrawl.tools.checkstyle.api.AuditListener */
54 public void auditFinished(AuditEvent aEvt)
55 {
56 mWriter.println("Audit finished. Total errors: " + mTotalErrors);
57 mWriter.flush();
58 if (mCloseOut) {
59 mWriter.close();
60 }
61 }
62
63 /** @see com.puppycrawl.tools.checkstyle.api.AuditListener */
64 public void fileStarted(AuditEvent aEvt)
65 {
66 mErrors = 0;
67 mWriter.println(
68 "Started checking file '" + aEvt.getFileName() + "'.");
69 }
70
71 /** @see com.puppycrawl.tools.checkstyle.api.AuditListener */
72 public void fileFinished(AuditEvent aEvt)
73 {
74 mWriter.println("Finished checking file '" + aEvt.getFileName()
75 + "'. Errors: " + mErrors);
76 }
77
78 /** @see com.puppycrawl.tools.checkstyle.api.AuditListener */
79 public void addError(AuditEvent aEvt)
80 {
81 printEvent(aEvt);
82 if (SeverityLevel.ERROR.equals(aEvt.getSeverityLevel())) {
83 mErrors++;
84 mTotalErrors++;
85 }
86 }
87
88 /** @see com.puppycrawl.tools.checkstyle.api.AuditListener */
89 public void addException(AuditEvent aEvt, Throwable aThrowable)
90 {
91 printEvent(aEvt);
92 aThrowable.printStackTrace(System.out);
93 mErrors++;
94 mTotalErrors++;
95 }
96
97 /**
98 * Prints event information to standard output.
99 * @param aEvt the event to print.
100 */
101 private void printEvent(AuditEvent aEvt)
102 {
103 mWriter.println("Logging error -"
104 + " file: '" + aEvt.getFileName() + "'"
105 + " line: " + aEvt.getLine()
106 + " column: " + aEvt.getColumn()
107 + " severity: " + aEvt.getSeverityLevel()
108 + " message: " + aEvt.getMessage()
109 + " source: " + aEvt.getSourceName());
110 }
111 }