Commit 022cb2d9 authored by Jason Simmons's avatar Jason Simmons Committed by GitHub

Do not buffer logs in the verbose logger (#6465)

With the old policy the most recent log would not be printed until the next
log is produced (which may be indefinitely).  This change prints logs
immediately along with a time delta since the previous log.
parent 379b1030
......@@ -206,9 +206,6 @@ Future<Null> _exit(int code) async {
// Run shutdown hooks before flushing logs
await runShutdownHooks();
// Write any buffered output.
logger.flush();
// Give the task / timer queue one cycle through before we hard exit.
Timer.run(() {
printTrace('exiting with code $code');
......
......@@ -34,9 +34,6 @@ abstract class Logger {
/// Start an indeterminate progress display.
Status startProgress(String message);
/// Flush any buffered output.
void flush() { }
}
class Status {
......@@ -87,9 +84,6 @@ class StdoutLogger extends Logger {
return new Status();
}
}
@override
void flush() { }
}
class BufferLogger extends Logger {
......@@ -123,34 +117,31 @@ class BufferLogger extends Logger {
printStatus(message);
return new Status();
}
@override
void flush() { }
}
class VerboseLogger extends Logger {
_LogMessage lastMessage;
Stopwatch stopwatch = new Stopwatch();
VerboseLogger() {
stopwatch.start();
}
@override
bool get isVerbose => true;
@override
void printError(String message, [StackTrace stackTrace]) {
_emit();
lastMessage = new _LogMessage(_LogType.error, message, stackTrace);
_emit(_LogType.error, message, stackTrace);
}
@override
void printStatus(String message, { bool emphasis: false, bool newline: true }) {
// TODO(ianh): We ignore newline and emphasis here.
_emit();
lastMessage = new _LogMessage(_LogType.status, message);
_emit(_LogType.status, message);
}
@override
void printTrace(String message) {
_emit();
lastMessage = new _LogMessage(_LogType.trace, message);
_emit(_LogType.trace, message);
}
@override
......@@ -159,40 +150,25 @@ class VerboseLogger extends Logger {
return new Status();
}
@override
void flush() => _emit();
void _emit() {
lastMessage?.emit();
lastMessage = null;
}
}
enum _LogType {
error,
status,
trace
}
class _LogMessage {
_LogMessage(this.type, this.message, [this.stackTrace]) {
stopwatch.start();
}
final _LogType type;
final String message;
final StackTrace stackTrace;
void _emit(_LogType type, String message, [StackTrace stackTrace]) {
if (message.trim().isEmpty)
return;
Stopwatch stopwatch = new Stopwatch();
int millis = stopwatch.elapsedMilliseconds;
stopwatch.reset();
void emit() {
stopwatch.stop();
String prefix;
const int prefixWidth = 8;
if (millis == 0) {
prefix = ''.padLeft(prefixWidth);
} else {
prefix = '+$millis ms'.padLeft(prefixWidth);
if (millis >= 100)
prefix = terminal.writeBold(prefix);
}
prefix = '[$prefix] ';
int millis = stopwatch.elapsedMilliseconds;
String prefix = '${millis.toString().padLeft(4)} ms • ';
String indent = ''.padLeft(prefix.length);
if (millis >= 100)
prefix = terminal.writeBold(prefix.substring(0, prefix.length - 3)) + ' • ';
String indentMessage = message.replaceAll('\n', '\n$indent');
if (type == _LogType.error) {
......@@ -207,6 +183,12 @@ class _LogMessage {
}
}
enum _LogType {
error,
status,
trace
}
class AnsiTerminal {
AnsiTerminal() {
// TODO(devoncarew): This detection does not work for Windows.
......
......@@ -415,9 +415,6 @@ class DevFS {
await _operations.writeSource(fsName, '.packages', sb.toString());
printTrace('DevFS: Sync finished');
// NB: You must call flush after a printTrace if you want to be printed
// immediately.
logger.flush();
}
void _scanFile(String devicePath, FileSystemEntity file) {
......
......@@ -129,11 +129,7 @@ class FlutterCommandRunner extends CommandRunner {
if (args.length == 1 && args.first == 'build')
args = <String>['build', '-h'];
return super.run(args).then((dynamic result) {
return result;
}).whenComplete(() {
logger.flush();
});
return super.run(args);
}
@override
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment