mirror of
https://github.com/Card-Forge/forge.git
synced 2025-11-20 04:38:00 +00:00
Add support for printing trace information
This commit is contained in:
70
forge-gui/src/main/java/forge/FTrace.java
Normal file
70
forge-gui/src/main/java/forge/FTrace.java
Normal file
@@ -0,0 +1,70 @@
|
||||
package forge;
|
||||
|
||||
import java.text.NumberFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class FTrace {
|
||||
private static long appStartTime;
|
||||
private static Map<String, FTrace> traces = new HashMap<String, FTrace>();
|
||||
private static SimpleDateFormat guiFormatter = new SimpleDateFormat("hh:mm:ss.SSS");
|
||||
|
||||
public static void initialize() {
|
||||
appStartTime = new Date().getTime();
|
||||
}
|
||||
|
||||
public static FTrace get(String name0) {
|
||||
FTrace trace = traces.get(name0);
|
||||
if (trace == null) {
|
||||
trace = new FTrace(name0);
|
||||
traces.put(name0, trace);
|
||||
}
|
||||
return trace;
|
||||
}
|
||||
|
||||
public static String formatTimestamp(Date timestamp) {
|
||||
if (GuiBase.getInterface().isGuiThread()) {
|
||||
return guiFormatter.format(timestamp); //use cache formatter for better performance on GUI thread
|
||||
}
|
||||
return new SimpleDateFormat("hh:mm:ss.SSS").format(timestamp);
|
||||
}
|
||||
|
||||
//dump total time of all traces into log file
|
||||
public static void dump() {
|
||||
long appTotalTime = new Date().getTime() - appStartTime;
|
||||
NumberFormat percent = NumberFormat.getPercentInstance();
|
||||
|
||||
System.out.println();
|
||||
System.out.println("Forge total time - " + appTotalTime + "ms");
|
||||
for (FTrace trace : traces.values()) {
|
||||
System.out.println(trace.name + " total time - " + trace.totalTime + "ms (" + percent.format((double)trace.totalTime / (double)appTotalTime) + ")");
|
||||
}
|
||||
traces.clear();
|
||||
}
|
||||
|
||||
private final String name;
|
||||
private long startTime;
|
||||
private long totalTime;
|
||||
|
||||
private FTrace(String name0) {
|
||||
name = name0;
|
||||
}
|
||||
|
||||
public void start() {
|
||||
Date now = new Date();
|
||||
startTime = now.getTime();
|
||||
System.out.println(name + " start - " + formatTimestamp(now));
|
||||
}
|
||||
|
||||
public void end() {
|
||||
if (startTime == 0) { return; }
|
||||
|
||||
Date now = new Date();
|
||||
long elapsed = now.getTime() - startTime;
|
||||
startTime = 0;
|
||||
totalTime += elapsed;
|
||||
System.out.println(name + " end - " + formatTimestamp(now) + " (" + elapsed + "ms)");
|
||||
}
|
||||
}
|
||||
@@ -28,6 +28,7 @@ import java.lang.Thread.UncaughtExceptionHandler;
|
||||
|
||||
import com.esotericsoftware.minlog.Log;
|
||||
|
||||
import forge.FTrace;
|
||||
import forge.interfaces.IGuiBase;
|
||||
import forge.properties.ForgeConstants;
|
||||
import forge.util.MultiplexOutputStream;
|
||||
@@ -90,6 +91,7 @@ public class ExceptionHandler implements UncaughtExceptionHandler {
|
||||
System.setErr(new PrintStream(new MultiplexOutputStream(System.err, logFileStream), true));
|
||||
|
||||
Log.debug("Error handling registered!");
|
||||
FTrace.initialize();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -97,6 +99,7 @@ public class ExceptionHandler implements UncaughtExceptionHandler {
|
||||
* stream and resets the system output streams.
|
||||
*/
|
||||
public static void unregisterErrorHandling() throws IOException {
|
||||
FTrace.dump(); //dump trace before unregistering error handling
|
||||
System.setOut(oldSystemOut);
|
||||
System.setErr(oldSystemErr);
|
||||
logFileStream.close();
|
||||
|
||||
Reference in New Issue
Block a user