com.arsdigita.developersupport
Class StackTraces

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

public final class StackTraces
extends Object

This class facilitates debugging by allowing you to capture a stack trace for an object for later retrieval.

Example usage:

 public abstract class Completable implements Component {
     public Completable() {
         if ( s_log.isDebugEnabled() ) {
             StackTraces.captureStackTrace(this);
         }
     }
     
    ...
 }
 

Once the Completable class has been instrumented in this way, we can generate better warning and error reports. For example,

 public class ModalPanel extends ComponentMap {
     ...

     public CancelListener(final FormSection form) {
         Assert.assertNotNull(form, "FormSection form");

         if (form instanceof Cancellable) {
             m_cancellable = (Cancellable) form;
         } else {
             m_cancellable = null;

             s_log.warn("Form " + form + " does not " +
                        "implement Cancellable.");
             StackTraces.log("The form was created at", form, s_log, "warn");
         }
     }
 }
 

If the form does not implement the Cancellable interface, we should be able to see where the form was created. Without the call to log(String msg, Object, Logger, String), the above piece would have only logged something like this:

 Form com.arsdigita.bebop.Form@2d72d [AddTemplate,null,null,false] does not implement Cancellable
 

This wouldn't be very informative, because it would not tell us what kind of form this is and where it was created. If you do use the log(String msg, Object, Logger, String) method provided by this class, then the output is more enlightening:

 2003-07-08 17:41:37,704 [800-2] WARN  ui.ModalPanel -
   Form com.arsdigita.bebop.Form@c624a [AddTemplate,null,null,false]
   does not implement Cancellable.
 2003-07-08 17:41:37,718 [800-2] WARN  ui.ModalPanel -
 The form was created at
 java.lang.Throwable
      at ..developersupport.StackTraces.captureStackTrace(StackTraces.java:114)
      at ..bebop.Completable.(Completable.java:43)
      at ..bebop.SimpleComponent.(SimpleComponent.java:36)
      at ..bebop.FormSection.(FormSection.java:127)
      at ..bebop.Form.(Form.java:165)
      at ..bebop.Form.(Form.java:151)
      at ..cms.ui.type.ContentTypeItemPane.(ContentTypeItemPane.java:82)
      at ..cms.ui.type.ContentTypeAdminPane.(ContentTypeAdminPane.java:68)
 

Note note for the above example to work as advertised, you must set the logging level to "debug" for the "com.arsdigita.bebop.Completable" logger. Otherwise, the stack trace will not be captured.

Since:
2003-07-08
Version:
$Revision: #6 $ $Date: 2004/04/07 $
Author:
Vadim Nasardinov (vadimn@redhat.com)

Method Summary
static void captureStackTrace(Object obj)
           
static Throwable getStackTrace(Object obj)
          Returns the stack trace previously captured for this obj via captureStackTrace(Object), or null if no such stack trace exists.
static void log(String msg, Object obj, org.apache.log4j.Logger logger, String level)
          If looking up the stack trace for obj fails, does nothing.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

captureStackTrace

public static void captureStackTrace(Object obj)

getStackTrace

public static Throwable getStackTrace(Object obj)
Returns the stack trace previously captured for this obj via captureStackTrace(Object), or null if no such stack trace exists.


log

public static void log(String msg,
                       Object obj,
                       org.apache.log4j.Logger logger,
                       String level)

If looking up the stack trace for obj fails, does nothing. Otherwise, logs the specified message to logger at the specified logging level. The looked up stack trace is also logged.



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