Flush logger IOSink. (#229)

If we are writing to a file sometimes the content is incomplete because
not all the output has been written to the file.

Bug:
  https://github.com/flutter/flutter/issues/67942
This commit is contained in:
godofredoc
2020-10-16 14:37:59 -07:00
committed by GitHub
parent d6a5498d0d
commit 1a3ef3390e
4 changed files with 15 additions and 2 deletions

View File

@ -1,5 +1,9 @@
# CHANGELOG # CHANGELOG
## version:0.0.27
- Flush the content of iosink when writing output to a file.
## version:0.0.26 ## version:0.0.26
- Replace amberctl with pkgctl. - Replace amberctl with pkgctl.

View File

@ -108,6 +108,12 @@ class PrintLogger implements Logger {
out.writeln('$message'); out.writeln('$message');
} }
} }
/// Flushes the IOSink to ensure all the data is written. This is specially
/// useful when writing to a file.
Future<void> flush() async {
await out.flush();
}
} }
/// Transforms a [message] with [level] to a string that contains the DateTime, /// Transforms a [message] with [level] to a string that contains the DateTime,

View File

@ -146,6 +146,9 @@ class SshClient {
stdoutSubscription.asFuture<void>(), stdoutSubscription.asFuture<void>(),
stderrSubscription.asFuture<void>(), stderrSubscription.asFuture<void>(),
]); ]);
await logger.flush();
// The streams as futures have already completed, so waiting for the // The streams as futures have already completed, so waiting for the
// potentially async stream cancellation to complete likely has no benefit. // potentially async stream cancellation to complete likely has no benefit.
stdoutSubscription.cancel(); stdoutSubscription.cancel();

View File

@ -18,7 +18,7 @@ void main() {
logger.info('cdf'); logger.info('cdf');
logger.warning('gh'); logger.warning('gh');
logger.error('jk'); logger.error('jk');
await data.flush(); await logger.flush();
final String content = fs.file('log.txt').readAsStringSync(); final String content = fs.file('log.txt').readAsStringSync();
expect(content, contains('ERROR jk')); expect(content, contains('ERROR jk'));
expect(content, contains('INFO cdf')); expect(content, contains('INFO cdf'));
@ -48,7 +48,7 @@ void main() {
logger.info('cdf'); logger.info('cdf');
logger.warning('gh'); logger.warning('gh');
logger.error('jk'); logger.error('jk');
await data.flush(); await logger.flush();
final String content = fs.file('log.txt').readAsStringSync(); final String content = fs.file('log.txt').readAsStringSync();
expect(content, contains('ERROR jk')); expect(content, contains('ERROR jk'));
expect(content, contains('INFO cdf')); expect(content, contains('INFO cdf'));