Statistics
| Revision:

root / trunk / libraries / libTopology / src / com / vividsolutions / jcs / debug / Debug.java @ 22873

History | View | Annotate | Download (4.8 KB)

1
/*
2
 * The JCS Conflation Suite (JCS) is a library of Java classes that
3
 * can be used to build automated or semi-automated conflation solutions.
4
 *
5
 * Copyright (C) 2003 Vivid Solutions
6
 *
7
 * This program is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU General Public License
9
 * as published by the Free Software Foundation; either version 2
10
 * of the License, or (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program; if not, write to the Free Software
19
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20
 *
21
 * For more information, contact:
22
 *
23
 * Vivid Solutions
24
 * Suite #1A
25
 * 2328 Government Street
26
 * Victoria BC  V8T 5G5
27
 * Canada
28
 *
29
 * (250)385-6040
30
 * www.vividsolutions.com
31
 */
32
package com.vividsolutions.jcs.debug;
33

    
34
import java.io.*;
35
import java.util.*;
36
import java.lang.reflect.*;
37

    
38
/**
39
 * Provides routines to simplify and localize debugging output.
40
 * <p>
41
 * To enable the debugging functions, the Java system property <code>debug</code>
42
 * must be set to <code>on</code> in the java command line, using the following
43
 * option:
44
 * <pre>
45
 *     -Ddebug=on
46
 * </pre>
47
 *
48
 * @version 1.4
49
 */
50
public class Debug {
51

    
52
  private static String DEBUG_PROPERTY_NAME = "debug";
53
  private static String DEBUG_PROPERTY_VALUE_ON = "on";
54
  private static String DEBUG_PROPERTY_VALUE_TRUE = "true";
55

    
56
  private static boolean debugOn = false;
57

    
58
  static {
59
    String debugValue = System.getProperty(DEBUG_PROPERTY_NAME);
60
    if (debugValue != null) {
61
      if (debugValue.equalsIgnoreCase(DEBUG_PROPERTY_VALUE_ON)
62
          || debugValue.equalsIgnoreCase(DEBUG_PROPERTY_VALUE_TRUE) )
63
        debugOn = true;
64
    }
65
  }
66

    
67
  public static void main(String[] args)
68
  {
69
    Debug.println("Debugging is ON");
70
  }
71

    
72
  private static Debug debug = new Debug();
73

    
74
  private static final String DEBUG_LINE_TAG = "D! ";
75

    
76
  private PrintStream out;
77
  private Class[] printArgs;
78
  private Object watchObj = null;
79
  private Object[] args = new Object[1];
80

    
81
  public static boolean isDebugging() { return debugOn; }
82

    
83
  public static void print(String str) {
84
    if (!debugOn) return;
85
    debug.instancePrint(str);
86
  }
87
/*
88
  public static void println(String str) {
89
    if (! debugOn) return;
90
    debug.instancePrint(str);
91
    debug.println();
92
  }
93
*/
94
  public static void print(Object obj) {
95
    if (! debugOn) return;
96
    debug.instancePrint(obj);
97
  }
98

    
99
  public static void print(boolean isTrue, Object obj) {
100
    if (! debugOn) return;
101
    if (! isTrue) return;
102
    debug.instancePrint(obj);
103
  }
104

    
105
  public static void println(Object obj) {
106
    if (!debugOn) {
107
      return;
108
    }
109
    debug.instancePrint(obj);
110
    debug.println();
111
  }
112

    
113
  public static void addWatch(Object obj) {
114
    debug.instanceAddWatch(obj);
115
  }
116

    
117
  public static void printWatch() {
118
    debug.instancePrintWatch();
119
  }
120

    
121
  public static void printIfWatch(Object obj) {
122
    debug.instancePrintIfWatch(obj);
123
  }
124

    
125
  private Debug() {
126
    out = System.out;
127
    printArgs = new Class[1];
128
    try {
129
      printArgs[0] = Class.forName("java.io.PrintStream");
130
    }
131
    catch (Exception ex) {
132
      // ignore this exception - it will fail later anyway
133
    }
134
  }
135

    
136

    
137
  public void instancePrintWatch() {
138
    if (watchObj == null) return;
139
    instancePrint(watchObj);
140
  }
141

    
142
  public void instancePrintIfWatch(Object obj) {
143
    if (obj != watchObj) return;
144
    if (watchObj == null) return;
145
    instancePrint(watchObj);
146
  }
147

    
148
  public void instancePrint(Object obj)
149
  {
150
    if (obj instanceof Collection) {
151
      instancePrint(((Collection) obj).iterator());
152
    }
153
    else if (obj instanceof Iterator) {
154
      instancePrint((Iterator) obj);
155
    }
156
    else {
157
      instancePrintObject(obj);
158
    }
159
  }
160

    
161
  public void instancePrint(Iterator it)
162
  {
163
    for (; it.hasNext(); ) {
164
      Object obj = it.next();
165
      instancePrintObject(obj);
166
    }
167
  }
168
  public void instancePrintObject(Object obj) {
169
    //if (true) throw new RuntimeException("DEBUG TRAP!");
170
    Method printMethod = null;
171
    try {
172
      Class cls = obj.getClass();
173
      try {
174
        printMethod = cls.getMethod("print", printArgs);
175
        args[0] = out;
176
        out.print(DEBUG_LINE_TAG);
177
        printMethod.invoke(obj, args);
178
      }
179
      catch (NoSuchMethodException ex) {
180
        instancePrint(obj.toString());
181
      }
182
    }
183
    catch (Exception ex) {
184
      ex.printStackTrace(out);
185
    }
186
  }
187

    
188
  public void println() {
189
    out.println();
190
  }
191

    
192
  private void instanceAddWatch(Object obj) {
193
    watchObj = obj;
194
  }
195

    
196
  private void instancePrint(String str) {
197
    out.print(DEBUG_LINE_TAG);
198
    out.print(str);
199
  }
200
}