Skip to content

Commit

Permalink
Print stderr/stdout from sauceconnect process
Browse files Browse the repository at this point in the history
  • Loading branch information
budziam committed Dec 23, 2024
1 parent 53fb62d commit d0d8414
Showing 1 changed file with 30 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONException;
import org.slf4j.Logger;
Expand Down Expand Up @@ -533,6 +534,11 @@ public Process openConnection(
final Process process =
prepAndCreateProcess(
username, apiKey, port, sauceConnectJar, options, printStream, sauceConnectPath);

// Print sauceconnect process stdout/stderr
new ProcessOutputPrinter(process.getInputStream(), (String x) -> logMessage(printStream, x)).start();
new ProcessOutputPrinter(process.getErrorStream(), (String x) -> logMessage(printStream, x)).start();

List<Process> openedProcesses = this.openedProcesses.get(name);
try {
Semaphore semaphore = new Semaphore(1);
Expand All @@ -544,7 +550,7 @@ public Process openConnection(
} else {
scMonitor = new SCMonitor("SCMonitor", port, LOGGER);
}

scMonitor.setSemaphore(semaphore);
scMonitor.start();

Expand Down Expand Up @@ -807,8 +813,30 @@ private void pollEndpoint() {
semaphore.release();
}
}

this.LOGGER.trace("No API response yet");
}
}

public class ProcessOutputPrinter extends Thread {
private final InputStream inputStream;
private final Consumer<String> printStream;

public ProcessOutputPrinter(InputStream inputStream, Consumer<String> printStream) {
this.inputStream = inputStream;
this.printStream = printStream;
}

@Override
public void run() {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
String line;
while ((line = reader.readLine()) != null) {
printStream.accept(line);
}
} catch (IOException e) {
LOGGER.error("Error reading process output", e);
}
}
}
}

0 comments on commit d0d8414

Please sign in to comment.