1   /*
2    * Copyright (C) The Apache Software Foundation. All rights reserved.
3    *
4    * This software is published under the terms of the Apache Software
5    * License version 1.1, a copy of which has been included with this
6    * distribution in the LICENSE.txt file.  */
7   
8   // Contributors: Mathias Rupprecht <mmathias.rupprecht@fja.com>
9   //   Vlad Skarzhevskyy <vlads at users.sourceforge.net> (Changed only for Java 1.4 performance test)
10  
11  package org.apache.log4j.spi;
12  
13  
14  /***
15     The internal representation of caller location information.
16  
17     @since 0.8.3
18  */
19  public class LocationInfo implements java.io.Serializable {
20  
21      /***
22       * Caller's line number.
23       */
24      String lineNumber;
25  
26      /***
27       * Caller's file name.
28       */
29      String fileName;
30  
31      /***
32       * Caller's fully qualified class name.
33       */
34      String className;
35  
36      /***
37       * Caller's method name.
38       */
39      String methodName;
40  
41      /***
42      All available caller information, in the format
43      <code>fully.qualified.classname.of.caller.methodName(Filename.java:line)</code>
44     */
45      public String fullInfo;
46   
47     /***
48       When location information is not available the constant
49       <code>NA</code> is returned. Current value of this string
50       constant is <b>?</b>.  */
51      public final static String NA = "?";
52  
53      static final long serialVersionUID = -1325822038990805636L;
54  
55      public LocationInfo(Throwable t, String fqnOfCallingClass) {
56          if (t != null) {
57              StackTraceElement[] ste = t.getStackTrace();
58              boolean found = false;
59              for (int i = 0; i < ste.length; i++) {
60                  // Find next class after logger.
61                  if (ste[i].getClassName().equals(fqnOfCallingClass)) {
62                      found = true;
63                  } else if (found) {
64                      StackTraceElement e = ste[i];
65                      this.lineNumber = new Integer(e.getLineNumber()).toString();
66                      this.fileName = e.getFileName();
67                      this.methodName = e.getMethodName();
68                      this.className = e.getClassName();
69                      StringBuffer b = new StringBuffer(128);
70                      b.append(this.className).append(".").append(this.methodName)
71                      	.append("[").append(this.fileName).append(":").append(this.lineNumber).append("]");
72                      this.fullInfo = b.toString(); 
73                          return;
74                  }
75              }
76          }
77          this.lineNumber = NA;
78          this.fileName = NA;
79          this.methodName = NA;
80          this.className = NA;
81          this.fullInfo = NA;
82      }
83  
84      /***
85       * @return Returns the className.
86       */
87      public String getClassName() {
88          return className;
89      }
90      /***
91       * @return Returns the fileName.
92       */
93      public String getFileName() {
94          return fileName;
95      }
96      /***
97       * @return Returns the lineNumber.
98       */
99      public String getLineNumber() {
100         return lineNumber;
101     }
102     /***
103      * @return Returns the methodName.
104      */
105     public String getMethodName() {
106         return methodName;
107     }
108 }