Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(710)

Side by Side Diff: observatory_pub_packages/csslib/src/messages.dart

Issue 816693004: Add observatory_pub_packages snapshot to third_party (Closed) Base URL: http://dart.googlecode.com/svn/third_party/
Patch Set: Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 csslib.src.messages;
6
7 import 'package:logging/logging.dart' show Level;
8 import 'package:source_span/source_span.dart';
9
10 import 'options.dart';
11
12 // TODO(terry): Remove the global messages, use some object that tracks
13 // compilation state.
14
15 /** The global [Messages] for tracking info/warnings/messages. */
16 Messages messages;
17
18 // Color constants used for generating messages.
19 final String GREEN_COLOR = '\u001b[32m';
20 final String RED_COLOR = '\u001b[31m';
21 final String MAGENTA_COLOR = '\u001b[35m';
22 final String NO_COLOR = '\u001b[0m';
23
24 /** Map between error levels and their display color. */
25 final Map<Level, String> _ERROR_COLORS = (() {
26 var colorsMap = new Map<Level, String>();
27 colorsMap[Level.SEVERE] = RED_COLOR;
28 colorsMap[Level.WARNING] = MAGENTA_COLOR;
29 colorsMap[Level.INFO] = GREEN_COLOR;
30 return colorsMap;
31 })();
32
33 /** Map between error levels and their friendly name. */
34 final Map<Level, String> _ERROR_LABEL = (() {
35 var labels = new Map<Level, String>();
36 labels[Level.SEVERE] = 'error';
37 labels[Level.WARNING] = 'warning';
38 labels[Level.INFO] = 'info';
39 return labels;
40 })();
41
42 /** A single message from the compiler. */
43 class Message {
44 final Level level;
45 final String message;
46 final SourceSpan span;
47 final bool useColors;
48
49 Message(this.level, this.message, {SourceSpan span, bool useColors: false})
50 : this.span = span, this.useColors = useColors;
51
52 String toString() {
53 var output = new StringBuffer();
54 bool colors = useColors && _ERROR_COLORS.containsKey(level);
55 var levelColor = colors ? _ERROR_COLORS[level] : null;
56 if (colors) output.write(levelColor);
57 output..write(_ERROR_LABEL[level])..write(' ');
58 if (colors) output.write(NO_COLOR);
59
60 if (span == null) {
61 output.write(message);
62 } else {
63 output.write('on ');
64 output.write(span.message(message, color: levelColor));
65 }
66
67 return output.toString();
68 }
69 }
70
71 typedef void PrintHandler(Message obj);
72
73 /**
74 * This class tracks and prints information, warnings, and errors emitted by the
75 * compiler.
76 */
77 class Messages {
78 /** Called on every error. Set to blank function to supress printing. */
79 final PrintHandler printHandler;
80
81 final PreprocessorOptions options;
82
83 final List<Message> messages = <Message>[];
84
85 Messages({PreprocessorOptions options, this.printHandler: print})
86 : options = options != null ? options : new PreprocessorOptions();
87
88 /** Report a compile-time CSS error. */
89 void error(String message, SourceSpan span) {
90 var msg = new Message(Level.SEVERE, message, span: span,
91 useColors: options.useColors);
92
93 messages.add(msg);
94
95 printHandler(msg);
96 }
97
98 /** Report a compile-time CSS warning. */
99 void warning(String message, SourceSpan span) {
100 if (options.warningsAsErrors) {
101 error(message, span);
102 } else {
103 var msg = new Message(Level.WARNING, message, span: span,
104 useColors: options.useColors);
105
106 messages.add(msg);
107 }
108 }
109
110 /** Report and informational message about what the compiler is doing. */
111 void info(String message, SourceSpan span) {
112 var msg = new Message(Level.INFO, message, span: span,
113 useColors: options.useColors);
114
115 messages.add(msg);
116
117 if (options.verbose) printHandler(msg);
118 }
119
120 /** Merge [newMessages] to this message lsit. */
121 void mergeMessages(Messages newMessages) {
122 messages.addAll(newMessages.messages);
123 newMessages.messages.where((message) =>
124 message.level.value == Level.SEVERE || options.verbose)
125 .forEach((message) { printHandler(message); });
126 }
127 }
OLDNEW
« no previous file with comments | « observatory_pub_packages/csslib/src/css_printer.dart ('k') | observatory_pub_packages/csslib/src/options.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698