Exec changes to support QNX, use logging instead of printStackTrace
package com.aws.iot.evergreen.util;
+import com.aws.iot.evergreen.logging.api.Logger;
+import com.aws.iot.evergreen.logging.impl.LogManager;
import lombok.Getter;
import java.io.BufferedReader;
@@ -11,8 +13,6 @@ import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
-import java.io.PrintWriter;
-import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
@@ -44,6 +44,7 @@ import javax.annotation.Nullable;
*/
@SuppressWarnings("PMD.AvoidCatchingThrowable")
public final class Exec implements Closeable {
+ private static final Logger logger = LogManager.getLogger(Exec.class);
public static final boolean isWindows = System.getProperty("os.name").toLowerCase().contains("wind");
public static final String EvergreenUid = Utils.generateRandomString(16).toUpperCase();
private static final Consumer<CharSequence> NOP = s -> {
@@ -64,8 +65,7 @@ public final class Exec implements Closeable {
// after the .profile script is executed. Fire up a login shell, then grab it's
// path variable, but without using Exec shorthands to avoid initialization
// order paradoxes.
- Process hack = Runtime.getRuntime()
- .exec(new String[]{"bash", "-c", "echo 'echo $PATH'|bash --login|egrep':[^ ]'"});
+ Process hack = Runtime.getRuntime().exec(new String[]{"sh", "-c", "echo 'echo $PATH' | grep -E ':[^ ]'"});
StringBuilder path = new StringBuilder();
Thread bg = new Thread(() -> {
@@ -81,9 +81,9 @@ public final class Exec implements Closeable {
bg.join(2000);
addPathEntries(path.toString().trim());
// Ensure some level of sanity
- ensurePresent("/usr/local/bin", "/bin", "/usr/bin", "/sbin", "/usr/sbin", System.getProperty("java.home"));
+ ensurePresent("/bin", "/usr/bin", "/sbin", "/usr/sbin");
} catch (Throwable ex) {
- ex.printStackTrace(System.out);
+ logger.atError().log("Error while initializing PATH", ex);
}
computePathString();
}
@@ -129,14 +129,6 @@ public final class Exec implements Closeable {
return this;
}
- private static void appendStackTrace(Throwable ex, Consumer<CharSequence> a) {
- StringWriter sw = new StringWriter();
- PrintWriter pw = new PrintWriter(sw);
- Utils.getUltimateCause(ex).printStackTrace(pw);
- pw.flush();
- a.accept(sw.toString());
- }
-
public static String cmd(String... command) throws InterruptedException {
return new Exec().withExec(command).execAndGetStringOutput();
}
@@ -341,10 +333,8 @@ public final class Exec implements Closeable {
// so that the caller knows to be interrupted and stop processing so that the
// thread can shutdown gracefully
throw ex;
- } catch (Throwable ex) {
- if (stderr != null) {
- appendStackTrace(ex, stderr);
- }
+ } catch (IOException ex) {
+ logger.atError().kv("command", cmds).kv("pwd", dir).log("Error while running process", ex);
}
}
@@ -472,8 +462,8 @@ public final class Exec implements Closeable {
// TODO: configurable timeout?
process.waitFor(10, TimeUnit.SECONDS); // be graceful
setClosed();
- } catch (Throwable t) {
- t.printStackTrace(System.out);
+ } catch (InterruptedException ignore) {
+ // Ignore as this thread is done running anyway and will exit
}
}
}
