CommonsLoggingListenerpublic class CommonsLoggingListener extends Object implements org.apache.tools.ant.BuildListener, org.apache.tools.ant.BuildLoggerJakarta Commons Logging listener.
Note: do not use the SimpleLog as your logger implementation as it
causes an infinite loop since it writes to System.err, which Ant traps
and reroutes to the logger/listener layer.
The following names are used for the log:
org.apache.tools.ant.Project.PROJECT_NAME - for project events
org.apache.tools.ant.Target.TARGET_NAME - for target events
TASK_CLASS_NAME.TARGET_NAME - for events in individual targets.
In all target and project names we replace "." and " " with "-".
TODO: we should use the advanced context logging features (and expose them
in c-l first :-)
TODO: this is _very_ inefficient. Switching the out and tracking the logs
can be optimized a lot - but may require few more changes to the core. |
Fields Summary |
---|
private boolean | initializedIndicates if the listener was initialized. | private LogFactory | logFactory | public static final String | TARGET_LOGname of the category under which target events are logged | public static final String | PROJECT_LOGname of the category under which project events are logged | PrintStream | out | PrintStream | err |
Constructors Summary |
---|
public CommonsLoggingListener()Construct the listener and make sure that a LogFactory
can be obtained.
|
Methods Summary |
---|
public void | buildFinished(org.apache.tools.ant.BuildEvent event){@inheritDoc}.
if (initialized) {
String categoryString = PROJECT_LOG;
Log log = getLog(categoryString, event.getProject().getName());
if (event.getException() == null) {
realLog(log, "Build finished.", Project.MSG_INFO, null);
} else {
realLog(log, "Build finished with error.", Project.MSG_ERR,
event.getException());
}
}
| public void | buildStarted(org.apache.tools.ant.BuildEvent event){@inheritDoc}.
String categoryString = PROJECT_LOG;
Log log = getLog(categoryString, null);
if (initialized) {
realLog(log, "Build started.", Project.MSG_INFO, null);
}
| private org.apache.commons.logging.Log | getLog(java.lang.String cat, java.lang.String suffix)
if (suffix != null) {
suffix = suffix.replace('.", '-");
suffix = suffix.replace(' ", '-");
cat = cat + "." + suffix;
}
PrintStream tmpOut = System.out;
PrintStream tmpErr = System.err;
System.setOut(out);
System.setErr(err);
if (!initialized) {
try {
logFactory = LogFactory.getFactory();
} catch (LogConfigurationException e) {
e.printStackTrace(System.err);
return null;
}
}
initialized = true;
Log log = logFactory.getInstance(cat);
System.setOut(tmpOut);
System.setErr(tmpErr);
return log;
| public void | messageLogged(org.apache.tools.ant.BuildEvent event){@inheritDoc}.
if (initialized) {
Object categoryObject = event.getTask();
String categoryString = null;
String categoryDetail = null;
if (categoryObject == null) {
categoryObject = event.getTarget();
if (categoryObject == null) {
categoryObject = event.getProject();
categoryString = PROJECT_LOG;
categoryDetail = event.getProject().getName();
} else {
categoryString = TARGET_LOG;
categoryDetail = event.getTarget().getName();
}
} else {
// It's a task - append the target
if (event.getTarget() != null) {
categoryString = categoryObject.getClass().getName();
categoryDetail = event.getTarget().getName();
} else {
categoryString = categoryObject.getClass().getName();
}
}
Log log = getLog(categoryString, categoryDetail);
int priority = event.getPriority();
String message = event.getMessage();
realLog(log, message, priority , null);
}
| private void | realLog(org.apache.commons.logging.Log log, java.lang.String message, int priority, java.lang.Throwable t)
PrintStream tmpOut = System.out;
PrintStream tmpErr = System.err;
System.setOut(out);
System.setErr(err);
switch (priority) {
case Project.MSG_ERR:
if (t == null) {
log.error(message);
} else {
log.error(message, t);
}
break;
case Project.MSG_WARN:
if (t == null) {
log.warn(message);
} else {
log.warn(message, t);
}
break;
case Project.MSG_INFO:
if (t == null) {
log.info(message);
} else {
log.info(message, t);
}
break;
case Project.MSG_VERBOSE:
log.debug(message);
break;
case Project.MSG_DEBUG:
log.debug(message);
break;
default:
log.error(message);
break;
}
System.setOut(tmpOut);
System.setErr(tmpErr);
| public void | setEmacsMode(boolean emacsMode)Set emacs mode.
This is ignored.
// Doesn't make sense for c-l. Use the logger config
| public void | setErrorPrintStream(java.io.PrintStream err)Set the error print stream.
this.err = err;
| public void | setMessageOutputLevel(int level)Set the the output level.
This is not used, the logger config is used instead.
// CheckStyle:VisibilityModifier ON
// Use the logger config
| public void | setOutputPrintStream(java.io.PrintStream output)Set the output print stream.
this.out = output;
| public void | targetFinished(org.apache.tools.ant.BuildEvent event){@inheritDoc}.
if (initialized) {
String targetName = event.getTarget().getName();
Log log = getLog(TARGET_LOG,
event.getTarget().getName());
if (event.getException() == null) {
realLog(log, "Target end: " + targetName, Project.MSG_DEBUG, null);
} else {
realLog(log, "Target \"" + targetName
+ "\" finished with error.", Project.MSG_ERR,
event.getException());
}
}
| public void | targetStarted(org.apache.tools.ant.BuildEvent event){@inheritDoc}.
if (initialized) {
Log log = getLog(TARGET_LOG,
event.getTarget().getName());
// Since task log category includes target, we don't really
// need this message
realLog(log, "Start: " + event.getTarget().getName(),
Project.MSG_VERBOSE, null);
}
| public void | taskFinished(org.apache.tools.ant.BuildEvent event){@inheritDoc}.
if (initialized) {
Task task = event.getTask();
Object real = task;
if (task instanceof UnknownElement) {
Object realObj = ((UnknownElement) task).getTask();
if (realObj != null) {
real = realObj;
}
}
Log log = getLog(real.getClass().getName(), null);
if (event.getException() == null) {
if (log.isTraceEnabled()) {
realLog(log, "Task \"" + task.getTaskName() + "\" finished.",
Project.MSG_VERBOSE, null);
}
} else {
realLog(log, "Task \"" + task.getTaskName()
+ "\" finished with error.", Project.MSG_ERR,
event.getException());
}
}
| public void | taskStarted(org.apache.tools.ant.BuildEvent event){@inheritDoc}.
if (initialized) {
Task task = event.getTask();
Object real = task;
if (task instanceof UnknownElement) {
Object realObj = ((UnknownElement) task).getTask();
if (realObj != null) {
real = realObj;
}
}
Log log = getLog(real.getClass().getName(), null);
if (log.isTraceEnabled()) {
realLog(log, "Task \"" + task.getTaskName() + "\" started ",
Project.MSG_VERBOSE, null);
}
}
|
|