KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mortbay > log > Frame


1 // ========================================================================
2
// Copyright (c) 1997 MortBay Consulting, Sydney
3
// $Id: Frame.java,v 1.1 2004/06/04 21:37:20 gregwilkins Exp $
4
// ========================================================================
5

6 package org.mortbay.log;
7
8
9
10
11 /*-----------------------------------------------------------------------*/
12 /** Access the current execution frame.
13  * This version of the Frame class uses the JDK 1.4 mechanisms to
14  * access the stack frame
15  */

16 public class Frame
17 {
18     
19     /* ------------------------------------------------------------ */
20     private static String JavaDoc __className=Frame.class.getName();
21     private static final String JavaDoc __lineSeparator = System.getProperty("line.separator");
22     
23     /*-------------------------------------------------------------------*/
24     private StackTraceElement JavaDoc[] _stack;
25     
26     /*-------------------------------------------------------------------*/
27     /** The full stack of where the Frame was created. */
28     private String JavaDoc _string;
29     
30     /** The Method (including the "(file.java:99)") the Frame was created in */
31     private String JavaDoc _method= "unknownMethod";
32     
33     /** The stack depth where the Frame was created (main is 1) */
34     private int _depth=0;
35     
36     /** Name of the Thread the Frame was created in */
37     private String JavaDoc _thread= "unknownThread";
38     
39     /** The file and linenumber of where the Frame was created. */
40     private String JavaDoc _file= "UnknownFile";
41
42     private String JavaDoc _where;
43     private int _top=0;
44     
45     /*-------------------------------------------------------------------*/
46     /** Construct a frame.
47      */

48     public Frame()
49     {
50         _stack=new Throwable JavaDoc().getStackTrace();
51         init(0, false);
52     }
53     
54     /*-------------------------------------------------------------------*/
55     /** Construct a frame.
56      * @param ignoreFrames number of levels of stack to ignore
57      */

58     public Frame(int ignoreFrames)
59     {
60         _stack=new Throwable JavaDoc().getStackTrace();
61         init(ignoreFrames, false);
62     }
63     
64     /* ------------------------------------------------------------ */
65     /** package private Constructor.
66      * @param ignoreFrames Number of frames to ignore
67      * @param partial Partial construction if true
68      */

69     Frame(int ignoreFrames, boolean partial)
70     {
71         _stack=new Throwable JavaDoc().getStackTrace();
72         init(ignoreFrames, partial);
73     }
74     
75     /* ------------------------------------------------------------ */
76     private Frame(StackTraceElement JavaDoc[] 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         // Extract stack components, after we look for the Frame constructor
88
// itself and pull that off the stack!
89
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     /** Complete partial constructor.
107      */

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 JavaDoc getStackTraceElement()
118     {
119         return _stack[_top];
120     }
121     
122     /*-------------------------------------------------------------------*/
123     public String JavaDoc getStack()
124     {
125         if (_string==null)
126         {
127             StringBuffer JavaDoc buf= new StringBuffer JavaDoc(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 JavaDoc 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 JavaDoc getThread()
159     {
160         if (_thread==null)
161             complete();
162         return _thread;
163     }
164     
165     /*-------------------------------------------------------------------*/
166     public String JavaDoc getFile()
167     {
168         if (_file==null)
169             complete();
170         return _file;
171     }
172     
173     /*-------------------------------------------------------------------*/
174     public String JavaDoc getWhere()
175     {
176         return _where;
177     }
178     
179     /*-------------------------------------------------------------------*/
180     public String JavaDoc toString()
181     {
182         if (_thread==null)
183             complete();
184         return "["+_thread + "] " + _method+"("+_file+")";
185     }
186     
187     /* ------------------------------------------------------------ */
188     /** Get a Frame representing the function one level up in this frame.
189      * @return parent frame or null if none
190      */

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