Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The Diag tool should display the full stack trace (fixes #1020) #1024

Merged
merged 1 commit into from
Jul 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 57 additions & 28 deletions terminal/src/main/java/org/jline/terminal/impl/Diag.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import java.io.PrintStream;
import java.nio.charset.StandardCharsets;
import java.util.ServiceLoader;
import java.util.Arrays;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;
import java.util.concurrent.TimeUnit;
Expand All @@ -24,10 +24,26 @@
public class Diag {

public static void main(String[] args) {
diag(System.out);
diag(System.out, Arrays.asList(args).contains("--verbose"));
}

public static void diag(PrintStream out) {
diag(out, true);
}

public static void diag(PrintStream out, boolean verbose) {
new Diag(out, verbose).run();
}

private final PrintStream out;
private final boolean verbose;

public Diag(PrintStream out, boolean verbose) {
this.out = out;
this.verbose = verbose;
}

public void run() {
out.println("System properties");
out.println("=================");
out.println("os.name = " + System.getProperty("os.name"));
Expand Down Expand Up @@ -56,39 +72,39 @@ public static void diag(PrintStream out) {
out.println("=================");
try {
TerminalProvider provider = TerminalProvider.load("ffm");
testProvider(out, provider);
testProvider(provider);
} catch (Throwable t) {
out.println("FFM support not available: " + t);
error("FFM support not available", t);
}
out.println();

out.println("JnaSupport");
out.println("=================");
try {
TerminalProvider provider = TerminalProvider.load("jna");
testProvider(out, provider);
testProvider(provider);
} catch (Throwable t) {
out.println("JNA support not available: " + t);
error("JNA support not available", t);
}
out.println();

out.println("Jansi2Support");
out.println("=================");
try {
TerminalProvider provider = TerminalProvider.load("jansi");
testProvider(out, provider);
testProvider(provider);
} catch (Throwable t) {
out.println("Jansi 2 support not available: " + t);
error("Jansi 2 support not available", t);
}
out.println();

out.println("JniSupport");
out.println("=================");
try {
TerminalProvider provider = TerminalProvider.load("jni");
testProvider(out, provider);
testProvider(provider);
} catch (Throwable t) {
out.println("JNI support not available: " + t);
error("JNI support not available", t);
}
out.println();

Expand All @@ -97,26 +113,31 @@ public static void diag(PrintStream out) {
out.println("=================");
try {
TerminalProvider provider = TerminalProvider.load("exec");
testProvider(out, provider);
testProvider(provider);
} catch (Throwable t) {
out.println("Exec support not available: " + t);
error("Exec support not available", t);
}

if (!verbose) {
out.println();
out.println("Run with --verbose argument to print stack traces");
}
}

private static void testProvider(PrintStream out, TerminalProvider provider) {
private void testProvider(TerminalProvider provider) {
try {
out.println("StdIn stream = " + provider.isSystemStream(SystemStream.Input));
out.println("StdOut stream = " + provider.isSystemStream(SystemStream.Output));
out.println("StdErr stream = " + provider.isSystemStream(SystemStream.Error));
} catch (Throwable t2) {
out.println("Unable to check stream: " + t2);
} catch (Throwable t) {
error("Unable to check stream", t);
}
try {
out.println("StdIn stream name = " + provider.systemStreamName(SystemStream.Input));
out.println("StdOut stream name = " + provider.systemStreamName(SystemStream.Output));
out.println("StdErr stream name = " + provider.systemStreamName(SystemStream.Error));
} catch (Throwable t2) {
out.println("Unable to check stream names: " + t2);
} catch (Throwable t) {
error("Unable to check stream names", t);
}
try (Terminal terminal = provider.sysTerminal(
"diag",
Expand All @@ -131,9 +152,14 @@ private static void testProvider(PrintStream out, TerminalProvider provider) {
Attributes attr = terminal.enterRawMode();
try {
out.println("Terminal size: " + terminal.getSize());
ForkJoinTask<Integer> t =
new ForkJoinPool(1).submit(() -> terminal.reader().read(1));
int r = t.get(1000, TimeUnit.MILLISECONDS);
ForkJoinPool forkJoinPool = new ForkJoinPool(1);
try {
ForkJoinTask<Integer> t =
forkJoinPool.submit(() -> terminal.reader().read(1));
t.get(1000, TimeUnit.MILLISECONDS);
} finally {
forkJoinPool.shutdown();
}
StringBuilder sb = new StringBuilder();
sb.append("The terminal seems to work: ");
sb.append("terminal ").append(terminal.getClass().getName());
Expand All @@ -145,22 +171,25 @@ private static void testProvider(PrintStream out, TerminalProvider provider) {
.getName());
}
out.println(sb);
} catch (Throwable t3) {
out.println("Unable to read from terminal: " + t3);
t3.printStackTrace();
} catch (Throwable t2) {
error("Unable to read from terminal", t2);
} finally {
terminal.setAttributes(attr);
}
} else {
out.println("Not supported by provider");
}
} catch (Throwable t2) {
out.println("Unable to open terminal: " + t2);
t2.printStackTrace();
} catch (Throwable t) {
error("Unable to open terminal", t);
}
}

static <S> S load(Class<S> clazz) {
return ServiceLoader.load(clazz, clazz.getClassLoader()).iterator().next();
private void error(String message, Throwable cause) {
if (verbose) {
out.println(message);
cause.printStackTrace(out);
} else {
out.println(message + ": " + cause);
}
}
}