def _exceptionStackTTB()

in scripts/yapl/Trace.py [0:0]


  def _exceptionStackTTB(self,methodName,exc,depth=10):
    """
      Return a string useable for output to stdout or a log file that provides a representation
      of the "exception stack" and the "frame stack" from "top to bottm" (TTB).
      The "exception stack" captures the code tree from main to where the exception was raised and 
      is usually the most interesting part of the stack.  The "frame stack" captures the code
      from the point to where the exception was caught. 
      
      Displaying the stack from top to bottom in an output log or stdout is the style in which
      Java displays the stack.
      
      There is another method named _exceptionStackBTT() that can be used to create a string that
      represents the execution stack from bottom to top, which is the style that Jython/Python
      uses by default.
    """
    stack = ""
    # Reconstruct the call stack from where the trace of the exception was initiated by invoking 
    # Trace.error() or Trace.severe().
    stackList = traceback.extract_stack()
    try:
      for stackData in stackList:
        sourcefile,line,function,text = stackData
        if (sourcefile.endswith("Trace.py") and (function == "error" or function == "severe")): break
        sepIndex = sourcefile.rfind(os.sep)
        if (sepIndex >=0 and Trace.SourceFileStyle == Trace.NameOnly):
          sourcefile = sourcefile[sepIndex+1:]
        #endIf
        if (text == None):
          if (not stack):
            # Leave out the newline for the bottom line on the stack
            stack = "\t%s(%s) [%s]" % (sourcefile,line,function)
          else:
            stack = "\t%s(%s) [%s]\n%s" % (sourcefile,line,function,stack)
          #endIf
        else:
          if (not stack):
            # Leave out the newline for the bottom line on the stack
            stack = "\t%s(%s) [%s] - %s" % (sourcefile,line,function,text)
          else:
            stack = "\t%s(%s) [%s] - %s\n%s" % (sourcefile,line,function,text,stack)
          #endIf
        #endIf
      #endFor
      stack = "\tFrame stack (most recent call first):\n%s" % stack
    except:
      # This shouldn't happen, but in case it does...
      exc_type,exc_value = sys.exc_info()[:2]
      stack = "\tException getting frame stack. Type: %s, Value: %s\n%s" % (exc_type,exc_value,stack)
    #endTry

    try:
      tb = sys.exc_info()[2]
      stackList = traceback.extract_tb(tb,depth)
      for stackData in stackList:
        sourcefile,line,function,text = stackData
        sepIndex = sourcefile.rfind(os.sep)
        if (sepIndex >=0 and Trace.SourceFileStyle == Trace.NameOnly):
          sourcefile = sourcefile[sepIndex+1:]
        #endIf
        if (text == None):
          stack = "\t%s(%s) [%s]\n%s" % (sourcefile,line,function,stack)
        else:
          stack = "\t%s(%s) [%s] - %s\n%s" % (sourcefile,line,function,text,stack)
        #endIf
      #endFor
      stack = "\tException stack (most recent call first):\n%s" % stack
    except:
      # This shouldn't happen, but in case it does...
      exc_type,exc_value = sys.exc_info()[:2]
      stack = "\tException getting exception stack. Type: %s, Value: %s\n%s" % (exc_type,exc_value,stack)
    #endTry
    
    # At the very top - put the exception string
    stack = "\t%s\n%s" % (exc,stack)
    
    return stack