start static method
Start writing the store's redacted log, event and exception records to
directory, creating it when absent, and make the sink current.
A sink already running is stopped first. maxFileBytes (bytes, at least
- caps one file; a single line larger than that still gets a file of
its own.
maxFiles(at least 1) caps the timeline files kept indirectory. Throws a FileSystemException whendirectorycannot be created or written; the sink then neither listens nor becomes current.
Implementation
static Future<TelescopeFileSink> start({
required String directory,
int maxFileBytes = 1048576,
int maxFiles = 8,
}) async {
assert(maxFileBytes > 0, 'maxFileBytes must be positive.');
assert(maxFiles > 0, 'maxFiles must be positive.');
// 1. One sink at a time: two would split the timeline across files.
await _current?.stop();
// 2. Subscribe before the first await on disk, so no record recorded
// while the directory is being created is missed; those lines wait in
// the batch until the first file is open.
final TelescopeFileSink sink = TelescopeFileSink._(
directory: Directory(directory),
maxFileBytes: maxFileBytes,
maxFiles: maxFiles,
launchStamp: _stamp(DateTime.now().toUtc()),
);
sink._subscriptions.addAll(<StreamSubscription<Object>>[
TelescopeStore.onLogRecord.listen(
(LogRecordEntry r) => sink._accept('log', r.redacted, r.toJson),
),
TelescopeStore.onEventRecord.listen(
(EventRecord r) => sink._accept('event', r.redacted, r.toJson),
),
TelescopeStore.onExceptionRecord.listen(
(ExceptionRecord r) => sink._accept('exception', r.redacted, r.toJson),
),
]);
// 3. The first file exists before the sink becomes current, so nothing
// writes without one; a sink that cannot open it stops listening.
try {
await sink._serial(() async {
await sink._directory.create(recursive: true);
await sink._rotate();
});
} on Object {
await sink._halt();
rethrow;
}
// 4. Hand the lines batched during startup to the usual schedule.
sink._opened = true;
_current = sink;
if (sink._pending.isNotEmpty) sink._scheduleFlush();
return sink;
}