DebugUtils.java

00001 package edu.stanford.hci.r3.util;
00002 
00020 public class DebugUtils {
00021 
00027         private static int debugPriorityMask = -1;
00028 
00033         private static boolean debugTraceOn = true;
00034 
00038         private static int stackTraceOffset = 3;
00039 
00040         private static final String WHITESPACE = StringUtils.repeat(" ", 54);
00041 
00045         public static int getDebugPriorityMask() {
00046                 return debugPriorityMask;
00047         }
00048 
00052         public static boolean isDebugTraceOn() {
00053                 return debugTraceOn;
00054         }
00055 
00059         public static synchronized void print(Object object) {
00060                 final String s = (object == null) ? "null" : object.toString();
00061                 if (DebugUtils.debugTraceOn) {
00062                         printDebugTrace(0);
00063                 }
00064                 System.out.print(s);
00065         }
00066 
00070         private static synchronized void printDebugTrace(int additionalStackOffset) {
00071                 final Thread currThread = Thread.currentThread();
00072                 final int actualOffset = stackTraceOffset + additionalStackOffset;
00073                 final StackTraceElement[] ste = currThread.getStackTrace();
00074                 final String className = ste[actualOffset].getClassName();
00075                 final String trace = className.substring(className.lastIndexOf(".") + 1) + "."
00076                                 + ste[actualOffset].getMethodName() + "[" + ste[actualOffset].getLineNumber()
00077                                 + "]: ";
00078                 System.out.print(trace);
00079                 System.out.print("    ");
00080                 int padding = WHITESPACE.length() - trace.length();
00081                 if (padding < 0) {
00082                         padding = 0;
00083                 }
00084                 System.out.print(WHITESPACE.substring(0, padding));
00085         }
00086 
00091         public synchronized static void println(Object object) {
00092                 printlnWithStackOffset(object, 1);
00093         }
00094 
00102         public synchronized static void println(Object object, int debugPriority) {
00103                 if (debugPriority <= debugPriorityMask) {
00104                         // priority is too low, so return
00105                         return;
00106                 }
00107                 stackTraceOffset++;
00108                 println(object);
00109                 stackTraceOffset--;
00110         }
00111 
00116         public synchronized static void printlnWithStackOffset(Object object, int additionalStackOffset) {
00117                 final String s = (object == null) ? "null" : object.toString();
00118                 if (DebugUtils.debugTraceOn) {
00119                         printDebugTrace(additionalStackOffset);
00120                 }
00121                 System.out.println(s);
00122         }
00123 
00128         public static void setDebugPriorityMask(int priorityMask) {
00129                 debugPriorityMask = priorityMask;
00130         }
00131 
00136         public static void setDebugTraceVisible(boolean debugTrace) {
00137                 debugTraceOn = debugTrace;
00138         }
00139 
00140 }

Generated on Sat Apr 14 18:21:33 2007 for R3 Paper Toolkit by  doxygen 1.4.7