处理未捕获异常时立即进行log,防止异常被忽略

THROWABLE_SET改名为CAUGHT_EXCEPTIONS,使用并发安全集合
This commit is contained in:
yushijinhun
2018-06-17 15:02:40 +08:00
parent bba1c559a3
commit c4608a949b

View File

@@ -22,11 +22,16 @@ import org.jackhuang.hmcl.Launcher;
import org.jackhuang.hmcl.ui.CrashWindow; import org.jackhuang.hmcl.ui.CrashWindow;
import org.jackhuang.hmcl.ui.construct.MessageBox; import org.jackhuang.hmcl.ui.construct.MessageBox;
import static java.util.Collections.newSetFromMap;
import static org.jackhuang.hmcl.util.Logging.LOG;
import java.io.IOException; import java.io.IOException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level; import java.util.logging.Level;
/** /**
@@ -34,7 +39,7 @@ import java.util.logging.Level;
*/ */
public class CrashReporter implements Thread.UncaughtExceptionHandler { public class CrashReporter implements Thread.UncaughtExceptionHandler {
private static final HashMap<String, String> SOURCE = new HashMap<String, String>() { private static final Map<String, String> SOURCE = new HashMap<String, String>() {
{ {
put("javafx.fxml.LoadException", Launcher.i18n("crash.NoClassDefFound")); put("javafx.fxml.LoadException", Launcher.i18n("crash.NoClassDefFound"));
put("Location is not set", Launcher.i18n("crash.NoClassDefFound")); put("Location is not set", Launcher.i18n("crash.NoClassDefFound"));
@@ -59,11 +64,11 @@ public class CrashReporter implements Thread.UncaughtExceptionHandler {
if (s.contains(entry.getKey())) { if (s.contains(entry.getKey())) {
if (StringUtils.isNotBlank(entry.getValue())) { if (StringUtils.isNotBlank(entry.getValue())) {
String info = entry.getValue(); String info = entry.getValue();
Logging.LOG.severe(info); LOG.severe(info);
try { try {
MessageBox.show(info); MessageBox.show(info);
} catch (Throwable t) { } catch (Throwable t) {
Logging.LOG.log(Level.SEVERE, "Unable to show message", t); LOG.log(Level.SEVERE, "Unable to show message", t);
} }
} }
return false; return false;
@@ -71,17 +76,21 @@ public class CrashReporter implements Thread.UncaughtExceptionHandler {
return true; return true;
} }
private static Set<String> CAUGHT_EXCEPTIONS = newSetFromMap(new ConcurrentHashMap<>());
@Override @Override
public void uncaughtException(Thread t, Throwable e) { public void uncaughtException(Thread t, Throwable e) {
LOG.log(Level.SEVERE, "Uncaught exception in thread " + t.getName(), e);
try {
String stackTrace = StringUtils.getStackTrace(e); String stackTrace = StringUtils.getStackTrace(e);
if (!stackTrace.contains("org.jackhuang")) if (!stackTrace.contains("org.jackhuang"))
return; return;
if (THROWABLE_SET.contains(stackTrace)) if (CAUGHT_EXCEPTIONS.contains(stackTrace))
return; return;
THROWABLE_SET.add(stackTrace); CAUGHT_EXCEPTIONS.add(stackTrace);
try {
String text = "---- Hello Minecraft! Crash Report ----\n" + String text = "---- Hello Minecraft! Crash Report ----\n" +
" Version: " + Launcher.VERSION + "\n" + " Version: " + Launcher.VERSION + "\n" +
" Time: " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + "\n" + " Time: " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + "\n" +
@@ -93,21 +102,18 @@ public class CrashReporter implements Thread.UncaughtExceptionHandler {
" Java Version: " + System.getProperty("java.version") + ", " + System.getProperty("java.vendor") + "\n" + " Java Version: " + System.getProperty("java.version") + ", " + System.getProperty("java.vendor") + "\n" +
" Java VM Version: " + System.getProperty("java.vm.name") + " (" + System.getProperty("java.vm.info") + "), " + System.getProperty("java.vm.vendor") + "\n"; " Java VM Version: " + System.getProperty("java.vm.name") + " (" + System.getProperty("java.vm.info") + "), " + System.getProperty("java.vm.vendor") + "\n";
Logging.LOG.log(Level.SEVERE, text); LOG.log(Level.SEVERE, text);
if (checkThrowable(e)) { if (checkThrowable(e)) {
Platform.runLater(() -> new CrashWindow(text).show()); Platform.runLater(() -> new CrashWindow(text).show());
if (!Launcher.UPDATE_CHECKER.isOutOfDate()) if (!Launcher.UPDATE_CHECKER.isOutOfDate())
reportToServer(text); reportToServer(text);
} }
} catch (Throwable ex) { } catch (Throwable handlingException) {
Logging.LOG.log(Level.SEVERE, "Unable to caught exception", ex); LOG.log(Level.SEVERE, "Unable to handle uncaught exception", handlingException);
Logging.LOG.log(Level.SEVERE, "There is the original exception", e);
} }
} }
private static final HashSet<String> THROWABLE_SET = new HashSet<>();
private void reportToServer(final String text) { private void reportToServer(final String text) {
Thread t = new Thread(() -> { Thread t = new Thread(() -> {
HashMap<String, String> map = new HashMap<>(); HashMap<String, String> map = new HashMap<>();
@@ -117,9 +123,9 @@ public class CrashReporter implements Thread.UncaughtExceptionHandler {
try { try {
String response = NetworkUtils.doPost(NetworkUtils.toURL("https://huangyuhui.duapp.com/hmcl/crash.php"), map); String response = NetworkUtils.doPost(NetworkUtils.toURL("https://huangyuhui.duapp.com/hmcl/crash.php"), map);
if (StringUtils.isNotBlank(response)) if (StringUtils.isNotBlank(response))
Logging.LOG.log(Level.SEVERE, "Crash server response: " + response); LOG.log(Level.SEVERE, "Crash server response: " + response);
} catch (IOException ex) { } catch (IOException ex) {
Logging.LOG.log(Level.SEVERE, "Unable to post HMCL server.", ex); LOG.log(Level.SEVERE, "Unable to post HMCL server.", ex);
} }
}); });
t.setDaemon(true); t.setDaemon(true);