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:com.oncore.calorders.core.utils.FormatHelper.java

/**
 * This method returns the stack trace of an exception
 *
 * @param ex The exception/*  w  ww . j a  v  a 2s.  c om*/
 * @return The stack trace in string
 */
public static String getStackTrace(Exception ex) {
    StringBuilder trace = new StringBuilder();
    trace.append(ex.getClass().getCanonicalName());
    trace.append("\n\t");

    trace.append(ex.getMessage());
    trace.append("\n");

    for (StackTraceElement stackTrace : ex.getStackTrace()) {
        trace.append(stackTrace.toString());
        trace.append("\n\t");
    }

    return trace.toString();
}

From source file:com.oncore.calorders.core.utils.FormatHelper.java

/**
 * This method returns the stack trace of an exception
 *
 * @param throwable The exception//from  w w w  . j a v a2  s . co m
 * @return The stack trace in string
 */
public static String getStackTrace(Throwable throwable) {
    StringBuilder trace = new StringBuilder();
    trace.append(throwable.getClass().getCanonicalName());
    trace.append("\n\t");

    trace.append(throwable.getMessage());
    trace.append("\n");

    for (StackTraceElement stackTrace : throwable.getStackTrace()) {
        trace.append(stackTrace.toString());
        trace.append("\n\t");
    }

    return trace.toString();
}

From source file:cascading.util.Util.java

public static String captureDebugTrace(Class type) {
    StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();

    for (int i = 3; i < stackTrace.length; i++) {
        StackTraceElement stackTraceElement = stackTrace[i];

        Package aPackage = type.getPackage();

        if (aPackage != null && stackTraceElement.getClassName().startsWith(aPackage.getName()))
            continue;

        return stackTraceElement.toString();
    }/*from w  ww.ja va 2 s.c om*/

    return null;
}

From source file:uk.co.armedpineapple.cth.SDLActivity.java

public static boolean initEGL(int majorVersion, int minorVersion) {
    if (SDLActivity.mEGLDisplay == null) {
        Log.v(SDLActivity.class.getSimpleName(), "Starting up OpenGL ES " + majorVersion + "." + minorVersion);

        try {//from   w  w w  .  j ava 2  s .co m
            EGL10 egl = (EGL10) EGLContext.getEGL();

            EGLDisplay dpy = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);

            int[] version = new int[2];
            egl.eglInitialize(dpy, version);

            int EGL_OPENGL_ES_BIT = 1;
            int EGL_OPENGL_ES2_BIT = 4;
            int renderableType = 0;
            if (majorVersion == 2) {
                renderableType = EGL_OPENGL_ES2_BIT;
            } else if (majorVersion == 1) {
                renderableType = EGL_OPENGL_ES_BIT;
            }

            int[] configSpec = {
                    // EGL10.EGL_DEPTH_SIZE, 16,
                    EGL10.EGL_RENDERABLE_TYPE, renderableType, EGL10.EGL_NONE };
            EGLConfig[] configs = new EGLConfig[1];
            int[] num_config = new int[1];
            if (!egl.eglChooseConfig(dpy, configSpec, configs, 1, num_config) || num_config[0] == 0) {
                Log.e(SDLActivity.class.getSimpleName(), "No EGL config available");
                return false;
            }
            EGLConfig config = configs[0];

            SDLActivity.mEGLDisplay = dpy;
            SDLActivity.mEGLConfig = config;
            SDLActivity.mGLMajor = majorVersion;
            SDLActivity.mGLMinor = minorVersion;

            SDLActivity.createEGLSurface();
        } catch (Exception e) {
            Log.v("SDL", e + "");
            for (StackTraceElement s : e.getStackTrace()) {
                Log.v("SDL", s.toString());
            }
        }
    } else
        SDLActivity.createEGLSurface();

    return true;
}

From source file:org.smartfrog.test.SmartFrogTestManager.java

/**
 * extract as much info as we can from a throwable.
 *
 * @param thrown what was thrown/*w  w w  .j a v  a  2s.  co m*/
 *
 * @return a string describing the throwable; includes a stack trace
 */
public static String extractDiagnosticsInfo(Throwable thrown) {
    StringBuilder buffer = new StringBuilder();
    thrown.getStackTrace();
    buffer.append("Message:  ");
    buffer.append(thrown.toString());
    buffer.append('\n');
    buffer.append("Class:    ");
    buffer.append(thrown.getClass().getName());
    buffer.append('\n');
    buffer.append("Stack:    ");
    StackTraceElement[] stackTrace = thrown.getStackTrace();
    for (StackTraceElement frame : stackTrace) {
        buffer.append(frame.toString());
        buffer.append('\n');
    }
    return buffer.toString();
}

From source file:com.kth.common.utils.etc.LogUtil.java

/**
 * Stack Trace  ?.//w  ww. java  2  s. c om
 * 
 * @param clazz  ??  Class.
 * @param level  .
 * @param msg .
 */
public static void stackTrace(final Class<?> clazz, final int level, final String msg) {
    if (Log.isLoggable(TAG, level)) {
        Thread th = Thread.currentThread();
        StackTraceElement[] stack = th.getStackTrace();

        StringBuilder sb = new StringBuilder();
        if (msg != null && !"".equals(msg)) {
            sb.append(msg).append("\n");
        }
        for (StackTraceElement element : stack) {
            if (!"getStackTrace".equals(element.getMethodName())
                    && !"stackTrace".equals(element.getMethodName())) {
                sb.append("\tat ").append(element.toString()).append("\n");
            }
        }
        Log.println(level, TAG, LogUtil.getClassLineNumber(clazz) + " - " + sb.toString());
    }
}

From source file:opensource.zeocompanion.ZeoCompanionApplication.java

public static void postToErrorLog(String method, Throwable theException, String extra, String threadName,
        boolean noAlert) {
    String eMsg = "Exception";
    if (method != null) {
        if (!method.isEmpty()) {
            eMsg = eMsg + " in " + method;
        }/*from   w ww . j  av  a2 s  .c  o  m*/
    }
    if (threadName != null) {
        if (!threadName.isEmpty()) {
            eMsg = eMsg + " in Thread " + threadName;
        }
    }
    if (extra != null) {
        if (!extra.isEmpty()) {
            eMsg = eMsg + " (" + extra + ")";
        }
    }
    eMsg = eMsg + ": " + theException.toString();
    Log.e(_CTAG + ".postToErrorLog", eMsg);
    theException.printStackTrace();

    int r = checkExternalStorage();
    if (r != 0) {
        Log.e(_CTAG + ".postToErrorLog", "Cannot write to external storage code " + r);
        return;
    }

    FileWriter wrt = null;
    try {
        // pull the stack trace
        StackTraceElement[] traces = theException.getStackTrace();

        // ensure the directory structure is present and compose the file name
        File internalsDir = new File(mBaseExtStorageDir + File.separator + "internals");
        internalsDir.mkdirs();
        File errLogFile = new File(internalsDir + File.separator + "error.log");

        // create and append to the file
        wrt = new FileWriter(errLogFile, true); // append if file already exists
        wrt.write(new Date().toString() + "\n");
        if (threadName != null) {
            if (!threadName.isEmpty()) {
                wrt.write(" in Thread " + threadName + " ");
            }
        }
        wrt.write("AppVerName " + BuildConfig.VERSION_NAME + " AppVerCode " + BuildConfig.VERSION_CODE);
        if (mDatabaseHandler != null) {
            wrt.write(" with DBver " + mDatabaseHandler.mVersion);
        }
        wrt.write("\n");
        if (mZeoAppHandler != null) {
            if (mZeoAppHandler.mZeoApp_versionName == null) {
                wrt.write("Zeo App not installed\n");
            } else {
                wrt.write("Zeo App version " + mZeoAppHandler.mZeoApp_versionName + " build "
                        + mZeoAppHandler.mZeoApp_versionCode + "\n");
            }
        }
        wrt.write("Android Version " + android.os.Build.VERSION.RELEASE + " API "
                + android.os.Build.VERSION.SDK_INT + "\n");
        wrt.write("Platform Manf " + Build.MANUFACTURER + " Model " + Build.MODEL + "\n");
        WindowManager windowManager = (WindowManager) mApp.getSystemService(Context.WINDOW_SERVICE);
        Display display = windowManager.getDefaultDisplay();
        Point screenSize = new Point();
        display.getSize(screenSize);
        wrt.write("Platform Screen Orientation X,Y " + screenSize.x + "," + screenSize.y + ", Density="
                + mScreenDensity + "\n");
        if (method != null) {
            if (!method.isEmpty()) {
                wrt.write(method + "\n");
            }
        }
        if (extra != null) {
            if (!extra.isEmpty()) {
                wrt.write(extra + "\n");
            }
        }
        wrt.write(theException.toString() + "\n");
        for (StackTraceElement st : traces) {
            wrt.write(st.toString() + "\n");
        }
        wrt.write("=====\n");
        wrt.write("=====\n");
        wrt.flush();
        wrt.close();

        // force it to be shown and post an alert
        forceShowOnPC(errLogFile);
        if (!noAlert) {
            postAlert("An abort occured; details are in \'internals/error.log\'; contact the Developer");
        } // noAlert is only needed when an Alert itself was being posted to the database and it failed to post

        // must send the toast indirectly in case this is being called from a utility thread
        Message msg = new Message();
        msg.what = ZeoCompanionApplication.MESSAGE_APP_SEND_TOAST;
        msg.obj = "Abort successfully logged to \'internals/error.log\'";
        mAppHandler.sendMessage(msg);
    } catch (Exception e) {
        if (wrt != null) {
            try {
                wrt.close();
            } catch (Exception ignored) {
            }
        }
        Log.e(_CTAG + ".postToErrLog", "Cannot write to error.log: " + e.toString());
        e.printStackTrace();
    }
}

From source file:Main.java

public Main() {
    setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    setSize(400, 300);/*from   w w w.jav  a 2s  . c  o  m*/

    Exception ex = new Exception();

    AttributeSet attr = null;
    StyledDocument doc = text.getStyledDocument();

    for (StackTraceElement trace : ex.getStackTrace()) {
        try {
            doc.insertString(doc.getLength(), trace.toString() + '\n', attr);
        } catch (BadLocationException ex1) {
            ex1.printStackTrace();
        }
    }
    getContentPane().add(new JScrollPane(text));
}

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

/**
 * Get stack trace for a given thread./*from   w  w w .  ja  v  a  2 s .  c  o  m*/
 */
public static String getStackTrace(Thread t) {
    final StackTraceElement[] stackTrace = t.getStackTrace();
    StringBuilder str = new StringBuilder();
    for (StackTraceElement e : stackTrace) {
        str.append(e.toString() + "\n");
    }
    return str.toString();
}

From source file:voltkvqa.AsyncBenchmark.java

/**
 * Fake an internal jstack to the log/*from w  ww .jav a  2 s  . com*/
 */
static public void printJStack() {
    prt(new Date().toString() + " Full thread dump");

    Map<String, List<String>> deduped = new HashMap<String, List<String>>();

    // collect all the output, but dedup the identical stack traces
    for (Entry<Thread, StackTraceElement[]> e : Thread.getAllStackTraces().entrySet()) {
        Thread t = e.getKey();
        String header = String.format("\"%s\" %sprio=%d tid=%d %s", t.getName(), t.isDaemon() ? "daemon " : "",
                t.getPriority(), t.getId(), t.getState().toString());

        String stack = "";
        for (StackTraceElement ste : e.getValue()) {
            stack += "    at " + ste.toString() + "\n";
        }

        if (deduped.containsKey(stack)) {
            deduped.get(stack).add(header);
        } else {
            ArrayList<String> headers = new ArrayList<String>();
            headers.add(header);
            deduped.put(stack, headers);
        }
    }

    for (Entry<String, List<String>> e : deduped.entrySet()) {
        String logline = "";
        for (String header : e.getValue()) {
            logline += header + "\n";
        }
        logline += e.getKey();
        prt(logline);
    }
}