Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed tag message generation with Proguard use #814

Merged
merged 2 commits into from
Nov 24, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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