Browse Source

Make sure <record> lets go of files when stopped.

PR: 26020
Submitted by:	Martijn Kruithof


git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@276986 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 20 years ago
parent
commit
f40450a1ea
2 changed files with 75 additions and 21 deletions
  1. +11
    -14
      src/main/org/apache/tools/ant/taskdefs/Recorder.java
  2. +64
    -7
      src/main/org/apache/tools/ant/taskdefs/RecorderEntry.java

+ 11
- 14
src/main/org/apache/tools/ant/taskdefs/Recorder.java View File

@@ -146,7 +146,15 @@ public class Recorder extends Task {
RecorderEntry recorder = getRecorder(filename, getProject()); RecorderEntry recorder = getRecorder(filename, getProject());
// set the values on the recorder // set the values on the recorder
recorder.setMessageOutputLevel(loglevel); recorder.setMessageOutputLevel(loglevel);
recorder.setRecordState(start);
if (start != null) {
if (start.booleanValue()) {
recorder.reopenFile();
recorder.setRecordState(start);
} else {
recorder.setRecordState(start);
recorder.closeFile();
}
}
recorder.setEmacsMode(emacsMode); recorder.setEmacsMode(emacsMode);
} }


@@ -193,24 +201,13 @@ public class Recorder extends Task {


if (o == null) { if (o == null) {
// create a recorder entry // create a recorder entry
try {
entry = new RecorderEntry(name); entry = new RecorderEntry(name);


PrintStream out = null;

if (append == null) { if (append == null) {
out = new PrintStream(
new FileOutputStream(name));
entry.openFile(false);
} else { } else {
out = new PrintStream(
new FileOutputStream(name, append.booleanValue()));
entry.openFile(append.booleanValue());
} }
entry.setErrorPrintStream(out);
entry.setOutputPrintStream(out);
} catch (IOException ioe) {
throw new BuildException("Problems creating a recorder entry",
ioe);
}
entry.setProject(proj); entry.setProject(proj);
recorderEntries.put(name, entry); recorderEntries.put(name, entry);
} else { } else {


+ 64
- 7
src/main/org/apache/tools/ant/taskdefs/RecorderEntry.java View File

@@ -16,8 +16,11 @@
*/ */
package org.apache.tools.ant.taskdefs; package org.apache.tools.ant.taskdefs;


import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream; import java.io.PrintStream;
import org.apache.tools.ant.BuildEvent; import org.apache.tools.ant.BuildEvent;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.BuildLogger; import org.apache.tools.ant.BuildLogger;
import org.apache.tools.ant.DefaultLogger; import org.apache.tools.ant.DefaultLogger;
import org.apache.tools.ant.Project; import org.apache.tools.ant.Project;
@@ -28,7 +31,6 @@ import org.apache.tools.ant.util.StringUtils;
* This is a class that represents a recorder. This is the listener to the * This is a class that represents a recorder. This is the listener to the
* build process. * build process.
* *
* @version 0.5
* @since Ant 1.4 * @since Ant 1.4
*/ */
public class RecorderEntry implements BuildLogger, SubBuildListener { public class RecorderEntry implements BuildLogger, SubBuildListener {
@@ -80,6 +82,7 @@ public class RecorderEntry implements BuildLogger, SubBuildListener {
*/ */
public void setRecordState(Boolean state) { public void setRecordState(Boolean state) {
if (state != null) { if (state != null) {
flush();
record = state.booleanValue(); record = state.booleanValue();
} }
} }
@@ -93,6 +96,7 @@ public class RecorderEntry implements BuildLogger, SubBuildListener {
public void buildFinished(BuildEvent event) { public void buildFinished(BuildEvent event) {
log("< BUILD FINISHED", Project.MSG_DEBUG); log("< BUILD FINISHED", Project.MSG_DEBUG);


if (record && out != null) {
Throwable error = event.getException(); Throwable error = event.getException();


if (error == null) { if (error == null) {
@@ -102,6 +106,7 @@ public class RecorderEntry implements BuildLogger, SubBuildListener {
+ StringUtils.LINE_SEP); + StringUtils.LINE_SEP);
error.printStackTrace(out); error.printStackTrace(out);
} }
}
cleanup(); cleanup();
} }


@@ -145,7 +150,7 @@ public class RecorderEntry implements BuildLogger, SubBuildListener {
String time = formatTime(System.currentTimeMillis() - targetStartTime); String time = formatTime(System.currentTimeMillis() - targetStartTime);


log(event.getTarget() + ": duration " + time, Project.MSG_VERBOSE); log(event.getTarget() + ": duration " + time, Project.MSG_VERBOSE);
out.flush();
flush();
} }




@@ -156,7 +161,7 @@ public class RecorderEntry implements BuildLogger, SubBuildListener {


public void taskFinished(BuildEvent event) { public void taskFinished(BuildEvent event) {
log("<<< TASK FINISHED -- " + event.getTask(), Project.MSG_DEBUG); log("<<< TASK FINISHED -- " + event.getTask(), Project.MSG_DEBUG);
out.flush();
flush();
} }




@@ -191,10 +196,16 @@ public class RecorderEntry implements BuildLogger, SubBuildListener {
* @param level The verbosity level of the message. * @param level The verbosity level of the message.
*/ */
private void log(String mesg, int level) { private void log(String mesg, int level) {
if (record && (level <= loglevel)) {
if (record && (level <= loglevel) && out != null) {
out.println(mesg); out.println(mesg);
} }
} }
private void flush() {
if (record && out != null) {
out.flush();
}
}




public void setMessageOutputLevel(int level) { public void setMessageOutputLevel(int level) {
@@ -205,6 +216,7 @@ public class RecorderEntry implements BuildLogger, SubBuildListener {




public void setOutputPrintStream(PrintStream output) { public void setOutputPrintStream(PrintStream output) {
closeFile();
out = output; out = output;
} }


@@ -215,7 +227,7 @@ public class RecorderEntry implements BuildLogger, SubBuildListener {




public void setErrorPrintStream(PrintStream err) { public void setErrorPrintStream(PrintStream err) {
out = err;
setOutputPrintStream(err);
} }




@@ -254,12 +266,57 @@ public class RecorderEntry implements BuildLogger, SubBuildListener {
* @since 1.6.2 * @since 1.6.2
*/ */
public void cleanup() { public void cleanup() {
out.flush();
out.close();
closeFile();
if (project != null) { if (project != null) {
project.removeBuildListener(this); project.removeBuildListener(this);
} }
project = null; project = null;
} }
/**
* Initially opens the file associated with this recorder.
* Used by Recorder.
* @param append Indicates if output must be appended to the logfile or that
* the logfile should be overwritten.
* @throws BuildException
* @since 1.6.3
*/
void openFile(boolean append) throws BuildException {
openFileImpl(append);
}

/**
* Closes the file associated with this recorder.
* Used by Recorder.
* @since 1.6.3
*/
void closeFile() {
if (out != null) {
out.close();
out = null;
}
}
/**
* Re-opens the file associated with this recorder.
* Used by Recorder.
* @throws BuildException
* @since 1.6.3
*/
void reopenFile() throws BuildException {
openFileImpl(true);
}

private void openFileImpl(boolean append) throws BuildException {
if (out == null) {
try {
out = new PrintStream(new FileOutputStream(filename, append));
} catch (IOException ioe) {
throw new BuildException("Problems opening file using a " +
"recorder entry", ioe);
}
}
}

} }



Loading…
Cancel
Save