From bc3834e9d14c003786b1e508f26d7db5b0504c12 Mon Sep 17 00:00:00 2001 From: dgelessus <dgelessus@users.noreply.github.com> Date: Thu, 9 Nov 2023 16:25:26 +0100 Subject: [PATCH] Report more information when starting probcli To help with debugging errors where the connection to probcli cannot be established. This code is based on what the ProB Java API (ProB 2) does. --- de.prob.core/src/de/prob/cli/CliStarter.java | 29 +++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/de.prob.core/src/de/prob/cli/CliStarter.java b/de.prob.core/src/de/prob/cli/CliStarter.java index 782df75f..273591f6 100644 --- a/de.prob.core/src/de/prob/cli/CliStarter.java +++ b/de.prob.core/src/de/prob/cli/CliStarter.java @@ -67,6 +67,20 @@ public final class CliStarter { } } + /** + * Return {@code process}'s exit code as an {@link Integer}, or {@link Optional#empty()} if it is still running. + * + * @param process the process whose exit code to get + * @return {@code process}'s exit code, or {@link Optional#empty()} if it is still running + */ + private static Optional<Integer> getProcessExitCode(Process process) { + try { + return Optional.of(process.exitValue()); + } catch (final IllegalThreadStateException ignored) { + return Optional.empty(); + } + } + private void startProlog(final File file) throws CliException { prologProcess = null; @@ -125,7 +139,19 @@ public final class CliStarter { startErrorLogger(output); - extractCliInformation(input); + try { + extractCliInformation(input); + } catch (CliException e) { + // Check if the CLI exited while extracting the information. + final Optional<Integer> exitCode = getProcessExitCode(prologProcess); + if (exitCode.isPresent()) { + // CLI exited, report the exit code. + throw new CliException("ProB CLI exited with status " + exitCode.get() + " before socket connection could be opened", e, false); + } else { + // CLI didn't exit, just rethrow the error. + throw e; + } + } // log output from Prolog startOutputLogger(input); @@ -187,6 +213,7 @@ public final class CliStarter { String line; boolean endReached = false; while (!endReached && (line = input.readLine()) != null) { + Logger.info("probcli startup output: " + line); applyPatterns(patterns, line); endReached = patterns.isEmpty() || line.contains("starting command loop"); // printed in prob_socketserver.pl -- GitLab