From edd5ec20a5155277167917624e23f4632c18fce8 Mon Sep 17 00:00:00 2001 From: Rohit Sangwan Date: Mon, 12 Feb 2024 16:30:57 +0530 Subject: [PATCH] Improve logging (#9) * Improve logging * Update changelog --------- Co-authored-by: Foti Dim --- CHANGELOG.md | 3 ++ example/pubspec.lock | 2 +- .../universal_ble_linux.dart | 39 ++++++++++++++----- lib/src/universal_ble_platform_interface.dart | 7 ++-- pubspec.yaml | 2 +- 5 files changed, 38 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 570bec3..f9c9f31 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## 0.9.1 +* Improve logging + ## 0.9.0 * Improve windows device name discovery * Improve linux implementation diff --git a/example/pubspec.lock b/example/pubspec.lock index 5499614..e9cbfb9 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -386,7 +386,7 @@ packages: path: ".." relative: true source: path - version: "0.8.4" + version: "0.9.0" vector_math: dependency: transitive description: diff --git a/lib/src/universal_ble_linux/universal_ble_linux.dart b/lib/src/universal_ble_linux/universal_ble_linux.dart index b51a006..38819a6 100644 --- a/lib/src/universal_ble_linux/universal_ble_linux.dart +++ b/lib/src/universal_ble_linux/universal_ble_linux.dart @@ -43,7 +43,10 @@ class UniversalBleLinux extends UniversalBlePlatform { await _activeAdapter?.setPowered(true); return _activeAdapter?.powered ?? false; } catch (e) { - logInfo('Error enabling bluetooth: $e'); + UniversalBlePlatform.logInfo( + 'Error enabling bluetooth: $e', + isError: true, + ); return false; } } @@ -67,7 +70,10 @@ class UniversalBleLinux extends UniversalBlePlatform { await _activeAdapter?.stopDiscovery(); } } catch (e) { - logInfo("stopScan error: $e"); + UniversalBlePlatform.logInfo( + "stopScan error: $e", + isError: true, + ); } } @@ -158,7 +164,8 @@ class UniversalBleLinux extends UniversalBlePlatform { ); break; default: - logInfo("UnhandledCharValuePropertyChange: $property"); + UniversalBlePlatform.logInfo( + "UnhandledCharValuePropertyChange: $property"); } } }); @@ -248,7 +255,8 @@ class UniversalBleLinux extends UniversalBlePlatform { .map((e) => e.uuid.toString()) .any((service) => withServices.contains(service)); } else { - logInfo('Skipping: ${device.address}: Services not resolved yet.'); + UniversalBlePlatform.logInfo( + 'Skipping: ${device.address}: Services not resolved yet.'); return false; } }).toList(); @@ -287,7 +295,7 @@ class UniversalBleLinux extends UniversalBlePlatform { _activeAdapter ??= _client.adapters.first; - logInfo( + UniversalBlePlatform.logInfo( 'BleAdapter: ${_activeAdapter?.name} - ${_activeAdapter?.address}', ); @@ -304,7 +312,9 @@ class UniversalBleLinux extends UniversalBlePlatform { break; case BluezProperty.propertyClass: default: - logInfo("UnhandledPropertyChanged: $property"); + UniversalBlePlatform.logInfo( + "UnhandledPropertyChanged: $property", + ); } } }); @@ -317,7 +327,10 @@ class UniversalBleLinux extends UniversalBlePlatform { _initializationCompleter?.complete(); _initializationCompleter = null; } catch (e) { - logInfo('Error initializing: $e'); + UniversalBlePlatform.logInfo( + 'Error initializing: $e', + isError: true, + ); _initializationCompleter?.completeError(e); await _client.close(); rethrow; @@ -381,7 +394,7 @@ class UniversalBleLinux extends UniversalBlePlatform { case BluezProperty.addressType: break; default: - logInfo( + UniversalBlePlatform.logInfo( "UnhandledDevicePropertyChanged ${device.name} ${device.address}: $property"); break; } @@ -428,11 +441,17 @@ extension BlueZDeviceExtension on BlueZDevice { List manufacturerDataValue = sorted.first.value; var byteData = ByteData(2); byteData.setInt16( - 0, companyId, Endian.host); // TODO: Verify that this works regardless of the endianess + 0, + companyId, + Endian + .host); // TODO: Verify that this works regardless of the endianess List bytes = byteData.buffer.asUint8List(); return Uint8List.fromList(bytes + manufacturerDataValue); } catch (e) { - logInfo('Error parsing manufacturerData: $e'); + UniversalBlePlatform.logInfo( + 'Error parsing manufacturerData: $e', + isError: true, + ); return Uint8List(0); } } diff --git a/lib/src/universal_ble_platform_interface.dart b/lib/src/universal_ble_platform_interface.dart index 9e80a10..a8deee4 100644 --- a/lib/src/universal_ble_platform_interface.dart +++ b/lib/src/universal_ble_platform_interface.dart @@ -50,8 +50,9 @@ abstract class UniversalBlePlatform { OnConnectionChanged? onConnectionChanged; OnValueChanged? onValueChanged; OnPairingStateChange? onPairingStateChange; -} -void logInfo(String message) { - log(message, name: 'UniversalBle'); + static void logInfo(String message, {bool isError = false}) { + if (isError) message = '\x1B[31m$message\x1B[31m'; + log(message, name: 'UniversalBle'); + } } diff --git a/pubspec.yaml b/pubspec.yaml index 3d07c74..41e46a4 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: universal_ble description: A cross-platform (Android/iOS/macOS/Windows/Linux/Web) Bluetooth Low Energy (BLE) plugin for Flutter -version: 0.9.0 +version: 0.9.1 homepage: https://navideck.com repository: https://github.com/Navideck/universal_ble issue_tracker: https://github.com/Navideck/universal_ble/issues