diff options
Diffstat (limited to 'third_party/sl4a/src/main/java/com/google/android/mobly/snippet/util/Log.java')
-rw-r--r-- | third_party/sl4a/src/main/java/com/google/android/mobly/snippet/util/Log.java | 32 |
1 files changed, 28 insertions, 4 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 c68631a..b5b1d9b 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 @@ -25,6 +25,9 @@ import android.os.Bundle; public final class Log { public static volatile String APK_LOG_TAG = null; + private static final String MY_CLASS_NAME = Log.class.getName(); + private static final String ANDROID_LOG_CLASS_NAME = android.util.Log.class.getName(); + private Log() {} public static synchronized void initLogTag(Context context) { @@ -56,10 +59,31 @@ public final class Log { throw new IllegalStateException("Logging called before initLogTag()"); } StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace(); - String fullClassName = stackTraceElements[4].getClassName(); - String className = fullClassName.substring(fullClassName.lastIndexOf(".") + 1); - int lineNumber = stackTraceElements[4].getLineNumber(); - return logTag + "." + className + ":" + lineNumber; + + boolean isCallerClassNameFound = false; + String fullClassName = null; + int lineNumber = 0; + // Walk up the stack and look for the first class name that is neither us nor + // 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) { + fullClassName = element.getClassName(); + if (!fullClassName.equals(MY_CLASS_NAME) && + !fullClassName.equals(ANDROID_LOG_CLASS_NAME)) { + lineNumber = element.getLineNumber(); + isCallerClassNameFound = true; + break; + } + } + + if (!isCallerClassNameFound) { + // Failed to determine caller's class name, fall back the the minimal one. + return logTag; + } else { + String className = fullClassName.substring(fullClassName.lastIndexOf(".") + 1); + return logTag + "." + className + ":" + lineNumber; + } } public static void v(String message) { |