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 printStack(ThreadInfo info, final StringBuilder sb, final StackTraceElement[] stack) {
    int i = 0;/*from w  w w.j a v a2  s . com*/
    for (final StackTraceElement element : stack) {
        sb.append("\tat ").append(element.toString());
        sb.append('\n');
        if (i == 0 && info.getLockInfo() != null) {
            final Thread.State ts = info.getThreadState();
            switch (ts) {
            case BLOCKED:
                sb.append("\t-  blocked on ");
                formatLock(sb, info.getLockInfo());
                sb.append('\n');
                break;
            case WAITING:
                sb.append("\t-  waiting on ");
                formatLock(sb, info.getLockInfo());
                sb.append('\n');
                break;
            case TIMED_WAITING:
                sb.append("\t-  waiting on ");
                formatLock(sb, info.getLockInfo());
                sb.append('\n');
                break;
            default:
            }
        }

        for (final MonitorInfo mi : info.getLockedMonitors()) {
            if (mi.getLockedStackDepth() == i) {
                sb.append("\t-  locked ");
                formatLock(sb, mi);
                sb.append('\n');
            }
        }
        ++i;
    }

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

From source file:org.wso2.carbon.micro.integrator.security.MicroIntegratorSecurityUtils.java

/**
 * This method converts a given stacktrace array to a string
 *
 * @param arr The stack trace array//from www.  j  a v a  2s . c o  m
 * @return the string generated from the stack trace array
 */
public static String stackTraceToString(StackTraceElement[] arr) {
    StringBuilder sb = new StringBuilder();
    for (StackTraceElement element : arr) {
        sb.append("\t" + element.toString() + "\n");
    }
    return sb.toString();
}

From source file:co.marcin.novaguilds.util.LoggerUtils.java

private static void printStackTrace(StackTraceElement[] stackTraceElements) {
    for (StackTraceElement st : stackTraceElements) {
        error("   at " + st.toString(), false);
    }// w  w  w.  j  av  a  2 s .  c  o  m
}

From source file:com.qwazr.utils.ExceptionUtils.java

public static List<String> getStackTraces(Throwable throwable) {
    StackTraceElement[] stElements = throwable.getStackTrace();
    if (stElements == null)
        return null;
    List<String> stList = new ArrayList<String>(stElements.length);
    for (StackTraceElement stElement : stElements)
        stList.add(stElement.toString());
    return stList;
}

From source file:com.jaeksoft.searchlib.util.ExceptionUtils.java

public final static String getLocation(StackTraceElement[] stackTrace) {
    for (StackTraceElement element : stackTrace)
        if (element.getClassName().startsWith("com.jaeksoft"))
            return element.toString();
    return null;/*  www.j  a  v  a  2s  .co  m*/
}

From source file:Main.java

public static String takeDumpJSON(ThreadMXBean threadMXBean) throws IOException {
    ThreadInfo[] threadInfos = threadMXBean.dumpAllThreads(true, true);
    List<Map<String, Object>> threads = new ArrayList<>();

    for (ThreadInfo thread : threadInfos) {
        Map<String, Object> threadMap = new HashMap<>();
        threadMap.put("name", thread.getThreadName());
        threadMap.put("id", thread.getThreadId());
        threadMap.put("state", thread.getThreadState().name());
        List<String> stacktrace = new ArrayList<>();
        for (StackTraceElement element : thread.getStackTrace()) {
            stacktrace.add(element.toString());
        }//www  .  j a va  2 s  .co  m
        threadMap.put("stack", stacktrace);

        if (thread.getLockName() != null) {
            threadMap.put("lock_name", thread.getLockName());
        }
        if (thread.getLockOwnerId() != -1) {
            threadMap.put("lock_owner_id", thread.getLockOwnerId());
        }
        if (thread.getBlockedTime() > 0) {
            threadMap.put("blocked_time", thread.getBlockedTime());
        }
        if (thread.getBlockedCount() > 0) {
            threadMap.put("blocked_count", thread.getBlockedCount());
        }
        if (thread.getLockedMonitors().length > 0) {
            threadMap.put("locked_monitors", thread.getLockedMonitors());
        }
        if (thread.getLockedSynchronizers().length > 0) {
            threadMap.put("locked_synchronizers", thread.getLockedSynchronizers());
        }
        threads.add(threadMap);
    }
    ObjectMapper om = new ObjectMapper();
    ObjectNode json = om.createObjectNode();
    json.put("date", new Date().toString());
    json.putPOJO("threads", threads);

    long[] deadlockedThreads = threadMXBean.findDeadlockedThreads();
    long[] monitorDeadlockedThreads = threadMXBean.findMonitorDeadlockedThreads();
    if (deadlockedThreads != null && deadlockedThreads.length > 0) {
        json.putPOJO("deadlocked_thread_ids", deadlockedThreads);
    }
    if (monitorDeadlockedThreads != null && monitorDeadlockedThreads.length > 0) {
        json.putPOJO("monitor_deadlocked_thread_ids", monitorDeadlockedThreads);
    }
    om.enable(SerializationFeature.INDENT_OUTPUT);
    return om.writerWithDefaultPrettyPrinter().writeValueAsString(json);
}

From source file:org.apache.tajo.master.querymaster.QueryMasterRunner.java

public static void printThreadInfo(PrintWriter stream, String title) {
    ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
    final int STACK_DEPTH = 60;
    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;
        }//  w  w  w.  j  a  v a  2s.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:com.bstek.dorado.view.resolver.PageOutputUtils.java

public static void outputException(Writer writer, Throwable throwable) throws IOException {
    writer.append("<h1 style=\"font-size:12pt; color:red\">");
    OutputUtils.outputString(writer,/*  w w w .  j  ava2 s .  c o  m*/
            StringUtils.defaultString(throwable.getMessage(), throwable.getClass().getName()));
    writer.append("</h1>\n");
    writer.append("<ul>\n");
    StackTraceElement[] stes = throwable.getStackTrace();
    for (int i = 0; i < stes.length; i++) {
        StackTraceElement ste = stes[i];
        writer.append("<li>").append("at ");
        OutputUtils.outputString(writer, ste.toString());
        writer.append("</li>\n");
    }
    writer.append("</ul>\n");
}

From source file:Main.java

private static String threadInfoToString(ThreadInfo threadInfo) {
    StringBuilder sb = new StringBuilder("\"" + threadInfo.getThreadName() + "\"" + " Id="
            + threadInfo.getThreadId() + " " + threadInfo.getThreadState());
    if (threadInfo.getLockName() != null) {
        sb.append(" on " + threadInfo.getLockName());
    }/*from w w w .  j  a  v a 2s .  c o m*/
    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 i = 0;
    for (; i < threadInfo.getStackTrace().length; i++) {
        StackTraceElement ste = threadInfo.getStackTrace()[i];
        sb.append("\tat " + ste.toString());
        sb.append('\n');
        if (i == 0 && threadInfo.getLockInfo() != null) {
            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() == i) {
                sb.append("\t-  locked " + mi);
                sb.append('\n');
            }
        }
    }

    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:org.structnetalign.CLI.java

/**
 * @see #printError(Exception)/*from  www .ja va  2s . c  o  m*/
 */
private static String printError(Exception e, String tabs) {
    StringBuilder sb = new StringBuilder();
    Throwable prime = e;
    while (prime != null) {
        if (tabs.length() > 0)
            sb.append(tabs + "Cause:" + NEWLINE);
        sb.append(tabs + prime.getClass().getSimpleName() + NEWLINE);
        if (prime.getMessage() != null)
            sb.append(tabs + prime.getMessage() + NEWLINE);
        if (prime instanceof Exception) {
            StackTraceElement[] trace = ((Exception) prime).getStackTrace();
            for (StackTraceElement element : trace) {
                sb.append(tabs + element.toString() + NEWLINE);
            }
        }
        prime = prime.getCause();
        tabs += "\t";
        sb.append(NEWLINE);
    }
    return sb.toString();
}