01: /**
02: * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
03: */package net.sourceforge.pmd.util;
04:
05: import java.io.PrintWriter;
06: import java.io.StringWriter;
07: import java.util.logging.Handler;
08: import java.util.logging.Level;
09: import java.util.logging.LogRecord;
10:
11: import org.apache.tools.ant.Project;
12: import org.apache.tools.ant.Task;
13:
14: /**
15: * AntLogHandler sends log messages to an Ant Task, so the regular Ant logging
16: * is used.
17: *
18: * @author Wouter Zelle
19: */
20: public class AntLogHandler extends Handler {
21: private Task antTask;
22:
23: public AntLogHandler(Task antTask) {
24: this .antTask = antTask;
25: }
26:
27: public void publish(LogRecord logRecord) {
28: //Map the log levels from java.util.logging to Ant
29: int antLevel;
30: Level level = logRecord.getLevel();
31: if (level == Level.FINEST)
32: antLevel = Project.MSG_DEBUG; //Shown when -debug is supplied to Ant
33: else if (level == Level.FINE || level == Level.FINER
34: || level == Level.CONFIG)
35: antLevel = Project.MSG_VERBOSE; //Shown when -verbose is supplied to Ant
36: else if (level == Level.INFO)
37: antLevel = Project.MSG_INFO; //Always shown
38: else if (level == Level.WARNING)
39: antLevel = Project.MSG_WARN; //Always shown
40: else if (level == Level.SEVERE)
41: antLevel = Project.MSG_ERR; //Always shown
42: else
43: throw new IllegalStateException("Unknown logging level"); //shouldn't get ALL or NONE
44:
45: antTask.log(logRecord.getMessage(), antLevel);
46: if (logRecord.getThrown() != null) {
47: StringWriter stringWriter = new StringWriter();
48: PrintWriter printWriter = new PrintWriter(stringWriter,
49: true);
50: logRecord.getThrown().printStackTrace(printWriter);
51: antTask.log(stringWriter.toString(), antLevel);
52: }
53: }
54:
55: public void close() throws SecurityException {
56: return;
57: }
58:
59: public void flush() {
60: return;
61: }
62: }
|