Chromium Code Reviews| Index: sdk/lib/_internal/pub/lib/src/log.dart |
| diff --git a/sdk/lib/_internal/pub/lib/src/log.dart b/sdk/lib/_internal/pub/lib/src/log.dart |
| index fd1166d99644e29795f0301e41fbcf2c3e532e8b..b08c83aabbffe89738a259d5d796a8832fa98935 100644 |
| --- a/sdk/lib/_internal/pub/lib/src/log.dart |
| +++ b/sdk/lib/_internal/pub/lib/src/log.dart |
| @@ -6,6 +6,7 @@ |
| library pub.log; |
| import 'dart:async'; |
| +import 'dart:convert'; |
| import 'dart:io'; |
| import 'package:path/path.dart' as p; |
| @@ -209,7 +210,9 @@ void dumpTranscript() { |
| if (_transcript == null) return; |
| stderr.writeln('---- Log transcript ----'); |
| - _transcript.forEach(_logToStderrWithLabel, (discarded) { |
| + _transcript.forEach((entry) { |
| + _printToStream(stderr, entry, showLabel: true); |
|
nweiz
2014/03/05 02:36:52
If you're keeping _logToStderrWithLabel, I don't u
Bob Nystrom
2014/03/07 00:42:18
Calling _printToStream() directly skips the if (js
|
| + }, (discarded) { |
| stderr.writeln('---- ($discarded discarded) ----'); |
| }); |
| stderr.writeln('---- End log transcript ----'); |
| @@ -219,6 +222,8 @@ void dumpTranscript() { |
| /// future returned by [callback] completes. If anything else is logged during |
| /// this, it cancels the progress. |
| Future progress(String message, Future callback()) { |
| + if (json.enabled) return callback(); |
| + |
| if (_progressTimer != null) throw new StateError("Already in progress."); |
| _progressMessage = '$message...'; |
| @@ -356,6 +361,12 @@ void _logToStderrWithLabel(Entry entry) { |
| } |
| void _logToStream(IOSink sink, Entry entry, {bool showLabel}) { |
| + if (json.enabled) return; |
| + |
| + _printToStream(sink, entry, showLabel: showLabel); |
| +} |
| + |
| +void _printToStream(IOSink sink, Entry entry, {bool showLabel}) { |
| _stopProgress(); |
| bool firstLine = true; |
| @@ -373,3 +384,41 @@ void _logToStream(IOSink sink, Entry entry, {bool showLabel}) { |
| firstLine = false; |
| } |
| } |
| + |
| +/// Namespace-like class for collecting the methods for JSON logging. |
| +class _JsonLogger { |
| + /// Whether logging should use machine-friendl JSON output or human-friendly |
|
nweiz
2014/03/05 02:36:52
"friendl" -> "friendly"
Bob Nystrom
2014/03/07 00:42:18
Done.
|
| + /// text. |
| + /// |
| + /// If set to `true`, then all regular logging is not printed. Logged |
|
nweiz
2014/03/05 02:36:52
"no regular logging is printed"
Bob Nystrom
2014/03/07 00:42:18
Done.
|
| + /// messages will still be recorded and displayed if the transcript is |
| + /// printed. |
| + bool enabled = false; |
| + |
| + /// Creates an error JSON object for [error] and prints it if JSON output |
| + /// is enabled. |
|
nweiz
2014/03/05 02:36:52
Mention that this prints to stdout as opposed to s
Bob Nystrom
2014/03/07 00:42:18
Done.
|
| + void error(error) { |
|
nweiz
2014/03/05 02:36:52
This should take an optional StackTrace as well.
Bob Nystrom
2014/03/07 00:42:18
Done.
|
| + var errorJson = {"error": error.toString()}; |
| + |
| + var trace; |
| + if (error is Error) trace = error.stackTrace; |
| + if (trace != null) { |
| + errorJson["stackTrace"] = trace; |
|
nweiz
2014/03/05 02:36:52
We should [new Chain.forTrace] this.
Bob Nystrom
2014/03/07 00:42:18
Done.
|
| + } |
| + |
| + this.message(errorJson); |
| + } |
| + |
| + /// Encodes [message] to JSON and prints it if JSON output is enabled. |
| + void message(message) { |
| + if (!enabled) return; |
| + |
| + // TODO(rnystrom): Should errors be printed to stderr? |
|
nweiz
2014/03/05 02:36:52
I think stdout is fine. Since the errors have iden
Bob Nystrom
2014/03/07 00:42:18
Done.
|
| + print(JSON.encode(message)); |
| + } |
| +} |
| + |
| +/// The singleton instance so that we can have a nice api like: |
| +/// |
| +/// log.json.error(...); |
| +final json = new _JsonLogger(); |
|
nweiz
2014/03/05 02:36:52
Nit: I'd expect this to be at the top of the file,
Bob Nystrom
2014/03/07 00:42:18
Done.
|