From 5fa939ae6550a531dd077c491305cedef909770a Mon Sep 17 00:00:00 2001 From: 0xb00bface <0xboobface@gmail.com> Date: Sat, 3 Oct 2020 20:22:20 +0200 Subject: [PATCH] Write script output to temporary log file --- .../recorder/postprocessing/Script.java | 23 +++++++++++-------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/common/src/main/java/ctbrec/recorder/postprocessing/Script.java b/common/src/main/java/ctbrec/recorder/postprocessing/Script.java index 9ae144e0..69cfb779 100644 --- a/common/src/main/java/ctbrec/recorder/postprocessing/Script.java +++ b/common/src/main/java/ctbrec/recorder/postprocessing/Script.java @@ -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();