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:collection'; | 9 import 'dart:collection'; |
10 import 'dart:io'; | 10 import 'dart:io'; |
(...skipping 17 matching lines...) Expand all Loading... |
28 /// [recordTranscript()] is called. | 28 /// [recordTranscript()] is called. |
29 Transcript<Entry> _transcript; | 29 Transcript<Entry> _transcript; |
30 | 30 |
31 /// The timer used to write "..." during a progress log. | 31 /// The timer used to write "..." during a progress log. |
32 Timer _progressTimer; | 32 Timer _progressTimer; |
33 | 33 |
34 /// The progress message as it's being incrementally appended. When the | 34 /// The progress message as it's being incrementally appended. When the |
35 /// progress is done, a single entry will be added to the log for it. | 35 /// progress is done, a single entry will be added to the log for it. |
36 String _progressMessage; | 36 String _progressMessage; |
37 | 37 |
| 38 final _cyan = getSpecial('\u001b[36m'); |
38 final _green = getSpecial('\u001b[32m'); | 39 final _green = getSpecial('\u001b[32m'); |
| 40 final _magenta = getSpecial('\u001b[35m'); |
39 final _red = getSpecial('\u001b[31m'); | 41 final _red = getSpecial('\u001b[31m'); |
40 final _yellow = getSpecial('\u001b[33m'); | 42 final _yellow = getSpecial('\u001b[33m'); |
41 final _none = getSpecial('\u001b[0m'); | 43 final _none = getSpecial('\u001b[0m'); |
| 44 final _bold = getSpecial('\u001b[1m'); |
42 | 45 |
43 /// An enum type for defining the different logging levels. By default, [ERROR] | 46 /// An enum type for defining the different logging levels. By default, [ERROR] |
44 /// and [WARNING] messages are printed to sterr. [MESSAGE] messages are printed | 47 /// and [WARNING] messages are printed to sterr. [MESSAGE] messages are printed |
45 /// to stdout, and others are ignored. | 48 /// to stdout, and others are ignored. |
46 class Level { | 49 class Level { |
47 /// An error occurred and an operation could not be completed. Usually shown | 50 /// An error occurred and an operation could not be completed. Usually shown |
48 /// to the user on stderr. | 51 /// to the user on stderr. |
49 static const ERROR = const Level._("ERR "); | 52 static const ERROR = const Level._("ERR "); |
50 | 53 |
51 /// Something unexpected happened, but the program was able to continue, | 54 /// Something unexpected happened, but the program was able to continue, |
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
211 stdout.write(_progressMessage); | 214 stdout.write(_progressMessage); |
212 | 215 |
213 _progressTimer = new Timer.periodic(new Duration(milliseconds: 500), (_) { | 216 _progressTimer = new Timer.periodic(new Duration(milliseconds: 500), (_) { |
214 stdout.write('.'); | 217 stdout.write('.'); |
215 _progressMessage += '.'; | 218 _progressMessage += '.'; |
216 }); | 219 }); |
217 | 220 |
218 return callback().whenComplete(_stopProgress); | 221 return callback().whenComplete(_stopProgress); |
219 } | 222 } |
220 | 223 |
| 224 /// Wraps [text] in the ANSI escape codes to make it bold when on a platform |
| 225 /// that supports that. |
| 226 /// |
| 227 /// Use this to highlight the most important piece of a long chunk of text. |
| 228 String bold(text) => "$_bold$text$_none"; |
| 229 |
| 230 /// Wraps [text] in the ANSI escape codes to color it cyan when on a platform |
| 231 /// that supports that. |
| 232 /// |
| 233 /// Use this to highlight something interesting but neither good nor bad. |
| 234 String cyan(text) => "$_cyan$text$_none"; |
| 235 |
221 /// Wraps [text] in the ANSI escape codes to color it green when on a platform | 236 /// Wraps [text] in the ANSI escape codes to color it green when on a platform |
222 /// that supports that. | 237 /// that supports that. |
| 238 /// |
| 239 /// Use this to highlight something successful or otherwise positive. |
223 String green(text) => "$_green$text$_none"; | 240 String green(text) => "$_green$text$_none"; |
224 | 241 |
| 242 /// Wraps [text] in the ANSI escape codes to color it magenta when on a |
| 243 /// platform that supports that. |
| 244 /// |
| 245 /// Use this to highlight something risky that the user should be aware of but |
| 246 /// may intend to do. |
| 247 String magenta(text) => "$_magenta$text$_none"; |
| 248 |
225 /// Wraps [text] in the ANSI escape codes to color it red when on a platform | 249 /// Wraps [text] in the ANSI escape codes to color it red when on a platform |
226 /// that supports that. | 250 /// that supports that. |
| 251 /// |
| 252 /// Use this to highlight unequivocal errors, problems, or failures. |
227 String red(text) => "$_red$text$_none"; | 253 String red(text) => "$_red$text$_none"; |
228 | 254 |
229 /// Wraps [text] in the ANSI escape codes to color it yellow when on a platform | 255 /// Wraps [text] in the ANSI escape codes to color it yellow when on a platform |
230 /// that supports that. | 256 /// that supports that. |
| 257 /// |
| 258 /// Use this to highlight warnings, cautions or other things that are bad but |
| 259 /// do not prevent the user's goal from being reached. |
231 String yellow(text) => "$_yellow$text$_none"; | 260 String yellow(text) => "$_yellow$text$_none"; |
232 | 261 |
233 /// Stops the running progress indicator, if currently running. | 262 /// Stops the running progress indicator, if currently running. |
234 _stopProgress() { | 263 _stopProgress() { |
235 if (_progressTimer == null) return; | 264 if (_progressTimer == null) return; |
236 | 265 |
237 // Stop the timer. | 266 // Stop the timer. |
238 _progressTimer.cancel(); | 267 _progressTimer.cancel(); |
239 _progressTimer = null; | 268 _progressTimer = null; |
240 stdout.writeln(); | 269 stdout.writeln(); |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
320 } else { | 349 } else { |
321 sink.write(' | '); | 350 sink.write(' | '); |
322 } | 351 } |
323 } | 352 } |
324 | 353 |
325 sink.writeln(line); | 354 sink.writeln(line); |
326 | 355 |
327 firstLine = false; | 356 firstLine = false; |
328 } | 357 } |
329 } | 358 } |
OLD | NEW |