1 6 package org.mortbay.log; 7 8 9 10 11 12 16 public class Frame 17 { 18 19 20 private static String __className=Frame.class.getName(); 21 private static final String __lineSeparator = System.getProperty("line.separator"); 22 23 24 private StackTraceElement [] _stack; 25 26 27 28 private String _string; 29 30 31 private String _method= "unknownMethod"; 32 33 34 private int _depth=0; 35 36 37 private String _thread= "unknownThread"; 38 39 40 private String _file= "UnknownFile"; 41 42 private String _where; 43 private int _top=0; 44 45 46 48 public Frame() 49 { 50 _stack=new Throwable ().getStackTrace(); 51 init(0, false); 52 } 53 54 55 58 public Frame(int ignoreFrames) 59 { 60 _stack=new Throwable ().getStackTrace(); 61 init(ignoreFrames, false); 62 } 63 64 65 69 Frame(int ignoreFrames, boolean partial) 70 { 71 _stack=new Throwable ().getStackTrace(); 72 init(ignoreFrames, partial); 73 } 74 75 76 private Frame(StackTraceElement [] stack,int top) 77 { 78 _stack=stack; 79 _top=top; 80 _where=_stack[_top].toString(); 81 complete(); 82 } 83 84 85 private void init(int ignoreFrames, boolean partial) 86 { 87 int check=_stack.length; 90 if (check>3) check=3; 91 for (int i=0;i<check;i++) 92 { 93 if (__className.equals(_stack[i].getClassName()) && 94 "<init>".equalsIgnoreCase(_stack[i].getMethodName())) 95 { 96 _top=i+1; 97 break; 98 } 99 } 100 _top+=ignoreFrames; 101 _where=_stack[_top].toString(); 102 if (!partial) complete(); 103 } 104 105 106 108 void complete() 109 { 110 _file=_stack[_top].getFileName()+":"+_stack[_top].getLineNumber(); 111 _method=_stack[_top].getClassName()+"."+_stack[_top].getMethodName(); 112 _depth=_stack.length-_top; 113 _thread = Thread.currentThread().getName(); 114 } 115 116 117 public StackTraceElement getStackTraceElement() 118 { 119 return _stack[_top]; 120 } 121 122 123 public String getStack() 124 { 125 if (_string==null) 126 { 127 StringBuffer buf= new StringBuffer (512); 128 129 for (int i=0;i<_stack.length;i++) 130 { 131 if (i>_top) 132 buf.append(__lineSeparator); 133 buf.append(_stack[i].toString()); 134 } 135 _string=buf.toString(); 136 } 137 138 return _string; 139 } 140 141 142 public String getMethod() 143 { 144 if (_method==null) 145 complete(); 146 return _method; 147 } 148 149 150 public int getDepth() 151 { 152 if (_thread==null) 153 complete(); 154 return _depth; 155 } 156 157 158 public String getThread() 159 { 160 if (_thread==null) 161 complete(); 162 return _thread; 163 } 164 165 166 public String getFile() 167 { 168 if (_file==null) 169 complete(); 170 return _file; 171 } 172 173 174 public String getWhere() 175 { 176 return _where; 177 } 178 179 180 public String toString() 181 { 182 if (_thread==null) 183 complete(); 184 return "["+_thread + "] " + _method+"("+_file+")"; 185 } 186 187 188 191 public Frame getParent() 192 { 193 if (_top+1>=_stack.length) 194 return null; 195 return new Frame(_stack,_top+1); 196 } 197 } 198 199 200 201 202 | Popular Tags |