Example usage for java.lang Throwable getStackTrace

List of usage examples for java.lang Throwable getStackTrace

Introduction

In this page you can find the example usage for java.lang Throwable getStackTrace.

Prototype

public StackTraceElement[] getStackTrace() 

Source Link

Document

Provides programmatic access to the stack trace information printed by #printStackTrace() .

Usage

From source file:jetbrains.exodus.entitystore.Explainer.java

public static String stripStackTrace(Throwable throwable) {
    StackTraceElement[] stackTrace = throwable.getStackTrace();
    int i = 0;/* w  w w.  jav a2s. co m*/
    while (i < stackTrace.length && !stackTrace[i].getClassName().startsWith(PACKAGE_TO_SKIP_IN_STACKTRACE)) {
        i++;
    }
    while (i < stackTrace.length && stackTrace[i].getClassName().startsWith(PACKAGE_TO_SKIP_IN_STACKTRACE)) {
        i++;
    }
    if (i >= stackTrace.length) {
        i = 0;
    }
    return stackTrace[i].toString();
}

From source file:FileUtil.java

/**
 *  Returns the class and method name (+a line number) in which the
 *  Throwable was thrown.//from   w  ww  .j a  v a2  s .co m
 *
 *  @param t A Throwable to analyze.
 *  @return A human-readable string stating the class and method.  Do not rely
 *          the format to be anything fixed.
 */
public static String getThrowingMethod(Throwable t) {
    StackTraceElement[] trace = t.getStackTrace();
    StringBuffer sb = new StringBuffer();

    if (trace == null || trace.length == 0) {
        sb.append("[Stack trace not available]");
    } else {
        sb.append(trace[0].isNativeMethod() ? "native method" : "");
        sb.append(trace[0].getClassName());
        sb.append(".");
        sb.append(trace[0].getMethodName() + "(), line " + trace[0].getLineNumber());
    }
    return sb.toString();
}

From source file:Main.java

public static void getStackTrace(StringBuffer sb, Throwable t, int max) {
    if (t == null)
        return;// w ww .  j ava  2  s .c om
    if (max <= 0) {
        max = 10000;
    }
    String CRLF = System.getProperty("line.separator");
    sb.append(t);
    StackTraceElement[] se = t.getStackTrace();
    if (se != null && se.length > 0) {
        for (int i = 0; i < se.length && i < max; i++) {
            sb.append(CRLF);
            sb.append("\t" + se[i]);
        }
        if (max < se.length) {
            sb.append(CRLF + "\t...more lines " + (se.length - max));
        }
    } else {
        sb.append(CRLF + "\tno stack info ");
    }
}

From source file:io.stallion.services.Log.java

public static void warning(String message, Object... args) {
    if (getLogLevel().intValue() > Level.WARNING.intValue()) {
        return;// w  w  w .  java2  s . c  o  m
    }
    Throwable t = new Throwable();
    StackTraceElement stackTraceElement = t.getStackTrace()[1];
    String clz = stackTraceElement.getClassName().replace("io.stallion.", "");
    String method = stackTraceElement.getMethodName() + ":" + t.getStackTrace()[1].getLineNumber();
    logger.logp(Level.WARNING, clz, method, message, args);

}

From source file:io.stallion.services.Log.java

public static void warn(String message, Object... args) {
    if (getLogLevel().intValue() > Level.WARNING.intValue()) {
        return;/* w  ww  . j a va  2 s .  co m*/
    }
    Throwable t = new Throwable();
    StackTraceElement stackTraceElement = t.getStackTrace()[1];
    String clz = stackTraceElement.getClassName().replace("io.stallion.", "");
    String method = stackTraceElement.getMethodName() + ":" + t.getStackTrace()[1].getLineNumber();
    logger.logp(Level.WARNING, clz, method, message, args);

}

From source file:io.stallion.services.Log.java

/**
 * Logs a message, setting the class, method and source line number using the stack frame index passed in.
 * This is useful if you are wrapping a logging class, and want to include the line number from where
 * the wrapper method is called.// w w w. j  a  v a 2  s  .  co m
 *
 * @param frame - the stack frame to use. Use '2' to log to one level above the caller
 * @param level
 * @param message
 * @param args
 */
public static void logForFrame(int frame, Level level, String message, Object... args) {
    if (getLogLevel().intValue() > level.intValue()) {
        return;
    }
    Throwable t = new Throwable();
    StackTraceElement stackTraceElement = t.getStackTrace()[frame];
    String clz = stackTraceElement.getClassName().replace("io.stallion.", "");
    String method = stackTraceElement.getMethodName() + ":" + t.getStackTrace()[frame].getLineNumber();
    logger.logp(level, clz, method, message, args);
}

From source file:io.stallion.services.Log.java

public static void warn(Throwable ex, String message, Object... args) {
    if (getLogLevel().intValue() > Level.WARNING.intValue()) {
        return;/*  www .  j  a  va 2s.c  om*/
    }
    if (args.length > 0) {
        message = MessageFormat.format(message, args);
    }
    Throwable t = new Throwable();
    StackTraceElement stackTraceElement = t.getStackTrace()[1];
    String clz = stackTraceElement.getClassName().replace("io.stallion.", "");
    String method = stackTraceElement.getMethodName() + ":" + t.getStackTrace()[1].getLineNumber();
    logger.logp(Level.WARNING, clz, method, message, ex);
}

From source file:Main.java

public static String toString(Throwable error) {
    StringBuffer buf = new StringBuffer(512);
    while (error != null) {
        buf.append(error.getClass().getName());
        buf.append(error.getMessage() == null ? "" : error.getMessage());
        buf.append("\r\n");
        StackTraceElement[] stack = error.getStackTrace();
        for (int i = 0; i < stack.length; i++) {
            buf.append("   ");
            buf.append(stack[i].toString());
            buf.append("\r\n");
        }//from   w  w  w  .ja v  a  2  s  .co m
        if (error.getCause() != null) {
            buf.append("\r\nCaused by :");
        }
        error = error.getCause();
    }
    return buf.toString();
}

From source file:io.stallion.services.Log.java

public static void exception(Throwable ex, String message, Object... args) {
    HealthTracker.instance().logException(ex);
    if (getLogLevel().intValue() > Level.SEVERE.intValue()) {
        return;/*w  w w  .j av a  2  s.  com*/
    }
    if (args.length > 0) {
        message = MessageFormat.format(message, args);
    }
    // TODO: WTF -- figure out how the hell the handler got removed;
    if (logger.getHandlers().length == 0) {
        logger.addHandler(handler);
    }
    Throwable t = new Throwable();
    StackTraceElement stackTraceElement = t.getStackTrace()[1];
    String clz = stackTraceElement.getClassName().replace("io.stallion.", "");
    String method = stackTraceElement.getMethodName() + ":" + t.getStackTrace()[1].getLineNumber();
    logger.logp(Level.SEVERE, clz, method, message, ex);
}

From source file:io.stallion.services.Log.java

public static void info(String message, Object... args) {
    if (getLogLevel().intValue() > Level.INFO.intValue()) {
        return;//www .j a va2s  .c o m
    }
    // Info statements don't include the class and line number, since that kills performance
    //System.out.println(message);
    if (alwaysIncludeLineNumber) {
        Throwable t = new Throwable();
        StackTraceElement stackTraceElement = t.getStackTrace()[1];
        String clz = stackTraceElement.getClassName().replace("io.stallion.", "");
        String method = stackTraceElement.getMethodName() + ":" + t.getStackTrace()[1].getLineNumber();
        logger.logp(Level.INFO, clz, method, message, args);
    } else {
        logger.logp(Level.INFO, "", "", message, args);
    }

}