Improved crash logs; source mod of crashes now included in report

This commit is contained in:
Anuken
2024-09-20 13:15:26 -04:00
parent c36e638826
commit 68dccab5ca
7 changed files with 71 additions and 40 deletions

View File

@@ -17,13 +17,17 @@ import java.util.*;
import static arc.Core.*;
import static mindustry.Vars.*;
public class CrashSender{
public class CrashHandler{
public static String createReport(String error){
String report = "Mindustry has crashed. How unfortunate.\n";
public static String createReport(Throwable exception){
String error = writeException(exception);
LoadedMod cause = getModCause(exception);
String report = cause == null ? "Mindustry has crashed. How unfortunate.\n" : "The mod '" + cause.meta.displayName + "' (" + cause.name + ")" + " has caused Mindustry to crash.\n";
if(mods != null && mods.list().size == 0 && Version.build != -1){
report += "Report this at " + Vars.reportIssueURL + "\n\n";
}
return report
+ "Version: " + Version.combined() + (Vars.headless ? " (Server)" : "") + "\n"
+ "OS: " + OS.osName + " x" + (OS.osArchBits) + " (" + OS.osArch + ")\n"
@@ -31,6 +35,7 @@ public class CrashSender{
+ "Java Version: " + OS.javaVersion + "\n"
+ "Runtime Available Memory: " + (Runtime.getRuntime().maxMemory() / 1024 / 1024) + "mb\n"
+ "Cores: " + Runtime.getRuntime().availableProcessors() + "\n"
+ (cause == null ? "" : "Likely Cause: " + cause.meta.displayName + " (" + cause.name + " v" + cause.meta.version + ")\n")
+ (mods == null ? "<no mod init>" : "Mods: " + (!mods.list().contains(LoadedMod::shouldBeEnabled) ? "none (vanilla)" : mods.list().select(LoadedMod::shouldBeEnabled).toString(", ", mod -> mod.name + ":" + mod.meta.version)))
+ "\n\n" + error;
}
@@ -38,12 +43,12 @@ public class CrashSender{
public static void log(Throwable exception){
try{
Core.settings.getDataDirectory().child("crashes").child("crash_" + System.currentTimeMillis() + ".txt")
.writeString(createReport(Strings.neatError(exception)));
.writeString(createReport(exception));
}catch(Throwable ignored){
}
}
public static void send(Throwable exception, Cons<File> writeListener){
public static void handle(Throwable exception, Cons<File> writeListener){
try{
try{
//log to file
@@ -59,14 +64,14 @@ public class CrashSender{
//don't create crash logs for custom builds, as it's expected
if(OS.username.equals("anuke") && !"steam".equals(Version.modifier)){
ret();
// System.exit(1);
}
//attempt to load version regardless
if(Version.number == 0){
try{
ObjectMap<String, String> map = new ObjectMap<>();
PropertiesUtils.load(map, new InputStreamReader(CrashSender.class.getResourceAsStream("/version.properties")));
PropertiesUtils.load(map, new InputStreamReader(CrashHandler.class.getResourceAsStream("/version.properties")));
Version.type = map.get("type");
Version.number = Integer.parseInt(map.get("number"));
@@ -87,7 +92,7 @@ public class CrashSender{
try{
File file = new File(OS.getAppDataDirectoryString(Vars.appName), "crashes/crash-report-" + new SimpleDateFormat("MM_dd_yyyy_HH_mm_ss").format(new Date()) + ".txt");
new Fi(OS.getAppDataDirectoryString(Vars.appName)).child("crashes").mkdirs();
new Fi(file).writeString(createReport(writeException(exception)));
new Fi(file).writeString(createReport(exception));
writeListener.get(file);
}catch(Throwable e){
Log.err("Failed to save local crash report.", e);
@@ -103,11 +108,41 @@ public class CrashSender{
death.printStackTrace();
}
ret();
System.exit(1);
}
private static void ret(){
System.exit(1);
/** @return the mod that is likely to have caused the supplied crash */
public static @Nullable LoadedMod getModCause(Throwable e){
if(Vars.mods == null) return null;
try{
for(var element : e.getStackTrace()){
String name = element.getClassName();
if(!name.matches("(mindustry|arc|java|javax|sun|jdk)\\..*")){
for(var mod : mods.list()){
if(mod.meta.main != null && getMatches(mod.meta.main, name) > 0){
return mod;
}else if(element.getFileName() != null && element.getFileName().endsWith(".js") && element.getFileName().startsWith(mod.name + "/")){
return mod;
}
}
}
}
}catch(Throwable ignored){}
return null;
}
private static int getMatches(String name1, String name2){
String[] arr1 = name1.split("\\."), arr2 = name2.split("\\.");
int matches = 0;
for(int i = 0; i < Math.min(arr1.length, arr2.length); i++){
if(!arr1[i].equals(arr2[i])){
return i;
}else if(!arr1[i].matches("net|org|com|io")){ //ignore common domain prefixes, as that's usually not enough to call something a "match"
matches ++;
}
}
return matches;
}
private static String writeException(Throwable e){