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