FileDocCategorySizeDatePackage
LocationInfo.javaAPI DocApache log4j 1.2.158334Sat Aug 25 00:09:40 BST 2007org.apache.log4j.spi

LocationInfo

public class LocationInfo extends Object implements Serializable
The internal representation of caller location information.
since
0.8.3

Fields Summary
transient String
lineNumber
Caller's line number.
transient String
fileName
Caller's file name.
transient String
className
Caller's fully qualified class name.
transient String
methodName
Caller's method name.
public String
fullInfo
All available caller information, in the format fully.qualified.classname.of.caller.methodName(Filename.java:line)
private static StringWriter
sw
private static PrintWriter
pw
public static final String
NA
When location information is not available the constant NA is returned. Current value of this string constant is ?.
static final long
serialVersionUID
public static final LocationInfo
NA_LOCATION_INFO
NA_LOCATION_INFO is provided for compatibility with log4j 1.3.
static boolean
inVisualAge
Constructors Summary
public LocationInfo(Throwable t, String fqnOfCallingClass)
Instantiate location information based on a Throwable. We expect the Throwable t, to be in the format
java.lang.Throwable
...
at org.apache.log4j.PatternLayout.format(PatternLayout.java:413)
at org.apache.log4j.FileAppender.doAppend(FileAppender.java:183)
at org.apache.log4j.Category.callAppenders(Category.java:131)
at org.apache.log4j.Category.log(Category.java:512)
at callers.fully.qualified.className.methodName(FileName.java:74)
...

However, we can also deal with JIT compilers that "lose" the location information, especially between the parentheses.

   
    try {
      inVisualAge = Class.forName("com.ibm.uvm.tools.DebugSupport") != null;
      LogLog.debug("Detected IBM VisualAge environment.");
    } catch(Throwable e) {
      // nothing to do
    }
  
      if(t == null || fqnOfCallingClass == null)
	return;

      String s;
      // Protect against multiple access to sw.
      synchronized(sw) {
	t.printStackTrace(pw);
	s = sw.toString();
	sw.getBuffer().setLength(0);
      }
      //System.out.println("s is ["+s+"].");
      int ibegin, iend;

      // Given the current structure of the package, the line
      // containing "org.apache.log4j.Category." should be printed just
      // before the caller.

      // This method of searching may not be fastest but it's safer
      // than counting the stack depth which is not guaranteed to be
      // constant across JVM implementations.
      ibegin = s.lastIndexOf(fqnOfCallingClass);
      if(ibegin == -1)
	return;


      ibegin = s.indexOf(Layout.LINE_SEP, ibegin);
      if(ibegin == -1)
	return;
      ibegin+= Layout.LINE_SEP_LEN;

      // determine end of line
      iend = s.indexOf(Layout.LINE_SEP, ibegin);
      if(iend == -1)
	return;

      // VA has a different stack trace format which doesn't
      // need to skip the inital 'at'
      if(!inVisualAge) {
	// back up to first blank character
	ibegin = s.lastIndexOf("at ", iend);
	if(ibegin == -1)
	  return;
	// Add 3 to skip "at ";
	ibegin += 3;
      }
      // everything between is the requested stack item
      this.fullInfo = s.substring(ibegin, iend);
    
public LocationInfo(String file, String classname, String method, String line)
Create new instance.

param
file source file name
param
classname class name
param
method method
param
line source line number
since
1.2.15

      this.fileName = file;
      this.className = classname;
      this.methodName = method;
      this.lineNumber = line;
      StringBuffer buf = new StringBuffer();
	  appendFragment(buf, classname);
	  buf.append(".");
	  appendFragment(buf, method);
	  buf.append("(");
	  appendFragment(buf, file);
	  buf.append(":");
	  appendFragment(buf, line);
	  buf.append(")");
	  this.fullInfo = buf.toString();
    
Methods Summary
private static final voidappendFragment(java.lang.StringBuffer buf, java.lang.String fragment)
Appends a location fragment to a buffer to build the full location info.

param
buf StringBuffer to receive content.
param
fragment fragment of location (class, method, file, line), if null the value of NA will be appended.
since
1.2.15

          if (fragment == null) {
             buf.append(NA);
          } else {
             buf.append(fragment);
          }
    
public java.lang.StringgetClassName()
Return the fully qualified class name of the caller making the logging request.

      if(fullInfo == null) return NA;
      if(className == null) {
	// Starting the search from '(' is safer because there is
	// potentially a dot between the parentheses.
	int iend = fullInfo.lastIndexOf('(");
	if(iend == -1)
	  className = NA;
	else {
	  iend =fullInfo.lastIndexOf('.", iend);

	  // This is because a stack trace in VisualAge looks like:

          //java.lang.RuntimeException
	  //  java.lang.Throwable()
	  //  java.lang.Exception()
	  //  java.lang.RuntimeException()
	  //  void test.test.B.print()
	  //  void test.test.A.printIndirect()
	  //  void test.test.Run.main(java.lang.String [])
          int ibegin = 0;
	  if (inVisualAge) {
	    ibegin = fullInfo.lastIndexOf(' ", iend)+1;
          }

	  if(iend == -1)
	    className = NA;
	  else
	    className = this.fullInfo.substring(ibegin, iend);
	}
      }
      return className;
    
public java.lang.StringgetFileName()
Return the file name of the caller.

This information is not always available.

      if(fullInfo == null) return NA;

      if(fileName == null) {
	int iend = fullInfo.lastIndexOf(':");
	if(iend == -1)
	  fileName = NA;
	else {
	  int ibegin = fullInfo.lastIndexOf('(", iend - 1);
	  fileName = this.fullInfo.substring(ibegin + 1, iend);
	}
      }
      return fileName;
    
public java.lang.StringgetLineNumber()
Returns the line number of the caller.

This information is not always available.

      if(fullInfo == null) return NA;

      if(lineNumber == null) {
	int iend = fullInfo.lastIndexOf(')");
	int ibegin = fullInfo.lastIndexOf(':", iend -1);
	if(ibegin == -1)
	  lineNumber = NA;
	else
	  lineNumber = this.fullInfo.substring(ibegin + 1, iend);
      }
      return lineNumber;
    
public java.lang.StringgetMethodName()
Returns the method name of the caller.

      if(fullInfo == null) return NA;
      if(methodName == null) {
	int iend = fullInfo.lastIndexOf('(");
	int ibegin = fullInfo.lastIndexOf('.", iend);
	if(ibegin == -1)
	  methodName = NA;
	else
	  methodName = this.fullInfo.substring(ibegin + 1, iend);
      }
      return methodName;