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 17 public class VerboseListener 18 extends AutomaticBean 19 implements AuditListener 20 { 21 22 private PrintWriter mWriter = new PrintWriter (System.out); 23 24 25 private boolean mCloseOut = false; 26 27 28 private int mTotalErrors; 29 30 31 private int mErrors; 32 33 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 47 public void auditStarted(AuditEvent aEvt) 48 { 49 mTotalErrors = 0; 50 mWriter.println("Audit started."); 51 } 52 53 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 64 public void fileStarted(AuditEvent aEvt) 65 { 66 mErrors = 0; 67 mWriter.println( 68 "Started checking file '" + aEvt.getFileName() + "'."); 69 } 70 71 72 public void fileFinished(AuditEvent aEvt) 73 { 74 mWriter.println("Finished checking file '" + aEvt.getFileName() 75 + "'. Errors: " + mErrors); 76 } 77 78 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 89 public void addException(AuditEvent aEvt, Throwable aThrowable) 90 { 91 printEvent(aEvt); 92 aThrowable.printStackTrace(System.out); 93 mErrors++; 94 mTotalErrors++; 95 } 96 97 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 } 112 | Popular Tags |