| 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_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, | |
| 51 this.useColors = useColors; | |
| 52 | |
| 53 String toString() { | |
| 54 var output = new StringBuffer(); | |
| 55 bool colors = useColors && _ERROR_COLORS.containsKey(level); | |
| 56 var levelColor = colors ? _ERROR_COLORS[level] : null; | |
| 57 if (colors) output.write(levelColor); | |
| 58 output | |
| 59 ..write(_ERROR_LABEL[level]) | |
| 60 ..write(' '); | |
| 61 if (colors) output.write(NO_COLOR); | |
| 62 | |
| 63 if (span == null) { | |
| 64 output.write(message); | |
| 65 } else { | |
| 66 output.write('on '); | |
| 67 output.write(span.message(message, color: levelColor)); | |
| 68 } | |
| 69 | |
| 70 return output.toString(); | |
| 71 } | |
| 72 } | |
| 73 | |
| 74 typedef void PrintHandler(Message obj); | |
| 75 | |
| 76 /** | |
| 77 * This class tracks and prints information, warnings, and errors emitted by the | |
| 78 * compiler. | |
| 79 */ | |
| 80 class Messages { | |
| 81 /** Called on every error. Set to blank function to supress printing. */ | |
| 82 final PrintHandler printHandler; | |
| 83 | |
| 84 final PreprocessorOptions options; | |
| 85 | |
| 86 final List<Message> messages = <Message>[]; | |
| 87 | |
| 88 Messages({PreprocessorOptions options, this.printHandler: print}) | |
| 89 : options = options != null ? options : new PreprocessorOptions(); | |
| 90 | |
| 91 /** Report a compile-time CSS error. */ | |
| 92 void error(String message, SourceSpan span) { | |
| 93 var msg = new Message(Level.SEVERE, message, | |
| 94 span: span, useColors: options.useColors); | |
| 95 | |
| 96 messages.add(msg); | |
| 97 | |
| 98 printHandler(msg); | |
| 99 } | |
| 100 | |
| 101 /** Report a compile-time CSS warning. */ | |
| 102 void warning(String message, SourceSpan span) { | |
| 103 if (options.warningsAsErrors) { | |
| 104 error(message, span); | |
| 105 } else { | |
| 106 var msg = new Message(Level.WARNING, message, | |
| 107 span: span, useColors: options.useColors); | |
| 108 | |
| 109 messages.add(msg); | |
| 110 } | |
| 111 } | |
| 112 | |
| 113 /** Report and informational message about what the compiler is doing. */ | |
| 114 void info(String message, SourceSpan span) { | |
| 115 var msg = new Message(Level.INFO, message, | |
| 116 span: span, useColors: options.useColors); | |
| 117 | |
| 118 messages.add(msg); | |
| 119 | |
| 120 if (options.verbose) printHandler(msg); | |
| 121 } | |
| 122 | |
| 123 /** Merge [newMessages] to this message lsit. */ | |
| 124 void mergeMessages(Messages newMessages) { | |
| 125 messages.addAll(newMessages.messages); | |
| 126 newMessages.messages | |
| 127 .where((message) => message.level == Level.SEVERE || options.verbose) | |
| 128 .forEach(printHandler); | |
| 129 } | |
| 130 } | |
| OLD | NEW |