scopeLogAsync<T> function

Future<T> scopeLogAsync<T>(
  1. Future<T> fn(),
  2. Logger log
)

Runs fn in an error handling Zone.

Any calls to print will be logged with log.warning, and any errors will be logged with log.severe.

Completes with the first error or result of fn, whichever comes first.

Implementation

Future<T> scopeLogAsync<T>(Future<T> Function() fn, Logger log) {
  var done = Completer<T>();
  runZonedGuarded(fn, (e, st) {
    log.severe('', e, st);
    if (done.isCompleted) return;
    done.completeError(e, st);
  }, zoneSpecification: ZoneSpecification(print: (self, parent, zone, message) {
    log.warning(message);
  }), zoneValues: {logKey: log})?.then((result) {
    if (done.isCompleted) return;
    done.complete(result);
  });
  return done.future;
}