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:com.momock.util.Logger.java

static String getLog(String level, String msg) {
    Throwable t = new Throwable();
    StackTraceElement trace = t.getStackTrace()[2];
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.ENGLISH);
    if (remoteLogger != null) {
        remoteLogger.log(level, msg, trace.getFileName() + "(" + trace.getLineNumber() + ")");
    }//from ww w. j a v  a 2s  .  c  om
    return "[" + level + "] " + sdf.format(new Date()) + " in " + trace.getFileName() + "("
            + trace.getLineNumber() + ") >" + msg;
}

From source file:com.momock.util.Logger.java

public static void debug(String msg) {
    if (!enabled || logLevel > LEVEL_DEBUG)
        return;//from  ww w .  ja va2 s  .  c om
    if (msg == null)
        msg = "";
    Throwable t = new Throwable();
    StackTraceElement trace = t.getStackTrace()[1];
    android.util.Log.d(logName, msg + " @ " + getSourceInfo(trace));
    checkLogFile();
    logStream.println(getLog("DEBUG", msg));
    logStream.flush();
}

From source file:com.momock.util.Logger.java

public static void info(String msg) {
    if (!enabled || logLevel > LEVEL_INFO)
        return;//from   w ww .  jav a2s .  c o m
    if (msg == null)
        msg = "";
    Throwable t = new Throwable();
    StackTraceElement trace = t.getStackTrace()[1];
    android.util.Log.i(logName, msg + " @ " + getSourceInfo(trace));
    checkLogFile();
    logStream.println(getLog("INFO", msg));
    logStream.flush();
}

From source file:com.momock.util.Logger.java

public static void warn(String msg) {
    if (!enabled || logLevel > LEVEL_WARN)
        return;//from   www  .  j av a  2  s .c o  m
    if (msg == null)
        msg = "";
    Throwable t = new Throwable();
    StackTraceElement trace = t.getStackTrace()[1];
    android.util.Log.w(logName, msg + " @ " + getSourceInfo(trace));
    checkLogFile();
    logStream.println(getLog("WARN", msg));
    logStream.flush();
}

From source file:com.momock.util.Logger.java

public static void error(Throwable e) {
    if (!enabled || logLevel > LEVEL_ERROR)
        return;/*  www.  j a  va2s  . co m*/
    String msg = e.getMessage() + " : " + getStackTrace(e);
    Throwable t = new Throwable();
    StackTraceElement trace = t.getStackTrace()[1];
    android.util.Log.e(logName, msg + " @ " + getSourceInfo(trace));
    checkLogFile();
    logStream.println(getLog("ERROR", msg));
    logStream.flush();
    event.fireEvent(null, new LogEventArgs(null, e));
}

From source file:com.momock.util.Logger.java

public static void error(String msg) {
    if (!enabled || logLevel > LEVEL_ERROR)
        return;/* www .jav  a 2  s  .  c  o m*/
    if (msg == null)
        msg = "";
    Throwable t = new Throwable();
    StackTraceElement trace = t.getStackTrace()[1];
    android.util.Log.e(logName, msg + " @ " + getSourceInfo(trace));
    checkLogFile();
    logStream.println(getLog("ERROR", msg));
    logStream.flush();
    event.fireEvent(null, new LogEventArgs(msg, null));
}

From source file:grails.util.GrailsUtil.java

public static void printSanitizedStackTrace(Throwable t, PrintWriter p, StackTraceFilterer stackTraceFilterer) {
    t = stackTraceFilterer.filter(t);//from  www  . j av a2 s.  c  o m

    StackTraceElement[] trace = t.getStackTrace();
    for (StackTraceElement stackTraceElement : trace) {
        p.println("at " + stackTraceElement.getClassName() + "(" + stackTraceElement.getMethodName() + ":"
                + stackTraceElement.getLineNumber() + ")");
    }
}

From source file:com.momock.util.Logger.java

public static void check(boolean condition, String msg) {
    if (!condition) {
        if (!enabled || logLevel > LEVEL_ERROR)
            return;
        if (msg == null)
            msg = "";
        Throwable t = new Throwable();
        StackTraceElement trace = t.getStackTrace()[1];
        android.util.Log.e(logName, msg + " @ " + getSourceInfo(trace));
        checkLogFile();/*from  w w  w. j  av  a  2s  .  c o m*/
        logStream.println(getLog("ASSERT", msg));
        logStream.flush();

        throw new RuntimeException(msg);
    }
}

From source file:org.rhq.enterprise.server.plugins.rhnhosted.xmlrpc.RhnCommTest.java

/**
 * method to find a file relative to the calling class.  primarily
 * useful when writing tests that need access to external data.
 * this lets you put the data relative to the test class file.
 *
 * @param path the path, relative to caller's location
 * @return URL a URL referencing the file
 * @throws ClassNotFoundException if the calling class can not be found
 * (i.e., should not happen)//from w  w  w  .ja v a  2s .  c o m
 */
public static URL findTestData(String path) throws ClassNotFoundException {
    Throwable t = new Throwable();
    StackTraceElement[] ste = t.getStackTrace();

    String className = ste[1].getClassName();
    Class clazz = Class.forName(className);

    URL ret = clazz.getResource(path);
    return ret;
}

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

public static IOException toThrift(Throwable t) {
    if (t == null) {
        return new IOException();
    }//from   w ww .j a v  a2s.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;
}