Moved reporting system to CoreBot; JSON-base reports

This commit is contained in:
Anuken
2018-08-29 22:54:15 -04:00
parent 9a41399178
commit 28b9de2e7a
7 changed files with 41 additions and 123 deletions

View File

@@ -1,21 +1,26 @@
package io.anuke.mindustry.desktop;
import com.badlogic.gdx.utils.JsonValue;
import com.badlogic.gdx.utils.JsonValue.ValueType;
import com.badlogic.gdx.utils.JsonWriter.OutputType;
import io.anuke.mindustry.Vars;
import io.anuke.mindustry.game.Version;
import io.anuke.mindustry.net.Net;
import io.anuke.ucore.core.Settings;
import io.anuke.ucore.util.Strings;
import io.anuke.ucore.util.Log;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.io.PrintWriter;
import java.io.StringWriter;
public class CrashHandler{
public static void handle(Throwable e){
e.printStackTrace();
//don't create crash logs for me (anuke), as it's expected
//also don't create logs for custom builds
if(System.getProperty("user.name").equals("anuke") || Version.build == -1) return;
boolean netActive = false, netServer = false;
//attempt to close connections, if applicable
@@ -27,47 +32,38 @@ public class CrashHandler{
p.printStackTrace();
}
//don't create crash logs for me (anuke), as it's expected
//if(System.getProperty("user.name").equals("anuke")) return;
JsonValue value = new JsonValue(ValueType.object);
String header = "--CRASH REPORT--\n";
boolean fn = netActive, fs = netServer;
//add all relevant info, ignoring exceptions
ex(() -> value.addChild("build", new JsonValue(Version.build)));
ex(() -> value.addChild("net", new JsonValue(fn)));
ex(() -> value.addChild("server", new JsonValue(fs)));
ex(() -> value.addChild("os", new JsonValue(System.getProperty("os.name"))));
ex(() -> value.addChild("multithreading", new JsonValue(Settings.getBool("multithread"))));
ex(() -> value.addChild("trace", new JsonValue(parseException(e))));
Log.info("Sending crash report.");
//post to crash report URL
Net.http(Vars.crashReportURL, "POST", value.toJson(OutputType.json), r -> System.exit(1), t -> System.exit(1));
//sleep forever
try{ Thread.sleep(Long.MAX_VALUE); }catch(InterruptedException ignored){}
}
private static String parseException(Throwable e){
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
return sw.toString();
}
private static void ex(Runnable r){
try{
header += "--GAME INFO--\n";
header += "Build: " + Version.build + "\n";
header += "Net Active: " + netActive + "\n";
header += "Net Server: " + netServer + "\n";
header += "OS: " + System.getProperty("os.name") + "\n";
header += "Multithreading: " + Settings.getBool("multithread") + "\n----\n";
}catch(Throwable e4){
header += "--error getting additional info--\n";
e4.printStackTrace();
}
//parse exception
String result = header + Strings.parseFullException(e);
boolean failed = false;
String filename = "";
Net.http(Vars.crashReportURL, "POST", result, r -> {}, Throwable::printStackTrace);
//try to write it
try{
filename = "crash-report-" + new SimpleDateFormat("dd-MM-yy h.mm.ss").format(new Date()) + ".txt";
Files.write(Paths.get(System.getProperty("user.home"), filename), result.getBytes());
}catch(Throwable i){
i.printStackTrace();
failed = true;
}
try{
javax.swing.JOptionPane.showMessageDialog(null, "An error has occured: \n" + result + "\n\n" +
(!failed ? "A crash report has been written to " + Paths.get(System.getProperty("user.home"), filename).toFile().getAbsolutePath() + ".\nPlease send this file to the developer!"
: "Failed to generate crash report.\nPlease send an image of this crash log to the developer!"));
}catch(Throwable i){
i.printStackTrace();
//what now?
r.run();
}catch(Throwable t){
t.printStackTrace();
}
}
}