From 74e0aa0bd25f0c44646a0b3957f599ab3c8ca6d6 Mon Sep 17 00:00:00 2001 From: Andrei Zhukouski Date: Tue, 5 Jan 2016 10:39:07 +0300 Subject: [PATCH] Write output to separate streams (error and output) This will allow to write errors and output to the console handler as usual, but will help to handle errors from external binaries (such as aapt, executed by exec method) in all initialized logger handlers, not only console, but text handler, for ex. --- brut.j.util/src/main/java/brut/util/OS.java | 49 +++++++++++---------- 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/brut.j.util/src/main/java/brut/util/OS.java b/brut.j.util/src/main/java/brut/util/OS.java index aaa564e3..7c508187 100644 --- a/brut.j.util/src/main/java/brut/util/OS.java +++ b/brut.j.util/src/main/java/brut/util/OS.java @@ -19,6 +19,7 @@ package brut.util; import brut.common.BrutException; import java.io.*; import java.util.Arrays; +import java.util.logging.Logger; import org.apache.commons.io.IOUtils; @@ -26,6 +27,9 @@ import org.apache.commons.io.IOUtils; * @author Ryszard Wiśniewski */ public class OS { + + private static final Logger LOGGER = Logger.getLogger(""); + public static void rmdir(File dir) throws BrutException { if (! dir.exists()) { return; @@ -80,21 +84,19 @@ public class OS { public static void exec(String[] cmd) throws BrutException { Process ps = null; + int exitValue = -99; try { - ps = Runtime.getRuntime().exec(cmd); - - new StreamForwarder(ps.getInputStream(), System.err).start(); - new StreamForwarder(ps.getErrorStream(), System.err).start(); - if (ps.waitFor() != 0) { - throw new BrutException( - "could not exec command: " + Arrays.toString(cmd)); - } + ProcessBuilder builder = new ProcessBuilder(cmd); + ps = builder.start(); + new StreamForwarder(ps.getErrorStream(), "ERROR").start(); + new StreamForwarder(ps.getInputStream(), "OUTPUT").start(); + exitValue = ps.waitFor(); + if (exitValue != 0) + throw new BrutException("could not exec (exit code = " + exitValue + "): " + Arrays.toString(cmd)); } catch (IOException ex) { - throw new BrutException( - "could not exec command: " + Arrays.toString(cmd), ex); + throw new BrutException("could not exec: " + Arrays.toString(cmd), ex); } catch (InterruptedException ex) { - throw new BrutException( - "could not exec command: " + Arrays.toString(cmd), ex); + throw new BrutException("could not exec : " + Arrays.toString(cmd), ex); } } @@ -115,30 +117,29 @@ public class OS { static class StreamForwarder extends Thread { - public StreamForwarder(InputStream in, OutputStream out) { - mIn = in; - mOut = out; + StreamForwarder(InputStream is, String type) { + mIn = is; + mType = type; } @Override public void run() { try { - BufferedReader in = new BufferedReader( - new InputStreamReader(mIn)); - BufferedWriter out = new BufferedWriter( - new OutputStreamWriter(mOut)); + BufferedReader br = new BufferedReader(new InputStreamReader(mIn)); String line; - while ((line = in.readLine()) != null) { - out.write(line); - out.newLine(); + while ((line = br.readLine()) != null) { + if (mType.equals("OUTPUT")) { + LOGGER.info(line); + } else { + LOGGER.warning(line); + } } - out.flush(); } catch (IOException ex) { ex.printStackTrace(); } } private final InputStream mIn; - private final OutputStream mOut; + private final String mType; } }