Example usage for java.lang StackTraceElement toString

List of usage examples for java.lang StackTraceElement toString

Introduction

In this page you can find the example usage for java.lang StackTraceElement toString.

Prototype

public String toString() 

Source Link

Document

Returns a string representation of this stack trace element.

Usage

From source file:org.apache.hadoop.util.ReflectionUtils.java

/**
 * Print all of the thread's information and stack traces.
 *
 * @param stream the stream to//from   ww  w .  java 2 s  . c  o  m
 * @param title a string title for the stack trace
 */
public static synchronized void printThreadInfo(PrintWriter stream, String title) {
    final int stackDepth = 20;
    boolean contention = threadBean.isThreadContentionMonitoringEnabled();
    long[] threadIds = threadBean.getAllThreadIds();
    stream.println("Process Thread Dump: " + title);
    stream.println(threadIds.length + " active threads");
    for (long tid : threadIds) {
        ThreadInfo info = threadBean.getThreadInfo(tid, stackDepth);
        if (info == null) {
            stream.println("  Inactive");
            continue;
        }
        stream.println("Thread " + getTaskName(info.getThreadId(), info.getThreadName()) + ":");
        Thread.State state = info.getThreadState();
        stream.println("  State: " + state);
        stream.println("  Blocked count: " + info.getBlockedCount());
        stream.println("  Waited count: " + info.getWaitedCount());
        if (contention) {
            stream.println("  Blocked time: " + info.getBlockedTime());
            stream.println("  Waited time: " + info.getWaitedTime());
        }
        if (state == Thread.State.WAITING) {
            stream.println("  Waiting on " + info.getLockName());
        } else if (state == Thread.State.BLOCKED) {
            stream.println("  Blocked on " + info.getLockName());
            stream.println("  Blocked by " + getTaskName(info.getLockOwnerId(), info.getLockOwnerName()));
        }
        stream.println("  Stack:");
        for (StackTraceElement frame : info.getStackTrace()) {
            stream.println("    " + frame.toString());
        }
    }
    stream.flush();
}

From source file:com.buaa.cfs.utils.ReflectionUtils.java

/**
 * Print all of the thread's information and stack traces.
 *
 * @param stream the stream to/*from www . j a  v a2  s.  c o m*/
 * @param title  a string title for the stack trace
 */
public synchronized static void printThreadInfo(PrintStream stream, String title) {
    final int STACK_DEPTH = 20;
    boolean contention = threadBean.isThreadContentionMonitoringEnabled();
    long[] threadIds = threadBean.getAllThreadIds();
    stream.println("Process Thread Dump: " + title);
    stream.println(threadIds.length + " active threads");
    for (long tid : threadIds) {
        ThreadInfo info = threadBean.getThreadInfo(tid, STACK_DEPTH);
        if (info == null) {
            stream.println("  Inactive");
            continue;
        }
        stream.println("Thread " + getTaskName(info.getThreadId(), info.getThreadName()) + ":");
        Thread.State state = info.getThreadState();
        stream.println("  State: " + state);
        stream.println("  Blocked count: " + info.getBlockedCount());
        stream.println("  Waited count: " + info.getWaitedCount());
        if (contention) {
            stream.println("  Blocked time: " + info.getBlockedTime());
            stream.println("  Waited time: " + info.getWaitedTime());
        }
        if (state == Thread.State.WAITING) {
            stream.println("  Waiting on " + info.getLockName());
        } else if (state == Thread.State.BLOCKED) {
            stream.println("  Blocked on " + info.getLockName());
            stream.println("  Blocked by " + getTaskName(info.getLockOwnerId(), info.getLockOwnerName()));
        }
        stream.println("  Stack:");
        for (StackTraceElement frame : info.getStackTrace()) {
            stream.println("    " + frame.toString());
        }
    }
    stream.flush();
}

From source file:com.palantir.atlasdb.keyvalue.cassandra.CassandraKeyValueServices.java

static String getFilteredStackTrace(String filter) {
    Exception e = new Exception();
    StackTraceElement[] stackTrace = e.getStackTrace();
    StringBuilder sb = new StringBuilder();
    for (StackTraceElement element : stackTrace) {
        if (element.getClassName().contains(filter)) {
            sb.append(element.toString()).append("\n");
        }// ww  w . j av  a 2 s  .co m
    }
    return sb.toString();
}

From source file:de.Keyle.MyPet.api.Util.java

public static String stackTraceToString() {
    String trace = "";
    for (StackTraceElement e1 : Thread.currentThread().getStackTrace()) {
        trace += "\t " + e1.toString() + "\n";
    }/*from  w  ww. j  a v a2s. c  o m*/
    return trace;
}

From source file:com.alicloud.tablestore.adaptor.client.IntegratedTest.java

public static void printThreadInfo() {
    String title = "Automatic Stack Trace";
    PrintWriter stream = new PrintWriter(System.out);
    int STACK_DEPTH = 20;
    boolean contention = threadBean.isThreadContentionMonitoringEnabled();
    long[] threadIds = threadBean.getAllThreadIds();
    stream.println("Process Thread Dump: " + title);
    stream.println(threadIds.length + " active threads");
    for (long tid : threadIds) {
        ThreadInfo info = threadBean.getThreadInfo(tid, 20);
        if (info == null) {
            stream.println("  Inactive");
        } else {//from   w ww  .  j  ava2 s  .  c  o m
            stream.println("Thread " + getTaskName(info.getThreadId(), info.getThreadName()) + ":");

            Thread.State state = info.getThreadState();
            stream.println("  State: " + state);
            stream.println("  Blocked count: " + info.getBlockedCount());
            stream.println("  Waited count: " + info.getWaitedCount());
            if (contention) {
                stream.println("  Blocked time: " + info.getBlockedTime());
                stream.println("  Waited time: " + info.getWaitedTime());
            }
            if (state == Thread.State.WAITING) {
                stream.println("  Waiting on " + info.getLockName());
            } else if (state == Thread.State.BLOCKED) {
                stream.println("  Blocked on " + info.getLockName());
                stream.println("  Blocked by " + getTaskName(info.getLockOwnerId(), info.getLockOwnerName()));
            }

            stream.println("  Stack:");
            for (StackTraceElement frame : info.getStackTrace())
                stream.println("    " + frame.toString());
        }
    }
    stream.flush();
}

From source file:org.apache.openejb.resource.AutoConnectionTracker.java

public static String stackTraceToString(final StackTraceElement[] stackTrace) {
    if (stackTrace == null) {
        return "";
    }/*from   www.ja v a  2  s .  c  o  m*/

    final StringBuilder sb = new StringBuilder();

    for (int i = 0; i < stackTrace.length; i++) {
        final StackTraceElement element = stackTrace[i];
        if (i > 0) {
            sb.append(", ");
        }

        sb.append(element.toString());
    }

    return sb.toString();
}

From source file:com.tussle.main.Utility.java

public static JsonValue exceptionToJson(Throwable ex) {
    JsonValue topValue = new JsonValue(JsonValue.ValueType.object);
    JsonValue exceptionClass = new JsonValue(ex.getClass().toString());
    topValue.addChild("Exception Class", exceptionClass);
    if (ex.getLocalizedMessage() != null) {
        JsonValue exceptionMessage = new JsonValue(ex.getLocalizedMessage());
        topValue.addChild("Exception Message", exceptionMessage);
    }// w w  w .j  a va  2  s. c  o  m
    if (ex.getCause() != null) {
        JsonValue exceptionCause = new JsonValue(ex.getCause().toString());
        topValue.addChild("Exception Cause", exceptionCause);
    }
    JsonValue stackTrace = new JsonValue(JsonValue.ValueType.array);
    for (StackTraceElement element : ex.getStackTrace())
        stackTrace.addChild(new JsonValue(element.toString()));
    topValue.addChild("Stack Trace", stackTrace);
    return topValue;
}

From source file:org.jajuk.util.log.Log.java

/**
 * Spool an exception with stack traces.
 * /*from www  .  ja  va  2  s .  c  o  m*/
 * @param e 
 */
private static void spool(Throwable e) {
    spool("<font color='red'>" + "[ERROR] " + e.getClass() + " / {{" + e.getMessage() + "}} / " + e.getCause());
    StackTraceElement[] ste = e.getStackTrace();
    for (StackTraceElement element : ste) {
        spool(element.toString());
    }
    spool(FONT_END);
}

From source file:password.pwm.util.java.JavaHelper.java

/**
 * Copy of {@link ThreadInfo#toString()} but with the MAX_FRAMES changed from 8 to 256.
 *//*from   ww w .ja v a 2  s  .  c o  m*/
public static String threadInfoToString(final ThreadInfo threadInfo) {
    final int MAX_FRAMES = 256;
    final StringBuilder sb = new StringBuilder("\"" + threadInfo.getThreadName() + "\"" + " Id="
            + threadInfo.getThreadId() + " " + threadInfo.getThreadState());
    if (threadInfo.getLockName() != null) {
        sb.append(" on " + threadInfo.getLockName());
    }
    if (threadInfo.getLockOwnerName() != null) {
        sb.append(" owned by \"" + threadInfo.getLockOwnerName() + "\" Id=" + threadInfo.getLockOwnerId());
    }
    if (threadInfo.isSuspended()) {
        sb.append(" (suspended)");
    }
    if (threadInfo.isInNative()) {
        sb.append(" (in native)");
    }
    sb.append('\n');

    int counter = 0;
    for (; counter < threadInfo.getStackTrace().length && counter < MAX_FRAMES; counter++) {
        final StackTraceElement ste = threadInfo.getStackTrace()[counter];
        sb.append("\tat ").append(ste.toString());
        sb.append('\n');
        if (counter == 0 && threadInfo.getLockInfo() != null) {
            final Thread.State ts = threadInfo.getThreadState();
            switch (ts) {
            case BLOCKED:
                sb.append("\t-  blocked on " + threadInfo.getLockInfo());
                sb.append('\n');
                break;
            case WAITING:
                sb.append("\t-  waiting on " + threadInfo.getLockInfo());
                sb.append('\n');
                break;
            case TIMED_WAITING:
                sb.append("\t-  waiting on " + threadInfo.getLockInfo());
                sb.append('\n');
                break;
            default:
            }
        }

        for (MonitorInfo mi : threadInfo.getLockedMonitors()) {
            if (mi.getLockedStackDepth() == counter) {
                sb.append("\t-  locked " + mi);
                sb.append('\n');
            }
        }
    }
    if (counter < threadInfo.getStackTrace().length) {
        sb.append("\t...");
        sb.append('\n');
    }

    final LockInfo[] locks = threadInfo.getLockedSynchronizers();
    if (locks.length > 0) {
        sb.append("\n\tNumber of locked synchronizers = " + locks.length);
        sb.append('\n');
        for (LockInfo li : locks) {
            sb.append("\t- " + li);
            sb.append('\n');
        }
    }
    sb.append('\n');
    return sb.toString();
}

From source file:com.qpark.eip.core.ToString.java

private static String getStackTrace(final Throwable t, final boolean isCause) {
    StringBuffer sb = new StringBuffer(1024);
    if (isCause) {
        sb.append("Caused by: ");
    }//www  .  ja v  a2s  .com
    sb.append(t.getClass().getName()).append(": ").append(t.getMessage()).append("\n");
    StackTraceElement[] stack = t.getStackTrace();
    int classNameLines = 0;
    int classNameLinesMax = 3;
    boolean printDots = false;
    boolean firstLine = true;
    for (StackTraceElement elem : stack) {
        if (firstLine || elem.getClassName().startsWith(CLASSNAME) && classNameLines <= classNameLinesMax) {
            sb.append("\tat ").append(elem.toString()).append("\n");
            classNameLines++;
            firstLine = false;
            printDots = false;
        } else {
            if (!printDots) {
                sb.append("\tat ...\n");
                printDots = true;
            }
            classNameLines = 0;
        }
    }
    Throwable cause = t.getCause();
    if (cause != null) {
        sb.append(getStackTrace(cause, true));
    }
    return sb.toString();
}