Search in sources :

Example 1 with MessageLogger

use of org.apache.ivy.util.MessageLogger in project ant-ivy by apache.

the class AntMessageLogger method register.

/**
 * Creates and register an {@link AntMessageLogger} for the given {@link Task}, with the given
 * {@link Ivy} instance.
 * <p>
 * The created instance will automatically be unregistered from the Ivy instance when the task
 * finishes.
 * </p>
 *
 * @param task
 *            the task the logger should use for logging
 * @param ivy
 *            the ivy instance on which the logger should be registered
 */
public static void register(ProjectComponent task, final Ivy ivy) {
    MessageLogger current = ivy.getLoggerEngine().peekLogger();
    if (current instanceof AntMessageLogger && task instanceof Task && ((AntMessageLogger) current).task instanceof Task) {
        Task currentTask = (Task) ((AntMessageLogger) current).task;
        if (currentTask.getTaskName() != null && currentTask.getTaskName().equals(((Task) task).getTaskName())) {
            // prefix as the given task. So we shouldn't do anything...
            return;
        }
    }
    AntMessageLogger logger = new AntMessageLogger(task);
    ivy.getLoggerEngine().pushLogger(logger);
    task.getProject().addBuildListener(new BuildListener() {

        private int stackDepth = 0;

        public void buildFinished(BuildEvent event) {
        }

        public void buildStarted(BuildEvent event) {
        }

        public void targetStarted(BuildEvent event) {
        }

        public void targetFinished(BuildEvent event) {
        }

        public void taskStarted(BuildEvent event) {
            stackDepth++;
        }

        public void taskFinished(BuildEvent event) {
            // NB: There is sometimes task created by an other task
            // in that case, we should not uninit Message. The log should stay associated
            // with the initial task, except if it was an antcall, ant or subant target
            // NB2 : Testing the identity of the task is not enough, event.getTask() return
            // an instance of UnknownElement is wrapping the concrete instance
            stackDepth--;
            if (stackDepth == -1) {
                ivy.getLoggerEngine().popLogger();
                event.getProject().removeBuildListener(this);
            }
        }

        public void messageLogged(BuildEvent event) {
        }
    });
}
Also used : BuildListener(org.apache.tools.ant.BuildListener) Task(org.apache.tools.ant.Task) AbstractMessageLogger(org.apache.ivy.util.AbstractMessageLogger) MessageLogger(org.apache.ivy.util.MessageLogger) BuildEvent(org.apache.tools.ant.BuildEvent)

Aggregations

AbstractMessageLogger (org.apache.ivy.util.AbstractMessageLogger)1 MessageLogger (org.apache.ivy.util.MessageLogger)1 BuildEvent (org.apache.tools.ant.BuildEvent)1 BuildListener (org.apache.tools.ant.BuildListener)1 Task (org.apache.tools.ant.Task)1