From 05a8f4b1c14c172ce1d9edf4c6bcd091db0c41df Mon Sep 17 00:00:00 2001 From: Dariusz Seweryn Date: Sun, 25 Sep 2022 10:58:53 +0200 Subject: [PATCH 1/2] Fixed tag message generation with Proguard use - attempt #0 --- .../rxandroidble2/internal/RxBleLog.java | 30 +++++++++++++++---- 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/rxandroidble/src/main/java/com/polidea/rxandroidble2/internal/RxBleLog.java b/rxandroidble/src/main/java/com/polidea/rxandroidble2/internal/RxBleLog.java index 081605c32..6f5fd342d 100644 --- a/rxandroidble/src/main/java/com/polidea/rxandroidble2/internal/RxBleLog.java +++ b/rxandroidble/src/main/java/com/polidea/rxandroidble2/internal/RxBleLog.java @@ -2,14 +2,20 @@ import androidx.annotation.IntDef; import androidx.annotation.Nullable; + import android.util.Log; import com.polidea.rxandroidble2.LogConstants; import com.polidea.rxandroidble2.LogOptions; import com.polidea.rxandroidble2.internal.logger.LoggerSetup; +import com.polidea.rxandroidble2.internal.logger.LoggerUtil; +import com.polidea.rxandroidble2.internal.logger.LoggerUtilBluetoothServices; + import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; +import java.util.Arrays; +import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -142,12 +148,22 @@ private static String createTag() { return tag; } - StackTraceElement[] stackTrace = new Throwable().getStackTrace(); - if (stackTrace.length < 5) { + List ignoreClasses = Arrays.asList( + RxBleLog.class.getName(), + LoggerUtil.class.getName(), + LoggerUtilBluetoothServices.class.getName()); + + Throwable throwable = new Throwable(); + StackTraceElement[] stackTrace = throwable.getStackTrace(); + int i = 0; + while (i < stackTrace.length && ignoreClasses.contains(stackTrace[i].getClassName())) { + i++; + } + if (stackTrace.length <= i) { throw new IllegalStateException( - "Synthetic stacktrace didn't have enough elements: are you using proguard?"); + "Synthetic stacktrace didn't have enough elements: are you using proguard?", throwable); } - tag = stackTrace[4].getClassName(); + tag = stackTrace[i].getClassName(); Matcher m = ANONYMOUS_CLASS.matcher(tag); if (m.find()) { tag = m.replaceAll(""); @@ -249,11 +265,13 @@ public static boolean isAtLeast(int expectedLogLevel) { return loggerSetup.logLevel <= expectedLogLevel; } - public static @LogConstants.MacAddressLogSetting int getMacAddressLogSetting() { + public static @LogConstants.MacAddressLogSetting + int getMacAddressLogSetting() { return loggerSetup.macAddressLogSetting; } - public static @LogConstants.UuidLogSetting int getUuidLogSetting() { + public static @LogConstants.UuidLogSetting + int getUuidLogSetting() { return loggerSetup.uuidLogSetting; } From deab0f11a9097831875642a5b192e33fdda24bcd Mon Sep 17 00:00:00 2001 From: Dariusz Seweryn Date: Thu, 24 Nov 2022 22:04:10 +0100 Subject: [PATCH 2/2] Minor code rearrangement --- .../java/com/polidea/rxandroidble2/internal/RxBleLog.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/rxandroidble/src/main/java/com/polidea/rxandroidble2/internal/RxBleLog.java b/rxandroidble/src/main/java/com/polidea/rxandroidble2/internal/RxBleLog.java index 6f5fd342d..0f46b1e50 100644 --- a/rxandroidble/src/main/java/com/polidea/rxandroidble2/internal/RxBleLog.java +++ b/rxandroidble/src/main/java/com/polidea/rxandroidble2/internal/RxBleLog.java @@ -265,13 +265,13 @@ public static boolean isAtLeast(int expectedLogLevel) { return loggerSetup.logLevel <= expectedLogLevel; } - public static @LogConstants.MacAddressLogSetting - int getMacAddressLogSetting() { + @LogConstants.MacAddressLogSetting + public static int getMacAddressLogSetting() { return loggerSetup.macAddressLogSetting; } - public static @LogConstants.UuidLogSetting - int getUuidLogSetting() { + @LogConstants.UuidLogSetting + public static int getUuidLogSetting() { return loggerSetup.uuidLogSetting; }