Skip to content

Commit

Permalink
Use logging package
Browse files Browse the repository at this point in the history
  • Loading branch information
Craftplacer committed Nov 4, 2024
1 parent 137e087 commit 3dbf0bd
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 34 deletions.
8 changes: 6 additions & 2 deletions packages/sane/example/main.dart
Original file line number Diff line number Diff line change
@@ -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<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.dart');
28 changes: 16 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,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)}');

Check notice on line 67 in packages/sane/lib/src/sane.dart

View workflow job for this annotation

GitHub Actions / analyze

Missing a required trailing comma.

See https://dart.dev/lints/require_trailing_commas to learn more about this problem.

ffi.calloc.free(versionCodePointer);
ffi.calloc.free(nativeAuthCallback);
Expand All @@ -82,7 +84,7 @@ class Sane {
_exited = true;

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

completer.complete();

Expand All @@ -106,7 +108,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 +136,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 +168,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 +310,8 @@ 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}');

Check notice on line 314 in packages/sane/lib/src/sane.dart

View workflow job for this annotation

GitHub Actions / analyze

Missing a required trailing comma.

See https://dart.dev/lints/require_trailing_commas to learn more about this problem.

status.check();

Expand Down Expand Up @@ -432,7 +436,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 +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();

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

status.check();

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

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

completer.complete();
});
Expand All @@ -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();

Expand Down
37 changes: 19 additions & 18 deletions packages/sane/lib/src/sane_dev.dart
Original file line number Diff line number Diff line change
@@ -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<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 +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: []);
});
}
Expand All @@ -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: []);
});
}
Expand All @@ -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: []);
});
}
Expand All @@ -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: []);
});
}
Expand All @@ -75,15 +76,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 +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,
Expand All @@ -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(
Expand All @@ -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',
Expand All @@ -151,7 +152,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 +169,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 3dbf0bd

Please sign in to comment.