View Javadoc

1   /**
2    * Copyright 2010 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
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(); // someone wants us enabled basically.
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  }