1 package com.puppycrawl.tools.checkstyle; 20 21 import java.io.OutputStream ; 22 import java.io.PrintWriter ; 23 24 import com.puppycrawl.tools.checkstyle.api.AuditEvent; 25 import com.puppycrawl.tools.checkstyle.api.AuditListener; 26 import com.puppycrawl.tools.checkstyle.api.AutomaticBean; 27 import com.puppycrawl.tools.checkstyle.api.SeverityLevel; 28 29 39 public class DefaultLogger 40 extends AutomaticBean 41 implements AuditListener 42 { 43 44 private static final int BUFFER_CUSHION = 12; 45 46 47 private final PrintWriter mInfoWriter; 48 49 private final boolean mCloseInfo; 50 51 52 private final PrintWriter mErrorWriter; 53 54 private final boolean mCloseError; 55 56 61 public DefaultLogger(OutputStream aOS, boolean aCloseStreamsAfterUse) 62 { 63 this(aOS, aCloseStreamsAfterUse, aOS, false); 65 } 66 67 75 public DefaultLogger(OutputStream aInfoStream, 76 boolean aCloseInfoAfterUse, 77 OutputStream aErrorStream, 78 boolean aCloseErrorAfterUse) 79 { 80 mCloseInfo = aCloseInfoAfterUse; 81 mCloseError = aCloseErrorAfterUse; 82 mInfoWriter = new PrintWriter (aInfoStream); 83 mErrorWriter = (aInfoStream == aErrorStream) 84 ? mInfoWriter 85 : new PrintWriter (aErrorStream); 86 } 87 88 94 public void addError(AuditEvent aEvt) 95 { 96 final SeverityLevel severityLevel = aEvt.getSeverityLevel(); 97 if (!SeverityLevel.IGNORE.equals(severityLevel)) { 98 99 final String fileName = aEvt.getFileName(); 100 final String message = aEvt.getMessage(); 101 102 final int bufLen = fileName.length() + message.length() 104 + BUFFER_CUSHION; 105 final StringBuffer sb = new StringBuffer (bufLen); 106 107 sb.append(fileName); 108 sb.append(':').append(aEvt.getLine()); 109 if (aEvt.getColumn() > 0) { 110 sb.append(':').append(aEvt.getColumn()); 111 } 112 if (SeverityLevel.WARNING.equals(severityLevel)) { 113 sb.append(": warning"); 114 } 115 sb.append(": ").append(message); 116 mErrorWriter.println(sb.toString()); 117 } 118 } 119 120 121 public void addException(AuditEvent aEvt, Throwable aThrowable) 122 { 123 synchronized (mErrorWriter) { 124 mErrorWriter.println("Error auditing " + aEvt.getFileName()); 125 aThrowable.printStackTrace(mErrorWriter); 126 } 127 } 128 129 130 public void auditStarted(AuditEvent aEvt) 131 { 132 mInfoWriter.println("Starting audit..."); 133 } 134 135 136 public void fileFinished(AuditEvent aEvt) 137 { 138 } 139 140 141 public void fileStarted(AuditEvent aEvt) 142 { 143 } 144 145 146 public void auditFinished(AuditEvent aEvt) 147 { 148 mInfoWriter.println("Audit done."); 149 closeStreams(); 150 } 151 152 155 protected void closeStreams() 156 { 157 mInfoWriter.flush(); 158 if (mCloseInfo) { 159 mInfoWriter.close(); 160 } 161 162 mErrorWriter.flush(); 163 if (mCloseError) { 164 mErrorWriter.close(); 165 } 166 } 167 } 168 | Popular Tags |