OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library messages; |
| 6 |
| 7 import 'dart:json' as json; |
| 8 |
| 9 import 'package:barback/barback.dart' show TransformLogger; |
| 10 import 'package:source_maps/span.dart' show Span; |
| 11 import 'package:logging/logging.dart' show Level; |
| 12 |
| 13 import 'compiler_options.dart'; |
| 14 import 'utils.dart'; |
| 15 |
| 16 /** Map between error levels and their display color. */ |
| 17 final Map<Level, String> _ERROR_COLORS = (() { |
| 18 var colorsMap = new Map<Level, String>(); |
| 19 colorsMap[Level.SEVERE] = RED_COLOR; |
| 20 colorsMap[Level.WARNING] = MAGENTA_COLOR; |
| 21 colorsMap[Level.INFO] = GREEN_COLOR; |
| 22 return colorsMap; |
| 23 })(); |
| 24 |
| 25 /** A single message from the compiler. */ |
| 26 class Message { |
| 27 final Level level; |
| 28 final String message; |
| 29 final Span span; |
| 30 final bool useColors; |
| 31 |
| 32 Message(this.level, this.message, {this.span, this.useColors: false}); |
| 33 |
| 34 String get kind => level == Level.SEVERE ? 'error' : |
| 35 (level == Level.WARNING ? 'warning' : 'info'); |
| 36 |
| 37 String toString() { |
| 38 var output = new StringBuffer(); |
| 39 bool colors = useColors && _ERROR_COLORS.containsKey(level); |
| 40 var levelColor = _ERROR_COLORS[level]; |
| 41 if (colors) output.write(levelColor); |
| 42 output..write(kind)..write(' '); |
| 43 if (colors) output.write(NO_COLOR); |
| 44 |
| 45 if (span == null) { |
| 46 output.write(message); |
| 47 } else { |
| 48 output.write(span.getLocationMessage(message, useColors: colors, |
| 49 color: levelColor)); |
| 50 } |
| 51 |
| 52 return output.toString(); |
| 53 } |
| 54 |
| 55 String toJson() { |
| 56 if (span == null) return toString(); |
| 57 return json.stringify([{ |
| 58 'method': kind, |
| 59 'params': { |
| 60 'file': span.sourceUrl, |
| 61 'message': message, |
| 62 'line': span.start.line + 1, |
| 63 'charStart': span.start.offset, |
| 64 'charEnd': span.end.offset, |
| 65 } |
| 66 }]); |
| 67 } |
| 68 } |
| 69 |
| 70 /** |
| 71 * This class tracks and prints information, warnings, and errors emitted by the |
| 72 * compiler. |
| 73 */ |
| 74 class Messages implements TransformLogger { |
| 75 final CompilerOptions options; |
| 76 final bool shouldPrint; |
| 77 |
| 78 final List<Message> messages = <Message>[]; |
| 79 |
| 80 Messages({CompilerOptions options, this.shouldPrint: true}) |
| 81 : options = options != null ? options : new CompilerOptions(); |
| 82 |
| 83 /** |
| 84 * Creates a new instance of [Messages] which doesn't write messages to |
| 85 * the console. |
| 86 */ |
| 87 Messages.silent(): this(shouldPrint: false); |
| 88 |
| 89 /** |
| 90 * True if we have an error that prevents correct codegen. |
| 91 * For example, if we failed to read an input file. |
| 92 */ |
| 93 bool get hasErrors => messages.any((m) => m.level == Level.SEVERE); |
| 94 |
| 95 // Convenience methods for testing |
| 96 int get length => messages.length; |
| 97 |
| 98 Message operator[](int index) => messages[index]; |
| 99 |
| 100 void clear() { |
| 101 messages.clear(); |
| 102 } |
| 103 |
| 104 /** [message] is considered a static compile-time error by the Dart lang. */ |
| 105 void error(String message, [Span span]) { |
| 106 var msg = new Message(Level.SEVERE, message, span: span, |
| 107 useColors: options.useColors); |
| 108 |
| 109 messages.add(msg); |
| 110 printMessage(msg); |
| 111 } |
| 112 |
| 113 /** [message] is considered a type warning by the Dart lang. */ |
| 114 void warning(String message, [Span span]) { |
| 115 if (options.warningsAsErrors) { |
| 116 error(message, span); |
| 117 } else { |
| 118 var msg = new Message(Level.WARNING, message, |
| 119 span: span, useColors: options.useColors); |
| 120 |
| 121 messages.add(msg); |
| 122 printMessage(msg); |
| 123 } |
| 124 } |
| 125 |
| 126 /// the list of error messages. Empty list, if there are no error messages. |
| 127 List<Message> get errors => |
| 128 messages.where((m) => m.level == Level.SEVERE).toList(); |
| 129 |
| 130 /// the list of warning messages. Empty list if there are no warning messages. |
| 131 List<Message> get warnings => |
| 132 messages.where((m) => m.level == Level.WARNING).toList(); |
| 133 |
| 134 /** |
| 135 * [message] at [span] will tell the user about what the compiler |
| 136 * is doing. |
| 137 */ |
| 138 void info(String message, [Span span]) { |
| 139 var msg = new Message(Level.INFO, message, span: span, |
| 140 useColors: options.useColors); |
| 141 |
| 142 messages.add(msg); |
| 143 if (options.verbose) printMessage(msg); |
| 144 } |
| 145 |
| 146 void printMessage(msg) { |
| 147 if (shouldPrint) print(options.jsonFormat ? msg.toJson() : msg); |
| 148 } |
| 149 } |
OLD | NEW |