1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.archive.util;
24
25 import java.io.BufferedReader;
26 import java.io.IOException;
27 import java.io.InputStreamReader;
28 import java.io.PrintWriter;
29 import java.io.StringWriter;
30 import java.util.logging.Logger;
31
32
33 /***
34 * Write a message and stack trace to the 'org.archive.util.DevUtils' logger.
35 *
36 * @author gojomo
37 * @version $Revision: 4644 $ $Date: 2006-09-20 22:40:21 +0000 (Wed, 20 Sep 2006) $
38 */
39 public class DevUtils {
40 public static Logger logger =
41 Logger.getLogger(DevUtils.class.getName());
42
43 /***
44 * Log a warning message to the logger 'org.archive.util.DevUtils' made of
45 * the passed 'note' and a stack trace based off passed exception.
46 *
47 * @param ex Exception we print a stacktrace on.
48 * @param note Message to print ahead of the stacktrace.
49 */
50 public static void warnHandle(Throwable ex, String note) {
51 logger.warning(TextUtils.exceptionToString(note, ex));
52 }
53
54 /***
55 * @return Extra information gotten from current Thread. May not
56 * always be available in which case we return empty string.
57 */
58 public static String extraInfo() {
59 StringWriter sw = new StringWriter();
60 PrintWriter pw = new PrintWriter(sw);
61 final Thread current = Thread.currentThread();
62 if (current instanceof Reporter) {
63 Reporter tt = (Reporter)current;
64 try {
65 tt.reportTo(pw);
66 } catch (IOException e) {
67
68 e.printStackTrace();
69 }
70 }
71 if (current instanceof ProgressStatisticsReporter) {
72 ProgressStatisticsReporter tt = (ProgressStatisticsReporter)current;
73 try {
74 tt.progressStatisticsLegend(pw);
75 tt.progressStatisticsLine(pw);
76 } catch (IOException e) {
77
78 e.printStackTrace();
79 }
80 }
81 pw.flush();
82 return sw.toString();
83 }
84
85 /***
86 * Nothing to see here, move along.
87 * @deprecated This method was never used.
88 */
89 @Deprecated
90 public static void betterPrintStack(RuntimeException re) {
91 re.printStackTrace(System.err);
92 }
93
94 /***
95 * Send this JVM process a SIGQUIT; giving a thread dump and possibly
96 * a heap histogram (if using -XX:+PrintClassHistogram).
97 *
98 * Used to automatically dump info, for example when a serious error
99 * is encountered. Would use 'jmap'/'jstack', but have seen JVM
100 * lockups -- perhaps due to lost thread wake signals -- when using
101 * those against Sun 1.5.0+03 64bit JVM.
102 */
103 public static void sigquitSelf() {
104 try {
105 Process p = Runtime.getRuntime().exec(
106 new String[] {"perl", "-e", "print getppid(). \"\n\";"});
107 BufferedReader br =
108 new BufferedReader(new InputStreamReader(p.getInputStream()));
109 String ppid = br.readLine();
110 Runtime.getRuntime().exec(
111 new String[] {"sh", "-c", "kill -3 "+ppid}).waitFor();
112 } catch (IOException e) {
113
114 e.printStackTrace();
115 } catch (InterruptedException e) {
116
117 e.printStackTrace();
118 }
119 }
120 }