forked from j62/ctbrec
1
0
Fork 0

Write script output to temporary log file

This commit is contained in:
0xb00bface 2020-10-03 20:22:20 +02:00
parent 8cc6f7ae0e
commit 5fa939ae65
1 changed files with 14 additions and 9 deletions

View File

@ -1,5 +1,7 @@
package ctbrec.recorder.postprocessing;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
@ -35,11 +37,15 @@ public class Script extends AbstractPlaceholderAwarePostProcessor {
LOG.debug("Running {}", Arrays.toString(args));
}
Process process = rt.exec(args, OS.getEnvironment());
startLogging(process);
int exitCode = process.waitFor();
LOG.debug("Process finished with exit code {}", exitCode);
if (exitCode != 0) {
throw new ProcessExitedUncleanException("Script finished with exit code " + exitCode);
File logFile = File.createTempFile("execute_script_" + rec.getId() + "_", ".log");
logFile.deleteOnExit();
try (FileOutputStream logStream = new FileOutputStream(logFile)) {
startLogging(process, logStream);
int exitCode = process.waitFor();
LOG.debug("Process finished with exit code {}", exitCode);
if (exitCode != 0) {
throw new ProcessExitedUncleanException("Script finished with exit code " + exitCode);
}
}
return true;
}
@ -54,13 +60,12 @@ public class Script extends AbstractPlaceholderAwarePostProcessor {
return cmdline;
}
private void startLogging(Process process) {
// TODO maybe write these to a separate log file, e.g. recname.ts.script.log
Thread std = new Thread(new StreamRedirectThread(process.getInputStream(), System.out));
private void startLogging(Process process, FileOutputStream logStream) {
Thread std = new Thread(new StreamRedirectThread(process.getInputStream(), logStream));
std.setName("Process stdout pipe");
std.setDaemon(true);
std.start();
Thread err = new Thread(new StreamRedirectThread(process.getErrorStream(), System.err));
Thread err = new Thread(new StreamRedirectThread(process.getErrorStream(), logStream));
err.setName("Process stderr pipe");
err.setDaemon(true);
err.start();