rotate method
Rotates the current file immediately, closing the live sink, shifting older files, and reopening a fresh live file.
Implementation
Future<void> rotate() async {
try {
await _out?.flush();
await _out?.close();
} catch (_) {
// Swallowed.
}
_out = null;
_openFuture = null;
// Delete the oldest.
if (keep > 0) {
final oldest = File('$path.$keep');
if (await oldest.exists()) {
try {
await oldest.delete();
} catch (_) {}
}
// Shift .n -> .n+1 in descending order.
for (var i = keep - 1; i >= 1; i--) {
final from = File('$path.$i');
if (await from.exists()) {
try {
await from.rename('$path.${i + 1}');
} catch (_) {}
}
}
// Rename live file to .1.
final live = File(path);
if (await live.exists()) {
try {
await live.rename('$path.1');
} catch (_) {}
}
} else {
final live = File(path);
if (await live.exists()) {
try {
await live.delete();
} catch (_) {}
}
}
_bytesWritten = 0;
}