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

Unified Diff: pkg/csslib/lib/src/messages.dart

Issue 23168002: move csslib into dart svn (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 4 months 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « pkg/csslib/lib/src/css_printer.dart ('k') | pkg/csslib/lib/src/options.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/csslib/lib/src/messages.dart
diff --git a/pkg/csslib/lib/src/messages.dart b/pkg/csslib/lib/src/messages.dart
new file mode 100644
index 0000000000000000000000000000000000000000..3c9f3fcc1b95bb544183c7b95f6feadd6a19f5ba
--- /dev/null
+++ b/pkg/csslib/lib/src/messages.dart
@@ -0,0 +1,129 @@
+// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+library csslib.src.messages;
+
+import 'package:logging/logging.dart' show Level;
+import 'package:source_maps/span.dart' show Span;
+
+import 'package:csslib/parser.dart';
+
+import 'options.dart';
+
+// TODO(terry): Remove the global messages, use some object that tracks
+// compilation state.
+
+/** The global [Messages] for tracking info/warnings/messages. */
+Messages messages;
+
+// Color constants used for generating messages.
+final String GREEN_COLOR = '\u001b[32m';
+final String RED_COLOR = '\u001b[31m';
+final String MAGENTA_COLOR = '\u001b[35m';
+final String NO_COLOR = '\u001b[0m';
+
+/** Map between error levels and their display color. */
+final Map<Level, String> _ERROR_COLORS = (() {
+ var colorsMap = new Map<Level, String>();
+ colorsMap[Level.SEVERE] = RED_COLOR;
+ colorsMap[Level.WARNING] = MAGENTA_COLOR;
+ colorsMap[Level.INFO] = GREEN_COLOR;
+ return colorsMap;
+})();
+
+/** Map between error levels and their friendly name. */
+final Map<Level, String> _ERROR_LABEL = (() {
+ var labels = new Map<Level, String>();
+ labels[Level.SEVERE] = 'error';
+ labels[Level.WARNING] = 'warning';
+ labels[Level.INFO] = 'info';
+ return labels;
+})();
+
+/** A single message from the compiler. */
+class Message {
+ final Level level;
+ final String message;
+ final Span span;
+ final bool useColors;
+
+ Message(this.level, this.message, {Span span, bool useColors: false})
+ : this.span = span, this.useColors = useColors;
+
+ String toString() {
+ var output = new StringBuffer();
+ bool colors = useColors && _ERROR_COLORS.containsKey(level);
+ var levelColor = _ERROR_COLORS[level];
+ if (colors) output.write(levelColor);
+ output..write(_ERROR_LABEL[level])..write(' ');
+ if (colors) output.write(NO_COLOR);
+
+ if (span == null) {
+ output.write(message);
+ } else {
+ output.write(span.getLocationMessage(message, useColors: colors,
+ color: levelColor));
+ }
+
+ return output.toString();
+ }
+}
+
+typedef void PrintHandler(Object obj);
+
+/**
+ * This class tracks and prints information, warnings, and errors emitted by the
+ * compiler.
+ */
+class Messages {
+ /** Called on every error. Set to blank function to supress printing. */
+ final PrintHandler printHandler;
+
+ final PreprocessorOptions options;
+
+ final List<Message> messages = <Message>[];
+
+ Messages({PreprocessorOptions options, this.printHandler: print})
+ : options = options != null ? options : new PreprocessorOptions();
+
+ /** Report a compile-time CSS error. */
+ void error(String message, Span span) {
+ var msg = new Message(Level.SEVERE, message, span: span,
+ useColors: options.useColors);
+
+ messages.add(msg);
+
+ printHandler(msg);
+ }
+
+ /** Report a compile-time CSS warning. */
+ void warning(String message, Span span) {
+ if (options.warningsAsErrors) {
+ error(message, span);
+ } else {
+ var msg = new Message(Level.WARNING, message, span: span,
+ useColors: options.useColors);
+
+ messages.add(msg);
+ }
+ }
+
+ /** Report and informational message about what the compiler is doing. */
+ void info(String message, Span span) {
+ var msg = new Message(Level.INFO, message, span: span,
+ useColors: options.useColors);
+
+ messages.add(msg);
+
+ if (options.verbose) printHandler(msg);
+ }
+
+ /** Merge [newMessages] to this message lsit. */
+ void mergeMessages(Messages newMessages) {
+ messages.addAll(newMessages.messages);
+ newMessages.messages.where((message) =>
+ message.level.value == Level.SEVERE || options.verbose)
+ .forEach((message) { printHandler(message); });
+ }
+}
« no previous file with comments | « pkg/csslib/lib/src/css_printer.dart ('k') | pkg/csslib/lib/src/options.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698