001: package com.mycompany.listeners;
002:
003: import org.apache.commons.logging.Log;
004: import org.apache.commons.logging.LogConfigurationException;
005: import org.apache.commons.logging.LogFactory;
006:
007: import com.puppycrawl.tools.checkstyle.Checker;
008: import com.puppycrawl.tools.checkstyle.api.AuditEvent;
009: import com.puppycrawl.tools.checkstyle.api.AuditListener;
010: import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
011: import com.puppycrawl.tools.checkstyle.api.SeverityLevel;
012:
013: /**
014: * Jakarta Commons Logging listener.
015: * Note: With Ant, do not use the SimpleLog as your logger implementation as it
016: * causes an infinite loop since it writes to System.err, which Ant traps
017: * and reroutes to the logger/listener layer.
018: * Based on
019: * <a href="http://ant.apache.org/index.html">org.apache.tools.ant.listener.CommonsLoggingListener>org.apache.tools.ant.listener.CommonsLoggingListener</a>
020: * @author Rick Giles
021: */
022: public class CommonsLoggingListener implements AuditListener {
023: /** cushion for avoiding StringBuffer.expandCapacity */
024: private static final int BUFFER_CUSHION = 30;
025:
026: /** true if the log factory has been initialized */
027: private boolean mInitialized = false;
028:
029: /** Factory for creating org.apache.commons.logging.Log instances */
030: private LogFactory mLogFactory;
031:
032: /**
033: * Creates a <code>CommonsLoggingListener. Initializes its log factory.
034: * @throws CheckstyleException if if the implementation class is not
035: * available or cannot be instantiated.
036: */
037: public CommonsLoggingListener() throws CheckstyleException {
038: try {
039: mLogFactory = LogFactory.getFactory();
040: } catch (LogConfigurationException e) {
041: throw new CheckstyleException(
042: "log configuration exception", e);
043: }
044: mInitialized = true;
045: }
046:
047: /** @see com.puppycrawl.tools.checkstyle.api.AuditListener */
048: public void auditStarted(AuditEvent aEvt) {
049: if (mInitialized) {
050: final Log log = mLogFactory.getInstance(Checker.class);
051: log.info("Audit started.");
052: }
053: }
054:
055: /** @see com.puppycrawl.tools.checkstyle.api.AuditListener */
056: public void auditFinished(AuditEvent aEvt) {
057: if (mInitialized) {
058: final Log log = mLogFactory.getInstance(Checker.class);
059: log.info("Audit finished.");
060: }
061: }
062:
063: /** @see com.puppycrawl.tools.checkstyle.api.AuditListener */
064: public void fileStarted(AuditEvent aEvt) {
065: if (mInitialized) {
066: final Log log = mLogFactory.getInstance(Checker.class);
067: log.info("File \"" + aEvt.getFileName() + "\" started.");
068: }
069: }
070:
071: /** @see com.puppycrawl.tools.checkstyle.api.AuditListener */
072: public void fileFinished(AuditEvent aEvt) {
073: if (mInitialized) {
074: final Log log = mLogFactory.getInstance(Checker.class);
075: log.info("File \"" + aEvt.getFileName() + "\" finished.");
076: }
077: }
078:
079: /** @see com.puppycrawl.tools.checkstyle.api.AuditListener */
080: public void addError(AuditEvent aEvt) {
081: final SeverityLevel severityLevel = aEvt.getSeverityLevel();
082: if (mInitialized && !SeverityLevel.IGNORE.equals(severityLevel)) {
083: final Log log = mLogFactory.getInstance(aEvt
084: .getSourceName());
085:
086: final String fileName = aEvt.getFileName();
087: final String message = aEvt.getMessage();
088:
089: // avoid StringBuffer.expandCapacity
090: final int bufLen = message.length() + BUFFER_CUSHION;
091: final StringBuffer sb = new StringBuffer(bufLen);
092:
093: sb.append("Line: ").append(aEvt.getLine());
094: if (aEvt.getColumn() > 0) {
095: sb.append(" Column: ").append(aEvt.getColumn());
096: }
097: sb.append(" Message: ").append(message);
098:
099: if (aEvt.getSeverityLevel().equals(SeverityLevel.WARNING)) {
100: log.warn(sb.toString());
101: } else if (aEvt.getSeverityLevel().equals(
102: SeverityLevel.INFO)) {
103: log.info(sb.toString());
104: } else {
105: log.error(sb.toString());
106: }
107: }
108: }
109:
110: /** @see com.puppycrawl.tools.checkstyle.api.AuditListener */
111: public void addException(AuditEvent aEvt, Throwable aThrowable) {
112: if (mInitialized) {
113: final Log log = mLogFactory.getInstance(aEvt
114: .getSourceName());
115: log.error("Error auditing " + aEvt.getFileName(),
116: aThrowable);
117: }
118: }
119:
120: }
|