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