Skip to content

Commit

Permalink
Formatted
Browse files Browse the repository at this point in the history
  • Loading branch information
Bungeefan committed Mar 22, 2024
1 parent 6657845 commit 154fef8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
21 changes: 10 additions & 11 deletions lib/src/outputs/advanced_file_output.dart
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class AdvancedFileOutput extends LogOutput {
],
_file = maxFileSizeKB > 0 ? File('$path/$latestFileName') : File(path);

/// Logs directory path by default, particular log file path if [_maxFileSizeKB] is 0
/// Logs directory path by default, particular log file path if [_maxFileSizeKB] is 0.
final String _path;

final bool _overrideExisting;
Expand Down Expand Up @@ -110,8 +110,8 @@ class AdvancedFileOutput extends LogOutput {
Future<void> init() async {
if (_rotatingFilesMode) {
final dir = Directory(_path);
//we use sync directory check to avoid losing
//potential initial boot logs in early crash scenarios
// We use sync directory check to avoid losing potential initial boot logs
// in early crash scenarios.
if (!dir.existsSync()) {
dir.createSync(recursive: true);
}
Expand All @@ -124,24 +124,23 @@ class AdvancedFileOutput extends LogOutput {

_bufferWriteTimer = Timer.periodic(_maxDelay, (_) => _writeOutBuffer());
await _openSink();
await _updateTargetFile(); //run first check without waiting for timer tick
await _updateTargetFile(); // Run first check without waiting for timer tick
}

@override
void output(OutputEvent event) {
_buffer.add(event);
// If event level is present in writeImmediately, write out the buffer
// along with any other possible elements that accumulated in it since
// the last timer tick
// Also write out if buffer is overfilled
// along with any other possible elements that accumulated since
// the last timer tick. Additionally, if the buffer is full.
if (_buffer.length > _maxBufferSize ||
_writeImmediately.contains(event.level)) {
_writeOutBuffer();
}
}

void _writeOutBuffer() {
if (_sink == null) return; //wait until _sink becomes available
if (_sink == null) return; // Wait until _sink becomes available
for (final event in _buffer) {
_sink?.writeAll(event.lines, Platform.isWindows ? '\r\n' : '\n');
_sink?.writeln();
Expand All @@ -153,15 +152,15 @@ class AdvancedFileOutput extends LogOutput {
try {
if (await _file.exists() &&
await _file.length() > _maxFileSizeKB * 1024) {
// rotate the log file
// Rotate the log file
await _closeSink();
await _file.rename('$_path/${_fileNameFormatter(DateTime.now())}');
await _openSink();
}
} catch (e, s) {
print(e);
print(s);
// try creating another file and working with it
// Try creating another file and working with it
await _closeSink();
await _openSink();
}
Expand All @@ -177,7 +176,7 @@ class AdvancedFileOutput extends LogOutput {
Future<void> _closeSink() async {
await _sink?.flush();
await _sink?.close();
_sink = null; //explicitly make null until assigned again
_sink = null; // Explicitly set null until assigned again
}

@override
Expand Down
6 changes: 3 additions & 3 deletions test/outputs/advanced_file_output_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ void main() {
output.output(event1);
output.output(event2);

//wait until buffer writes out to file
// Wait until buffer is flushed to file
await Future.delayed(const Duration(seconds: 1));

await output.destroy();
Expand Down Expand Up @@ -88,13 +88,13 @@ void main() {
output.output(event0);
await output.destroy();

//Start again to roll files on init without waiting for timer tick
// Start again to roll files on init without waiting for timer tick
await output.init();
final event1 = OutputEvent(LogEvent(Level.fatal, ""), ["2" * 1500]);
output.output(event1);
await output.destroy();

//And again for another roll
// And again for another roll
await output.init();
final event2 = OutputEvent(LogEvent(Level.fatal, ""), ["3" * 1500]);
output.output(event2);
Expand Down

0 comments on commit 154fef8

Please sign in to comment.