Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 23 additions & 10 deletions lib/src/outputs/advanced_file_output.dart
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ class AdvancedFileOutput extends LogOutput {
Timer? _bufferFlushTimer;
Timer? _targetFileUpdater;

bool isClosingSink;

final List<OutputEvent> _buffer = [];

bool get _rotatingFilesMode => _maxFileSizeKB > 0;
Expand Down Expand Up @@ -183,7 +185,7 @@ class AdvancedFileOutput extends LogOutput {
}

void _flushBuffer() {
if (_sink == null) return; // Wait until _sink becomes available
if (_sink == null || isClosingSink) return; // Wait until _sink becomes available
for (final event in _buffer) {
_sink?.writeAll(event.lines, Platform.isWindows ? '\r\n' : '\n');
_sink?.writeln();
Expand All @@ -192,6 +194,7 @@ class AdvancedFileOutput extends LogOutput {
}

Future<void> _updateTargetFile() async {
if (isClosingSink) return; // if we are already closing, do nothing, because other functions will reopen the sink
try {
if (await _file.exists() &&
await _file.length() > _maxFileSizeKB * 1024) {
Expand All @@ -205,8 +208,10 @@ class AdvancedFileOutput extends LogOutput {
print(e);
print(s);
// Try creating another file and working with it
await _closeSink();
await _openSink();
if (!isClosingSink) {
await _closeSink();
await _openSink();
}
}
}

Expand All @@ -222,15 +227,23 @@ class AdvancedFileOutput extends LogOutput {
}

Future<void> _closeSink() async {
if (fileFooter != null) {
_sink?.writeln(fileFooter);
}
isClosingSink = true;
try {
if (fileFooter != null) {
_sink?.writeln(fileFooter);
}

final sink = _sink;
_sink = null; // disable writing in flushBuffer
final sink = _sink;
_sink = null; // disable writing in flushBuffer

await sink?.flush();
await sink?.close();
await sink?.flush();
await sink?.close();
} catch (e, s) {
print('Failed to close sink: $e');
print(s);
} finally {
isClosingSink = false;
}
}

Future<void> _deleteRotatedFiles() async {
Expand Down