1 16 17 19 package org.apache.log4j.spi; 20 21 import java.io.StringWriter ; 22 import java.io.PrintWriter ; 23 import org.apache.log4j.helpers.LogLog; 24 import org.apache.log4j.Layout; 25 26 31 public class LocationInfo implements java.io.Serializable { 32 33 36 transient String lineNumber; 37 40 transient String fileName; 41 44 transient String className; 45 48 transient String methodName; 49 53 public String fullInfo; 54 55 private static StringWriter sw = new StringWriter (); 56 private static PrintWriter pw = new PrintWriter (sw); 57 58 62 public final static String NA = "?"; 63 64 static final long serialVersionUID = -1325822038990805636L; 65 66 67 static boolean inVisualAge = false; 69 static { 70 try { 71 Class dummy = Class.forName("com.ibm.uvm.tools.DebugSupport"); 72 inVisualAge = true; 73 LogLog.debug("Detected IBM VisualAge environment."); 74 } catch(Throwable e) { 75 } 77 } 78 79 98 public LocationInfo(Throwable t, String fqnOfCallingClass) { 99 if(t == null) 100 return; 101 102 String s; 103 synchronized(sw) { 105 t.printStackTrace(pw); 106 s = sw.toString(); 107 sw.getBuffer().setLength(0); 108 } 109 int ibegin, iend; 111 112 116 ibegin = s.lastIndexOf(fqnOfCallingClass); 120 if(ibegin == -1) 121 return; 122 123 124 ibegin = s.indexOf(Layout.LINE_SEP, ibegin); 125 if(ibegin == -1) 126 return; 127 ibegin+= Layout.LINE_SEP_LEN; 128 129 iend = s.indexOf(Layout.LINE_SEP, ibegin); 131 if(iend == -1) 132 return; 133 134 if(!inVisualAge) { 137 ibegin = s.lastIndexOf("at ", iend); 139 if(ibegin == -1) 140 return; 141 ibegin += 3; 143 } 144 this.fullInfo = s.substring(ibegin, iend); 146 } 147 148 152 public 153 String getClassName() { 154 if(fullInfo == null) return NA; 155 if(className == null) { 156 int iend = fullInfo.lastIndexOf('('); 159 if(iend == -1) 160 className = NA; 161 else { 162 iend =fullInfo.lastIndexOf('.', iend); 163 164 166 int ibegin = 0; 174 if (inVisualAge) { 175 ibegin = fullInfo.lastIndexOf(' ', iend)+1; 176 } 177 178 if(iend == -1) 179 className = NA; 180 else 181 className = this.fullInfo.substring(ibegin, iend); 182 } 183 } 184 return className; 185 } 186 187 192 public 193 String getFileName() { 194 if(fullInfo == null) return NA; 195 196 if(fileName == null) { 197 int iend = fullInfo.lastIndexOf(':'); 198 if(iend == -1) 199 fileName = NA; 200 else { 201 int ibegin = fullInfo.lastIndexOf('(', iend - 1); 202 fileName = this.fullInfo.substring(ibegin + 1, iend); 203 } 204 } 205 return fileName; 206 } 207 208 213 public 214 String getLineNumber() { 215 if(fullInfo == null) return NA; 216 217 if(lineNumber == null) { 218 int iend = fullInfo.lastIndexOf(')'); 219 int ibegin = fullInfo.lastIndexOf(':', iend -1); 220 if(ibegin == -1) 221 lineNumber = NA; 222 else 223 lineNumber = this.fullInfo.substring(ibegin + 1, iend); 224 } 225 return lineNumber; 226 } 227 228 231 public 232 String getMethodName() { 233 if(fullInfo == null) return NA; 234 if(methodName == null) { 235 int iend = fullInfo.lastIndexOf('('); 236 int ibegin = fullInfo.lastIndexOf('.', iend); 237 if(ibegin == -1) 238 methodName = NA; 239 else 240 methodName = this.fullInfo.substring(ibegin + 1, iend); 241 } 242 return methodName; 243 } 244 } 245 | Popular Tags |