001: package com.mycompany.listeners;
002:
003: import java.io.FileNotFoundException;
004: import java.io.FileOutputStream;
005: import java.io.OutputStream;
006: import java.io.PrintWriter;
007:
008: import com.puppycrawl.tools.checkstyle.api.AuditEvent;
009: import com.puppycrawl.tools.checkstyle.api.AuditListener;
010: import com.puppycrawl.tools.checkstyle.api.AutomaticBean;
011: import com.puppycrawl.tools.checkstyle.api.SeverityLevel;
012:
013: /**
014: * An AuditListener that reports every event to an output stream.
015: * @author Rick Giles
016: */
017: public class VerboseListener extends AutomaticBean implements
018: AuditListener {
019: /** where to write messages */
020: private PrintWriter mWriter = new PrintWriter(System.out);
021:
022: /** close output stream */
023: private boolean mCloseOut = false;
024:
025: /** total number of errors and exceptions */
026: private int mTotalErrors;
027:
028: /** number of errors and exceptions in the audit of one file */
029: private int mErrors;
030:
031: /**
032: * Sets the output stream to a file.
033: * @param aFileName name of the output file.
034: * @throws FileNotFoundException if an error occurs.
035: */
036: public void setFile(String aFileName) throws FileNotFoundException {
037: final OutputStream out = new FileOutputStream(aFileName);
038: mWriter = new PrintWriter(out);
039: mCloseOut = true;
040: }
041:
042: /** @see com.puppycrawl.tools.checkstyle.api.AuditListener */
043: public void auditStarted(AuditEvent aEvt) {
044: mTotalErrors = 0;
045: mWriter.println("Audit started.");
046: }
047:
048: /** @see com.puppycrawl.tools.checkstyle.api.AuditListener */
049: public void auditFinished(AuditEvent aEvt) {
050: mWriter
051: .println("Audit finished. Total errors: "
052: + mTotalErrors);
053: mWriter.flush();
054: if (mCloseOut) {
055: mWriter.close();
056: }
057: }
058:
059: /** @see com.puppycrawl.tools.checkstyle.api.AuditListener */
060: public void fileStarted(AuditEvent aEvt) {
061: mErrors = 0;
062: mWriter.println("Started checking file '" + aEvt.getFileName()
063: + "'.");
064: }
065:
066: /** @see com.puppycrawl.tools.checkstyle.api.AuditListener */
067: public void fileFinished(AuditEvent aEvt) {
068: mWriter.println("Finished checking file '" + aEvt.getFileName()
069: + "'. Errors: " + mErrors);
070: }
071:
072: /** @see com.puppycrawl.tools.checkstyle.api.AuditListener */
073: public void addError(AuditEvent aEvt) {
074: printEvent(aEvt);
075: if (SeverityLevel.ERROR.equals(aEvt.getSeverityLevel())) {
076: mErrors++;
077: mTotalErrors++;
078: }
079: }
080:
081: /** @see com.puppycrawl.tools.checkstyle.api.AuditListener */
082: public void addException(AuditEvent aEvt, Throwable aThrowable) {
083: printEvent(aEvt);
084: aThrowable.printStackTrace(System.out);
085: mErrors++;
086: mTotalErrors++;
087: }
088:
089: /**
090: * Prints event information to standard output.
091: * @param aEvt the event to print.
092: */
093: private void printEvent(AuditEvent aEvt) {
094: mWriter.println("Logging error -" + " file: '"
095: + aEvt.getFileName() + "'" + " line: " + aEvt.getLine()
096: + " column: " + aEvt.getColumn() + " severity: "
097: + aEvt.getSeverityLevel() + " message: "
098: + aEvt.getMessage() + " source: "
099: + aEvt.getSourceName());
100: }
101: }
|