From a106370d91b289b8d40b4ed2496c5295e232cf61 Mon Sep 17 00:00:00 2001 From: Devon Carew Date: Mon, 14 May 2018 16:17:58 -0700 Subject: [PATCH 1/3] change the verbose logging output --- .travis.yml | 6 ++ CHANGELOG.md | 6 ++ example/main.dart | 1 + lib/cli_logging.dart | 135 ++++++++++++++++++++++++------------------- pubspec.yaml | 2 +- tool/travis.sh | 3 + 6 files changed, 93 insertions(+), 60 deletions(-) diff --git a/.travis.yml b/.travis.yml index 7fe752e..3c087ca 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,4 +1,10 @@ language: dart +dart: + - dev script: ./tool/travis.sh dist: trusty sudo: false + +branches: + only: + - master diff --git a/CHANGELOG.md b/CHANGELOG.md index 372cddd..338c92c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +## 0.1.3 +- In verbose mode, instead of printing the diff from the last log message, + print the total time since the tool started +- Change to not buffer the last log message sent in verbose logging mode +- Expose more classes from the logging library + ## 0.1.2+1 - Remove unneeded change to Dart SDK constraint. diff --git a/example/main.dart b/example/main.dart index 0cc6846..afafdc3 100644 --- a/example/main.dart +++ b/example/main.dart @@ -21,5 +21,6 @@ main(List args) async { progress.finish(showTiming: true); logger.stdout('All ${logger.ansi.emphasized('done')}.'); + // ignore: deprecated_member_use logger.flush(); } diff --git a/lib/cli_logging.dart b/lib/cli_logging.dart index 2da62ca..5392485 100644 --- a/lib/cli_logging.dart +++ b/lib/cli_logging.dart @@ -23,12 +23,19 @@ class Ansi { Ansi(this.useAnsi); String get cyan => _code('\u001b[36m'); + String get green => _code('\u001b[32m'); + String get magenta => _code('\u001b[35m'); + String get red => _code('\u001b[31m'); + String get yellow => _code('\u001b[33m'); + String get blue => _code('\u001b[34m'); + String get gray => _code('\u001b[1;30m'); + String get noColor => _code('\u001b[39m'); String get none => _code('\u001b[0m'); @@ -55,10 +62,14 @@ class Ansi { /// standard status messages, trace level output, and indeterminate progress. abstract class Logger { /// Create a normal [Logger]; this logger will not display trace level output. - factory Logger.standard({Ansi ansi}) => new _StandardLogger(ansi: ansi); + factory Logger.standard({Ansi ansi}) => new StandardLogger(ansi: ansi); /// Create a [Logger] that will display trace level output. - factory Logger.verbose({Ansi ansi}) => new _VerboseLogger(ansi: ansi); + /// + /// If [logTime] is `true`, this logger will display the time of the message. + factory Logger.verbose({Ansi ansi, bool logTime: true}) { + return new VerboseLogger(ansi: ansi, logTime: logTime); + } Ansi get ansi; @@ -75,9 +86,9 @@ abstract class Logger { /// Start an indeterminate progress display. Progress progress(String message); - void _progressFinished(Progress progress); /// Flush any un-written output. + @Deprecated('This method will be removed in the future') void flush(); } @@ -86,7 +97,7 @@ abstract class Progress { final String message; final Stopwatch _stopwatch; - Progress._(this.message) : _stopwatch = new Stopwatch()..start(); + Progress(this.message) : _stopwatch = new Stopwatch()..start(); Duration get elapsed => _stopwatch.elapsed; @@ -97,10 +108,10 @@ abstract class Progress { void cancel(); } -class _StandardLogger implements Logger { +class StandardLogger implements Logger { Ansi ansi; - _StandardLogger({this.ansi}) { + StandardLogger({this.ansi}) { ansi ??= new Ansi(Ansi.terminalSupportsAnsi); } @@ -109,67 +120,68 @@ class _StandardLogger implements Logger { Progress _currentProgress; void stderr(String message) { + if (_currentProgress != null) { + Progress progress = _currentProgress; + _currentProgress = null; + progress.cancel(); + } + io.stderr.writeln(message); - _currentProgress?.cancel(); - _currentProgress = null; } void stdout(String message) { + if (_currentProgress != null) { + Progress progress = _currentProgress; + _currentProgress = null; + progress.cancel(); + } + print(message); - _currentProgress?.cancel(); - _currentProgress = null; } void trace(String message) {} Progress progress(String message) { - _currentProgress?.cancel(); - _currentProgress = null; + if (_currentProgress != null) { + Progress progress = _currentProgress; + _currentProgress = null; + progress.cancel(); + } Progress progress = ansi.useAnsi - ? new _AnsiProgress(this, ansi, message) - : new _SimpleProgress(this, message); + ? new AnsiProgress(ansi, message) + : new SimpleProgress(this, message); _currentProgress = progress; return progress; } - void _progressFinished(Progress progress) { - if (_currentProgress == progress) { - _currentProgress = null; - } - } - + @Deprecated('This method will be removed in the future') void flush() {} } -class _SimpleProgress extends Progress { +class SimpleProgress extends Progress { final Logger logger; - _SimpleProgress(this.logger, String message) : super._(message) { + SimpleProgress(this.logger, String message) : super(message) { logger.stdout('$message...'); } @override - void cancel() { - logger._progressFinished(this); - } + void cancel() {} @override - void finish({String message, bool showTiming}) { - logger._progressFinished(this); - } + void finish({String message, bool showTiming}) {} } -class _AnsiProgress extends Progress { +class AnsiProgress extends Progress { static const List kAnimationItems = const ['/', '-', '\\', '|']; - final Logger logger; final Ansi ansi; int _index = 0; Timer _timer; - _AnsiProgress(this.logger, this.ansi, String message) : super._(message) { + AnsiProgress(this.ansi, String message) : super(message) { io.stdout.write('${message}... '.padRight(40)); _timer = new Timer.periodic(new Duration(milliseconds: 80), (t) { @@ -185,7 +197,6 @@ class _AnsiProgress extends Progress { if (_timer.isActive) { _timer.cancel(); _updateDisplay(cancelled: true); - logger._progressFinished(this); } } @@ -194,7 +205,6 @@ class _AnsiProgress extends Progress { if (_timer.isActive) { _timer.cancel(); _updateDisplay(isFinal: true, message: message, showTiming: showTiming); - logger._progressFinished(this); } } @@ -222,52 +232,59 @@ class _AnsiProgress extends Progress { } } -class _VerboseLogger implements Logger { +class VerboseLogger implements Logger { Ansi ansi; + bool logTime; Stopwatch _timer; - String _previousErr; - String _previousMsg; - - _VerboseLogger({this.ansi}) { + VerboseLogger({this.ansi, this.logTime}) { ansi ??= new Ansi(Ansi.terminalSupportsAnsi); + logTime ??= false; + _timer = new Stopwatch()..start(); } bool get isVerbose => true; - void stderr(String message) { - flush(); - _previousErr = '${ansi.red}$message${ansi.none}'; + void stdout(String message) { + io.stdout.writeln('${_createPrefix()}$message'); } - void stdout(String message) { - flush(); - _previousMsg = message; + void stderr(String message) { + io.stderr.writeln('${_createPrefix()}${ansi.red}$message${ansi.none}'); } void trace(String message) { - flush(); - _previousMsg = '${ansi.gray}$message${ansi.none}'; + io.stdout.writeln('${_createPrefix()}${ansi.gray}$message${ansi.none}'); } - Progress progress(String message) => new _SimpleProgress(this, message); + Progress progress(String message) => new SimpleProgress(this, message); - void _progressFinished(Progress progress) {} + @Deprecated('This method will be removed in the future') + void flush() {} - void flush() { - if (_previousErr != null) { - io.stderr.writeln('${_createTag()} $_previousErr'); - _previousErr = null; - } else if (_previousMsg != null) { - io.stdout.writeln('${_createTag()} $_previousMsg'); - _previousMsg = null; + String _createPrefix() { + if (!logTime) { + return ''; } - } - String _createTag() { int millis = _timer.elapsedMilliseconds; - _timer.reset(); - return '[${millis.toString().padLeft(4)} ms]'; + int seconds = millis ~/ 1000; + int minutes = seconds ~/ 60; + + millis = millis % 1000; + seconds = seconds % 60; + + StringBuffer buf = new StringBuffer(); + if (minutes > 0) { + buf.write(minutes); + buf.write(':'); + } + + buf.write(seconds.toString().padLeft(minutes > 0 ? 2 : 1, '0')); + buf.write('.'); + buf.write(millis.toString().padLeft(3, '0')); + + return '[${buf.toString().padLeft(9)}] '; } } diff --git a/pubspec.yaml b/pubspec.yaml index d7ee512..d274ae6 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,5 +1,5 @@ name: cli_util -version: 0.1.2+1 +version: 0.1.3 author: Dart Team description: A library to help in building Dart command-line apps. homepage: https://github.com/dart-lang/cli_util diff --git a/tool/travis.sh b/tool/travis.sh index 0b0aef9..59a283c 100755 --- a/tool/travis.sh +++ b/tool/travis.sh @@ -15,6 +15,9 @@ dartanalyzer --fatal-warnings \ # Run the tests. dart test/cli_util_test.dart +# Ensure we can run in Dart 2. +dart --preview-dart-2 example/main.dart + # Install dart_coveralls; gather and send coverage data. if [ "$COVERALLS_TOKEN" ]; then pub global activate dart_coveralls From 8653bd8a110e5ae0420479a5af0d447ae361547a Mon Sep 17 00:00:00 2001 From: Devon Carew Date: Mon, 14 May 2018 16:38:53 -0700 Subject: [PATCH 2/3] update the timestamp message --- lib/cli_logging.dart | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/lib/cli_logging.dart b/lib/cli_logging.dart index 5392485..9646cd6 100644 --- a/lib/cli_logging.dart +++ b/lib/cli_logging.dart @@ -268,23 +268,19 @@ class VerboseLogger implements Logger { return ''; } - int millis = _timer.elapsedMilliseconds; - int seconds = millis ~/ 1000; + double seconds = _timer.elapsedMilliseconds / 1000.0; int minutes = seconds ~/ 60; - - millis = millis % 1000; - seconds = seconds % 60; + seconds -= minutes * 60.0; StringBuffer buf = new StringBuffer(); if (minutes > 0) { - buf.write(minutes); - buf.write(':'); + buf.write((minutes % 60)); + buf.write('m '); } - buf.write(seconds.toString().padLeft(minutes > 0 ? 2 : 1, '0')); - buf.write('.'); - buf.write(millis.toString().padLeft(3, '0')); + buf.write(seconds.toStringAsFixed(3).padLeft(minutes > 0 ? 6 : 1, '0')); + buf.write('s'); - return '[${buf.toString().padLeft(9)}] '; + return '[${buf.toString().padLeft(11)}] '; } } From e838ecb806e917e43d35689372460c226c86f955 Mon Sep 17 00:00:00 2001 From: Devon Carew Date: Wed, 16 May 2018 14:23:42 -0700 Subject: [PATCH 3/3] review comments --- example/main.dart | 2 -- 1 file changed, 2 deletions(-) diff --git a/example/main.dart b/example/main.dart index afafdc3..71c0156 100644 --- a/example/main.dart +++ b/example/main.dart @@ -21,6 +21,4 @@ main(List args) async { progress.finish(showTiming: true); logger.stdout('All ${logger.ansi.emphasized('done')}.'); - // ignore: deprecated_member_use - logger.flush(); }