KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > util > Trace


1 /******************************************************************************
2  * The contents of this file are subject to the Compiere License Version 1.1
3  * ("License"); You may not use this file except in compliance with the License
4  * You may obtain a copy of the License at http://www.compiere.org/license.html
5  * Software distributed under the License is distributed on an "AS IS" basis,
6  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
7  * the specific language governing rights and limitations under the License.
8  * The Original Code is Compiere ERP & CRM Business Solution
9  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
10  * Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

14 package org.compiere.util;
15
16 import java.util.ArrayList JavaDoc;
17
18 /**
19  * Trace Information
20  *
21  * @author Jorg Janke
22  * @version $Id: Trace.java,v 1.6 2003/09/27 11:13:27 jjanke Exp $
23  */

24 public class Trace
25 {
26     /**
27      * Get Caller Array
28      *
29      * @param caller Optional Thowable/Exception
30      * @param maxNestLevel maximum call nesting level - 0 is all
31      * @return Array of class.method(file:line)
32      */

33     public static String JavaDoc[] getCallerClasses (Throwable JavaDoc caller, int maxNestLevel)
34     {
35         int nestLevel = maxNestLevel;
36         if (nestLevel < 1)
37             nestLevel = 99;
38         //
39
ArrayList JavaDoc list = new ArrayList JavaDoc();
40         Throwable JavaDoc t = caller;
41         if (t == null)
42             t = new Throwable JavaDoc();
43
44         StackTraceElement JavaDoc[] elements = t.getStackTrace();
45         for (int i = 0; i < elements.length && list.size() <= maxNestLevel; i++)
46         {
47             String JavaDoc className = elements[i].getClassName();
48         // System.out.println(list.size() + ": " + className);
49
if (!(className.startsWith("org.compiere.util.Trace")
50                 || className.startsWith("java.lang.Throwable")))
51                 list.add(className);
52         }
53
54         String JavaDoc[] retValue = new String JavaDoc[list.size()];
55         list.toArray(retValue);
56         return retValue;
57     } // getCallerClasses
58

59     /**
60      * Get Caller with nest Level
61      * @param nestLevel Nesting Level - 0=calling method, 1=previous, ..
62      * @return class name and line info of nesting level or "" if not exist
63      */

64     public static String JavaDoc getCallerClass (int nestLevel)
65     {
66         String JavaDoc[] array = getCallerClasses (null, nestLevel);
67         if (array.length < nestLevel)
68             return "";
69         return array[nestLevel];
70     } // getCallerClass
71

72     /**
73      * Print Stack Tace Info (raw)
74      */

75     public static void printStack()
76     {
77         Throwable JavaDoc t = new Throwable JavaDoc();
78         t.printStackTrace();
79     } // printStack
80
} // Trace
81
Popular Tags