diff options
author | Jinghao Shi <jhshi@users.noreply.github.com> | 2017-09-05 13:36:53 -0700 |
---|---|---|
committer | Ang Li <angli@google.com> | 2017-09-05 13:36:53 -0700 |
commit | 94d17997b7468ce1b6947fdc25e8992937ac4884 (patch) | |
tree | b3caaab9efb661e32e02374393c9e42eb8c73ee9 /third_party/sl4a/src | |
parent | 100d1edd8f81a52c6d91e0979e07d144a89708d1 (diff) | |
download | mobly-snippet-lib-94d17997b7468ce1b6947fdc25e8992937ac4884.tar.gz |
Walk up the stack trace from the correct index (#76)
Diffstat (limited to 'third_party/sl4a/src')
-rw-r--r-- | third_party/sl4a/src/main/java/com/google/android/mobly/snippet/util/Log.java | 12 |
1 files changed, 11 insertions, 1 deletions
diff --git a/third_party/sl4a/src/main/java/com/google/android/mobly/snippet/util/Log.java b/third_party/sl4a/src/main/java/com/google/android/mobly/snippet/util/Log.java index fdb4454..81018f7 100644 --- a/third_party/sl4a/src/main/java/com/google/android/mobly/snippet/util/Log.java +++ b/third_party/sl4a/src/main/java/com/google/android/mobly/snippet/util/Log.java @@ -28,6 +28,15 @@ public final class Log { private static final String MY_CLASS_NAME = Log.class.getName(); private static final String ANDROID_LOG_CLASS_NAME = android.util.Log.class.getName(); + // Skip the first two entries in stack trace when trying to infer the caller. + // The first two entries are: + // - dalvik.system.VMStack.getThreadStackTrace(Native Method) + // - java.lang.Thread.getStackTrace(Thread.java:580) + // The {@code getStackTrace()} function returns the stack trace at where the trace is collected + // (inisde the JNI function {@code getThreadStackTrace()} instead of at where the {@code + // getStackTrace()} is called (althrought this is the natual expectation). + private static final int STACK_TRACE_WALK_START_INDEX = 2; + private Log() {} public static synchronized void initLogTag(Context context) { @@ -67,7 +76,8 @@ public final class Log { // android.util.Log: that's the caller. // Do not used hard-coded stack depth: that does not work all the time because of proguard // inline optimization. - for (StackTraceElement element : stackTraceElements) { + for (int i = STACK_TRACE_WALK_START_INDEX; i < stackTraceElements.length; i++) { + StackTraceElement element = stackTraceElements[i]; fullClassName = element.getClassName(); if (!fullClassName.equals(MY_CLASS_NAME) && !fullClassName.equals(ANDROID_LOG_CLASS_NAME)) { |