OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 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 | 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 barback.transformer.transform_logger; | 5 library barback.transformer.transform_logger; |
6 | 6 |
7 import 'package:source_maps/span.dart'; | |
8 import 'package:source_span/source_span.dart'; | 7 import 'package:source_span/source_span.dart'; |
9 | 8 |
10 import '../asset/asset_id.dart'; | 9 import '../asset/asset_id.dart'; |
11 import '../log.dart'; | 10 import '../log.dart'; |
12 import '../span_wrapper.dart'; | |
13 | 11 |
14 typedef void LogFunction(AssetId asset, LogLevel level, String message, | 12 typedef void LogFunction(AssetId asset, LogLevel level, String message, |
15 Span span); | 13 SourceSpan span); |
16 | 14 |
17 /// Object used to report warnings and errors encountered while running a | 15 /// Object used to report warnings and errors encountered while running a |
18 /// transformer. | 16 /// transformer. |
19 class TransformLogger { | 17 class TransformLogger { |
20 final LogFunction _logFunction; | 18 final LogFunction _logFunction; |
21 | 19 |
22 TransformLogger(this._logFunction); | 20 TransformLogger(this._logFunction); |
23 | 21 |
24 /// Logs an informative message. | 22 /// Logs an informative message. |
25 /// | 23 /// |
26 /// If [asset] is provided, the log entry is associated with that asset. | 24 /// If [asset] is provided, the log entry is associated with that asset. |
27 /// Otherwise it's associated with the primary input of [transformer]. If | 25 /// Otherwise it's associated with the primary input of [transformer]. If |
28 /// present, [span] indicates the location in the input asset that caused the | 26 /// present, [span] indicates the location in the input asset that caused the |
29 /// error. It may be a [SourceSpan] or a [Span], although the former is | 27 /// error. |
30 /// recommended. | 28 void info(String message, {AssetId asset, SourceSpan span}) { |
31 void info(String message, {AssetId asset, span}) { | |
32 if (span != null && span is SourceSpan) span = new SpanWrapper(span, false); | |
33 _logFunction(asset, LogLevel.INFO, message, span); | 29 _logFunction(asset, LogLevel.INFO, message, span); |
34 } | 30 } |
35 | 31 |
36 /// Logs a message that won't be displayed unless the user is running in | 32 /// Logs a message that won't be displayed unless the user is running in |
37 /// verbose mode. | 33 /// verbose mode. |
38 /// | 34 /// |
39 /// If [asset] is provided, the log entry is associated with that asset. | 35 /// If [asset] is provided, the log entry is associated with that asset. |
40 /// Otherwise it's associated with the primary input of [transformer]. If | 36 /// Otherwise it's associated with the primary input of [transformer]. If |
41 /// present, [span] indicates the location in the input asset that caused the | 37 /// present, [span] indicates the location in the input asset that caused the |
42 /// error. It may be a [SourceSpan] or a [Span], although the former is | 38 /// error. |
43 /// recommended. | 39 void fine(String message, {AssetId asset, SourceSpan span}) { |
44 void fine(String message, {AssetId asset, span}) { | |
45 if (span != null && span is SourceSpan) span = new SpanWrapper(span, false); | |
46 _logFunction(asset, LogLevel.FINE, message, span); | 40 _logFunction(asset, LogLevel.FINE, message, span); |
47 } | 41 } |
48 | 42 |
49 /// Logs a warning message. | 43 /// Logs a warning message. |
50 /// | 44 /// |
51 /// If [asset] is provided, the log entry is associated with that asset. | 45 /// If [asset] is provided, the log entry is associated with that asset. |
52 /// Otherwise it's associated with the primary input of [transformer]. If | 46 /// Otherwise it's associated with the primary input of [transformer]. If |
53 /// present, [span] indicates the location in the input asset that caused the | 47 /// present, [span] indicates the location in the input asset that caused the |
54 /// error. It may be a [SourceSpan] or a [Span], although the former is | 48 /// error. |
55 /// recommended. | 49 void warning(String message, {AssetId asset, SourceSpan span}) { |
56 void warning(String message, {AssetId asset, span}) { | |
57 if (span != null && span is SourceSpan) span = new SpanWrapper(span, false); | |
58 _logFunction(asset, LogLevel.WARNING, message, span); | 50 _logFunction(asset, LogLevel.WARNING, message, span); |
59 } | 51 } |
60 | 52 |
61 /// Logs an error message. | 53 /// Logs an error message. |
62 /// | 54 /// |
63 /// If [asset] is provided, the log entry is associated with that asset. | 55 /// If [asset] is provided, the log entry is associated with that asset. |
64 /// Otherwise it's associated with the primary input of [transformer]. If | 56 /// Otherwise it's associated with the primary input of [transformer]. If |
65 /// present, [span] indicates the location in the input asset that caused the | 57 /// present, [span] indicates the location in the input asset that caused the |
66 /// error. It may be a [SourceSpan] or a [Span], although the former is | 58 /// error. |
67 /// recommended. | |
68 /// | 59 /// |
69 /// Logging any errors will cause Barback to consider the transformation to | 60 /// Logging any errors will cause Barback to consider the transformation to |
70 /// have failed, much like throwing an exception. This means that neither the | 61 /// have failed, much like throwing an exception. This means that neither the |
71 /// primary input nor any outputs emitted by the transformer will be passed on | 62 /// primary input nor any outputs emitted by the transformer will be passed on |
72 /// to the following phase, and the build will be reported as having failed. | 63 /// to the following phase, and the build will be reported as having failed. |
73 /// | 64 /// |
74 /// Unlike throwing an exception, this doesn't cause a transformer to stop | 65 /// Unlike throwing an exception, this doesn't cause a transformer to stop |
75 /// running. This makes it useful in cases where a single input may have | 66 /// running. This makes it useful in cases where a single input may have |
76 /// multiple errors that the user wants to know about. | 67 /// multiple errors that the user wants to know about. |
77 void error(String message, {AssetId asset, span}) { | 68 void error(String message, {AssetId asset, SourceSpan span}) { |
78 if (span != null && span is SourceSpan) span = new SpanWrapper(span, false); | |
79 _logFunction(asset, LogLevel.ERROR, message, span); | 69 _logFunction(asset, LogLevel.ERROR, message, span); |
80 } | 70 } |
81 } | 71 } |
OLD | NEW |