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