OLD | NEW |
---|---|
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 /// Message logging. | 5 /// Message logging. |
6 library pub.log; | 6 library pub.log; |
7 | 7 |
8 import 'dart:async'; | 8 import 'dart:async'; |
9 import 'dart:convert'; | 9 import 'dart:convert'; |
10 import 'dart:io'; | 10 import 'dart:io'; |
(...skipping 24 matching lines...) Expand all Loading... | |
35 /// | 35 /// |
36 /// This can occur when the backtracking solver stumbles into a pathological | 36 /// This can occur when the backtracking solver stumbles into a pathological |
37 /// dependency graph. It generally will find a solution, but it may log | 37 /// dependency graph. It generally will find a solution, but it may log |
38 /// thousands and thousands of entries to get there. | 38 /// thousands and thousands of entries to get there. |
39 const _MAX_TRANSCRIPT = 10000; | 39 const _MAX_TRANSCRIPT = 10000; |
40 | 40 |
41 /// The list of recorded log messages. Will only be recorded if | 41 /// The list of recorded log messages. Will only be recorded if |
42 /// [recordTranscript()] is called. | 42 /// [recordTranscript()] is called. |
43 Transcript<Entry> _transcript; | 43 Transcript<Entry> _transcript; |
44 | 44 |
45 /// All currently-running progress indicators. | |
46 final _progresses = new Set<Progress>(); | |
47 | |
48 /// The currently-animated progress indicator, if any. | 45 /// The currently-animated progress indicator, if any. |
49 /// | 46 /// |
50 /// This will also be in [_progresses]. | 47 /// This will also be in [_progresses]. |
51 Progress _animatedProgress; | 48 Progress _animatedProgress; |
52 | 49 |
53 final _cyan = getSpecial('\u001b[36m'); | 50 final _cyan = getSpecial('\u001b[36m'); |
54 final _green = getSpecial('\u001b[32m'); | 51 final _green = getSpecial('\u001b[32m'); |
55 final _magenta = getSpecial('\u001b[35m'); | 52 final _magenta = getSpecial('\u001b[35m'); |
56 final _red = getSpecial('\u001b[31m'); | 53 final _red = getSpecial('\u001b[31m'); |
57 final _yellow = getSpecial('\u001b[33m'); | 54 final _yellow = getSpecial('\u001b[33m'); |
(...skipping 309 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
367 | 364 |
368 /// Prints [message] then displays an updated elapsed time until the future | 365 /// Prints [message] then displays an updated elapsed time until the future |
369 /// returned by [callback] completes. | 366 /// returned by [callback] completes. |
370 /// | 367 /// |
371 /// If anything else is logged during this (including another call to | 368 /// If anything else is logged during this (including another call to |
372 /// [progress]) that cancels the progress animation, although the total time | 369 /// [progress]) that cancels the progress animation, although the total time |
373 /// will still be printed once it finishes. If [fine] is passed, the progress | 370 /// will still be printed once it finishes. If [fine] is passed, the progress |
374 /// information will only be visible at [Level.FINE]. | 371 /// information will only be visible at [Level.FINE]. |
375 Future progress(String message, Future callback(), {bool fine: false}) { | 372 Future progress(String message, Future callback(), {bool fine: false}) { |
376 _stopProgress(); | 373 _stopProgress(); |
374 | |
377 var progress = new Progress(message, fine: fine); | 375 var progress = new Progress(message, fine: fine); |
378 _animatedProgress = progress; | 376 _animatedProgress = progress; |
379 _progresses.add(progress); | 377 return callback().whenComplete(progress.stop); |
380 return callback().whenComplete(() { | |
381 progress.stop(); | |
382 _progresses.remove(progress); | |
383 }); | |
384 } | 378 } |
385 | 379 |
386 /// Stops animating the running progress indicator, if currently running. | 380 /// Stops animating the running progress indicator, if currently running. |
387 void _stopProgress() { | 381 void _stopProgress() { |
388 if (_animatedProgress != null) _animatedProgress.stopAnimating(); | 382 if (_animatedProgress != null) _animatedProgress.stopAnimating(); |
389 _animatedProgress = null; | 383 _animatedProgress = null; |
390 } | 384 } |
391 | 385 |
386 /// The number of outstanding calls to [muteProgress] that have not been unmuted | |
387 /// yet. | |
388 int _numMutes = 0; | |
389 | |
390 /// Whether progress animation should be muted or not. | |
391 bool get isMuted => _numMutes > 0; | |
392 | |
393 /// Stops animating any ongoing progress. | |
394 /// | |
395 /// This is called before spawning Git since Git sometimes writes directly to | |
396 /// the terminal to ask for login credentials, which would then get overwritten | |
397 /// by the progress animation. | |
398 /// | |
399 /// Each call to this must be paired with a call to [unmuteProgress]. | |
400 void muteProgress() { | |
401 _numMutes++; | |
402 } | |
403 | |
404 /// Resumes animating any ongoing progress once all calls to [muteProgress] | |
405 /// have made their matching [unmuteProgress]. | |
406 void unmuteProgress() { | |
407 _numMutes--; | |
Jacob
2015/01/08 23:46:25
nit:
consider adding
assert(_numMutes > 0);
Bob Nystrom
2015/01/08 23:58:19
Done.
| |
408 } | |
409 | |
392 /// Wraps [text] in the ANSI escape codes to make it bold when on a platform | 410 /// Wraps [text] in the ANSI escape codes to make it bold when on a platform |
393 /// that supports that. | 411 /// that supports that. |
394 /// | 412 /// |
395 /// Use this to highlight the most important piece of a long chunk of text. | 413 /// Use this to highlight the most important piece of a long chunk of text. |
396 /// | 414 /// |
397 /// This is disabled under [withPrejudice] since all text is bold with | 415 /// This is disabled under [withPrejudice] since all text is bold with |
398 /// prejudice. | 416 /// prejudice. |
399 String bold(text) => withPrejudice ? text : "$_bold$text$_none"; | 417 String bold(text) => withPrejudice ? text : "$_bold$text$_none"; |
400 | 418 |
401 /// Wraps [text] in the ANSI escape codes to make it gray when on a platform | 419 /// Wraps [text] in the ANSI escape codes to make it gray when on a platform |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
518 this.message(errorJson); | 536 this.message(errorJson); |
519 } | 537 } |
520 | 538 |
521 /// Encodes [message] to JSON and prints it if JSON output is enabled. | 539 /// Encodes [message] to JSON and prints it if JSON output is enabled. |
522 void message(message) { | 540 void message(message) { |
523 if (!enabled) return; | 541 if (!enabled) return; |
524 | 542 |
525 print(JSON.encode(message)); | 543 print(JSON.encode(message)); |
526 } | 544 } |
527 } | 545 } |
OLD | NEW |