KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mchange > v2 > debug > ThreadNameStackTraceRecorder


1 /*
2  * Distributed as part of c3p0 v.0.9.1
3  *
4  * Copyright (C) 2005 Machinery For Change, Inc.
5  *
6  * Author: Steve Waldman <swaldman@mchange.com>
7  *
8  * This library is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License version 2.1, as
10  * published by the Free Software Foundation.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this software; see the file LICENSE. If not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */

22
23
24 package com.mchange.v2.debug;
25
26 import java.text.*;
27 import java.util.*;
28 import com.mchange.lang.ThrowableUtils;
29
30 public class ThreadNameStackTraceRecorder
31 {
32     final static String JavaDoc NL = System.getProperty("line.separator", "\r\n");
33
34     Set set = new HashSet();
35
36     String JavaDoc dumpHeader;
37     String JavaDoc stackTraceHeader;
38
39     public ThreadNameStackTraceRecorder( String JavaDoc dumpHeader )
40     { this( dumpHeader, "Debug Stack Trace." ); }
41
42     public ThreadNameStackTraceRecorder( String JavaDoc dumpHeader, String JavaDoc stackTraceHeader )
43     {
44     this.dumpHeader = dumpHeader;
45     this.stackTraceHeader = stackTraceHeader;
46     }
47
48     public synchronized Object JavaDoc record()
49     {
50     Record r = new Record( stackTraceHeader );
51     set.add( r );
52     return r;
53     }
54
55     public synchronized void remove( Object JavaDoc rec )
56     { set.remove( rec ); }
57
58     public synchronized int size()
59     { return set.size(); }
60
61     public synchronized String JavaDoc getDump()
62     { return getDump(null); }
63
64     public synchronized String JavaDoc getDump(String JavaDoc locationSpecificNote)
65     {
66     DateFormat df = new SimpleDateFormat("dd-MMMM-yyyy HH:mm:ss.SSSS");
67
68     StringBuffer JavaDoc sb = new StringBuffer JavaDoc(2047);
69     sb.append(NL);
70     sb.append("----------------------------------------------------");
71     sb.append(NL);
72     sb.append( dumpHeader );
73     sb.append(NL);
74     if (locationSpecificNote != null)
75         {
76         sb.append( locationSpecificNote );
77         sb.append( NL );
78         }
79     boolean first = true;
80     for (Iterator ii = set.iterator(); ii.hasNext(); )
81         {
82         if (first)
83             first = false;
84         else
85             {
86             sb.append("---");
87             sb.append( NL );
88             }
89
90         Record r = (Record) ii.next();
91         sb.append(df.format( new Date( r.time ) ));
92         sb.append(" --> Thread Name: ");
93         sb.append(r.threadName);
94         sb.append(NL);
95         sb.append("Stack Trace: ");
96         sb.append( ThrowableUtils.extractStackTrace( r.stackTrace ) );
97         }
98     sb.append("----------------------------------------------------");
99     sb.append(NL);
100     return sb.toString();
101     }
102
103     private final static class Record implements Comparable JavaDoc
104     {
105     long time;
106     String JavaDoc threadName;
107     Throwable JavaDoc stackTrace;
108
109     Record(String JavaDoc sth)
110     {
111         this.time = System.currentTimeMillis();
112         this.threadName = Thread.currentThread().getName();
113         this.stackTrace = new Exception JavaDoc( sth );
114     }
115
116     public int compareTo( Object JavaDoc o )
117     {
118         Record oo = (Record) o;
119         if ( this.time > oo.time )
120         return 1;
121         else if (this.time < oo.time )
122         return -1;
123         else
124         {
125             int mine = System.identityHashCode( this );
126             int yours = System.identityHashCode( oo );
127             if (mine > yours)
128             return 1;
129             else if (mine < yours)
130             return -1;
131             return 0;
132         }
133     }
134     }
135 }
Popular Tags