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 csslib.src.messages; | 5 library csslib.src.messages; |
6 | 6 |
7 import 'package:logging/logging.dart' show Level; | 7 import 'package:logging/logging.dart' show Level; |
8 import 'package:source_maps/span.dart' show Span; | 8 import 'package:source_span/source_span.dart'; |
9 | 9 |
10 import 'options.dart'; | 10 import 'options.dart'; |
11 | 11 |
12 // TODO(terry): Remove the global messages, use some object that tracks | 12 // TODO(terry): Remove the global messages, use some object that tracks |
13 // compilation state. | 13 // compilation state. |
14 | 14 |
15 /** The global [Messages] for tracking info/warnings/messages. */ | 15 /** The global [Messages] for tracking info/warnings/messages. */ |
16 Messages messages; | 16 Messages messages; |
17 | 17 |
18 // Color constants used for generating messages. | 18 // Color constants used for generating messages. |
(...skipping 17 matching lines...) Expand all Loading... |
36 labels[Level.SEVERE] = 'error'; | 36 labels[Level.SEVERE] = 'error'; |
37 labels[Level.WARNING] = 'warning'; | 37 labels[Level.WARNING] = 'warning'; |
38 labels[Level.INFO] = 'info'; | 38 labels[Level.INFO] = 'info'; |
39 return labels; | 39 return labels; |
40 })(); | 40 })(); |
41 | 41 |
42 /** A single message from the compiler. */ | 42 /** A single message from the compiler. */ |
43 class Message { | 43 class Message { |
44 final Level level; | 44 final Level level; |
45 final String message; | 45 final String message; |
46 final Span span; | 46 final SourceSpan span; |
47 final bool useColors; | 47 final bool useColors; |
48 | 48 |
49 Message(this.level, this.message, {Span span, bool useColors: false}) | 49 Message(this.level, this.message, {SourceSpan span, bool useColors: false}) |
50 : this.span = span, this.useColors = useColors; | 50 : this.span = span, this.useColors = useColors; |
51 | 51 |
52 String toString() { | 52 String toString() { |
53 var output = new StringBuffer(); | 53 var output = new StringBuffer(); |
54 bool colors = useColors && _ERROR_COLORS.containsKey(level); | 54 bool colors = useColors && _ERROR_COLORS.containsKey(level); |
55 var levelColor = _ERROR_COLORS[level]; | 55 var levelColor = colors ? _ERROR_COLORS[level] : null; |
56 if (colors) output.write(levelColor); | 56 if (colors) output.write(levelColor); |
57 output..write(_ERROR_LABEL[level])..write(' '); | 57 output..write(_ERROR_LABEL[level])..write(' '); |
58 if (colors) output.write(NO_COLOR); | 58 if (colors) output.write(NO_COLOR); |
59 | 59 |
60 if (span == null) { | 60 if (span == null) { |
61 output.write(message); | 61 output.write(message); |
62 } else { | 62 } else { |
63 output.write('on '); | 63 output.write('on '); |
64 output.write(span.getLocationMessage(message, useColors: colors, | 64 output.write(span.message(message, color: levelColor)); |
65 color: levelColor)); | |
66 } | 65 } |
67 | 66 |
68 return output.toString(); | 67 return output.toString(); |
69 } | 68 } |
70 } | 69 } |
71 | 70 |
72 typedef void PrintHandler(Message obj); | 71 typedef void PrintHandler(Message obj); |
73 | 72 |
74 /** | 73 /** |
75 * This class tracks and prints information, warnings, and errors emitted by the | 74 * This class tracks and prints information, warnings, and errors emitted by the |
76 * compiler. | 75 * compiler. |
77 */ | 76 */ |
78 class Messages { | 77 class Messages { |
79 /** Called on every error. Set to blank function to supress printing. */ | 78 /** Called on every error. Set to blank function to supress printing. */ |
80 final PrintHandler printHandler; | 79 final PrintHandler printHandler; |
81 | 80 |
82 final PreprocessorOptions options; | 81 final PreprocessorOptions options; |
83 | 82 |
84 final List<Message> messages = <Message>[]; | 83 final List<Message> messages = <Message>[]; |
85 | 84 |
86 Messages({PreprocessorOptions options, this.printHandler: print}) | 85 Messages({PreprocessorOptions options, this.printHandler: print}) |
87 : options = options != null ? options : new PreprocessorOptions(); | 86 : options = options != null ? options : new PreprocessorOptions(); |
88 | 87 |
89 /** Report a compile-time CSS error. */ | 88 /** Report a compile-time CSS error. */ |
90 void error(String message, Span span) { | 89 void error(String message, SourceSpan span) { |
91 var msg = new Message(Level.SEVERE, message, span: span, | 90 var msg = new Message(Level.SEVERE, message, span: span, |
92 useColors: options.useColors); | 91 useColors: options.useColors); |
93 | 92 |
94 messages.add(msg); | 93 messages.add(msg); |
95 | 94 |
96 printHandler(msg); | 95 printHandler(msg); |
97 } | 96 } |
98 | 97 |
99 /** Report a compile-time CSS warning. */ | 98 /** Report a compile-time CSS warning. */ |
100 void warning(String message, Span span) { | 99 void warning(String message, SourceSpan span) { |
101 if (options.warningsAsErrors) { | 100 if (options.warningsAsErrors) { |
102 error(message, span); | 101 error(message, span); |
103 } else { | 102 } else { |
104 var msg = new Message(Level.WARNING, message, span: span, | 103 var msg = new Message(Level.WARNING, message, span: span, |
105 useColors: options.useColors); | 104 useColors: options.useColors); |
106 | 105 |
107 messages.add(msg); | 106 messages.add(msg); |
108 } | 107 } |
109 } | 108 } |
110 | 109 |
111 /** Report and informational message about what the compiler is doing. */ | 110 /** Report and informational message about what the compiler is doing. */ |
112 void info(String message, Span span) { | 111 void info(String message, SourceSpan span) { |
113 var msg = new Message(Level.INFO, message, span: span, | 112 var msg = new Message(Level.INFO, message, span: span, |
114 useColors: options.useColors); | 113 useColors: options.useColors); |
115 | 114 |
116 messages.add(msg); | 115 messages.add(msg); |
117 | 116 |
118 if (options.verbose) printHandler(msg); | 117 if (options.verbose) printHandler(msg); |
119 } | 118 } |
120 | 119 |
121 /** Merge [newMessages] to this message lsit. */ | 120 /** Merge [newMessages] to this message lsit. */ |
122 void mergeMessages(Messages newMessages) { | 121 void mergeMessages(Messages newMessages) { |
123 messages.addAll(newMessages.messages); | 122 messages.addAll(newMessages.messages); |
124 newMessages.messages.where((message) => | 123 newMessages.messages.where((message) => |
125 message.level.value == Level.SEVERE || options.verbose) | 124 message.level.value == Level.SEVERE || options.verbose) |
126 .forEach((message) { printHandler(message); }); | 125 .forEach((message) { printHandler(message); }); |
127 } | 126 } |
128 } | 127 } |
OLD | NEW |