use status(), error(), and trace() for logging

rename to printTrace(), printStatus(), and printError()
This commit is contained in:
Devon Carew
2016-01-27 22:38:57 -08:00
parent 91c0af60f4
commit d7fbf5302d
40 changed files with 445 additions and 304 deletions

View File

@@ -0,0 +1,70 @@
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:async';
import 'dart:io';
final AppContext _defaultContext = new _DefaultAppContext();
/// A singleton for application functionality. This singleton can be different
/// on a per-Zone basis.
AppContext get context {
AppContext currentContext = Zone.current['context'];
return currentContext == null ? _defaultContext : currentContext;
}
/// Display an error level message to the user. Commands should use this if they
/// fail in some way.
void printError(String message, [StackTrace stackTrace]) => context.printError(message, stackTrace);
/// Display normal output of the command. This should be used for things like
/// progress messages, success messages, or just normal command output.
void printStatus(String message) => context.printStatus(message);
/// Use this for verbose tracing output. Users can turn this output on in order
/// to help diagnose issues with the toolchain or with their setup.
void printTrace(String message) => context.printTrace(message);
abstract class AppContext {
bool get verbose;
set verbose(bool value);
void printError(String message, [StackTrace stackTrace]);
void printStatus(String message);
void printTrace(String message);
}
class _DefaultAppContext implements AppContext {
DateTime _startTime = new DateTime.now();
bool _verbose = false;
bool get verbose => _verbose;
set verbose(bool value) {
_verbose = value;
}
void printError(String message, [StackTrace stackTrace]) {
stderr.writeln(_prefix + message);
if (stackTrace != null)
stderr.writeln(stackTrace);
}
void printStatus(String message) {
print(_prefix + message);
}
void printTrace(String message) {
if (_verbose)
print('$_prefix- $message');
}
String get _prefix {
if (!_verbose)
return '';
Duration elapsed = new DateTime.now().difference(_startTime);
return '[${elapsed.inMilliseconds.toString().padLeft(4)} ms] ';
}
}

View File

@@ -1,7 +0,0 @@
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:logging/logging.dart';
final Logger logging = new Logger('flutter_tools');

View File

@@ -6,7 +6,7 @@ import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'logging.dart';
import 'context.dart';
/// This runs the command and streams stdout/stderr from the child process to
/// this process' stdout/stderr.
@@ -15,7 +15,7 @@ Future<int> runCommandAndStreamOutput(List<String> cmd, {
RegExp filter,
String workingDirectory
}) async {
logging.info(cmd.join(' '));
printTrace(cmd.join(' '));
Process process = await Process.start(
cmd[0],
cmd.sublist(1),
@@ -28,7 +28,7 @@ Future<int> runCommandAndStreamOutput(List<String> cmd, {
dataLines = dataLines.where((String s) => filter.hasMatch(s)).toList();
}
if (dataLines.length > 0) {
stdout.write('$prefix${dataLines.join('\n$prefix')}\n');
printStatus('$prefix${dataLines.join('\n$prefix')}');
}
});
process.stderr.transform(UTF8.decoder).listen((String data) {
@@ -38,7 +38,7 @@ Future<int> runCommandAndStreamOutput(List<String> cmd, {
dataLines = dataLines.where((String s) => filter.hasMatch(s));
}
if (dataLines.length > 0) {
stderr.write('$prefix${dataLines.join('\n$prefix')}\n');
printError('$prefix${dataLines.join('\n$prefix')}');
}
});
return await process.exitCode;
@@ -47,13 +47,13 @@ Future<int> runCommandAndStreamOutput(List<String> cmd, {
Future runAndKill(List<String> cmd, Duration timeout) {
Future<Process> proc = runDetached(cmd);
return new Future.delayed(timeout, () async {
logging.info('Intentionally killing ${cmd[0]}');
printTrace('Intentionally killing ${cmd[0]}');
Process.killPid((await proc).pid);
});
}
Future<Process> runDetached(List<String> cmd) {
logging.info(cmd.join(' '));
printTrace(cmd.join(' '));
Future<Process> proc = Process.start(
cmd[0], cmd.getRange(1, cmd.length).toList(),
mode: ProcessStartMode.DETACHED);
@@ -81,20 +81,20 @@ String _runWithLoggingSync(List<String> cmd, {
bool checked: false,
String workingDirectory
}) {
logging.info(cmd.join(' '));
printTrace(cmd.join(' '));
ProcessResult results =
Process.runSync(cmd[0], cmd.getRange(1, cmd.length).toList(), workingDirectory: workingDirectory);
if (results.exitCode != 0) {
String errorDescription = 'Error code ${results.exitCode} '
'returned when attempting to run command: ${cmd.join(' ')}';
logging.info(errorDescription);
printTrace(errorDescription);
if (results.stderr.length > 0)
logging.info('Errors logged: ${results.stderr.trim()}');
printTrace('Errors logged: ${results.stderr.trim()}');
if (checked)
throw errorDescription;
}
if (results.stdout.trim().isNotEmpty)
logging.fine(results.stdout.trim());
printTrace(results.stdout.trim());
return results.stdout;
}