Updates to the DebugSnapshot system (stack-free logs, global disable switch, print once only)

This commit is contained in:
Reinier Zwitserloot committed 2011-08-01 22:26:51 +02:00
1 parent ced7a8d554
commit 3be2152a0e
2 files changed
+42 -6

No files matched your search

+13 -5
View File
@@ -22,9 +22,13 @@ public class DebugSnapshot implements Comparable<DebugSnapshot> {
public DebugSnapshot(CompilationUnitDeclaration owner, int stackHiding, String message, Object... params) {
this.when = System.currentTimeMillis();
this.bits = owner.bits;
StackTraceElement[] stackTrace = new Throwable().getStackTrace();
this.trace = new ArrayList<StackTraceElement>(Math.max(0, stackTrace.length - stackHiding - 1));
for (int i = 1 + stackHiding; i < stackTrace.length; i++) trace.add(stackTrace[i]);
if (stackHiding < 0) {
this.trace = null;
} else {
StackTraceElement[] stackTrace = new Throwable().getStackTrace();
this.trace = new ArrayList<StackTraceElement>(Math.max(0, stackTrace.length - stackHiding - 1));
for (int i = 1 + stackHiding; i < stackTrace.length; i++) trace.add(stackTrace[i]);
}
this.threadName = Thread.currentThread().getName();
this.message = message;
this.params = params == null ? new Object[0] : params;
@@ -53,8 +57,12 @@ public class DebugSnapshot implements Comparable<DebugSnapshot> {
@Override public String toString() {
StringBuilder out = new StringBuilder();
out.append(shortToString()).append("\n");
for (StackTraceElement elem : trace) {
out.append(" ").append(elem.toString()).append("\n");
if (trace == null) {
out.append(" Stack Omitted");
} else {
for (StackTraceElement elem : trace) {
out.append(" ").append(elem.toString()).append("\n");
}
}
return out.toString();
}
@@ -13,11 +13,13 @@ import org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration;
public class DebugSnapshotStore {
public static final DebugSnapshotStore INSTANCE = new DebugSnapshotStore();
public static boolean GLOBAL_DSS_DISABLE_SWITCH = true;
private final Map<CompilationUnitDeclaration, List<DebugSnapshot>> map =
new WeakHashMap<CompilationUnitDeclaration, List<DebugSnapshot>>();
public void snapshot(CompilationUnitDeclaration owner, String message, Object... params) {
if (GLOBAL_DSS_DISABLE_SWITCH) return;
DebugSnapshot snapshot = new DebugSnapshot(owner, 1, message, params);
List<DebugSnapshot> list;
@@ -26,18 +28,44 @@ public class DebugSnapshotStore {
if (list == null) {
list = new ArrayList<DebugSnapshot>();
map.put(owner, list);
list.add(snapshot);
} else if (!list.isEmpty()) {
list.add(snapshot);
} else {
// An empty list is an indicator that we no longer care about that particular CUD.
}
}
}
public void log(CompilationUnitDeclaration owner, String message, Object... params) {
if (GLOBAL_DSS_DISABLE_SWITCH) return;
DebugSnapshot snapshot = new DebugSnapshot(owner, -1, message, params);
List<DebugSnapshot> list;
synchronized (map) {
list = map.get(owner);
if (list == null) {
list = new ArrayList<DebugSnapshot>();
map.put(owner, list);
list.add(snapshot);
} else if (!list.isEmpty()) {
list.add(snapshot);
} else {
// An empty list is an indicator that we no longer care about that particular CUD.
}
list.add(snapshot);
}
}
public String print(CompilationUnitDeclaration owner, String message, Object... params) {
if (GLOBAL_DSS_DISABLE_SWITCH) return null;
List<DebugSnapshot> list;
synchronized (map) {
snapshot(owner, message == null ? "Printing" : message, params);
list = new ArrayList<DebugSnapshot>();
list.addAll(map.get(owner));
if (list.isEmpty()) return null; // An empty list is an indicator that we no longer care about that particular CUD.
map.get(owner).clear();
}
Collections.sort(list);