001: /*
002: * Debug.java
003: *
004: * Copyright (C) 1998-2002 Peter Graves
005: * $Id: Debug.java,v 1.6 2003/02/12 15:09:33 piso Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.j;
023:
024: import javax.swing.SwingUtilities;
025:
026: public final class Debug {
027: // Assertions.
028: public static final void assertTrue(boolean b) {
029: if (!b) {
030: Log.error("Assertion failed!");
031: AssertionException e = new AssertionException();
032: Log.error(e);
033: throw e;
034: }
035: }
036:
037: public static final void assertFalse(boolean b) {
038: if (b) {
039: Log.error("Assertion failed!");
040: AssertionException e = new AssertionException();
041: Log.error(e);
042: throw e;
043: }
044: }
045:
046: // Does not throw an exception.
047: public static void bug(String s) {
048: Log.error("BUG! " + s);
049: bug();
050: }
051:
052: // Does not throw an exception.
053: public static void bug() {
054: Log.error(new Exception("BUG!"));
055: }
056:
057: // A kinder, gentler form of assertion.
058: public static void bugIfNot(boolean b) {
059: if (!b)
060: bug();
061: }
062:
063: public static void bugIf(boolean b) {
064: if (b)
065: bug();
066: }
067:
068: public static void dumpStack() {
069: if (Editor.isDebugEnabled())
070: Log.debug(new Exception("Stack trace"));
071: }
072:
073: public static void throttle() {
074: if (Editor.isDebugEnabled()
075: && !SwingUtilities.isEventDispatchThread()) {
076: String throttle = Editor.preferences().getStringProperty(
077: "throttle");
078: if (throttle != null) {
079: try {
080: int delay = Integer.parseInt(throttle);
081: Thread.sleep(delay);
082: } catch (NumberFormatException e) {
083: Log.error(e);
084: } catch (InterruptedException e) {
085: }
086: }
087: }
088: }
089:
090: public static void listThreads() {
091: int threadCount = Thread.currentThread().activeCount();
092: Thread[] threads = new Thread[threadCount];
093: threadCount = Thread.currentThread().enumerate(threads);
094: FastStringBuffer sb = new FastStringBuffer();
095: Log.debug("----- listThreads -----");
096: for (int i = 0; i < threadCount; i++) {
097: Thread thread = threads[i];
098: sb.setText(thread.getName());
099: sb.append(' '); // Follow with at least one space.
100: while (sb.length() < 24)
101: sb.append(' ');
102: sb.append(thread.getPriority());
103: if (thread.isDaemon())
104: sb.append(" (daemon)");
105: Log.debug(sb.toString());
106: }
107: int processCount = 0;
108: if (Platform.isPlatformLinux()) {
109: String[] cmdarray = { "bash", "-c",
110: "ps -o pid,pri,%cpu,rss,start,time,command" };
111: String output = Utilities.exec(cmdarray);
112: FastStringReader reader = new FastStringReader(output);
113: String s = reader.readLine();
114: if (s != null)
115: Log.debug(s);
116: while ((s = reader.readLine()) != null) {
117: if (s.indexOf("java") >= 0) {
118: Log.debug(s);
119: ++processCount;
120: }
121: }
122: }
123: sb.setText("listThreads: ");
124: sb.append(threadCount);
125: sb.append(" Java threads");
126: if (processCount > 0) {
127: sb.append(", ");
128: sb.append(processCount);
129: sb.append(" processes");
130: }
131: Log.debug(sb.toString());
132: }
133: }
|