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'; |
11 | 11 |
12 import 'package:path/path.dart' as p; | 12 import 'package:path/path.dart' as p; |
13 import 'package:source_maps/source_maps.dart'; | |
14 import 'package:source_span/source_span.dart'; | 13 import 'package:source_span/source_span.dart'; |
15 import 'package:stack_trace/stack_trace.dart'; | 14 import 'package:stack_trace/stack_trace.dart'; |
16 | 15 |
17 import 'exceptions.dart'; | 16 import 'exceptions.dart'; |
18 import 'io.dart'; | 17 import 'io.dart'; |
19 import 'progress.dart'; | 18 import 'progress.dart'; |
20 import 'transcript.dart'; | 19 import 'transcript.dart'; |
21 import 'utils.dart'; | 20 import 'utils.dart'; |
22 | 21 |
23 /// The singleton instance so that we can have a nice api like: | 22 /// The singleton instance so that we can have a nice api like: |
(...skipping 285 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
309 } | 308 } |
310 | 309 |
311 /// Logs an exception. | 310 /// Logs an exception. |
312 void exception(exception, [StackTrace trace]) { | 311 void exception(exception, [StackTrace trace]) { |
313 if (exception is SilentException) return; | 312 if (exception is SilentException) return; |
314 | 313 |
315 var chain = trace == null ? new Chain.current() : new Chain.forTrace(trace); | 314 var chain = trace == null ? new Chain.current() : new Chain.forTrace(trace); |
316 | 315 |
317 // This is basically the top-level exception handler so that we don't | 316 // This is basically the top-level exception handler so that we don't |
318 // spew a stack trace on our users. | 317 // spew a stack trace on our users. |
319 if (exception is SpanException) { | 318 if (exception is SourceSpanException) { |
320 error(exception.toString(useColors: canUseSpecialChars)); | |
321 } else if (exception is SourceSpanException) { | |
322 error(exception.toString(color: canUseSpecialChars)); | 319 error(exception.toString(color: canUseSpecialChars)); |
323 } else { | 320 } else { |
324 error(getErrorMessage(exception)); | 321 error(getErrorMessage(exception)); |
325 } | 322 } |
326 fine("Exception type: ${exception.runtimeType}"); | 323 fine("Exception type: ${exception.runtimeType}"); |
327 | 324 |
328 if (json.enabled) { | 325 if (json.enabled) { |
329 if (exception is UsageException) { | 326 if (exception is UsageException) { |
330 // Don't print usage info in JSON output. | 327 // Don't print usage info in JSON output. |
331 json.error(exception.message); | 328 json.error(exception.message); |
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
503 /// Always prints to stdout. | 500 /// Always prints to stdout. |
504 void error(error, [stackTrace]) { | 501 void error(error, [stackTrace]) { |
505 var errorJson = {"error": error.toString()}; | 502 var errorJson = {"error": error.toString()}; |
506 | 503 |
507 if (stackTrace == null && error is Error) stackTrace = error.stackTrace; | 504 if (stackTrace == null && error is Error) stackTrace = error.stackTrace; |
508 if (stackTrace != null) { | 505 if (stackTrace != null) { |
509 errorJson["stackTrace"] = new Chain.forTrace(stackTrace).toString(); | 506 errorJson["stackTrace"] = new Chain.forTrace(stackTrace).toString(); |
510 } | 507 } |
511 | 508 |
512 // If the error came from a file, include the path. | 509 // If the error came from a file, include the path. |
513 if ((error is SpanException || error is SourceSpanException) && | 510 if (error is SourceSpanException && error.span.sourceUrl != null) { |
514 error.span.sourceUrl != null) { | |
515 errorJson["path"] = p.fromUri(error.span.sourceUrl); | 511 errorJson["path"] = p.fromUri(error.span.sourceUrl); |
516 } | 512 } |
517 | 513 |
518 if (error is FileException) { | 514 if (error is FileException) { |
519 errorJson["path"] = error.path; | 515 errorJson["path"] = error.path; |
520 } | 516 } |
521 | 517 |
522 this.message(errorJson); | 518 this.message(errorJson); |
523 } | 519 } |
524 | 520 |
525 /// Encodes [message] to JSON and prints it if JSON output is enabled. | 521 /// Encodes [message] to JSON and prints it if JSON output is enabled. |
526 void message(message) { | 522 void message(message) { |
527 if (!enabled) return; | 523 if (!enabled) return; |
528 | 524 |
529 print(JSON.encode(message)); | 525 print(JSON.encode(message)); |
530 } | 526 } |
531 } | 527 } |
OLD | NEW |