forked from hborders/JavaInJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrace.java
More file actions
executable file
·265 lines (236 loc) · 8.63 KB
/
Copy pathTrace.java
File metadata and controls
executable file
·265 lines (236 loc) · 8.63 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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*
* Copyright (c) 2007 Sun Microsystems, Inc. All rights reserved.
*
* Sun Microsystems, Inc. has intellectual property rights relating to technology embodied in the product
* that is described in this document. In particular, and without limitation, these intellectual property
* rights may include one or more of the U.S. patents listed at http://www.sun.com/patents and one or
* more additional patents or pending patent applications in the U.S. and in other countries.
*
* U.S. Government Rights - Commercial software. Government users are subject to the Sun
* Microsystems, Inc. standard license agreement and applicable provisions of the FAR and its
* supplements.
*
* Use is subject to license terms. Sun, Sun Microsystems, the Sun logo, Java and Solaris are trademarks or
* registered trademarks of Sun Microsystems, Inc. in the U.S. and other countries. All SPARC trademarks
* are used under license and are trademarks or registered trademarks of SPARC International, Inc. in the
* U.S. and other countries.
*
* UNIX is a registered trademark in the U.S. and other countries, exclusively licensed through X/Open
* Company, Ltd.
*/
package com.sun.max.program;
import java.io.*;
import com.sun.max.annotate.*;
import com.sun.max.io.*;
import com.sun.max.program.option.*;
/**
* Tracing output for debugging purposes. No performance impact when disabled. Some performance impact when active, even
* without output. Possibly significant performance impact when producing a lot of output.
*
* @author Bernd Mathiske
*/
public final class Trace {
private Trace() {
// do nothing.
}
private static PrintStream stream;
public static PrintStream stream() {
return stream;
}
public static void setStream(PrintStream stream) {
Trace.stream = stream;
}
private static final boolean showThread;
static {
showThread = System.getProperty("max.trace.showThread") != null;
final String traceFileName = System.getProperty("max.trace.file");
stream = System.out;
if (traceFileName != null) {
final File traceFile = new File(traceFileName);
try {
final OutputStream fileStream = new BufferedOutputStream(new FileOutputStream(traceFile));
if (System.getProperty("max.trace.noconsole") != null) {
stream = new PrintStream(fileStream);
} else {
stream = new PrintStream(new MultiOutputStream(fileStream, System.out));
}
} catch (IOException ioException) {
System.err.println("Could not open file for trace output: " + traceFile.getAbsolutePath());
}
}
}
/**
* Static master switch. Set by source code editing only (for now).
*
* Set '_enabled' to 'true' to enable tracing output, according to your other trace-related settings.
*
* Set '_enabled' to 'false' to prevent any tracing. All tracing routines will thus become dead code. The optimizing
* compiler should then be able to eliminate the runtime overhead.
*/
private static final boolean ENABLED = true;
public static void addTo(OptionSet options) {
options.addOption(new Option<Integer>("trace", 0, OptionTypes.INT_TYPE, "Sets tracing level.") {
@Override
public void setValue(Integer value) {
super.setValue(value);
level = value;
}
});
}
/**
* Dynamically sets tracing level, causing trace commands at this or lower levels to produce output.
*/
public static void on(int newLevel) {
assert newLevel >= 0;
level = newLevel;
}
/**
* Dynamically turns tracing on for all levels.
*/
public static void on() {
on(Integer.MAX_VALUE);
}
@RESET
private static long count;
/**
* The threshold of trace calls before traces are actually sent to the trace stream.
*
* This field can be updated by the inspector.
*/
@RESET
@INSPECTED
private static long threshold;
/**
* The current trace level.
*
* This field can be updated by the inspector.
*/
@RESET
@INSPECTED
private static int level;
/**
* Dynamically sets the current tracing level to the greater of the current level or the specified new level.
*/
public static void atLeast(int l) {
if (l > level) {
level = l;
}
}
/**
* Dynamically turns tracing off by setting current level to zero.
*/
public static void off() {
level = 0;
}
/**
* @return current tracing level, which must equal or exceed the level specified by trace commands for output to be produced.
*/
public static int level() {
return level;
}
/**
* Does the current tracing level equal or exceed the specified level.
*/
public static boolean hasLevel(int requiredLevel) {
count++;
return level >= requiredLevel && count >= threshold;
}
private static final int MAX_INDENTATION = 10;
@RESET
private static int indentation;
private static void printInt(int n) {
if (n < 10) {
stream.write(((char) n) + '0');
} else {
final int m = n / 10;
printInt(m);
printInt(n - (m * 10));
}
}
/**
* This should not cause allocation/GC.
*/
private static void printPrefix(int requiredLevel) {
if (showThread) {
stream.print(Thread.currentThread().getName() + " <Trace");
} else {
stream.print("<Trace ");
}
printInt(requiredLevel);
stream.print("> ");
for (int i = 0; i < indentation; i++) {
stream.print(" ");
}
}
/**
* Prints a newline on trace output if tracing is globally enabled and current tracing level is at least the level required.
*/
public static void line(int requiredLevel) {
if (ENABLED) {
if (hasLevel(requiredLevel)) {
stream.println();
stream.flush();
}
}
}
/**
* Prints a line of trace output if tracing is globally enabled and if current tracing level is at least the level required.
*/
public static void line(int requiredLevel, Object message) {
if (ENABLED) {
if (hasLevel(requiredLevel)) {
printPrefix(requiredLevel);
stream.println(message);
stream.flush();
}
}
}
/**
* Prints a "BEGIN" line of trace output if tracing is globally enabled and if current tracing level is at least the level required; increases indentation.
*/
public static void begin(int requiredLevel, Object message) {
if (ENABLED) {
if (hasLevel(requiredLevel)) {
printPrefix(requiredLevel);
stream.print("BEGIN: ");
stream.println(message);
stream.flush();
indentation++;
}
}
}
/**
* Prints an "END" line of trace output if tracing is globally enabled and if current tracing level is at least the level required; decreases indentation.
*/
public static void end(int requiredLevel, Object message) {
end(requiredLevel, message, 0);
}
/**
* Prints an "END" line of trace output if tracing is globally enabled and if current tracing level is at least the level required; decreases indentation;
* appends a timing message, expressed in milliseconds, if a non-zero starting time is supplied.
* @param requiredLevel
* @param message
* @param startTimeMillis a starting time, output from {@link System#currentTimeMillis()}; no timing message appears if zero.
*/
public static void end(int requiredLevel, Object message, long startTimeMillis) {
if (ENABLED) {
if (hasLevel(requiredLevel)) {
final long endTimeMillis = System.currentTimeMillis();
indentation--;
// It's quite possible for indentation to go negative in a multithreaded environment
//assert _indentation >= 0;
printPrefix(requiredLevel);
stream.print("END: ");
if (startTimeMillis > 0) {
stream.print(message);
stream.print(" (");
stream.print(endTimeMillis - startTimeMillis);
stream.println("ms)");
} else {
stream.println(message);
stream.flush();
}
}
}
}
}