-
Notifications
You must be signed in to change notification settings - Fork 348
/
stderr.dart
58 lines (49 loc) · 1.87 KB
/
stderr.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Copyright 2018 Google Inc. Use of this source code is governed by an
// MIT-style license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.
import 'package:path/path.dart' as p;
import 'package:source_span/source_span.dart';
import 'package:stack_trace/stack_trace.dart';
import '../io.dart';
import '../logger.dart';
import '../utils.dart';
/// A logger that prints warnings to standard error or browser console.
final class StderrLogger implements Logger {
/// Whether to use terminal colors in messages.
final bool color;
const StderrLogger({this.color = false});
void warn(String message,
{FileSpan? span, Trace? trace, bool deprecation = false}) {
var result = StringBuffer();
if (color) {
// Bold yellow.
result.write('\u001b[33m\u001b[1m');
if (deprecation) result.write('Deprecation ');
result.write('Warning\u001b[0m');
} else {
if (deprecation) result.write('DEPRECATION ');
result.write('WARNING');
}
if (span == null) {
result.writeln(': $message');
} else if (trace != null) {
// If there's a span and a trace, the span's location information is
// probably duplicated in the trace, so we just use it for highlighting.
result.writeln(': $message\n\n${span.highlight(color: color)}');
} else {
result.writeln(' on ${span.message("\n" + message, color: color)}');
}
if (trace != null) result.writeln(indent(trace.toString().trimRight(), 4));
printError(result);
}
void debug(String message, SourceSpan span) {
var result = StringBuffer();
var url = isRealUrl(span.start.sourceUrl)
? p.prettyUri(span.start.sourceUrl)
: '-';
result.write('$url:${span.start.line + 1} ');
result.write(color ? '\u001b[1mDebug\u001b[0m' : 'DEBUG');
result.write(': $message');
printError(result.toString());
}
}