Skip to content

Commit

Permalink
Use logging package (#26)
Browse files Browse the repository at this point in the history
* Use `logging` package

* Enforce `avoid_print`
  • Loading branch information
Craftplacer authored Nov 6, 2024
1 parent 137e087 commit 03edaef
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 36 deletions.
4 changes: 0 additions & 4 deletions packages/sane/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
include: package:flutter_lints/flutter.yaml

analyzer:
errors:
avoid_print: ignore

linter:
rules:
prefer_single_quotes: true
Expand Down
6 changes: 6 additions & 0 deletions packages/sane/example/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@
import 'dart:io';
import 'dart:typed_data';

import 'package:logging/logging.dart';
import 'package:sane/sane.dart';

void main(List<String> 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();

Expand Down
3 changes: 3 additions & 0 deletions packages/sane/lib/src/logger.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import 'package:logging/logging.dart';

final logger = Logger('sane');
30 changes: 18 additions & 12 deletions packages/sane/lib/src/sane.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -57,12 +58,14 @@ 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);
Expand All @@ -82,7 +85,7 @@ class Sane {
_exited = true;

dylib.sane_exit();
print('sane_exit()');
logger.finest('sane_exit()');

completer.complete();

Expand All @@ -106,7 +109,8 @@ class Sane {
deviceListPointer,
saneBoolFromDartBool(localOnly),
);
print('sane_get_devices() -> ${status.name}');

logger.finest('sane_get_devices() -> ${status.name}');

status.check();

Expand All @@ -133,7 +137,7 @@ class Sane {
final nativeHandlePointer = ffi.calloc<SANE_Handle>();
final deviceNamePointer = saneStringFromDartString(deviceName);
final status = dylib.sane_open(deviceNamePointer, nativeHandlePointer);
print('sane_open() -> ${status.name}');
logger.finest('sane_open() -> ${status.name}');

status.check();

Expand Down Expand Up @@ -165,7 +169,7 @@ class Sane {
Future(() {
dylib.sane_close(_getNativeHandle(handle));
_nativeHandles.remove(handle);
print('sane_close()');
logger.finest('sane_close()');

completer.complete();
});
Expand Down Expand Up @@ -307,7 +311,9 @@ class Sane {
valuePointer.cast<ffi.Void>(),
infoPointer,
);
print('sane_control_option($index, $action, $value) -> ${status.name}');
logger.finest(
'sane_control_option($index, $action, $value) -> ${status.name}',
);

status.check();

Expand Down Expand Up @@ -432,7 +438,7 @@ class Sane {
_getNativeHandle(handle),
nativeParametersPointer,
);
print('sane_get_parameters() -> ${status.name}');
logger.finest('sane_get_parameters() -> ${status.name}');

status.check();

Expand All @@ -453,7 +459,7 @@ class Sane {

Future(() {
final status = dylib.sane_start(_getNativeHandle(handle));
print('sane_start() -> ${status.name}');
logger.finest('sane_start() -> ${status.name}');

status.check();

Expand All @@ -478,7 +484,7 @@ class Sane {
bufferSize,
bytesReadPointer,
);
print('sane_read() -> ${status.name}');
logger.finest('sane_read() -> ${status.name}');

status.check();

Expand All @@ -505,7 +511,7 @@ class Sane {

Future(() {
dylib.sane_cancel(_getNativeHandle(handle));
print('sane_cancel()');
logger.finest('sane_cancel()');

completer.complete();
});
Expand All @@ -523,7 +529,7 @@ class Sane {
_getNativeHandle(handle),
saneBoolFromIOMode(mode),
);
print('sane_set_io_mode() -> ${status.name}');
logger.finest('sane_set_io_mode() -> ${status.name}');

status.check();

Expand Down
39 changes: 21 additions & 18 deletions packages/sane/lib/src/sane_dev.dart
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import 'dart:typed_data';

import 'package:logging/logging.dart';
import 'package:sane/sane.dart';

final _logger = Logger('sane.dev');

class SaneDev implements Sane {
@override
Future<void> cancel(SaneHandle handle) {
return Future.delayed(const Duration(seconds: 1), () {
print('sane_cancel()');
_logger.finest('sane_cancel()');
});
}

@override
Future<void> close(SaneHandle handle) {
return Future.delayed(const Duration(seconds: 1), () {
print('sane_close()');
_logger.finest('sane_close()');
});
}

Expand All @@ -25,7 +28,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: []);
});
}
Expand All @@ -36,7 +39,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: []);
});
}
Expand All @@ -49,7 +52,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: []);
});
}
Expand All @@ -62,7 +65,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: []);
});
}
Expand All @@ -75,15 +78,15 @@ 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: []);
});
}

@override
Future<void> exit() {
return Future(() {
print('sane_exit()');
_logger.finest('sane_exit()');
});
}

Expand All @@ -92,7 +95,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,
Expand All @@ -114,7 +117,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(
Expand All @@ -133,7 +136,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',
Expand All @@ -151,7 +154,7 @@ class SaneDev implements Sane {
@override
Future<SaneParameters> getParameters(SaneHandle handle) {
return Future.delayed(const Duration(seconds: 1), () {
print('sane_getParameters()');
_logger.finest('sane_getParameters()');
return SaneParameters(
format: SaneFrameFormat.gray,
lastFrame: true,
Expand All @@ -168,46 +171,46 @@ class SaneDev implements Sane {
AuthCallback? authCallback,
}) {
return Future(() {
print('sane_init()');
_logger.finest('sane_init()');
return 1;
});
}

@override
Future<SaneHandle> open(String deviceName) {
return Future.delayed(const Duration(seconds: 1), () {
print('sane_open()');
_logger.finest('sane_open()');
return SaneHandle(deviceName: deviceName);
});
}

@override
Future<SaneHandle> openDevice(SaneDevice device) {
return Future.delayed(const Duration(seconds: 1), () {
print('sane_openDevice()');
_logger.finest('sane_openDevice()');
return SaneHandle(deviceName: device.name);
});
}

@override
Future<Uint8List> read(SaneHandle handle, int bufferSize) {
return Future.delayed(const Duration(seconds: 1), () {
print('sane_read()');
_logger.finest('sane_read()');
return Uint8List.fromList([]);
});
}

@override
Future<void> setIOMode(SaneHandle handle, SaneIOMode mode) {
return Future.delayed(const Duration(seconds: 1), () {
print('sane_setIOMode()');
_logger.finest('sane_setIOMode()');
});
}

@override
Future<void> start(SaneHandle handle) {
return Future.delayed(const Duration(seconds: 1), () {
print('sane_start()');
_logger.finest('sane_start()');
});
}
}
4 changes: 2 additions & 2 deletions packages/sane/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ packages:
source: hosted
version: "4.0.0"
logging:
dependency: transitive
dependency: "direct main"
description:
name: logging
sha256: c8245ada5f1717ed44271ed1c26b8ce85ca3228fd2ffdb75468ab01979309d61
Expand All @@ -199,7 +199,7 @@ packages:
source: hosted
version: "0.12.16+1"
meta:
dependency: transitive
dependency: "direct main"
description:
name: meta
sha256: e3641ec5d63ebf0d9b41bd43201a66e3fc79a65db5f61fc181f04cd27aab950c
Expand Down
1 change: 1 addition & 0 deletions packages/sane/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ environment:

dependencies:
ffi: ^2.1.3
logging: ^1.3.0
meta: ^1.16.0

dev_dependencies:
Expand Down

0 comments on commit 03edaef

Please sign in to comment.