Java Mailing List Archive

http://www.ant-tasks.com/

Apache Ant Archive

» Ant Users List
» Ant Developers List
Unexpected behaviour of Ant task logging

Unexpected behaviour of Ant task logging

2003-03-28       - By Joern Engmann

Hi!

I found an unexpected behavior of the Ant task. When I use a special logger
in the parent process, normally I want to use the same logger type for the
sub process. At this time the Ant task uses DefaultLogger in any way.
Additionally its not possible to set a special Logger in the Ant task.

The following code should solve the Problem. It would be nice if it could be
in the next releases of Ant.

   protected void initializeProject()
   {
       newProject.setInputHandler(getProject().getInputHandler());

       Vector listeners = getProject().getBuildListeners();
       final int count = listeners.size();
       for (int i = 0; i < count; i++) {
           newProject.addBuildListener((BuildListener)
listeners.elementAt(i));
       }

       if (output != null) {
           File outfile = null;
           if (dir != null) {
               outfile = FileUtils.newFileUtils().resolveFile(dir, output);
           } else {
               outfile = getProject().resolveFile(output);
           }
           //changed Code
           try {
               out = new PrintStream(new FileOutputStream(outfile));
               BuildLogger logger = null;
               if (loggerName!=null)
               {
                logger =
(BuildLogger)Class.forName(this.loggerName).newInstance();
               }
               else
               {
                int i = listeners.size()-1;
                while ((!(listeners.elementAt(i) instanceof BuildLogger))
&& (i > -1))
                 i--;
                if(i > -1)
                 logger =
(BuildLogger)listeners.elementAt(i).getClass().newInstance();
                else
                 logger = new DefaultLogger();
               }
               logger.setMessageOutputLevel(Project.MSG_INFO);
               logger.setOutputPrintStream(out);
               logger.setErrorPrintStream(out);
               newProject.addBuildListener(logger);
           } catch (Exception ex) {
               log("Ant: Can't set output to " + output);
           }
       }
...    
}

//additional code
protected String loggerName = null;
   public void setLogger(String logger)
   {
       this.loggerName = logger;
   }

©2008 ant-tasks.com - Jax Systems, LLC, U.S.A.