Java Throwable to String getStackTrace(Throwable t)

Here you can find the source of getStackTrace(Throwable t)

Description

Returns a String containing the stack trace for a throwable.

License

Apache License

Declaration

public static String getStackTrace(Throwable t) 

Method Source Code

//package com.java2s;
/*/*from  w ww.  j a  va  2s.  c  o m*/
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.ByteArrayOutputStream;

import java.io.PrintStream;

public class Main {
    /**
     * Controls the amount of information printed.
     *  0 -- Just print final results and failures.
     *  5 -- Include stack trace for unexpected exceptions (default).
     * 10 -- Print test number and class for each new top level test class
     * 15 -- Print test number and class for every test.
     * 20 -- Print full test entry and pass/fail for every test.
     * 25 -- Include passing results.
     * 30 -- Include additional test debugging output, including time.
     */
    public static final int testLevel = Integer.getInteger("testLevel", 5).intValue();

    /** Returns a String containing the stack trace for a throwable. */
    public static String getStackTrace(Throwable t) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PrintStream ps = new PrintStream(baos);
        t.printStackTrace(ps);
        ps.flush();
        String result = baos.toString();
        ps.close();
        return result;
    }

    /**
     * Returns the appropriate String for describing a Throwable at the current
     * test level.
     */
    public static String toString(Throwable t) {
        return (testLevel < 5) ? String.valueOf(t) : getStackTrace(t);
    }

    /** Converts the contents of an Object array to a String. */
    public static String toString(Object[] array) {
        if (array == null) {
            return "null";
        }
        StringBuffer buf = new StringBuffer("[");
        for (int i = 0; i < array.length; i++) {
            if (i != 0) {
                buf.append(", ");
            }
            buf.append(array[i]);
        }
        buf.append("]");
        return buf.toString();
    }
}

Related

  1. getStackTrace(Throwable t)
  2. getStackTrace(Throwable t)
  3. getStackTrace(Throwable t)
  4. getStackTrace(Throwable t)
  5. getStackTrace(Throwable t)
  6. getStackTrace(Throwable t)
  7. getStackTrace(Throwable t)
  8. getStackTrace(Throwable t)
  9. getStackTrace(Throwable t)