Skip to content

Commit

Permalink
Fixed tag message generation with Proguard use (#814)
Browse files Browse the repository at this point in the history
  • Loading branch information
dariuszseweryn authored Nov 24, 2022
1 parent be3ee50 commit f3c4032
Showing 1 changed file with 24 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -142,12 +148,22 @@ private static String createTag() {
return tag;
}

StackTraceElement[] stackTrace = new Throwable().getStackTrace();
if (stackTrace.length < 5) {
List<String> 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("");
Expand Down Expand Up @@ -249,11 +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;
}

Expand Down

0 comments on commit f3c4032

Please sign in to comment.