From 3dbf0bde8ea5655e70df4b4ac083de75dd371929 Mon Sep 17 00:00:00 2001 From: Craftplacer <22963120+Craftplacer@users.noreply.github.com> Date: Mon, 4 Nov 2024 11:56:45 +0100 Subject: [PATCH 1/5] Use `logging` package --- packages/sane/example/main.dart | 8 +++++-- packages/sane/lib/src/logger.dart | 3 +++ packages/sane/lib/src/sane.dart | 28 ++++++++++++---------- packages/sane/lib/src/sane_dev.dart | 37 +++++++++++++++-------------- packages/sane/pubspec.lock | 4 ++-- packages/sane/pubspec.yaml | 1 + 6 files changed, 47 insertions(+), 34 deletions(-) create mode 100644 packages/sane/lib/src/logger.dart diff --git a/packages/sane/example/main.dart b/packages/sane/example/main.dart index 340b880..5866021 100644 --- a/packages/sane/example/main.dart +++ b/packages/sane/example/main.dart @@ -1,11 +1,15 @@ -// ignore_for_file: avoid_print - import 'dart:io'; import 'dart:typed_data'; +import 'package:logging/logging.dart'; import 'package:sane/sane.dart'; void main(List args) async { + Logger.root.level = Level.ALL; + Logger.root.onRecord.listen((record) { + print('${record.level.name}: ${record.time}: ${record.message}'); + }); + final sane = SaneIsolate(sane: SaneDev()); await sane.spawn(); diff --git a/packages/sane/lib/src/logger.dart b/packages/sane/lib/src/logger.dart new file mode 100644 index 0000000..fad646c --- /dev/null +++ b/packages/sane/lib/src/logger.dart @@ -0,0 +1,3 @@ +import 'package:logging/logging.dart'; + +final logger = Logger('sane.dart'); diff --git a/packages/sane/lib/src/sane.dart b/packages/sane/lib/src/sane.dart index b71ac63..c9679fb 100644 --- a/packages/sane/lib/src/sane.dart +++ b/packages/sane/lib/src/sane.dart @@ -7,6 +7,7 @@ import 'package:sane/src/bindings.g.dart'; import 'package:sane/src/dylib.dart'; import 'package:sane/src/exceptions.dart'; import 'package:sane/src/extensions.dart'; +import 'package:sane/src/logger.dart'; import 'package:sane/src/structures.dart'; import 'package:sane/src/type_conversion.dart'; import 'package:sane/src/utils.dart'; @@ -57,12 +58,13 @@ class Sane { ).nativeFunction : ffi.nullptr; final status = dylib.sane_init(versionCodePointer, nativeAuthCallback); - print('sane_init() -> ${status.name}'); + logger.finest('sane_init() -> ${status.name}'); status.check(); final versionCode = versionCodePointer.value; - print('SANE version: ${SaneUtils.version(versionCodePointer.value)}'); + logger.finest( + 'SANE version: ${SaneUtils.version(versionCodePointer.value)}'); ffi.calloc.free(versionCodePointer); ffi.calloc.free(nativeAuthCallback); @@ -82,7 +84,7 @@ class Sane { _exited = true; dylib.sane_exit(); - print('sane_exit()'); + logger.finest('sane_exit()'); completer.complete(); @@ -106,7 +108,8 @@ class Sane { deviceListPointer, saneBoolFromDartBool(localOnly), ); - print('sane_get_devices() -> ${status.name}'); + + logger.finest('sane_get_devices() -> ${status.name}'); status.check(); @@ -133,7 +136,7 @@ class Sane { final nativeHandlePointer = ffi.calloc(); final deviceNamePointer = saneStringFromDartString(deviceName); final status = dylib.sane_open(deviceNamePointer, nativeHandlePointer); - print('sane_open() -> ${status.name}'); + logger.finest('sane_open() -> ${status.name}'); status.check(); @@ -165,7 +168,7 @@ class Sane { Future(() { dylib.sane_close(_getNativeHandle(handle)); _nativeHandles.remove(handle); - print('sane_close()'); + logger.finest('sane_close()'); completer.complete(); }); @@ -307,7 +310,8 @@ class Sane { valuePointer.cast(), infoPointer, ); - print('sane_control_option($index, $action, $value) -> ${status.name}'); + logger.finest( + 'sane_control_option($index, $action, $value) -> ${status.name}'); status.check(); @@ -432,7 +436,7 @@ class Sane { _getNativeHandle(handle), nativeParametersPointer, ); - print('sane_get_parameters() -> ${status.name}'); + logger.finest('sane_get_parameters() -> ${status.name}'); status.check(); @@ -453,7 +457,7 @@ class Sane { Future(() { final status = dylib.sane_start(_getNativeHandle(handle)); - print('sane_start() -> ${status.name}'); + logger.finest('sane_start() -> ${status.name}'); status.check(); @@ -478,7 +482,7 @@ class Sane { bufferSize, bytesReadPointer, ); - print('sane_read() -> ${status.name}'); + logger.finest('sane_read() -> ${status.name}'); status.check(); @@ -505,7 +509,7 @@ class Sane { Future(() { dylib.sane_cancel(_getNativeHandle(handle)); - print('sane_cancel()'); + logger.finest('sane_cancel()'); completer.complete(); }); @@ -523,7 +527,7 @@ class Sane { _getNativeHandle(handle), saneBoolFromIOMode(mode), ); - print('sane_set_io_mode() -> ${status.name}'); + logger.finest('sane_set_io_mode() -> ${status.name}'); status.check(); diff --git a/packages/sane/lib/src/sane_dev.dart b/packages/sane/lib/src/sane_dev.dart index 8438e01..d1a243c 100644 --- a/packages/sane/lib/src/sane_dev.dart +++ b/packages/sane/lib/src/sane_dev.dart @@ -1,19 +1,20 @@ import 'dart:typed_data'; import 'package:sane/sane.dart'; +import 'package:sane/src/logger.dart'; class SaneDev implements Sane { @override Future cancel(SaneHandle handle) { return Future.delayed(const Duration(seconds: 1), () { - print('sane_cancel()'); + logger.finest('sane_cancel()'); }); } @override Future close(SaneHandle handle) { return Future.delayed(const Duration(seconds: 1), () { - print('sane_close()'); + logger.finest('sane_close()'); }); } @@ -25,7 +26,7 @@ class SaneDev implements Sane { bool? value, }) { return Future.delayed(const Duration(seconds: 1), () { - print('sane_controlBoolOption()'); + logger.finest('sane_controlBoolOption()'); return SaneOptionResult(result: value ?? true, infos: []); }); } @@ -36,7 +37,7 @@ class SaneDev implements Sane { required int index, }) { return Future.delayed(const Duration(seconds: 1), () { - print('sane_controlButtonOption()'); + logger.finest('sane_controlButtonOption()'); return SaneOptionResult(result: null, infos: []); }); } @@ -49,7 +50,7 @@ class SaneDev implements Sane { double? value, }) { return Future.delayed(const Duration(seconds: 1), () { - print('sane_controlFixedOption()'); + logger.finest('sane_controlFixedOption()'); return SaneOptionResult(result: value ?? .1, infos: []); }); } @@ -62,7 +63,7 @@ class SaneDev implements Sane { int? value, }) { return Future.delayed(const Duration(seconds: 1), () { - print('sane_controlIntOption()'); + logger.finest('sane_controlIntOption()'); return SaneOptionResult(result: value ?? 1, infos: []); }); } @@ -75,7 +76,7 @@ class SaneDev implements Sane { String? value, }) { return Future.delayed(const Duration(seconds: 1), () { - print('sane_controlStringOption()'); + logger.finest('sane_controlStringOption()'); return SaneOptionResult(result: value ?? 'value', infos: []); }); } @@ -83,7 +84,7 @@ class SaneDev implements Sane { @override Future exit() { return Future(() { - print('sane_exit()'); + logger.finest('sane_exit()'); }); } @@ -92,7 +93,7 @@ class SaneDev implements Sane { SaneHandle handle, ) { return Future.delayed(const Duration(seconds: 1), () { - print('sane_getAllOptionDescriptors()'); + logger.finest('sane_getAllOptionDescriptors()'); return [ SaneOptionDescriptor( index: 0, @@ -114,7 +115,7 @@ class SaneDev implements Sane { required bool localOnly, }) { return Future.delayed(const Duration(seconds: 1), () { - print('sane_getDevices()'); + logger.finest('sane_getDevices()'); return [ for (var i = 0; i < 3; i++) SaneDevice( @@ -133,7 +134,7 @@ class SaneDev implements Sane { int index, ) { return Future.delayed(const Duration(seconds: 1), () { - print('sane_getOptionDescriptor()'); + logger.finest('sane_getOptionDescriptor()'); return SaneOptionDescriptor( index: index, name: 'name', @@ -151,7 +152,7 @@ class SaneDev implements Sane { @override Future getParameters(SaneHandle handle) { return Future.delayed(const Duration(seconds: 1), () { - print('sane_getParameters()'); + logger.finest('sane_getParameters()'); return SaneParameters( format: SaneFrameFormat.gray, lastFrame: true, @@ -168,7 +169,7 @@ class SaneDev implements Sane { AuthCallback? authCallback, }) { return Future(() { - print('sane_init()'); + logger.finest('sane_init()'); return 1; }); } @@ -176,7 +177,7 @@ class SaneDev implements Sane { @override Future open(String deviceName) { return Future.delayed(const Duration(seconds: 1), () { - print('sane_open()'); + logger.finest('sane_open()'); return SaneHandle(deviceName: deviceName); }); } @@ -184,7 +185,7 @@ class SaneDev implements Sane { @override Future openDevice(SaneDevice device) { return Future.delayed(const Duration(seconds: 1), () { - print('sane_openDevice()'); + logger.finest('sane_openDevice()'); return SaneHandle(deviceName: device.name); }); } @@ -192,7 +193,7 @@ class SaneDev implements Sane { @override Future read(SaneHandle handle, int bufferSize) { return Future.delayed(const Duration(seconds: 1), () { - print('sane_read()'); + logger.finest('sane_read()'); return Uint8List.fromList([]); }); } @@ -200,14 +201,14 @@ class SaneDev implements Sane { @override Future setIOMode(SaneHandle handle, SaneIOMode mode) { return Future.delayed(const Duration(seconds: 1), () { - print('sane_setIOMode()'); + logger.finest('sane_setIOMode()'); }); } @override Future start(SaneHandle handle) { return Future.delayed(const Duration(seconds: 1), () { - print('sane_start()'); + logger.finest('sane_start()'); }); } } diff --git a/packages/sane/pubspec.lock b/packages/sane/pubspec.lock index b2a2893..5cb05be 100644 --- a/packages/sane/pubspec.lock +++ b/packages/sane/pubspec.lock @@ -175,7 +175,7 @@ packages: source: hosted version: "4.0.0" logging: - dependency: transitive + dependency: "direct main" description: name: logging sha256: c8245ada5f1717ed44271ed1c26b8ce85ca3228fd2ffdb75468ab01979309d61 @@ -199,7 +199,7 @@ packages: source: hosted version: "0.12.16+1" meta: - dependency: transitive + dependency: "direct main" description: name: meta sha256: e3641ec5d63ebf0d9b41bd43201a66e3fc79a65db5f61fc181f04cd27aab950c diff --git a/packages/sane/pubspec.yaml b/packages/sane/pubspec.yaml index 4846fdd..fccbe9b 100644 --- a/packages/sane/pubspec.yaml +++ b/packages/sane/pubspec.yaml @@ -11,6 +11,7 @@ environment: dependencies: ffi: ^2.1.3 + logging: ^1.3.0 meta: ^1.16.0 dev_dependencies: From 89707fd8120005223f89e074d8262410571d8031 Mon Sep 17 00:00:00 2001 From: Craftplacer <22963120+Craftplacer@users.noreply.github.com> Date: Mon, 4 Nov 2024 11:59:19 +0100 Subject: [PATCH 2/5] Trailing commas --- packages/sane/lib/src/sane.dart | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/sane/lib/src/sane.dart b/packages/sane/lib/src/sane.dart index c9679fb..d72532c 100644 --- a/packages/sane/lib/src/sane.dart +++ b/packages/sane/lib/src/sane.dart @@ -64,7 +64,8 @@ class Sane { final versionCode = versionCodePointer.value; logger.finest( - 'SANE version: ${SaneUtils.version(versionCodePointer.value)}'); + 'SANE version: ${SaneUtils.version(versionCodePointer.value)}', + ); ffi.calloc.free(versionCodePointer); ffi.calloc.free(nativeAuthCallback); @@ -311,7 +312,8 @@ class Sane { infoPointer, ); logger.finest( - 'sane_control_option($index, $action, $value) -> ${status.name}'); + 'sane_control_option($index, $action, $value) -> ${status.name}', + ); status.check(); From 759adc00a2782820f18aab5ecbe7741e23840102 Mon Sep 17 00:00:00 2001 From: Craftplacer <22963120+Craftplacer@users.noreply.github.com> Date: Mon, 4 Nov 2024 12:11:24 +0100 Subject: [PATCH 3/5] Enforce `avoid_print` --- packages/sane/analysis_options.yaml | 4 ---- packages/sane/example/main.dart | 2 ++ 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/packages/sane/analysis_options.yaml b/packages/sane/analysis_options.yaml index 139ca46..9ad1a9b 100644 --- a/packages/sane/analysis_options.yaml +++ b/packages/sane/analysis_options.yaml @@ -1,9 +1,5 @@ include: package:flutter_lints/flutter.yaml -analyzer: - errors: - avoid_print: ignore - linter: rules: prefer_single_quotes: true diff --git a/packages/sane/example/main.dart b/packages/sane/example/main.dart index 5866021..d93c5a7 100644 --- a/packages/sane/example/main.dart +++ b/packages/sane/example/main.dart @@ -1,3 +1,5 @@ +// ignore_for_file: avoid_print + import 'dart:io'; import 'dart:typed_data'; From c7a69e6c775d361648ec1a91a40617daa096a8fb Mon Sep 17 00:00:00 2001 From: Craftplacer <22963120+Craftplacer@users.noreply.github.com> Date: Mon, 4 Nov 2024 12:11:37 +0100 Subject: [PATCH 4/5] Redo logger structure --- packages/sane/lib/src/logger.dart | 2 +- packages/sane/lib/src/sane_dev.dart | 40 +++++++++++++++-------------- 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/packages/sane/lib/src/logger.dart b/packages/sane/lib/src/logger.dart index fad646c..7117d01 100644 --- a/packages/sane/lib/src/logger.dart +++ b/packages/sane/lib/src/logger.dart @@ -1,3 +1,3 @@ import 'package:logging/logging.dart'; -final logger = Logger('sane.dart'); +final logger = Logger('sane'); diff --git a/packages/sane/lib/src/sane_dev.dart b/packages/sane/lib/src/sane_dev.dart index d1a243c..0708a70 100644 --- a/packages/sane/lib/src/sane_dev.dart +++ b/packages/sane/lib/src/sane_dev.dart @@ -1,20 +1,22 @@ import 'dart:typed_data'; +import 'package:logging/logging.dart'; import 'package:sane/sane.dart'; -import 'package:sane/src/logger.dart'; + +final _logger = Logger('sane.sane_dev'); class SaneDev implements Sane { @override Future cancel(SaneHandle handle) { return Future.delayed(const Duration(seconds: 1), () { - logger.finest('sane_cancel()'); + _logger.finest('sane_cancel()'); }); } @override Future close(SaneHandle handle) { return Future.delayed(const Duration(seconds: 1), () { - logger.finest('sane_close()'); + _logger.finest('sane_close()'); }); } @@ -26,7 +28,7 @@ class SaneDev implements Sane { bool? value, }) { return Future.delayed(const Duration(seconds: 1), () { - logger.finest('sane_controlBoolOption()'); + _logger.finest('sane_controlBoolOption()'); return SaneOptionResult(result: value ?? true, infos: []); }); } @@ -37,7 +39,7 @@ class SaneDev implements Sane { required int index, }) { return Future.delayed(const Duration(seconds: 1), () { - logger.finest('sane_controlButtonOption()'); + _logger.finest('sane_controlButtonOption()'); return SaneOptionResult(result: null, infos: []); }); } @@ -50,7 +52,7 @@ class SaneDev implements Sane { double? value, }) { return Future.delayed(const Duration(seconds: 1), () { - logger.finest('sane_controlFixedOption()'); + _logger.finest('sane_controlFixedOption()'); return SaneOptionResult(result: value ?? .1, infos: []); }); } @@ -63,7 +65,7 @@ class SaneDev implements Sane { int? value, }) { return Future.delayed(const Duration(seconds: 1), () { - logger.finest('sane_controlIntOption()'); + _logger.finest('sane_controlIntOption()'); return SaneOptionResult(result: value ?? 1, infos: []); }); } @@ -76,7 +78,7 @@ class SaneDev implements Sane { String? value, }) { return Future.delayed(const Duration(seconds: 1), () { - logger.finest('sane_controlStringOption()'); + _logger.finest('sane_controlStringOption()'); return SaneOptionResult(result: value ?? 'value', infos: []); }); } @@ -84,7 +86,7 @@ class SaneDev implements Sane { @override Future exit() { return Future(() { - logger.finest('sane_exit()'); + _logger.finest('sane_exit()'); }); } @@ -93,7 +95,7 @@ class SaneDev implements Sane { SaneHandle handle, ) { return Future.delayed(const Duration(seconds: 1), () { - logger.finest('sane_getAllOptionDescriptors()'); + _logger.finest('sane_getAllOptionDescriptors()'); return [ SaneOptionDescriptor( index: 0, @@ -115,7 +117,7 @@ class SaneDev implements Sane { required bool localOnly, }) { return Future.delayed(const Duration(seconds: 1), () { - logger.finest('sane_getDevices()'); + _logger.finest('sane_getDevices()'); return [ for (var i = 0; i < 3; i++) SaneDevice( @@ -134,7 +136,7 @@ class SaneDev implements Sane { int index, ) { return Future.delayed(const Duration(seconds: 1), () { - logger.finest('sane_getOptionDescriptor()'); + _logger.finest('sane_getOptionDescriptor()'); return SaneOptionDescriptor( index: index, name: 'name', @@ -152,7 +154,7 @@ class SaneDev implements Sane { @override Future getParameters(SaneHandle handle) { return Future.delayed(const Duration(seconds: 1), () { - logger.finest('sane_getParameters()'); + _logger.finest('sane_getParameters()'); return SaneParameters( format: SaneFrameFormat.gray, lastFrame: true, @@ -169,7 +171,7 @@ class SaneDev implements Sane { AuthCallback? authCallback, }) { return Future(() { - logger.finest('sane_init()'); + _logger.finest('sane_init()'); return 1; }); } @@ -177,7 +179,7 @@ class SaneDev implements Sane { @override Future open(String deviceName) { return Future.delayed(const Duration(seconds: 1), () { - logger.finest('sane_open()'); + _logger.finest('sane_open()'); return SaneHandle(deviceName: deviceName); }); } @@ -185,7 +187,7 @@ class SaneDev implements Sane { @override Future openDevice(SaneDevice device) { return Future.delayed(const Duration(seconds: 1), () { - logger.finest('sane_openDevice()'); + _logger.finest('sane_openDevice()'); return SaneHandle(deviceName: device.name); }); } @@ -193,7 +195,7 @@ class SaneDev implements Sane { @override Future read(SaneHandle handle, int bufferSize) { return Future.delayed(const Duration(seconds: 1), () { - logger.finest('sane_read()'); + _logger.finest('sane_read()'); return Uint8List.fromList([]); }); } @@ -201,14 +203,14 @@ class SaneDev implements Sane { @override Future setIOMode(SaneHandle handle, SaneIOMode mode) { return Future.delayed(const Duration(seconds: 1), () { - logger.finest('sane_setIOMode()'); + _logger.finest('sane_setIOMode()'); }); } @override Future start(SaneHandle handle) { return Future.delayed(const Duration(seconds: 1), () { - logger.finest('sane_start()'); + _logger.finest('sane_start()'); }); } } From feecef011db341a870fae0547b7da4d1d5bee43c Mon Sep 17 00:00:00 2001 From: Craftplacer <22963120+Craftplacer@users.noreply.github.com> Date: Wed, 6 Nov 2024 23:54:25 +0100 Subject: [PATCH 5/5] Shorten logger name --- packages/sane/lib/src/sane_dev.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/sane/lib/src/sane_dev.dart b/packages/sane/lib/src/sane_dev.dart index 0708a70..8e39549 100644 --- a/packages/sane/lib/src/sane_dev.dart +++ b/packages/sane/lib/src/sane_dev.dart @@ -3,7 +3,7 @@ import 'dart:typed_data'; import 'package:logging/logging.dart'; import 'package:sane/sane.dart'; -final _logger = Logger('sane.sane_dev'); +final _logger = Logger('sane.dev'); class SaneDev implements Sane { @override