Browse Source

Improve error messages when library loading fails during installation.

pull/245/head
weisj 4 years ago
parent
commit
6abca5cdd5
No known key found for this signature in database
GPG Key ID: 31124CB75461DA2A
  1. 3
      core/src/main/java/com/github/weisj/darklaf/LafInstaller.java
  2. 4
      native-utils/src/main/java/com/github/weisj/darklaf/platform/NativeUtil.java
  3. 79
      utils/src/main/java/com/github/weisj/darklaf/log/LogFormatter.java

3
core/src/main/java/com/github/weisj/darklaf/LafInstaller.java

@ -22,6 +22,7 @@
package com.github.weisj.darklaf; package com.github.weisj.darklaf;
import java.awt.Window; import java.awt.Window;
import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import javax.swing.SwingUtilities; import javax.swing.SwingUtilities;
@ -49,7 +50,7 @@ final class LafInstaller {
SwingUtilities.invokeLater(transition::runTransition); SwingUtilities.invokeLater(transition::runTransition);
notifyThemeInstalled(theme); notifyThemeInstalled(theme);
} catch (final UnsupportedLookAndFeelException e) { } catch (final UnsupportedLookAndFeelException e) {
e.printStackTrace(); LOGGER.log(Level.SEVERE, "Could not install LaF", e);
} }
} }

4
native-utils/src/main/java/com/github/weisj/darklaf/platform/NativeUtil.java

@ -91,14 +91,12 @@ public final class NativeUtil {
Path temp = temporaryDir.resolve(filename); Path temp = temporaryDir.resolve(filename);
try (InputStream is = NativeUtil.class.getResourceAsStream(path)) { try (InputStream is = NativeUtil.class.getResourceAsStream(path)) {
if (is == null) throw new FileNotFoundException("File " + path + " was not found inside JAR.");
if (!temporaryDir.toFile().canWrite()) throw new IOException("Can't write to temporary directory."); if (!temporaryDir.toFile().canWrite()) throw new IOException("Can't write to temporary directory.");
Files.copy(is, temp.toAbsolutePath(), StandardCopyOption.REPLACE_EXISTING); Files.copy(is, temp.toAbsolutePath(), StandardCopyOption.REPLACE_EXISTING);
} catch (final IOException e) { } catch (final IOException e) {
delete(temp); delete(temp);
throw e; throw e;
} catch (final NullPointerException e) {
delete(temp);
throw new FileNotFoundException("File " + path + " was not found inside JAR.");
} }
try { try {

79
utils/src/main/java/com/github/weisj/darklaf/log/LogFormatter.java

@ -21,15 +21,22 @@
*/ */
package com.github.weisj.darklaf.log; package com.github.weisj.darklaf.log;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date; import java.util.Date;
import java.util.IdentityHashMap;
import java.util.Set;
import java.util.logging.Formatter; import java.util.logging.Formatter;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.LogRecord; import java.util.logging.LogRecord;
import com.github.weisj.darklaf.util.StringUtil; import com.github.weisj.darklaf.util.StringUtil;
/** @author Jannis Weis */ /**
* @author Jannis Weis
*/
public class LogFormatter extends Formatter { public class LogFormatter extends Formatter {
public static final String ANSI_RESET = "\u001B[0m"; public static final String ANSI_RESET = "\u001B[0m";
public static final String ANSI_BLACK = "\u001B[30m"; public static final String ANSI_BLACK = "\u001B[30m";
@ -92,9 +99,79 @@ public class LogFormatter extends Formatter {
builder.append(ANSI_RESET); builder.append(ANSI_RESET);
builder.append("\n"); builder.append("\n");
if (record.getThrown() != null) {
builder.append(getMessageColor(record));
appendExceptionMessage(builder, record.getThrown());
}
return builder.toString(); return builder.toString();
} }
private void appendExceptionMessage(final StringBuilder builder, final Throwable throwable) {
builder.append(throwable.getClass().getCanonicalName()).append(": ");
builder.append(throwable.getMessage());
builder.append('\n');
StackTraceElement[] trace = throwable.getStackTrace();
for (StackTraceElement element : trace) {
builder.append("\tat ").append(element).append('\n');
}
Set<Throwable> dejaVu = Collections.newSetFromMap(new IdentityHashMap<>());
// Print suppressed exceptions, if any
for (Throwable se : throwable.getSuppressed()) {
printEnclosedStackTrace(builder, se, trace, "Suppressed: ", "\t", dejaVu);
}
// Print cause, if any
Throwable cause = throwable.getCause();
if (cause != null) {
printEnclosedStackTrace(builder, cause, trace, "Caused by: ", "", dejaVu);
}
}
private void printEnclosedStackTrace(final StringBuilder builder,
final Throwable throwable,
final StackTraceElement[] enclosingTrace,
final String caption,
final String prefix,
final Set<Throwable> dejaVu) {
if (dejaVu.contains(throwable)) {
builder.append(prefix).append(caption).append("[CIRCULAR REFERENCE: ").append(this).append("]\n");
} else {
dejaVu.add(throwable);
// Compute number of frames in common between this and enclosing trace
StackTraceElement[] trace = throwable.getStackTrace();
int m = trace.length - 1;
int n = enclosingTrace.length - 1;
while (m >= 0 && n >= 0 && trace[m].equals(enclosingTrace[n])) {
m--;
n--;
}
int framesInCommon = trace.length - 1 - m;
// Print the stack trace
builder.append(prefix).append(caption).append(throwable).append('\n');
for (int i = 0; i <= m; i++) {
builder.append(prefix).append("\tat ").append(trace[i]);
}
if (framesInCommon != 0) {
builder.append(prefix).append("\t... ").append(framesInCommon).append(" more\n");
}
// Print suppressed exceptions, if any
for (Throwable se : throwable.getSuppressed()) {
printEnclosedStackTrace(builder, se, trace, "Suppressed: ", prefix + "\t", dejaVu);
}
// Print cause, if any
Throwable cause = throwable.getCause();
if (cause != null) {
printEnclosedStackTrace(builder, cause, trace, "Caused by: ", prefix, dejaVu);
}
}
}
private String calculateDateString(final long milliseconds) { private String calculateDateString(final long milliseconds) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date(milliseconds); Date date = new Date(milliseconds);

Loading…
Cancel
Save