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

Side by Side Diff: observatory_pub_packages/code_transformers/messages/build_logger.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) 2013, 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 code_transformers.messages.messages_logger;
6
7 import 'dart:async';
8 import 'dart:convert' show JSON;
9
10 import 'package:barback/barback.dart';
11 import 'package:source_span/source_span.dart';
12
13 import 'messages.dart' show Message, MessageId, BuildLogEntry, LogEntryTable;
14
15 /// A [TransformLogger] used to track error and warning messages produced during
16 /// a build.
17 ///
18 /// This logger records all messages that were logged and then forwards
19 /// the calls to an underlying [TransformLogger]. The internal records support
20 /// serializing the errors and emiting them to an asset (so they can be
21 /// presented to the user in a web-based client), clustering similar messages
22 /// together, sorting messages in order of importance, etc.
23 ///
24 /// The logger also supports reporting error messages as warnings. Barback makes
25 /// error messages stop the transformation process, which sometimes can surprise
26 /// users. Turning errors into warnings is especially useful when used within
27 /// `pub serve`, where we would like the transformation to continue as far as it
28 /// can. When this flag is turned on, the level is still recorded as an error,
29 /// so a web client UI can still highlight their importance.
30 // TODO(sigmund): also cluster messages when they are reported on the
31 // command-line.
32 class BuildLogger implements TransformLogger {
33 /// Underling transform that is currently active.
34 final Transform _transform;
35
36 /// Logs created during the current transform.
37 final LogEntryTable _logs = new LogEntryTable();
38
39 /// Whether to use `warning` or `error` when forwarding error messages to the
40 /// underlying logger in `_transform.logger`.
41 final bool convertErrorsToWarnings;
42
43 /// Uri prefix to link for additional details. If set, messages logged through
44 /// this logger will contain an additional sentence, telling users to find
45 /// more details at `$detailsUri#packagename_id`.
46 final String detailsUri;
47
48 BuildLogger(this._transform, {this.convertErrorsToWarnings: false,
49 this.detailsUri});
50
51 /// Records a message at the fine level. If [msg] is a [Message] it is
52 /// recorded directly, otherwise it is first converted to a [String].
53 void fine(msg, {AssetId asset, SourceSpan span}) {
54 msg = msg is Message ? msg : new Message.unknown('$msg');
55 _transform.logger.fine(_snippet(msg), asset: asset, span: span);
56 _logs.add(new BuildLogEntry(msg, span, LogLevel.FINE.name));
57 }
58
59 /// Records a message at the info level. If [msg] is a [Message] it is
60 /// recorded directly, otherwise it is first converted to a [String].
61 void info(msg, {AssetId asset, SourceSpan span}) {
62 msg = msg is Message ? msg : new Message.unknown('$msg');
63 _transform.logger.info(_snippet(msg), asset: asset, span: span);
64 _logs.add(new BuildLogEntry(msg, span, LogLevel.INFO.name));
65 }
66
67 /// Records a warning message. If [msg] is a [Message] it is recorded
68 /// directly, otherwise it is first converted to a [String].
69 void warning(msg, {AssetId asset, SourceSpan span}) {
70 msg = msg is Message ? msg : new Message.unknown('$msg');
71 _transform.logger.warning(_snippet(msg), asset: asset, span: span);
72 _logs.add(new BuildLogEntry(msg, span, LogLevel.WARNING.name));
73 }
74
75 /// Records an error message. If [msg] is a [Message] it is recorded
76 /// directly, otherwise it is first converted to a [String].
77 void error(msg, {AssetId asset, SourceSpan span}) {
78 msg = msg is Message ? msg : new Message.unknown('$msg');
79 if (convertErrorsToWarnings) {
80 _transform.logger.warning(_snippet(msg), asset: asset, span: span);
81 } else {
82 _transform.logger.error(_snippet(msg), asset: asset, span: span);
83 }
84 _logs.add(new BuildLogEntry(msg, span, LogLevel.ERROR.name));
85 }
86
87 String _snippet(Message msg) {
88 var s = msg.snippet;
89 if (detailsUri == null) return s;
90 var dot = s.endsWith('.') || s.endsWith('!') || s.endsWith('?') ? '' : '.';
91 var hashTag = '${msg.id.package}_${msg.id.id}';
92 return '$s$dot See $detailsUri#$hashTag for details.';
93 }
94
95 /// Outputs the log data to a JSON serialized file.
96 Future writeOutput() {
97 return _getNextLogAssetPath().then((path) {
98 _transform.addOutput(new Asset.fromString(path,
99 JSON.encode(_logs)));
100 });
101 }
102
103 // Each phase outputs a new log file with an incrementing # appended, this
104 // figures out the next # to use.
105 Future<String> _getNextLogAssetPath([int nextNumber = 1]) {
106 var nextAssetPath = _transform.primaryInput.id.addExtension(
107 '${LOG_EXTENSION}.$nextNumber');
108 return _transform.hasInput(nextAssetPath).then((exists) {
109 if (!exists) return nextAssetPath;
110 return _getNextLogAssetPath(++nextNumber);
111 });
112 }
113
114 // Reads all log files for an Asset into [logs].
115 static Future _readLogFilesForAsset(AssetId id, Transform transform,
116 LogEntryTable entries, [nextNumber = 1]) {
117 var nextAssetPath = id.addExtension('${LOG_EXTENSION}.$nextNumber');
118 return transform.hasInput(nextAssetPath).then((exists) {
119 if (!exists) return null;
120 return transform.readInputAsString(nextAssetPath).then((data) {
121 entries.addAll(new LogEntryTable.fromJson(JSON.decode(data)));
122 return _readLogFilesForAsset(id, transform, entries, ++nextNumber);
123 });
124 });
125 }
126
127 // Combines all existing ._buildLogs.* files into a single ._buildLogs file.
128 static Future combineLogFiles(Transform transform) {
129 var entries = new LogEntryTable();
130 var id = transform.primaryInput.id;
131 return _readLogFilesForAsset(id, transform, entries).then((_) {
132 return transform.addOutput(new Asset.fromString(
133 id.addExtension(LOG_EXTENSION),
134 JSON.encode(entries.toJson())));
135 });
136 }
137
138 // Reads all logs for an asset and adds them to this loggers log output.
139 Future addLogFilesFromAsset(AssetId id, [int nextNumber = 1]) {
140 return _readLogFilesForAsset(id, _transform, _logs);
141 }
142 }
143
144 /// Extension used for assets that contained serialized logs.
145 const String LOG_EXTENSION = '._buildLogs';
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698