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

Added possibility to disable scanned peripherals logging #579

Merged
merged 1 commit into from
May 17, 2019
Merged
Show file tree
Hide file tree
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 @@ -19,14 +19,17 @@ public class LogOptions {
@Nullable
private Boolean shouldLogAttributeValues;
@Nullable
private Boolean shouldLogScannedPeripherals;
@Nullable
private Logger logger;

private LogOptions(@Nullable Integer logLevel, @Nullable Integer macAddressLogSetting, @Nullable Integer uuidLogSetting,
@Nullable Boolean shouldLogAttributeValues, @Nullable Logger logger) {
@Nullable Boolean shouldLogAttributeValues, @Nullable Boolean shouldLogScannedPeripherals, @Nullable Logger logger) {
this.logLevel = logLevel;
this.macAddressLogSetting = macAddressLogSetting;
this.uuidLogSetting = uuidLogSetting;
this.shouldLogAttributeValues = shouldLogAttributeValues;
this.shouldLogScannedPeripherals = shouldLogScannedPeripherals;
this.logger = logger;
}

Expand All @@ -50,6 +53,11 @@ public Boolean getShouldLogAttributeValues() {
return shouldLogAttributeValues;
}

@Nullable
public Boolean getShouldLogScannedPeripherals() {
return shouldLogScannedPeripherals;
}

@Nullable
public Logger getLogger() {
return logger;
Expand All @@ -62,14 +70,15 @@ public String toString() {
+ ", macAddressLogSetting=" + macAddressLogSetting
+ ", uuidLogSetting=" + uuidLogSetting
+ ", shouldLogAttributeValues=" + shouldLogAttributeValues
+ ", shouldLogScannedPeripherals=" + shouldLogScannedPeripherals
+ ", logger=" + logger
+ '}';
}

/**
* The builder for {@link #LogOptions(Integer, Integer, Integer, Boolean, Logger)}
* The builder for {@link #LogOptions(Integer, Integer, Integer, Boolean, Boolean, Logger)}
* If a particular setting will not be defined on the builder the produced
* {@link #LogOptions(Integer, Integer, Integer, Boolean, Logger)} will not overwrite them in the library when passed to
* {@link #LogOptions(Integer, Integer, Integer, Boolean, Boolean, Logger)} will not overwrite them in the library when passed to
* {@link com.polidea.rxandroidble2.RxBleClient#updateLogOptions(LogOptions)}.
*/
public static class Builder {
Expand All @@ -83,6 +92,8 @@ public static class Builder {
@Nullable
private Boolean shouldLogAttributeValues;
@Nullable
private Boolean shouldLogScannedPeripherals;
@Nullable
private Logger logger;

/**
Expand Down Expand Up @@ -142,6 +153,17 @@ public Builder setShouldLogAttributeValues(@Nullable Boolean shouldLogAttributeV
return this;
}

/**
* Set if scan results should be logged.
*
* @param shouldLogScannedPeripherals the setting
* @return the builder
*/
public Builder setShouldLogScannedPeripherals(@Nullable Boolean shouldLogScannedPeripherals) {
this.shouldLogScannedPeripherals = shouldLogScannedPeripherals;
return this;
}

/**
* Set the logger to get the output
*
Expand All @@ -154,7 +176,8 @@ public Builder setLogger(@Nullable Logger logger) {
}

public LogOptions build() {
return new LogOptions(logLevel, macAddressLogSetting, uuidsLogSetting, shouldLogAttributeValues, logger);
return new LogOptions(logLevel, macAddressLogSetting, uuidsLogSetting, shouldLogAttributeValues,
shouldLogScannedPeripherals, logger);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ public static void setLogLevel(@RxBleLog.LogLevel int logLevel) {
* Method for updating logging options. Properties not set in {@link LogOptions} will not get updated.
* <p>
* Default behaviour: MAC addresses are not logged (MAC='XX:XX:XX:XX:XX:XX'), uuids are not logged (uuid='...'), byte array values
* are not logged (value=[...]), logger is logging to the logcat ({@link android.util.Log}), log level is set to not logging
* anything ({@link LogConstants#NONE})
* are not logged (value=[...]), logger is logging to the logcat ({@link android.util.Log}), all scanned peripherals are logged if
* log level allows it, log level is set not to log anything ({@link LogConstants#NONE})
* </p>
*
* @param logOptions the logging options
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public Observable<ScanResult> call() {
.doOnNext(new Consumer<ScanResult>() {
@Override
public void accept(ScanResult scanResult) {
RxBleLog.i("%s", scanResult);
if (RxBleLog.getShouldLogScannedPeripherals()) RxBleLog.i("%s", scanResult);
}
})
.mergeWith(RxBleClientImpl.this.<ScanResult>bluetoothAdapterOffExceptionObservable());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public void log(final int level, final String tag, final String msg) {
LogConstants.NONE,
LogConstants.NONE,
false,
true,
logcatLogger
);

Expand Down Expand Up @@ -259,4 +260,8 @@ public static boolean isAtLeast(int expectedLogLevel) {
public static boolean getShouldLogAttributeValues() {
return loggerSetup.shouldLogAttributeValues;
}

public static boolean getShouldLogScannedPeripherals() {
return loggerSetup.shouldLogScannedPeripherals;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,22 @@ public class LoggerSetup {
@LogConstants.UuidLogSetting
public final int uuidLogSetting;
public final boolean shouldLogAttributeValues;
public final boolean shouldLogScannedPeripherals;
public final LogOptions.Logger logger;

public LoggerSetup(
int logLevel,
int macAddressLogSetting,
int uuidLogSetting,
boolean shouldLogAttributeValues,
boolean shouldLogScannedPeripherals,
LogOptions.Logger logger
) {
this.logLevel = logLevel;
this.macAddressLogSetting = macAddressLogSetting;
this.uuidLogSetting = uuidLogSetting;
this.shouldLogAttributeValues = shouldLogAttributeValues;
this.shouldLogScannedPeripherals = shouldLogScannedPeripherals;
this.logger = logger;
}

Expand All @@ -35,8 +38,11 @@ public LoggerSetup merge(LogOptions logOptions) {
int uuidLogSetting = logOptions.getUuidLogSetting() != null ? logOptions.getUuidLogSetting() : this.uuidLogSetting;
boolean shouldLogAttributeValues =
logOptions.getShouldLogAttributeValues() != null ? logOptions.getShouldLogAttributeValues() : this.shouldLogAttributeValues;
boolean shouldLogScanResults = logOptions.getShouldLogScannedPeripherals() != null
? logOptions.getShouldLogScannedPeripherals()
: this.shouldLogScannedPeripherals;
LogOptions.Logger logger = logOptions.getLogger() != null ? logOptions.getLogger() : this.logger;
return new LoggerSetup(logLevel, macAddressLogSetting, uuidLogSetting, shouldLogAttributeValues, logger);
return new LoggerSetup(logLevel, macAddressLogSetting, uuidLogSetting, shouldLogAttributeValues, shouldLogScanResults, logger);
}

@Override
Expand All @@ -46,6 +52,7 @@ public String toString() {
+ ", macAddressLogSetting=" + macAddressLogSetting
+ ", uuidLogSetting=" + uuidLogSetting
+ ", shouldLogAttributeValues=" + shouldLogAttributeValues
+ ", shouldLogScannedPeripherals=" + shouldLogScannedPeripherals
+ ", logger=" + logger
+ '}';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ BluetoothAdapter.LeScanCallback createScanCallback(final Emitter<RxBleInternalSc
return new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
if (!scanFilterMatcher.isEmpty() && RxBleLog.isAtLeast(LogConstants.DEBUG)) {
if (!scanFilterMatcher.isEmpty() && RxBleLog.isAtLeast(LogConstants.DEBUG) && RxBleLog.getShouldLogScannedPeripherals()) {
RxBleLog.d("%s, name=%s, rssi=%d, data=%s",
LoggerUtil.commonMacMessage(device.getAddress()),
device.getName(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ ScanCallback createScanCallback(final Emitter<RxBleInternalScanResult> emitter)
return new ScanCallback() {
@Override
public void onScanResult(int callbackType, ScanResult result) {
if (!emulatedScanFilterMatcher.isEmpty() && RxBleLog.isAtLeast(LogConstants.DEBUG)) {
if (!emulatedScanFilterMatcher.isEmpty()
&& RxBleLog.isAtLeast(LogConstants.DEBUG)
&& RxBleLog.getShouldLogScannedPeripherals()) {
ScanRecord scanRecord = result.getScanRecord();
RxBleLog.d("%s, name=%s, rssi=%d, data=%s",
LoggerUtil.commonMacMessage(result.getDevice().getAddress()),
Expand Down