KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > Debug


1 /*
2  * Debug.java
3  *
4  * Copyright (C) 1998-2002 Peter Graves
5  * $Id: Debug.java,v 1.6 2003/02/12 15:09:33 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program 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 General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.j;
23
24 import javax.swing.SwingUtilities JavaDoc;
25
26 public final class Debug
27 {
28     // Assertions.
29
public static final void assertTrue(boolean b)
30     {
31         if (!b) {
32             Log.error("Assertion failed!");
33             AssertionException e = new AssertionException();
34             Log.error(e);
35             throw e;
36         }
37     }
38
39     public static final void assertFalse(boolean b)
40     {
41         if (b) {
42             Log.error("Assertion failed!");
43             AssertionException e = new AssertionException();
44             Log.error(e);
45             throw e;
46         }
47     }
48
49     // Does not throw an exception.
50
public static void bug(String JavaDoc s)
51     {
52         Log.error("BUG! " + s);
53         bug();
54     }
55
56     // Does not throw an exception.
57
public static void bug()
58     {
59         Log.error(new Exception JavaDoc("BUG!"));
60     }
61
62     // A kinder, gentler form of assertion.
63
public static void bugIfNot(boolean b)
64     {
65         if (!b)
66             bug();
67     }
68
69     public static void bugIf(boolean b)
70     {
71         if (b)
72             bug();
73     }
74
75     public static void dumpStack()
76     {
77         if (Editor.isDebugEnabled())
78             Log.debug(new Exception JavaDoc("Stack trace"));
79     }
80
81     public static void throttle()
82     {
83         if (Editor.isDebugEnabled() && !SwingUtilities.isEventDispatchThread()) {
84             String JavaDoc throttle = Editor.preferences().getStringProperty("throttle");
85             if (throttle != null) {
86                 try {
87                     int delay = Integer.parseInt(throttle);
88                     Thread.sleep(delay);
89                 }
90                 catch (NumberFormatException JavaDoc e ) {
91                     Log.error(e);
92                 }
93                 catch (InterruptedException JavaDoc e) {}
94             }
95         }
96     }
97
98     public static void listThreads()
99     {
100         int threadCount = Thread.currentThread().activeCount();
101         Thread JavaDoc[] threads = new Thread JavaDoc[threadCount];
102         threadCount = Thread.currentThread().enumerate(threads);
103         FastStringBuffer sb = new FastStringBuffer();
104         Log.debug("----- listThreads -----");
105         for (int i = 0; i < threadCount; i++) {
106             Thread JavaDoc thread = threads[i];
107             sb.setText(thread.getName());
108             sb.append(' '); // Follow with at least one space.
109
while (sb.length() < 24)
110                 sb.append(' ');
111             sb.append(thread.getPriority());
112             if (thread.isDaemon())
113                 sb.append(" (daemon)");
114             Log.debug(sb.toString());
115         }
116         int processCount = 0;
117         if (Platform.isPlatformLinux()) {
118             String JavaDoc[] cmdarray = {"bash", "-c",
119                 "ps -o pid,pri,%cpu,rss,start,time,command"};
120             String JavaDoc output = Utilities.exec(cmdarray);
121             FastStringReader reader = new FastStringReader(output);
122             String JavaDoc s = reader.readLine();
123             if (s != null)
124                 Log.debug(s);
125             while ((s = reader.readLine()) != null) {
126                 if (s.indexOf("java") >= 0) {
127                     Log.debug(s);
128                     ++processCount;
129                 }
130             }
131         }
132         sb.setText("listThreads: ");
133         sb.append(threadCount);
134         sb.append(" Java threads");
135         if (processCount > 0) {
136             sb.append(", ");
137             sb.append(processCount);
138             sb.append(" processes");
139         }
140         Log.debug(sb.toString());
141     }
142 }
143
Popular Tags