Skip to content

Commit

Permalink
fix: redirected process outputs to logs
Browse files Browse the repository at this point in the history
  • Loading branch information
bekoenig committed Aug 3, 2024
1 parent b0020ee commit 9406e05
Showing 1 changed file with 14 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import io.github.bekoenig.getdown.util.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.event.Level;

import javax.imageio.ImageIO;
import javax.swing.*;
Expand All @@ -27,6 +28,8 @@
import java.util.*;
import java.util.concurrent.TimeUnit;

import static java.util.stream.Collectors.joining;

/**
* Manages the main control for the Getdown application updater and deployment system.
*/
Expand Down Expand Up @@ -761,7 +764,7 @@ protected void launch() {

// spawn a daemon thread that will catch the early bits of stderr in case the
// launch fails
Thread t = new Thread(() -> copyStream(stderr, System.err));
Thread t = new Thread(() -> logStream(stderr, Level.ERROR));
t.setDaemon(true);
t.start();
}
Expand Down Expand Up @@ -1024,22 +1027,22 @@ protected boolean invokeDirect() {
protected abstract void exit(int exitCode);

/**
* Copies the supplied stream from the specified input to the specified output. Used to copy
* our child processes stderr and stdout to our own stderr and stdout.
* Logs the supplied stream from the specified input at level.
*/
protected static void copyStream(InputStream in, PrintStream out) {
protected static void logStream(InputStream in, Level level) {
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line;
while ((line = reader.readLine()) != null) {
out.print(line);
out.flush();
String result = new BufferedReader(new InputStreamReader(in))
.lines().collect(joining(System.lineSeparator()));

if (!result.isEmpty()) {
LOGGER.atLevel(level)
.setMessage(result)
.log();
}
} catch (IOException ioe) {
} catch (UncheckedIOException ioe) {
LOGGER.atWarn()
.setMessage("Failure copying")
.addKeyValue("in", in)
.addKeyValue("out", out)
.addKeyValue("error", ioe)
.log();
}
Expand Down

0 comments on commit 9406e05

Please sign in to comment.