    1   /* DevUtils
    2    *
    3    * Created on Oct 29, 2003
    4    *
    5    * Copyright (C) 2003 Internet Archive.
    6    *
    7    * This file is part of the Heritrix web crawler (crawler.archive.org).
    8    *
    9    * Heritrix is free software; you can redistribute it and/or modify
    10   * it under the terms of the GNU Lesser Public License as published by
    11   * the Free Software Foundation; either version 2.1 of the License, or
    12   * any later version.
    13   *
    14   * Heritrix is distributed in the hope that it will be useful,
    15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    17   * GNU Lesser Public License for more details.
    18   *
    19   * You should have received a copy of the GNU Lesser Public License
    20   * along with Heritrix; if not, write to the Free Software
    21   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    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                  // Not really possible w/ a StringWriter
    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                  // Not really possible w/ a StringWriter
    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             // TODO Auto-generated catch block
    114             e.printStackTrace();
    115         } catch (InterruptedException e) {
    116             // TODO Auto-generated catch block
    117             e.printStackTrace();
    118         }
    119     }
    120 }