1 21 22 package org.armedbear.j; 23 24 import javax.swing.SwingUtilities ; 25 26 public final class Debug 27 { 28 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 public static void bug(String s) 51 { 52 Log.error("BUG! " + s); 53 bug(); 54 } 55 56 public static void bug() 58 { 59 Log.error(new Exception ("BUG!")); 60 } 61 62 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 ("Stack trace")); 79 } 80 81 public static void throttle() 82 { 83 if (Editor.isDebugEnabled() && !SwingUtilities.isEventDispatchThread()) { 84 String 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 e ) { 91 Log.error(e); 92 } 93 catch (InterruptedException e) {} 94 } 95 } 96 } 97 98 public static void listThreads() 99 { 100 int threadCount = Thread.currentThread().activeCount(); 101 Thread [] threads = new Thread [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 thread = threads[i]; 107 sb.setText(thread.getName()); 108 sb.append(' '); 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 [] cmdarray = {"bash", "-c", 119 "ps -o pid,pri,%cpu,rss,start,time,command"}; 120 String output = Utilities.exec(cmdarray); 121 FastStringReader reader = new FastStringReader(output); 122 String 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 |