Java Stacktrace to String getStackTraces()

Here you can find the source of getStackTraces()

Description

Get a string representation of the current stack state of all the active threads.

License

Open Source License

Declaration

public static String getStackTraces() 

Method Source Code

//package com.java2s;
/*******************************************************************************
 *  Copyright (c) 2012 Google, Inc.//from  w ww .  j  av a2s .  c o  m
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License v1.0
 *  which accompanies this distribution, and is available at
 *  http://www.eclipse.org/legal/epl-v10.html
 *  
 *  Contributors:
 *  Google, Inc. - initial API and implementation
 *******************************************************************************/

import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Map;

import java.util.Map.Entry;

public class Main {
    /**
     * Get a string representation of the current stack state of all the active threads.
     */
    public static String getStackTraces() {
        StringWriter stringWriter = new StringWriter(5000);
        printStackTraces(new PrintWriter(stringWriter));
        return stringWriter.toString();
    }

    /**
     * Print a string representation of the current stack state of all the active threads.
     */
    public static void printStackTraces() {
        printStackTraces(new PrintWriter(new OutputStreamWriter(System.err)));
    }

    /**
     * Print a string representation of the current stack state of all the active threads.
     */
    public static void printStackTraces(PrintWriter writer) {
        Map<Thread, StackTraceElement[]> map;
        try {
            map = Thread.getAllStackTraces();
        } catch (Throwable e) {
            writer.println("Failed to obtain stack traces: " + e);
            return;
        }
        if (map == null) {
            writer.println("No stack traces available");
            return;
        }
        for (Entry<Thread, StackTraceElement[]> entry : map.entrySet())
            printStackTrace(writer, entry.getKey(), entry.getValue());
        writer.flush();
    }

    private static void printStackTrace(PrintWriter writer, Thread thread, StackTraceElement[] trace) {
        try {
            writer.println(thread.toString() + ":");
            for (int i = 0; i < trace.length; i++)
                writer.println("\tat " + trace[i]);
        } catch (Exception e) {
            writer.println("\t*** Exception printing stack trace: " + e);
        }
        writer.flush();
    }
}

Related

  1. getStackTraceAsString(Exception e)
  2. getStackTraceAsString(Exception e)
  3. getStackTraceAsString(String pstrMessage, Exception poException)
  4. getStackTraceFromException(Exception _e)
  5. getStackTraceFromException(Exception e)
  6. getStackTraceStr(Exception e)
  7. getStackTraceString()
  8. getStackTraceString(Exception e)
  9. getStackTraceString(Exception e)