Chromium Code Reviews| 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 messages; | 5 library messages; |
| 6 | 6 |
| 7 import 'dart:json'; | |
| 8 | |
| 7 import 'package:html5lib/dom_parsing.dart' show SourceSpan; | 9 import 'package:html5lib/dom_parsing.dart' show SourceSpan; |
| 8 import 'package:logging/logging.dart' show Level; | 10 import 'package:logging/logging.dart' show Level; |
| 9 | 11 |
| 10 import 'file_system/path.dart'; | 12 import 'file_system/path.dart'; |
| 11 import 'options.dart'; | 13 import 'options.dart'; |
| 12 import 'utils.dart'; | 14 import 'utils.dart'; |
| 13 | 15 |
| 14 // TODO(jmesserly): remove the global messages. We instead use some | 16 // TODO(jmesserly): remove the global messages. We instead use some |
| 15 // object that tracks compilation state. | 17 // object that tracks compilation state. |
| 16 | 18 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 48 if (span == null) { | 50 if (span == null) { |
| 49 if (file != null) output.add('$file: '); | 51 if (file != null) output.add('$file: '); |
| 50 output.add(message); | 52 output.add(message); |
| 51 } else { | 53 } else { |
| 52 output.add(span.toMessageString( | 54 output.add(span.toMessageString( |
| 53 file.toString(), message, useColors: colors)); | 55 file.toString(), message, useColors: colors)); |
| 54 } | 56 } |
| 55 | 57 |
| 56 return output.toString(); | 58 return output.toString(); |
| 57 } | 59 } |
| 60 | |
| 61 String toJson() { | |
| 62 if (file == null) return toString(); | |
| 63 | |
| 64 var kind = (level == Level.SEVERE ? 'error' : | |
| 65 (level == Level.WARNING ? 'warning' : 'info')); | |
| 66 var json = { | |
| 67 'method': kind, | |
| 68 'params': { | |
| 69 'file': file.toString(), | |
| 70 'message': message, | |
| 71 'line': span == null ? 1 : span.line + 1, | |
| 72 } | |
| 73 }; | |
| 74 if (span != null) { | |
| 75 json['params']['charStart'] = span.start; | |
| 76 json['params']['charEnd'] = span.end; | |
| 77 } | |
| 78 return JSON.stringify([json]); | |
| 79 } | |
| 58 } | 80 } |
| 59 | 81 |
| 60 typedef void PrintHandler(Object obj); | |
| 61 | |
| 62 /** | 82 /** |
| 63 * This class tracks and prints information, warnings, and errors emitted by the | 83 * This class tracks and prints information, warnings, and errors emitted by the |
| 64 * compiler. | 84 * compiler. |
| 65 */ | 85 */ |
| 66 class Messages { | 86 class Messages { |
| 67 /** Called on every error. Set to blank function to supress printing. */ | |
| 68 final PrintHandler printHandler; | |
| 69 | |
| 70 final CompilerOptions options; | 87 final CompilerOptions options; |
| 88 final bool shouldPrint; | |
| 71 | 89 |
| 72 final List<Message> messages = <Message>[]; | 90 final List<Message> messages = <Message>[]; |
| 73 | 91 |
| 74 Messages({CompilerOptions options, this.printHandler: print}) | 92 Messages({CompilerOptions options, this.shouldPrint: true}) |
| 75 : options = options != null ? options : new CompilerOptions(); | 93 : options = options != null ? options : new CompilerOptions(); |
| 76 | 94 |
| 77 // Convenience methods for testing | 95 // Convenience methods for testing |
| 78 int get length => messages.length; | 96 int get length => messages.length; |
| 79 Message operator[](int index) => messages[index]; | 97 Message operator[](int index) => messages[index]; |
| 80 void clear() { | 98 void clear() { |
| 81 messages.clear(); | 99 messages.clear(); |
| 82 } | 100 } |
| 83 | 101 |
| 84 /** [message] is considered a static compile-time error by the Dart lang. */ | 102 /** [message] is considered a static compile-time error by the Dart lang. */ |
| 85 void error(String message, SourceSpan span, {Path file}) { | 103 void error(String message, SourceSpan span, {Path file}) { |
| 86 var msg = new Message(Level.SEVERE, message, file: file, span: span, | 104 var msg = new Message(Level.SEVERE, message, file: file, span: span, |
| 87 useColors: options.useColors); | 105 useColors: options.useColors); |
| 88 | 106 |
| 89 messages.add(msg); | 107 messages.add(msg); |
| 90 | 108 if (shouldPrint) { |
|
Jennifer Messerly
2012/12/06 19:59:54
Put this in a helper function?
(since we had that
Siggi Cherem (dart-lang)
2012/12/06 20:03:14
Done.
| |
| 91 printHandler(msg); | 109 print(options.jsonFormat ? msg.toJson() : msg); |
| 110 } | |
| 92 } | 111 } |
| 93 | 112 |
| 94 /** [message] is considered a type warning by the Dart lang. */ | 113 /** [message] is considered a type warning by the Dart lang. */ |
| 95 void warning(String message, SourceSpan span, {Path file}) { | 114 void warning(String message, SourceSpan span, {Path file}) { |
| 96 if (options.warningsAsErrors) { | 115 if (options.warningsAsErrors) { |
| 97 error(message, span, file: file); | 116 error(message, span, file: file); |
| 98 } else { | 117 } else { |
| 99 var msg = new Message(Level.WARNING, message, file: file, | 118 var msg = new Message(Level.WARNING, message, file: file, |
| 100 span: span, useColors: options.useColors); | 119 span: span, useColors: options.useColors); |
| 101 | 120 |
| 102 messages.add(msg); | 121 messages.add(msg); |
| 103 | 122 if (shouldPrint) { |
| 104 printHandler(msg); | 123 print(options.jsonFormat ? msg.toJson() : msg); |
| 124 } | |
| 105 } | 125 } |
| 106 } | 126 } |
| 107 | 127 |
| 108 /** | 128 /** |
| 109 * [message] at [file] will tell the user about what the compiler | 129 * [message] at [file] will tell the user about what the compiler |
| 110 * is doing. | 130 * is doing. |
| 111 */ | 131 */ |
| 112 void info(String message, SourceSpan span, {Path file}) { | 132 void info(String message, SourceSpan span, {Path file}) { |
| 113 var msg = new Message(Level.INFO, message, file: file, span: span, | 133 var msg = new Message(Level.INFO, message, file: file, span: span, |
| 114 useColors: options.useColors); | 134 useColors: options.useColors); |
| 115 | 135 |
| 116 messages.add(msg); | 136 messages.add(msg); |
| 137 if (shouldPrint && options.verbose) { | |
| 138 print(options.jsonFormat ? msg.toJson() : msg); | |
| 139 } | |
| 140 } | |
| 117 | 141 |
| 118 if (options.verbose) printHandler(msg); | |
| 119 } | |
| 120 } | 142 } |
| OLD | NEW |