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 library messages; | 5 library messages; |
6 | 6 |
7 import 'dart:json' as json; | 7 import 'dart:json' as json; |
8 | 8 |
| 9 import 'package:barback/barback.dart' show TransformLogger; |
9 import 'package:source_maps/span.dart' show Span; | 10 import 'package:source_maps/span.dart' show Span; |
10 import 'package:logging/logging.dart' show Level; | 11 import 'package:logging/logging.dart' show Level; |
11 | 12 |
12 import 'compiler_options.dart'; | 13 import 'compiler_options.dart'; |
13 import 'utils.dart'; | 14 import 'utils.dart'; |
14 | 15 |
15 /** Map between error levels and their display color. */ | 16 /** Map between error levels and their display color. */ |
16 final Map<Level, String> _ERROR_COLORS = (() { | 17 final Map<Level, String> _ERROR_COLORS = (() { |
17 var colorsMap = new Map<Level, String>(); | 18 var colorsMap = new Map<Level, String>(); |
18 colorsMap[Level.SEVERE] = RED_COLOR; | 19 colorsMap[Level.SEVERE] = RED_COLOR; |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
63 'charEnd': span.end.offset, | 64 'charEnd': span.end.offset, |
64 } | 65 } |
65 }]); | 66 }]); |
66 } | 67 } |
67 } | 68 } |
68 | 69 |
69 /** | 70 /** |
70 * This class tracks and prints information, warnings, and errors emitted by the | 71 * This class tracks and prints information, warnings, and errors emitted by the |
71 * compiler. | 72 * compiler. |
72 */ | 73 */ |
73 class Messages { | 74 class Messages implements TransformLogger { |
74 final CompilerOptions options; | 75 final CompilerOptions options; |
75 final bool shouldPrint; | 76 final bool shouldPrint; |
76 | 77 |
77 final List<Message> messages = <Message>[]; | 78 final List<Message> messages = <Message>[]; |
78 | 79 |
79 Messages({CompilerOptions options, this.shouldPrint: true}) | 80 Messages({CompilerOptions options, this.shouldPrint: true}) |
80 : options = options != null ? options : new CompilerOptions(); | 81 : options = options != null ? options : new CompilerOptions(); |
81 | 82 |
82 /** | 83 /** |
83 * Creates a new instance of [Messages] which doesn't write messages to | 84 * Creates a new instance of [Messages] which doesn't write messages to |
(...skipping 10 matching lines...) Expand all Loading... |
94 // Convenience methods for testing | 95 // Convenience methods for testing |
95 int get length => messages.length; | 96 int get length => messages.length; |
96 | 97 |
97 Message operator[](int index) => messages[index]; | 98 Message operator[](int index) => messages[index]; |
98 | 99 |
99 void clear() { | 100 void clear() { |
100 messages.clear(); | 101 messages.clear(); |
101 } | 102 } |
102 | 103 |
103 /** [message] is considered a static compile-time error by the Dart lang. */ | 104 /** [message] is considered a static compile-time error by the Dart lang. */ |
104 void error(String message, Span span) { | 105 void error(String message, [Span span]) { |
105 var msg = new Message(Level.SEVERE, message, span: span, | 106 var msg = new Message(Level.SEVERE, message, span: span, |
106 useColors: options.useColors); | 107 useColors: options.useColors); |
107 | 108 |
108 messages.add(msg); | 109 messages.add(msg); |
109 printMessage(msg); | 110 printMessage(msg); |
110 } | 111 } |
111 | 112 |
112 /** [message] is considered a type warning by the Dart lang. */ | 113 /** [message] is considered a type warning by the Dart lang. */ |
113 void warning(String message, Span span) { | 114 void warning(String message, [Span span]) { |
114 if (options.warningsAsErrors) { | 115 if (options.warningsAsErrors) { |
115 error(message, span); | 116 error(message, span); |
116 } else { | 117 } else { |
117 var msg = new Message(Level.WARNING, message, | 118 var msg = new Message(Level.WARNING, message, |
118 span: span, useColors: options.useColors); | 119 span: span, useColors: options.useColors); |
119 | 120 |
120 messages.add(msg); | 121 messages.add(msg); |
121 printMessage(msg); | 122 printMessage(msg); |
122 } | 123 } |
123 } | 124 } |
124 | 125 |
125 /// the list of error messages. Empty list, if there are no error messages. | 126 /// the list of error messages. Empty list, if there are no error messages. |
126 List<Message> get errors => | 127 List<Message> get errors => |
127 messages.where((m) => m.level == Level.SEVERE).toList(); | 128 messages.where((m) => m.level == Level.SEVERE).toList(); |
128 | 129 |
129 /// the list of warning messages. Empty list if there are no warning messages. | 130 /// the list of warning messages. Empty list if there are no warning messages. |
130 List<Message> get warnings => | 131 List<Message> get warnings => |
131 messages.where((m) => m.level == Level.WARNING).toList(); | 132 messages.where((m) => m.level == Level.WARNING).toList(); |
132 | 133 |
133 /** | 134 /** |
134 * [message] at [span] will tell the user about what the compiler | 135 * [message] at [span] will tell the user about what the compiler |
135 * is doing. | 136 * is doing. |
136 */ | 137 */ |
137 void info(String message, Span span) { | 138 void info(String message, [Span span]) { |
138 var msg = new Message(Level.INFO, message, span: span, | 139 var msg = new Message(Level.INFO, message, span: span, |
139 useColors: options.useColors); | 140 useColors: options.useColors); |
140 | 141 |
141 messages.add(msg); | 142 messages.add(msg); |
142 if (options.verbose) printMessage(msg); | 143 if (options.verbose) printMessage(msg); |
143 } | 144 } |
144 | 145 |
145 void printMessage(msg) { | 146 void printMessage(msg) { |
146 if (shouldPrint) print(options.jsonFormat ? msg.toJson() : msg); | 147 if (shouldPrint) print(options.jsonFormat ? msg.toJson() : msg); |
147 } | 148 } |
148 } | 149 } |
OLD | NEW |