com.arsdigita.developersupport
Class Debug

java.lang.Object
  extended bycom.arsdigita.developersupport.Debug

public class Debug
extends Object

Collection of miscellaneous utility methods that may occasionally aid debugging.

Since:
2002-08-23
Version:
$Id: //core-platform/dev/src/com/arsdigita/developersupport/Debug.java#5 $ $Date: 2004/04/07 $
Author:
Vadim Nasardinov (vadimn@redhat.com)

Method Summary
static void dumpToFile(String filename, char[] chars)
          Dump a character array to the file.
static void dumpToFile(String filename, String text)
          Useful if you want to examine the string text in an editor.
static Object getPrivateField(Class klass, Object obj, String fieldName)
          This method allows you to access a private field named fieldName of the object obj.
static String readFile(String filename)
           
static String setLevel(String loggerName, String level)
          Manipulates the logging level for the specified logger.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

dumpToFile

public static void dumpToFile(String filename,
                              String text)
Useful if you want to examine the string text in an editor. Typical use pattern:
 Debug.dumpToFile("/tmp/suspect-string.txt", suspectString);
 

Possible IO exceptions will be logged but not rethrown.


dumpToFile

public static void dumpToFile(String filename,
                              char[] chars)
Dump a character array to the file.

See Also:
dumpToFile(String, String)

readFile

public static String readFile(String filename)

getPrivateField

public static Object getPrivateField(Class klass,
                                     Object obj,
                                     String fieldName)
                              throws SecurityException
This method allows you to access a private field named fieldName of the object obj.

Example usage:

 public void doStuff(Foo foo) {
     Baz baz = (Baz) Debug.getPrivateField(Foo.class, foo, "m_baz");
     System.err.println("foo's private field m_baz is " + baz);
     // do stuff with foo
     // ...
 }
 

Parameters:
klass - the class of obj
obj - the object whose field is being accessed
fieldName - the name of the field being accessed
Throws:
SecurityException - if there is a security manager and its SecurityManager.checkPermission(java.security.Permission) method returns true for the ReflectPermission("suppressAccessChecks") permission. Note that this is an unchecked exception.
See Also:
AccessibleObject.setAccessible(boolean), ReflectPermission

setLevel

public static String setLevel(String loggerName,
                              String level)
Manipulates the logging level for the specified logger.

Possible use case: Suppose you want to log db queries generated as a result of executing the method foo(). One way to do this is to set the level for the "com.redhat.persistence.engine.rdbms" logger to "info" in enterprise.init. However, this will result in all queries being logged, producing a flood of debugging information that you have to wade through to find queries that are of interest to you.

An alternative is to do something like this:

  String old = Debug.setLevel("com.redhat.persistence.engine.rdbms", "info");
  foo();
  Debug.setLevel("com.redhat.persistence.engine.rdbms", old);
  // or
  Debug.setLevel("com.redhat.persistence.engine.rdbms", "off");
 

Note, however, that although this method allows you to eliminate a lot of unnecessary clutter, it fails to eliminate all of it. In the presence of multiple threads of execution, methods other than foo() may execute within the same time slice. These other methods executing concurrently may produce additional logging in the com.redhat.persistence.engine package.

Parameters:
loggerName - the name of the logger, usually the name of a class
level - the logger level; one of "debug", "info", "warn", "error", "fatal", or "off". If an invalid level name is passed, the level will default to "debug".
Returns:
the previous level prior to this call
See Also:
Level, Logger


Copyright (c) 2004 Red Hat, Inc. Corporation. All Rights Reserved. Generated at July 21 2004:2337 UTC