1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.regionserver;
20
21 import java.io.FileWriter;
22 import java.io.IOException;
23 import java.util.concurrent.atomic.AtomicBoolean;
24
25 import org.apache.hadoop.classification.InterfaceAudience;
26
27 @InterfaceAudience.Private
28 public class DebugPrint {
29
30 private static final AtomicBoolean enabled = new AtomicBoolean(false);
31 private static final Object sync = new Object();
32 public static StringBuilder out = new StringBuilder();
33
34 static public void enable() {
35 enabled.set(true);
36 }
37 static public void disable() {
38 enabled.set(false);
39 }
40
41 static public void reset() {
42 synchronized (sync) {
43 enable();
44
45 out = new StringBuilder();
46 }
47 }
48 static public void dumpToFile(String file) throws IOException {
49 FileWriter f = new FileWriter(file);
50 synchronized (sync) {
51 f.write(out.toString());
52 }
53 f.close();
54 }
55
56 public static void println(String m) {
57 if (!enabled.get()) {
58 System.out.println(m);
59 return;
60 }
61
62 synchronized (sync) {
63 String threadName = Thread.currentThread().getName();
64 out.append("<");
65 out.append(threadName);
66 out.append("> ");
67 out.append(m);
68 out.append("\n");
69 }
70 }
71 }