-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDebugSystem.java
More file actions
241 lines (198 loc) · 7.7 KB
/
Copy pathDebugSystem.java
File metadata and controls
241 lines (198 loc) · 7.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// DebugSystem.java
package cod.debug;
import java.util.*;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DebugSystem {
public enum Level {
OFF(0),
ERROR(1),
WARN(2),
INFO(3),
DEBUG(4),
TRACE(5);
private final int level;
Level(int level) {
this.level = level;
}
public int getLevel() {
return level;
}
}
private static Level currentLevel = Level.INFO;
private static boolean showTimestamp = true;
private static boolean showThread = false;
private static Map<String, Long> timers = new HashMap<String, Long>(); // Stores nanoseconds
private static SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss.SSS");
private static final boolean BENCHMARK_MODE = parseBenchmarkMode();
// Level-based timers (support nested same-name timers per thread)
private static final ThreadLocal<Map<String, Deque<Long>>> levelTimerStacks =
ThreadLocal.withInitial(HashMap::new);
private static final ThreadLocal<Map<String, Deque<Level>>> timerLevelStacks =
ThreadLocal.withInitial(HashMap::new);
private static boolean parseBenchmarkMode() {
String raw = System.getProperty("cod.benchmark.mode");
if (raw == null || raw.trim().isEmpty()) {
raw = System.getenv("COD_BENCHMARK_MODE");
}
return raw != null && "true".equalsIgnoreCase(raw.trim());
}
public static boolean isBenchmarkMode() {
return BENCHMARK_MODE;
}
public static void setLevel(Level level) {
currentLevel = level;
if (level == Level.OFF) {
clearTimerStateForCurrentThread();
}
}
public static void setShowTimestamp(boolean show) {
showTimestamp = show;
}
public static void setShowThread(boolean show) {
showThread = show;
}
public static void error(String category, String message) {
log(Level.ERROR, category, message);
}
public static void warn(String category, String message) {
log(Level.WARN, category, message);
}
public static void info(String category, String message) {
log(Level.INFO, category, message);
}
public static void debug(String category, String message) {
log(Level.DEBUG, category, message);
}
public static void trace(String category, String message) {
log(Level.TRACE, category, message);
}
public static void methodEntry(String methodName, Map<String, Object> params) {
if (shouldLog(Level.DEBUG)) {
debug("METHODS", "→ " + methodName + "(" + params + ")");
}
}
public static void methodExit(String methodName, Object result) {
if (shouldLog(Level.DEBUG)) {
debug("METHODS", "← " + methodName + " = " + result);
}
}
public static void slotUpdate(String slotName, Object oldValue, Object newValue) {
if (shouldLog(Level.DEBUG)) {
debug("SLOTS", "Slot " + slotName + ": " + oldValue + " → " + newValue);
}
}
public static void fieldUpdate(String fieldName, Object value) {
if (shouldLog(Level.DEBUG)) {
debug("FIELDS", "Field " + fieldName + " = " + value);
}
}
// Original timer - unchanged
public static void startTimer(String name) {
timers.put(name, System.nanoTime());
}
// New level-based timer
public static void startTimer(Level level, String name) {
if (shouldLog(level)) {
Map<String, Deque<Long>> startsByName = levelTimerStacks.get();
Deque<Long> starts = startsByName.computeIfAbsent(name, k -> new ArrayDeque<>());
starts.push(System.nanoTime());
Map<String, Deque<Level>> levelsByName = timerLevelStacks.get();
Deque<Level> levels = levelsByName.computeIfAbsent(name, k -> new ArrayDeque<>());
levels.push(level);
}
}
// Unified stopTimer - works for both original and level-based timers
public static double stopTimer(String name) {
// Check level-based timers first (LIFO for nested same-name timers)
Map<String, Deque<Long>> startsByName = levelTimerStacks.get();
Deque<Long> starts = startsByName.get(name);
if (starts != null && !starts.isEmpty()) {
long levelStart = starts.pop();
if (starts.isEmpty()) {
startsByName.remove(name);
}
Map<String, Deque<Level>> levelsByName = timerLevelStacks.get();
Deque<Level> levels = levelsByName.get(name);
Level originalLevel = null;
if (levels != null && !levels.isEmpty()) {
originalLevel = levels.pop();
if (levels.isEmpty()) {
levelsByName.remove(name);
}
}
long durationNs = System.nanoTime() - levelStart;
double durationMs = durationNs / 1_000_000.0;
if (originalLevel != null && shouldLog(originalLevel)) {
log(originalLevel, "PERF", String.format("%s took %.3f ms", name, durationMs));
}
return durationMs;
}
// Original timer behavior
Long start = timers.remove(name);
if (start != null) {
long durationNs = System.nanoTime() - start;
double durationMs = durationNs / 1_000_000.0;
if (shouldLog(Level.DEBUG)) {
debug("PERF", String.format("%s took %.3f ms", name, durationMs));
}
return durationMs;
}
return -1.0;
}
public static double getTimerDuration(String name) {
// Check level-based timers first
Map<String, Deque<Long>> startsByName = levelTimerStacks.get();
Deque<Long> starts = startsByName.get(name);
if (starts != null && !starts.isEmpty()) {
long durationNs = System.nanoTime() - starts.peek();
return durationNs / 1_000_000.0;
}
// Original timer
Long start = timers.get(name);
if (start != null) {
long durationNs = System.nanoTime() - start;
return durationNs / 1_000_000.0;
}
return -1.0;
}
public static void astBuilding(String nodeType, String details) {
if (shouldLog(Level.TRACE)) {
trace("AST", "Building " + nodeType + ": " + details);
}
}
public static void astComplete(String nodeType, String summary) {
if (shouldLog(Level.DEBUG)) {
debug("AST", "Built " + nodeType + " → " + summary);
}
}
private static void log(Level level, String category, String message) {
if (shouldLog(level)) {
StringBuilder logLine = new StringBuilder();
if (showTimestamp) {
logLine.append("[")
.append(timeFormat.format(new Date()))
.append("] ");
}
logLine.append("[").append(level).append("] ");
if (showThread) {
logLine.append("[")
.append(Thread.currentThread().getName())
.append("] ");
}
logLine.append(category).append(": ").append(message);
System.out.println(logLine.toString());
}
}
private static boolean shouldLog(Level level) {
if (currentLevel == Level.OFF) return false;
return level.getLevel() <= currentLevel.getLevel();
}
public static Level getLevel() {
return currentLevel;
}
public static void clearTimerStateForCurrentThread() {
levelTimerStacks.remove();
timerLevelStacks.remove();
}
}