tags:

views:

284

answers:

3

I'm running Ant with output fed to a log file:

ant -logfile file.txt target-name

I'd also like to print some simple progress information to the console though. The answer seems to be a BuildEvent listener that writes to the console every time a new target is hit, but the documentation explicitly states:

A listener must not access System.out and System.err directly since ouput on these streams is redirected by Ant's core to the build event system.

Did I miss something? Is there a way to do this?

+2  A: 

Actually, the answer is Log4jListener.

There is a sample log4j configuration for logging into both console and file shown in the above link. You can then use an <echo> task with an appropriate level parameter to selectively decide what gets printed to console.

ChssPly76
+1  A: 

Ant replaces the System.out & System.err streams to remap messages printed there through it's own logging system.

That said, you can still get access to the ACTUAL OS streams by using java.io.FileDescriptor#out

Thanks for you answer. Before I accept your answer, I'm just hoping to get some insight into the possible pitfalls with this approach (I've elaborated a bit in my own answer to this question).
zakvdm
A: 

Thanks for the answers! I'm slow, but this is still something that I'd like to get right.

I've managed to get something working more or less like I want using carej's suggested approach with the java.io.FileDescriptor#out stream and an Ant scriptdef like this:

<scriptdef name="progress-text" language="javascript" >
  output = new java.io.PrintStream(new java.io.FileOutputStream(java.io.FileDescriptor.err))
  output.println(self.text)
</scriptdef>

Now I'm just left wondering how wize is this approach? Is there inherit risk in using the underlying OS streams directly?


EDIT: 2 Points which might be useful to anyone else with a similar question:

  1. This article has a very good description of the Ant I/O system: http://codefeed.com/blog/?p=68
  2. java.lang.System does something very similar to set System.out and System.err in the first place.

All of this gave me a little more confidence in this approach.

zakvdm