-
Notifications
You must be signed in to change notification settings - Fork 0
/
Debug.java
54 lines (46 loc) · 1.59 KB
/
Debug.java
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
package simpledb;
/**
* Debug is a utility class that wraps println statements and allows
* more or less command line output to be turned on.
* <p>
* Change the value of the DEBUG_LEVEL constant using a system property:
* simpledb.Debug. For example, on the command line, use -Dsimpledb.Debug=x,
* or simply -Dsimpledb.Debug to enable it at level 0.
* The log(level, message, ...) method will print to standard output if the
* level number is less than or equal to the currently set DEBUG_LEVEL.
*/
public class Debug {
private static final int DEBUG_LEVEL;
static {
String debug = System.getProperty("simpledb.Debug");
if (debug == null) {
// No system property = disabled
DEBUG_LEVEL = -1;
} else if (debug == "") {
// Empty property = level 0
DEBUG_LEVEL = 0;
} else {
DEBUG_LEVEL = Integer.parseInt(debug);
}
}
private static final int DEFAULT_LEVEL = 0;
/** Log message if the log level >= level. Uses printf. */
public static void log(int level, String message, Object... args) {
if (isEnabled(level)) {
System.out.printf(message, args);
System.out.println();
}
}
/** @return true if level is being logged. */
public static boolean isEnabled(int level) {
return level <= DEBUG_LEVEL;
}
/** @return true if the default level is being logged. */
public static boolean isEnabled() {
return isEnabled(DEFAULT_LEVEL);
}
/** Logs message at the default log level. */
public static void log(String message, Object... args) {
log(DEFAULT_LEVEL, message, args);
}
}