wrapWithPatrolLog<T> method

Future<T> wrapWithPatrolLog<T>({
  1. required String action,
  2. String? value,
  3. Finder? finder,
  4. required String color,
  5. required Future<T> function(),
  6. bool enablePatrolLog = true,
})
inherited

Wraps a function with a log entry for the start and end of the function.

Implementation

Future<T> wrapWithPatrolLog<T>({
  required String action,
  String? value,
  Finder? finder,
  required String color,
  required Future<T> Function() function,
  bool enablePatrolLog = true,
}) async {
  if (!(config.printLogs && enablePatrolLog)) {
    return function();
  }

  final finderText =
      finder
          ?.toString(describeSelf: true)
          .replaceAll('A finder that searches for', '')
          .replaceAll(' (considering only hit-testable ones)', '')
          .replaceAll(' (ignoring all but first)', '') ??
      '';
  final valueText = value != null ? ' "$value"' : '';
  final text = '$color$action${AnsiCodes.reset}$valueText$finderText';
  patrolLog.log(StepEntry(action: text, status: StepEntryStatus.start));
  try {
    final result = await function();
    patrolLog.log(StepEntry(action: text, status: StepEntryStatus.success));
    return result;
  } catch (err) {
    patrolLog.log(StepEntry(action: text, status: StepEntryStatus.failure));
    rethrow;
  }
}