KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > error > StackTraceUtil


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.error;
18
19 /**
20  * Helper class around outputting stack traces.
21  *
22  * @author Derek Hulley
23  */

24 public class StackTraceUtil
25 {
26     /**
27      * Builds a message with the stack trace of the form:
28      * <pre>
29      * SOME MESSAGE:
30      * Started at:
31      * com.package...
32      * com.package...
33      * ...
34      * </pre>
35      *
36      * @param msg the initial error message
37      * @param stackTraceElements the stack trace elements
38      * @param sb the buffer to append to
39      * @param maxDepth the maximum number of trace elements to output. 0 or less means output all.
40      */

41     public static void buildStackTrace(
42             String JavaDoc msg,
43             StackTraceElement JavaDoc[] stackTraceElements,
44             StringBuilder JavaDoc sb,
45             int maxDepth)
46     {
47         sb.append(msg).append(" \n")
48           .append(" Started at: \n");
49         for (int i = 0; i < stackTraceElements.length; i++)
50         {
51             if (i > maxDepth && maxDepth > 0)
52             {
53                 sb.append(" ...");
54                 break;
55             }
56             sb.append(" ").append(stackTraceElements[i]);
57             if (i < stackTraceElements.length - 1)
58             {
59                 sb.append("\n");
60             }
61         }
62     }
63 }
64
Popular Tags