importInto method

Future<int> importInto(
  1. LogSink sink
)

Imports all complete files into sink: each entry goes to LogSink.add, and the file is deleted after LogSink.flush. Returns the number of entries passed.

A file is deleted only after the sink has delivered its entries (LogSink.flush; for RootLogger.asSink that is the store write), so a failure between add and delete re-imports the same file instead of losing it: background-isolate entries are delivered at least once. A corrupt line is skipped with a message in the sink's LogSink.diagnostics; the remaining lines are imported and the file is deleted, otherwise it would be imported forever. A read or delete failure also goes to diagnostics and does not block the other files.

Implementation

Future<int> importInto(LogSink sink) async {
  var imported = 0;
  for (final file in await files()) {
    try {
      imported += await _importFile(file, sink);
    } on Object catch (error, stackTrace) {
      sink.diagnostics.report(
        LogDiagnostic('LogSpool: failed to import ${file.path}', error: error, stackTrace: stackTrace),
      );
    }
  }
  return imported;
}