1 package com.bonevich.util;
2
3 import java.util.StringTokenizer;
4 import java.io.*;
5
6 /***
7 * A class with static methods that allow you to dump
8 * a stack trace from any point in the thread of execution
9 * without having to disrupt that thread.
10 */
11 public class StackDump extends RuntimeException
12 {
13 private StackDump() { super(); }
14 private StackDump(String message) { super(message); }
15
16 public static void print(Object obj)
17 {
18 StackDump.print(obj,System.err);
19 }
20 public static void print(Object obj, PrintStream s)
21 {
22 StackDump.print(obj,new PrintWriter(s));
23 }
24 public static void print(Object obj, PrintWriter s)
25 {
26 try {
27 StackDump stackDump = new StackDump("Trace for class " + obj.getClass().getName());
28 throw stackDump;
29 } catch (StackDump sd) {
30 sd.dump(s);
31 }
32 }
33
34 /***
35 * Dumps only the relevant stack trace lines to the <code>PrintWriter</code>
36 * (i.e. all lines related to calls within <code>StackDump</code> are
37 * discarded).
38 */
39 private void dump(PrintWriter s)
40 {
41 StringWriter buffer = new StringWriter();
42 PrintWriter dump = new PrintWriter(buffer,true);
43 printStackTrace(dump);
44 //System.out.println(buffer.toString());
45 StringTokenizer st = new StringTokenizer(buffer.toString(),"\n");
46 int count = 0;
47 while (st.hasMoreTokens())
48 {
49 String str = st.nextToken();
50 //System.out.println("token = " + str + ", count = " + count);
51 if (str.indexOf("StackDump") < 0 || count == 0) {
52 s.println(str);
53 count++;
54 }
55 }
56 s.flush();
57 }
58 }
This page was automatically generated by Maven