001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.visualweb.extension.openide.util;
042:
043: import java.io.*;
044: import java.util.*;
045:
046: /**
047: * Used for logging output to help debug netbeans modules. Debug messages
048: * are logged into a specific category which must be "enabled" in order for
049: * the output to make it to the console or logger.
050: * <p>
051: * The trace messages all return true (always). This makes them suitable
052: * as assertions - to take advantage of the conditional processing of
053: * assertions. E.g "assert Trace.trace(compute());". Here the Trace call
054: * will only be processed (and its arguments evaluated) when assertions
055: * are enabled. Obviously you must run with assertions enabled if you
056: * use the Trace library this way.
057: *
058: * @author Joe Nuxoll
059: */
060: public class Trace extends TraceBase {
061:
062: public static PrintStream out = System.err;
063:
064: /**
065: * This is a global counter variable that is handy to use when tracing
066: * repetitive tasks.
067: */
068: public static int counter = 0;
069:
070: /**
071: * turns on/off tracing for all categories
072: *
073: * @param enabled true to enable tracing, false to disable it
074: */
075: public static void setOutputEnabled(boolean enabled) {
076: out = enabled ? System.err : nullPS;
077: }
078:
079: public static boolean isOutputEnabled() {
080: return !(nullPS == out);
081: }
082:
083: /**
084: * Sets the output stream to be used for tracing. This will
085: * assign the static Trace.out variable.
086: *
087: * @param log The PrintStream to use for Trace.out
088: */
089: public static void setOutputStream(PrintStream log) {
090: out = log;
091: }
092:
093: /**
094: * This enables tracing for the specified category of messages
095: *
096: * @param category Enable tracing of this category
097: */
098: public static void enableTraceCategory(String category) {
099: traceCategories.add(category);
100: }
101:
102: /**
103: * This enables tracing for the specified category of messages
104: *
105: * @param category Enable tracing of this category
106: */
107: public static void enableTraceCategory(Class category) {
108: traceCategories.add(category.getName());
109: }
110:
111: /**
112: * Disables tracing for the specified category of messages
113: *
114: * @param category Remove specified category to disable tracing
115: */
116: public static void disableTraceCategory(String category) {
117: traceCategories.remove(category);
118: }
119:
120: /**
121: * Disables tracing for the specified category of messages
122: *
123: * @param category Remove specified category to disable tracing
124: */
125: public static void disableTraceCategory(Class category) {
126: traceCategories.remove(category.getName());
127: }
128:
129: /**
130: * Sends a message to Trace.out if the category is enabled and global output is enabled
131: *
132: * @param category Category to which this message belongs
133: * @param message Message to print if the category is enabled
134: * @return true
135: */
136: public static boolean trace(String category, String message) {
137: if (traceCategories.contains(category))
138: out.println("[" + category + "] " + message);
139: return true;
140: }
141:
142: /**
143: * Sends a message to Trace.out if the category is enabled and global output is enabled
144: *
145: * @param category Category to which this message belongs
146: * @param message Message to print if the category is enabled
147: * @return true
148: */
149: public static boolean trace(Class category, String message) {
150: trace(category.getName(), message);
151: return true;
152: }
153:
154: /**
155: * Prints a stack trace to Trace.out if the category is enabled and global output is enabled
156: *
157: * @param category Category to which this message belongs
158: * @param t Throwable for stack trace to print
159: * @return true
160: */
161: public static boolean trace(String category, Throwable t) {
162: if (traceCategories.contains(category))
163: t.printStackTrace(out);
164: return true;
165: }
166:
167: /**
168: * Prints a stack trace to Trace.out if the category is enabled and global output is enabled
169: *
170: * @param category Category to which this message belongs
171: * @param t Throwable for stack trace to print
172: * @return true
173: */
174: public static boolean trace(Class category, Throwable t) {
175: trace(category.getName(), t);
176: return true;
177: }
178:
179: /**
180: * Print the specified text to Trace.out if the specified category is enabled, and specified
181: * condition evaluates to true, and global output is enabled
182: *
183: * @param category Category to which this message belongs
184: * @param condition If this evaluates to true, the message will be printed
185: * @param message Message to print if the category is enabled and the condition evals to true
186: * @return true
187: */
188: public static boolean warnTrace(String category, boolean condition,
189: String message) {
190: if (condition && traceCategories.contains(category))
191: out.println("warn[" + category + "] " + message);
192: return true;
193: }
194:
195: /**
196: * Prints the specified message to Trace.out if the specified category is enabled and specified
197: * condition evaluates to true, and global output is enabled
198: *
199: * @param category Category to which this message belongs
200: * @param condition If this evaluates to true, the message will be printed
201: * @param message Message to print if the category is enabled and the condition evals to true
202: * @return true
203: */
204: public static boolean warnTrace(Class category, boolean condition,
205: String message) {
206: warnTrace(category.getName(), condition, message);
207: return true;
208: }
209:
210: /**
211: * Prints the specified message to Trace.out if the specified category is enabled and global
212: * output is enabled
213: *
214: * @param category Category to which this message belongs
215: * @param message Message to print if the category is enabled
216: * @return true
217: */
218: static public boolean warnTrace(String category, String message) {
219: warnTrace(category, true, message);
220: return true;
221: }
222:
223: /**
224: * Prints the specified message to Trace.out if the specified category is enabled and global
225: * output is enabled
226: *
227: * @param category Category to which this message belongs
228: * @param message Message to print if the category is enabled
229: * @return true
230: */
231: static public boolean warnTrace(Class category, String message) {
232: warnTrace(category.getName(), true, message);
233: return true;
234: }
235:
236: /**
237: * Prints a stack trace of the current thread to the Trace.out stream.
238: * @return true
239: */
240: public static boolean printStackTrace() {
241: new Exception("Trace.printStackTrace()").printStackTrace(out);
242: return true;
243: }
244:
245: /**
246: * Prints the specified exception stack trace to the Trace.out stream.
247: *
248: * @param t Source Throwable to print a stack trace from
249: * @return true
250: */
251: public static boolean printStackTrace(Throwable t) {
252: out
253: .println("Trace.printStackTrace(Thowable) ----------------------8<");
254: t.printStackTrace(out);
255: out
256: .println("Trace.printStackTrace(Thowable) ----------------------8<");
257: return true;
258: }
259:
260: /**
261: * Flushes the Trace.out stream
262: * @return true
263: */
264: public static boolean flush() {
265: out.flush();
266: return true;
267: }
268:
269: private static final Trace.NullPrintStream nullPS = new Trace.NullPrintStream();
270: private static ArrayList traceCategories = new ArrayList();
271:
272: //------------------------------------------------------------------------------
273: // NullPrintStream - for blank output
274: //------------------------------------------------------------------------------
275: private static class NullPrintStream extends PrintStream {
276: public NullPrintStream() {
277: super (System.err);
278: }
279:
280: public void write(int b) {
281: }
282:
283: public void write(byte[] b, int off, int len) {
284: }
285:
286: public void flush() {
287: }
288:
289: public void close() {
290: }
291:
292: public boolean checkError() {
293: return false;
294: }
295:
296: public void print(Object obj) {
297: }
298:
299: public void print(String s) {
300: }
301:
302: public void print(char[] s) {
303: }
304:
305: public void print(char c) {
306: }
307:
308: public void print(int i) {
309: }
310:
311: public void print(long l) {
312: }
313:
314: public void print(float f) {
315: }
316:
317: public void print(double d) {
318: }
319:
320: public void print(boolean b) {
321: }
322:
323: public void println() {
324: }
325:
326: public void println(Object obj) {
327: }
328:
329: public void println(String s) {
330: }
331:
332: public void println(char[] s) {
333: }
334:
335: public void println(char c) {
336: }
337:
338: public void println(int i) {
339: }
340:
341: public void println(long l) {
342: }
343:
344: public void println(float f) {
345: }
346:
347: public void println(double d) {
348: }
349:
350: public void println(boolean b) {
351: }
352: }
353: }
|