Example usage for java.lang StackTraceElement getLineNumber

List of usage examples for java.lang StackTraceElement getLineNumber

Introduction

In this page you can find the example usage for java.lang StackTraceElement getLineNumber.

Prototype

public int getLineNumber() 

Source Link

Document

Returns the line number of the source line containing the execution point represented by this stack trace element.

Usage

From source file:Main.java

private static void print(StackTraceElement element, Object message, Throwable error) {
    String className = element.getClassName();
    className = className.substring(className.lastIndexOf(".") + 1);
    String tag = className + '.' + element.getMethodName() + '(' + element.getFileName() + ':'
            + element.getLineNumber() + ')';
    String text = toString(message);

    if (error != null) {
        Log.e("[KakaCache]", tag + "\n\t" + text, error);
    } else {//from  w w  w .j a  v  a  2 s.co  m
        Log.e("[KakaCache]", tag + "\n\t" + text);
    }
}

From source file:jp.techie.achicoco.framework.util.LogUtil.java

/**
 * ??// w w  w  . ja va 2s.com
 * 
 * @param className ??
 * @return 
 */
protected static int getLineNumber(String className) {
    try {
        throw new Exception();
    } catch (Exception e) {
        StackTraceElement[] stackTraceElements = e.getStackTrace();
        for (int i = 0; i < stackTraceElements.length; i++) {
            StackTraceElement element = stackTraceElements[i];
            if (element != null && className.equals(element.getClassName())) {
                return element.getLineNumber();
            }
        }
    }
    return 0;
}

From source file:Main.java

/**
 * Formats a stack trace into a single line that provides relevant information for debugging
 * @param element the element to format//from   ww  w .  j  ava 2s .c o  m
 * @return a well-formatted stack-trace line containing the class name, method name, and line number
 * that, when clicked in the logcat, will display the line or source from where the message originated.
 */
public static String formatStackTrace(StackTraceElement element) {
    StringBuilder b = new StringBuilder();

    b.append(" at ");
    String clazz = element.getClassName();
    b.append(clazz).append(".");
    b.append(element.getMethodName()).append("(");
    b.append(clazz.substring(clazz.lastIndexOf(".") + 1)).append(".java:");
    b.append(element.getLineNumber()).append(")").append(" , ##");
    return b.toString();
}

From source file:com.ikanow.aleph2.logging.utils.Log4JUtils.java

public static String getLog4JMessage(final JsonNode logObject, final Level level, final StackTraceElement stack,
        final String date_field, final Map<String, Object> map, final String hostname) {
    StringBuilder sb = new StringBuilder();
    final String c = stack.getClassName().substring(stack.getClassName().lastIndexOf(".") + 1);
    final String l = Integer.toString(stack.getLineNumber());
    final String thread = Thread.currentThread().getName().isEmpty()
            ? Long.toString(Thread.currentThread().getId())
            : Thread.currentThread().getName();
    sb.append(String.format(message_format, date_format.format(new Date(logObject.get(date_field).asLong())),
            thread, level.name(), c, l, logObject.get("message").asText()));
    sb.append(String.format(field_format, "bucket", logObject.get("bucket").asText()));
    sb.append(String.format(field_format, "hostname", hostname));
    sb.append(String.format(field_format, "subsystem", logObject.get("subsystem").asText()));
    sb.append(String.format(field_format, "command", logObject.get("command").asText()));
    Optional.ofNullable(map).orElse(Collections.emptyMap()).entrySet().stream()
            .forEach(e -> sb.append(String.format(field_format, e.getKey(), e.getValue())));
    return sb.toString();
}

From source file:org.apache.samza.logging.log4j2.serializers.LoggingEventJsonSerde.java

/**
 * Encodes a LoggingEvent into a HashMap using the logstash JSON format.
 *
 * @param loggingEvent/*from w ww  . jav  a  2  s .co m*/
 *          The LoggingEvent to encode.
 * @param includeLocationInfo
 *          Whether to include LocationInfo in the map, or not.
 * @return A Map representing the LoggingEvent, which is suitable to be
 *         serialized by a JSON encoder such as Jackson.
 */
@SuppressWarnings("rawtypes")
public static Map<String, Object> encodeToMap(LogEvent loggingEvent, boolean includeLocationInfo) {
    Map<String, Object> logstashEvent = new LoggingEventJsonSerde.LoggingEventMap();
    String threadName = loggingEvent.getThreadName();
    long timestamp = loggingEvent.getTimeMillis();
    HashMap<String, Object> exceptionInformation = new HashMap<String, Object>();
    Map mdc = loggingEvent.getContextData().toMap();
    ThreadContext.ContextStack ndc = loggingEvent.getContextStack();

    logstashEvent.put("@version", VERSION);
    logstashEvent.put("@timestamp", dateFormat(timestamp));
    logstashEvent.put("source_host", getHostname());
    logstashEvent.put("message", loggingEvent.getMessage());

    if (loggingEvent.getThrown() != null) {
        final Throwable throwableInformation = loggingEvent.getThrown();
        if (throwableInformation.getClass().getCanonicalName() != null) {
            exceptionInformation.put("exception_class", throwableInformation.getClass().getCanonicalName());
        }
        if (throwableInformation.getMessage() != null) {
            exceptionInformation.put("exception_message", throwableInformation.getMessage());
        }
        if (throwableInformation.getMessage() != null) {
            StringBuilder stackTrace = new StringBuilder(ExceptionUtils.getStackTrace(throwableInformation));
            exceptionInformation.put("stacktrace", stackTrace);
        }
        logstashEvent.put("exception", exceptionInformation);
    }

    if (includeLocationInfo) {
        StackTraceElement info = loggingEvent.getSource();
        logstashEvent.put("file", info.getFileName());
        logstashEvent.put("line_number", info.getLineNumber());
        logstashEvent.put("class", info.getClassName());
        logstashEvent.put("method", info.getMethodName());
    }

    logstashEvent.put("logger_name", loggingEvent.getLoggerName());
    logstashEvent.put("mdc", mdc);
    logstashEvent.put("ndc", ndc);
    logstashEvent.put("level", loggingEvent.getLevel().toString());
    logstashEvent.put("thread_name", threadName);

    return logstashEvent;
}

From source file:com.hellofyc.base.net.http.HttpUtils.java

private static void getInvokeStackTraceElement() {
    FLog.i("Thread ID: " + Thread.currentThread().getId() + " getInvokeStackTraceElement");
    StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
    for (StackTraceElement element : stackTraceElements) {
        FLog.i("(" + element.getLineNumber() + ") " + "class name:" + element.getClassName() + ", method name:"
                + element.getMethodName());
    }/*from w ww . j a  v  a 2  s  .  c o  m*/
}

From source file:org.kepler.sms.SemanticType.java

private static boolean isGoodTrace(StackTraceElement[] stackTrace) {
    for (StackTraceElement ste : stackTrace) {
        if ("org.kepler.sms.SemanticType".equals(ste.getClassName()) && ste.getLineNumber() == 75) {
            return false;
        }// w  w w  .j  a v a 2s  . c  o m
    }
    return true;
}

From source file:com.dmitrymalkovich.android.githubanalytics.Utils.java

public static void openFeedback(Activity activity) {
    try {//  ww w  . j  a va 2  s  . co  m
        throw new IOException();
    } catch (IOException e) {
        ApplicationErrorReport report = new ApplicationErrorReport();
        report.packageName = report.processName = activity.getApplication().getPackageName();
        report.time = System.currentTimeMillis();
        report.type = ApplicationErrorReport.TYPE_CRASH;
        report.systemApp = false;
        ApplicationErrorReport.CrashInfo crash = new ApplicationErrorReport.CrashInfo();
        crash.exceptionClassName = e.getClass().getSimpleName();
        crash.exceptionMessage = e.getMessage();
        StringWriter writer = new StringWriter();
        PrintWriter printer = new PrintWriter(writer);
        e.printStackTrace(printer);
        crash.stackTrace = writer.toString();
        StackTraceElement stack = e.getStackTrace()[0];
        crash.throwClassName = stack.getClassName();
        crash.throwFileName = stack.getFileName();
        crash.throwLineNumber = stack.getLineNumber();
        crash.throwMethodName = stack.getMethodName();
        report.crashInfo = crash;
        Intent intent = new Intent(Intent.ACTION_APP_ERROR);
        intent.putExtra(Intent.EXTRA_BUG_REPORT, report);
        activity.startActivity(intent);
    }
}

From source file:com.codelanx.codelanxlib.listener.ListenerManager.java

/**
 * Returns {@code true} if the passed {@link Listener} has another Listener
 * of the same class type already registered for bukkit. This should not be
 * used with any listeners that are from an anonymous class, as this will
 * return {@code true} for any other anonymous classes as well
 * //from  w w w.j  a  v a 2  s  .c  o  m
 * @since 0.1.0
 * @version 0.1.0
 * 
 * @param p The {@link Plugin} that registers this {@link Listener}
 * @param l The {@link Listener} to check
 * @return {@code true} if registered to bukkit
 */
public static boolean isRegisteredToBukkit(Plugin p, Listener l) {
    if (l.getClass().isAnonymousClass()) {
        StackTraceElement t = Reflections.getCaller();
        Logging.simple().here().print(Level.WARNING, "Passed an anonymous class from %s:%d",
                t.getClass().getName(), t.getLineNumber());
    }
    return HandlerList.getRegisteredListeners(p).stream()
            .anyMatch(r -> r.getListener().getClass() == l.getClass());
}

From source file:com.aotome202.lostjason.e202project.apputils.MLog.java

public static int printMethodName(String TAG) {
    if (!LOG_OUT) {
        return -1;
    }//w w w  . java2s .co  m
    String msg = "";
    StackTraceElement info = LogInfo.getInfoInternal(LENGTH);
    if (info != null) {
        msg = info.getMethodName() + " # Line " + info.getLineNumber();
    }

    return Log.i(TAG, msg);

}