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:Main.java

public static void logToFile(Throwable e) {
    try {//from www .  j av a2s.com
        StringBuffer sb = new StringBuffer(e.toString() + "\n");
        StackTraceElement[] stElements = e.getStackTrace();
        String newLine = "";

        for (StackTraceElement stElement : stElements) {
            sb.append(newLine);
            sb.append("\tat ");
            sb.append(stElement.toString());
            newLine = "\n";
        }
    } catch (Exception ee) {
        e.printStackTrace();
    }
}

From source file:Log.java

/** Converts a stack trace (StackTraceElement[]) to string form */
public static String traceToString(StackTraceElement[] trace) {
    final StringBuilder traceImage = new StringBuilder();
    for (StackTraceElement e : trace)
        traceImage.append("\n\tat " + e.toString());
    return traceImage.toString();
}

From source file:ReflectionUtils.java

/**
 * Print all of the thread's information and stack traces.
 * /*w w  w. j a  v a2s  .com*/
 * @param stream
 *          the stream to
 * @param title
 *          a string title for the stack trace
 */
public static void printThreadInfo(PrintWriter 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.qwazr.utils.ExceptionUtils.java

public final static String getLocation(StackTraceElement[] stackTrace, String prefix) {
    for (StackTraceElement element : stackTrace)
        if (element.getClassName().startsWith(prefix))
            return element.toString();
    return null;//from  ww  w. j  a v a  2s . com
}

From source file:org.apache.bookkeeper.common.testing.util.TimedOutTestsListener.java

private static void printThreadInfo(ThreadInfo ti, PrintWriter out) {
    // print thread information
    printThread(ti, out);// w w w .j  a  v a  2 s  . c  om

    // print stack trace with locks
    StackTraceElement[] stacktrace = ti.getStackTrace();
    MonitorInfo[] monitors = ti.getLockedMonitors();
    for (int i = 0; i < stacktrace.length; i++) {
        StackTraceElement ste = stacktrace[i];
        out.println(indent + "at " + ste.toString());
        for (MonitorInfo mi : monitors) {
            if (mi.getLockedStackDepth() == i) {
                out.println(indent + "  - locked " + mi);
            }
        }
    }
    out.println();
}

From source file:org.apache.hadoop.thriftfs.ThriftUtils.java

public static IOException toThrift(Throwable t) {
    if (t == null) {
        return new IOException();
    }//  www  .j a  v  a2  s  .c o  m

    IOException ret = new IOException();
    ret.clazz = t.getClass().getName();
    ret.msg = t.getMessage();
    ret.stack = "";
    for (StackTraceElement frame : t.getStackTrace()) {
        ret.stack += frame.toString() + "\n";
    }
    return ret;
}

From source file:org.n52.util.CommonUtilities.java

public static String convertExceptionToString(Exception e) {
    StringBuilder sb = new StringBuilder(e.getMessage());
    sb.append(":");
    sb.append(NEW_LINE_CHAR);// w w w  . jav a  2 s  . com

    int count = 0;
    for (StackTraceElement ste : e.getStackTrace()) {
        sb.append(TAB_CHAR);
        sb.append(ste.toString());
        if (++count < e.getStackTrace().length)
            sb.append(NEW_LINE_CHAR);
    }

    return sb.toString();
}

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

/**
 * Print all of the thread's information and stack traces.
 *
 * @param stream the stream to/*w w  w .  j  a  v  a2 s . co  m*/
 * @param title a string title for the stack trace
 */
private 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:org.mapfish.print.map.readers.WMSServerInfo.java

private static synchronized WMSServerInfo getInfoImpl(URI uri, RenderingContext context) {
    WMSServerInfo result = cache.get(uri);
    if (result == null) {
        try {/*  w w w .  j a  va2s. c  om*/
            result = requestInfo(uri, context);
        } catch (Exception e) {
            LOGGER.info("Error while getting capabilities for " + uri
                    + ". The print module will assume it's a standard WMS.");
            String stackTrace = "";
            for (StackTraceElement el : e.getStackTrace()) {
                stackTrace += el.toString() + "\n";
            }
            LOGGER.info(stackTrace);
            result = new WMSServerInfo();
        }
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("GetCapabilities " + uri + ": " + result);
        }
        cache.put(uri, result);
    }
    return result;
}

From source file:org.mrgeo.gce.MrGeoFormatFactorySpi.java

private static void logstack(Throwable t) {
    StackTraceElement[] st = t.getStackTrace();
    boolean first = true;
    for (StackTraceElement tr : st) {
        if (first) {
            first = false;//from  w w w . ja  v  a2 s.c o  m
            log.severe(tr.toString());
        } else {
            log.severe("  " + tr.toString());
        }
    }
}